Templating json_attributes from an array

The following RESTful sensor retrieves the temperature from one of the locations from the “Badetassen” API. The GET returns an array with the data from 50+ locations, and using the json_attributes_path I am able to filter out a single location:

sensor:
  - platform: rest
    name: Badetassen Sjøbadet
    device_class: temperature
    unit_of_measurement: "°C"
    resource: "https://prdl-apimgmt.lyse.no/apis/t/prod.altibox.lyse.no/temp/1.0/api/location/"
    method: GET
    scan_interval: 300
    headers:
      accept: "application/json"
      content-type: "application/json"
      Authorization: "Bearer 9df43895-3d09-30d5-afe4-db2bf92a86f0"
    json_attributes_path: "$.[?(@.id==10)]"
    json_attributes:
      - lastReadingTime
      - lastTemperature
      - GPSLat
      - GPSLong
      - PictureURL
    #      - Name
    value_template: >
      {% for x in value_json %}
      {% if (x.id == 10 and (now() - as_datetime(x.lastReadingTime)) < timedelta( hours = 3 )) %}
      {{x.lastTemperature}}
      {% endif %}
      {% endfor %}

Next I wanted to get a hold of the temperatures from all the 4 sensors in my area, without doing the accessing the API four times. That should be possible by changing json_attributes_path as I have done below to get the array I want and then adding template sensors for each separate location. However, when I do this it the attributes are gone. It looks like they are not recognized when they are arrays? How do I solve this?

sensor:
  - platform: rest
    name: Badetassen
    resource: "https://prdl-apimgmt.lyse.no/apis/t/prod.altibox.lyse.no/temp/1.0/api/location/"
    method: GET
    scan_interval: 300
    headers:
      accept: "application/json"
      content-type: "application/json"
      Authorization: "Bearer 9df43895-3d09-30d5-afe4-db2bf92a86f0"
    json_attributes_path: "$.[?(@.area_id==4)]"
    value_template: "OK" #dummy value to avoid 255 char limit

The array should look like this:

[{'id': 107, 'Area_id': 4, 'Name': 'Kyvannet', 'GPSLat': '63.404594', 'GPSLong': '10.342120', 'PictureURL': 'https://las-smc-pd-cdn-badetemp.azureedge.net/images/Kyvannet_badeplass.jpeg', 'lastReadingTime': '2021-10-04T16:56:02+02:00', 'lastTemperature': '9.8'}
{'id': 11, 'Area_id': 4, 'Name': 'Munkholmen', 'GPSLat': '63.451380', 'GPSLong': '10.384790', 'PictureURL': 'https://las-smc-pd-cdn-badetemp.azureedge.net/images/Munkholmen_badeplass.JPG', 'lastReadingTime': '2022-06-01T13:09:52.562000+02:00', 'lastTemperature': '11.2'}
{'id': 10, 'Area_id': 4, 'Name': 'Sjøbadet', 'GPSLat': '63.435770', 'GPSLong': '10.390610', 'PictureURL': 'https://las-smc-pd-cdn-badetemp.azureedge.net/images/Sjobadet_badeplass.JPG', 'lastReadingTime': '2022-06-01T13:08:46.895000+02:00', 'lastTemperature': '7.5'}]

Use the rest integration, rather than sensor platform, to extract multiple sensors from a single call

1 Like