REST Platform

I have a REST template set up for the Octopus Agile rates and the value_template has [-1] which is throwing an error. I dont know much about REST and the significance of the number inside the [ ] brackets. Please could someone give me some guidance why Im getting the below error meesage in my logs:

Error from Logs:

Output from the rest sensor:

{"count":30,"next":null,"previous":null,"results":[{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T14:30:00Z","valid_to":"2022-08-15T15:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T14:00:00Z","valid_to":"2022-08-15T14:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T13:30:00Z","valid_to":"2022-08-15T14:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T13:00:00Z","valid_to":"2022-08-15T13:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T12:30:00Z","valid_to":"2022-08-15T13:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T12:00:00Z","valid_to":"2022-08-15T12:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T11:30:00Z","valid_to":"2022-08-15T12:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T11:00:00Z","valid_to":"2022-08-15T11:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T10:30:00Z","valid_to":"2022-08-15T11:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T10:00:00Z","valid_to":"2022-08-15T10:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T09:30:00Z","valid_to":"2022-08-15T10:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T09:00:00Z","valid_to":"2022-08-15T09:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T08:30:00Z","valid_to":"2022-08-15T09:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T08:00:00Z","valid_to":"2022-08-15T08:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T07:30:00Z","valid_to":"2022-08-15T08:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T07:00:00Z","valid_to":"2022-08-15T07:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T06:30:00Z","valid_to":"2022-08-15T07:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T06:00:00Z","valid_to":"2022-08-15T06:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T05:30:00Z","valid_to":"2022-08-15T06:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T05:00:00Z","valid_to":"2022-08-15T05:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T04:30:00Z","valid_to":"2022-08-15T05:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T04:00:00Z","valid_to":"2022-08-15T04:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T03:30:00Z","valid_to":"2022-08-15T04:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T03:00:00Z","valid_to":"2022-08-15T03:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T02:30:00Z","valid_to":"2022-08-15T03:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T02:00:00Z","valid_to":"2022-08-15T02:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T01:30:00Z","valid_to":"2022-08-15T02:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T01:00:00Z","valid_to":"2022-08-15T01:30:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T00:30:00Z","valid_to":"2022-08-15T01:00:00Z"},{"value_exc_vat":33.33,"value_inc_vat":34.9965,"valid_from":"2022-08-15T00:00:00Z","valid_to":"2022-08-15T00:30:00Z"}]}

My Rest template:

  - platform: rest
    name: Agile rates today
    resource_template: >-
      {% set region = 'A' %}
      {% set from = as_timestamp(as_timestamp(now()) | timestamp_custom('%Y-%m-%d') + 'T00:00:00') %}
      {% set to = from + 24 * 60 * 60 %}
      {% set format = '%Y-%m-%dT%H:%M:%SZ' %}
      https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-{{ region }}/standard-unit-rates/?period_from={{ from | timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}&period_to={{ to | timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}
    value_template: "{{ as_timestamp(value_json.results[-1].valid_from) | timestamp_custom('%Y-%m-%d') }}"
    json_attributes:
      - "results"

Thank you in advance for any assistance.

Hmm…not being a real specialist but what is -1 (minus 1)?
image

1 Like

The number inside the bracket is which list value you want to look at in the list in the case that there are multiple.

As @vingerha pasted a pic of. You can see the numbers. Thet start at zero. Omitting that bracket will give you the entire list.

Negative numbers mean that you count from the right instead of the left. I believe a -1 is the last value returned.

1 Like

Thank you both for the explanation and I see the website that allows proper formatting of the json output so I shall save that and make use of it in the future.

I just looked at the states of the entity and see that it is being reported, so maybe is a frequent dropout of the REST sensor that the error log pick up (414 times over a couple hours)

You can guard against the missing data error like this:

    value_template: >
      {% if value_json.results[-1].valid_from is defined %}
        {{ as_timestamp(value_json.results[-1].valid_from) | timestamp_custom('%Y-%m-%d') }}
      {% else %}
        unknown
      {% endif %}
1 Like

Hi again,

I tried to past in the above value template but cant get it to parse the configuration checks:

  - platform: rest
    name: Agile rates today
    resource_template: >-
      {% set region = 'A' %}
      {% set from = as_timestamp(as_timestamp(now()) | timestamp_custom('%Y-%m-%d') + 'T00:00:00') %}
      {% set to = from + 24 * 60 * 60 %}
      {% set format = '%Y-%m-%dT%H:%M:%SZ' %}
      https://api.octopus.energy/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-{{ region }}/standard-unit-rates/?period_from={{ from | timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}&period_to={{ to | timestamp_custom('%Y-%m-%dT%H:%M:%SZ', False) }}
    value_template: >
      {% if as_timestamp(value_json.results[-1].valid_from is defined %}
        {{ as_timestamp(value_json.results[-1].valid_from) | timestamp_custom('%Y-%m-%d') }}
      {% else %}
        unknown
      {% endif %}
    json_attributes:
      - "results"

This is the error message about unexpected “}” when expecting a “)”

Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['value_template']. Got "{% if as_timestamp(value_json.results[-1].valid_from is defined %} {{ as_timestamp(value_json.results[-1].valid_from) | timestamp_custom('%Y-%m-%d') }} {% else %}\n unknown\n{% endif %}\n". (See ?, line ?).

I have updated my post.

1 Like

Thanx Tom,

This morning when I checked the logs I’m still seeing an error on that REST sensor j

Is it just one of those things that I have to live with out of there anything else I can try?