RESTful Sensor state_attr "unknow" and "json_attributes_path" (SOLVED)

Greetings to all the community.

Sorry for my English.

I’m trying to get data from https://openweathermap.org/ but I have a problem.

These are the 2 sensors:

  - platform: rest
    name: own_report
    json_attributes:
      - max
      - day
    resource: https://api.openweathermap.org/data/2.5/onecall?lat=*****169&lon=*****1406&lang=it&APPID=*****&units=metric&exclude=current,minutely,hourly,alerts
    value_template: "{{ value_json['daily'][0]['temp'] }}"

  - platform: template
    sensors:
      own_report_tempo_domani:
        friendly_name: "OWN Report Tempo Domani"
        value_template: "{{ state_attr('sensor.own_report', 'day') }}"

On “sensor.own_report” I get the json string:

{'day': 11.14, 'min': 7.83, 'max': 11.26, 'night': 7.83, 'eve': 10.06, 'morn': 9.04}

but on “own_report_tempo_domani” nothing, I get a nice one

unknown

No errors in “home-assistant.log”.
I’m confused … I don’t understand where the mistake is …

json_attributes is looking at the root of the json data. Which is not where max and day are. Hence you are getting no attributes for sensor.own_report, only a state. You need to supply a path to the attributes:

  - platform: rest
    name: own_report
    json_attributes_path: "$['daily'][0]['temp']"
    json_attributes:
      - max
      - day
    resource: https://api.openweathermap.org/data/2.5/onecall?lat=*****169&lon=*****1406&lang=it&APPID=*****&units=metric&exclude=current,minutely,hourly,alerts
    value_template: "{{ value_json['daily'][0]['temp'] }}"

A better way to do this, without the need for template sensors would be to use the new rest platform to create as many sensors as you want from the one call to the resource:

rest:
  - resource: https://api.openweathermap.org/data/2.5/onecall?lat=*****169&lon=*****1406&lang=it&APPID=*****&units=metric&exclude=current,minutely,hourly,alerts
    sensor:
      - name: "Day"
        value_template: "{{ value_json['daily'][0]['temp']['day'] }}"
        device_class: temperature
        state_class: measurement
      - name: "Max"
        value_template: "{{ value_json['daily'][0]['temp']['max'] }}"
        device_class: temperature
        state_class: measurement

Including state_class give you access to long term statistics, this is optional.

Including device_class makes the sensor look pretty in Lovelace (adds a unit of measurement and an icon), again this is optional. You can provide your own unit_of_measurement and icon if you prefer. Though you would have to add the icon via customize as it is not a configuration option supported directly in the rest platform. Much easier to use device_class in this case.

Hi tom_l and thanks for the reply.

You are fantastic !

Everything works.

I had seen “json_attributes_path” but didn’t understand how to use it … and didn’t know about the new rest platform.

Yeah !!

1 Like

Sorry but you are wrong.

The rest sensor platform makes a call to a resource for every sensor.

The restful integration does not. You can have as many sensors as you want for the one resource and they will all be updated from each single call to the resource. That was the whole point behind developing it as an integration.

Please don’t revive a thread with incorrect information.

The rest integration… i.e.

rest:
  resource: ...
  sensors:
   ..

Will only make 1 call for all sensors listed below it.

The rest sensor platform will make 1 call for 1 sensor. e.g.

sensor:
- platform: rest
  resource: ...

So to reiterate for anyone who joins this thread.

1 call multiple sensors

The rest integration

rest:
  resource: ...
  sensors:
   ..

1 call 1 sensor

The rest sensor platform

sensor:
- platform: rest
  resource: ...