#Issues with restful sensor

11 messages · Page 1 of 1 (latest)

austere sundial
#

Hi there! I'm trying to fetch live pricing data from my energy supplier, and found an API endpoint that returns said pricing data
The data can be fetched from the following endpoint:
https://www.energiaxxi.com/content/endesa-com/es/facturas/como-saber-cual-es-el-precio-de-la-luz.getPriceToday.sendmail.json?ivaRate=21&lang=es
The pricing data is available hourly in the $.priceData array, with the array index being the hour
However, after creating a rest sensor I see the following error:

Logger: homeassistant.components.rest.util
Source: components/rest/util.py:33
integration: RESTful (documentation, issues)
First occurred: 20:45:28 (1 occurrences)
Last logged: 20:45:28

JSON result was not a dictionary or list with 0th element a dictionary

Here is my sensor code:

rest:
  - resource: https://www.energiaxxi.com/content/endesa-com/es/facturas/como-saber-cual-es-el-precio-de-la-luz.getPriceToday.sendmail.json?ivaRate=21&lang=es
    scan_interval: 600
    method: "GET"
    sensor:
      - name: energiaxxi_price_data
        json_attributes_path: "$.priceData"
        value_template: "OK"
        json_attributes:
          - precio
          - hora
          - dia

Am I doing this correctly? priceData is an array after all, so I'm not sure if this is correct

If I change the template $priceData[*] it works, but only shows the first hour as those attributes

austere sundial
#

Is this a good way to do this design wise? Like a rest sensor for all the hours then a template for the current hour?
Or should I create multiple sensors one for each hour? But then how do I get the current price?

fickle swallow
#

You need to change json_attributes_path: "$.priceData" to json_attributes_path: "$.priceData".0 because priceData is an array

austere sundial
#

Right, but I don't want only the first hour

fickle swallow
#

The alternative is to use some kind of script which fetches the data and creates/updates multiple entities

#

Or you can try something like this which is a bit janky:

rest:
  - resource: https://www.energiaxxi.com/content/endesa-com/es/facturas/como-saber-cual-es-el-precio-de-la-luz.getPriceToday.sendmail.json?ivaRate=21&lang=es
    scan_interval: 600
    method: "GET"
    sensor:
      - name: energiaxxi_price_data_0
        json_attributes_path: "$.priceData.0"
        value_template: "OK"
        json_attributes:
          - precio
          - hora
          - dia
      - name: energiaxxi_price_data_1
        json_attributes_path: "$.priceData.1"
        value_template: "OK"
        json_attributes:
          - precio
          - hora
          - dia
austere sundial
#

I guess yeah
I mean I could create one sensor for each hour, and then just use $.priceData[0] then $.priceData[1] $.priceData[2] for each one?

#

And then I guess I'd use a template for the current price?

#

Is there not a better way to do this?

fickle swallow
#

You could have one sensor with the current price, and add attributes for each hour. But a script is probably going to be easier for getting all of the data from the array at once and creating/updating the other sensors. Something like PyScript would probably be my go to: https://github.com/custom-components/pyscript

austere sundial
#

I just did it with a template