Optimize (or fix) this rest sensor

found below rest sensor definition, (which goes on to uur 47…)

rest:

  - resource_template: >
      https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?
      startTimestamp={{now().strftime('%Y-%m-%d')}}&
      endTimestamp={{(now()+ timedelta(days=2)).strftime('%Y-%m-%d')}}

    scan_interval: 900
    sensor:
      - name: Energieprijs uur 0
        value_template: >
          {{value_json[0]['TariffUsage']|float(0) + states('sensor.prijsopslag_energie')|float(0)}}
        json_attributes_path: "$[0]"
        json_attributes: &attributes
          - SupplierID
          - TariffReturn
          - TariffUsage
          - Timestamp

      - name: Energieprijs uur 1
        value_template: >
          {{value_json[1]['TariffUsage']|float(0) + states('sensor.prijsopslag_energie')|float(0)}}
        json_attributes_path: "$[1]"
        json_attributes: *attributes

      - name: Energieprijs uur 2
        value_template: >
          {{value_json[2]['TariffUsage']|float(0) + states('sensor.prijsopslag_energie')|float(0)}}
        json_attributes_path: "$[2]"
        json_attributes: *attributes

      - name: Energieprijs uur 3
        value_template: >
          {{value_json[3]['TariffUsage']|float(0) + states('sensor.prijsopslag_energie')|float(0)}}
        json_attributes_path: "$[3]"
        json_attributes: *attributes

I loaded the first time, and all sensors got populated, but now it shows all unvailables. the
{{states('sensor.prijsopslag_energie')|float(0)}} is ok, so that wont be it.

Also, if I enter

      https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?
      startTimestamp={{now().strftime('%Y-%m-%d')}}&
      endTimestamp={{(now()+ timedelta(days=2)).strftime('%Y-%m-%d')}}

directly in the browser, it finally shows up with the long list of values.

Please have a look where this rest sensor needs to be fixed/optimized? Can we somehow prevent these timeouts, (if its not the syntax causing it, which would seem odd, as its first rendered by the Jinja interpreter in HA)

they finally showed up, but, after a restart, are all ‘unknown’ again…

Logger: homeassistant.components.rest.sensor
Source: components/rest/sensor.py:170 
Integration: RESTful (documentation, issues) 
First occurred: 22:36:05 (48 occurrences) 
Last logged: 22:36:05

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

I was also hoping I could set the + states('sensor.prijsopslag_energie')|float(0)}} as a variable in the main config, so I could shorten that in the individual sensors.

update

apparently we can not use multiline in the resource_template, but need:

  - resource_template: >
      https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?startTimestamp={{now().strftime('%Y-%m-%d')}}&endTimestamp={{(now()+ timedelta(days=2)).strftime('%Y-%m-%d')}}

here they are :wink:

1 Like

HACS material

btw, I completely forgot to post to fix for this:

          {{(value_json|default(['default']))[0]['TariffUsage']|float(0) +
             states('sensor.prijsopslag_energie')|float(0)}}

setting the default prevents the issue.
We can however NOT prevent errors on the attributes, so, when the entities for the next day are made, on a time they are not available yet, they will still error out. Not the value (template), but the attributes on the

        json_attributes_path: "$[0]"

no way around that I am afraid

1 Like