Restful sensor - using template in json attributes path

Hi there,

i tried to get some info with restful integration.
All is working fine, but i need sth like template in the json_attributes_path.

This is what i have:

  - resource: http://opendata.dwd.de/climate_environment/health/alerts/uvi.json
    scan_interval: 604800  #a week in seconds
    sensor:
      - name: "DWD UV-Index Hannover"
        unique_id: dwd_uvi_hannover
        json_attributes_path: "$.content.7.forecast"
        json_attributes:
          - "today"
          - "tomorrow"
          - "dayafter_to"
        icon: mdi:sun-wireless

Every 24h the id (7) changes… It could be every number.
I need sth like this:

  - resource: http://opendata.dwd.de/climate_environment/health/alerts/uvi.json
    scan_interval: 604800  #a week in seconds
    sensor:
      - name: "DWD UV-Index Hannover"
        unique_id: dwd_uvi_hannover
        json_attributes_path: >-
          {% for x in range(value_json.content | length) %}
            {% if value_json.content[x].city == 'Hannover' %}
              {{ "$.content.**x**.forecast" }}
            {% endif %}
          {% endfor %} 
        json_attributes:
          - "today"
          - "tomorrow"
          - "dayafter_to"
        icon: mdi:sun-wireless

How to solve it?
Thanks

You need a value_template or it tries to pull in the entire response into the state, breaking the 255-character limit. Here is a solution with that issue fixed and the Hannover data selected via the json_attributes_path:

  - resource: http://opendata.dwd.de/climate_environment/health/alerts/uvi.json
    scan_interval: 604800  #a week in seconds
    sensor:
      - name: "DWD UV-Index Hannover"
        unique_id: dwd_uvi_hannover
        value_template: "{{ value_json.last_update }}"
        json_attributes_path: "$.content[?(@.city=='Hannover')].forecast"
        json_attributes:
          - "today"
          - "tomorrow"
          - "dayafter_to"
        icon: mdi:sun-wireless

image

1 Like

Pretty cool - thanks!:slight_smile: