Invalid config for [sensor.rest]: invalid template with [(@.length-1)]

Hey,
I am getting an error for a rest sensor:

Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: expected name or number) for dictionary value @ data[‘value_template’]. Got ‘{{ value_json.incidenceFullyVaccinated.values.[(@.length-1)] }}’. (See /config/configuration.yaml, line 196). Please check the docs at RESTful - Home Assistant

This is what the sensor looks like:

  - platform: rest
    resource: https://www.coronavirus.sachsen.de/corona-statistics/rest/incidenceVaccinationDevelopment.jsp?_=1636644181472
    name: 'Inzidenz Sachsen geimpfte'
    timeout: 60
    value_template: "{{ value_json.incidenceFullyVaccinated.values.[(@.length-1)] }}"
    unit_of_measurement: 'Fälle'

It works without problem in JSONPath Online Evaluator jsonpath.com.

Any ideas?

Templates do not use json path

1 Like

Ah, thanks. If I use json_attributes_path: “$.incidenceFullyVaccinated.values.[(@.length-1)]”. I can successfully restart the rest entities. How can I then set the correct json attribute? I has to be the last one, because they json is extended every day by one.

don’t bother with the path, just grab the last item from the values with [-1] inside the template.

1 Like

I am trying:

  - platform: rest
    resource: https://www.coronavirus.sachsen.de/corona-statistics/rest/incidenceVaccinationDevelopment.jsp?_=1636644181472
    name: 'Inzidenz Sachsen ungeimpfte'
    timeout: 60
    value_template: "{{ value_json.incidenceNotVaccinated.values[-1] }}"
    unit_of_measurement: 'Fälle'

But I get:

Template variable warning: builtin_function_or_method object has no element -1 when rendering ‘{{ value_json.incidenceNotVaccinated.values[-1] }}’

the code is correct and it should work.

It works in the bracketed notation:

  - platform: rest
    resource: https://www.coronavirus.sachsen.de/corona-statistics/rest/incidenceVaccinationDevelopment.jsp?_=1636644181472
    name: 'Inzidenz Sachsen ungeimpfte'
    value_template: "{{ value_json['incidenceNotVaccinated']['values'][-1] }}"
    unit_of_measurement: 'Fälle'

Thank you @petro and @koying