Fetch multiple JSON values and present them as attributes

Hello everyone.

I am new on Home assistant. I am trying to do exactly the same as restful documentation with jsontest date and time (https://www.home-assistant.io/integrations/rest/, part “Fetch multiple JSON values and present them as attributes”).

I have the following on a custom sensor which publish this on http request :

{ "temperature": 23.1, "humidity": 42.2 }

So I have made the following code under sensor on configuration.yaml :

- platform: rest
  resource: http://192.168.1.42/dht22
  name: DHT22
  json_attributes: humidity
  scan_interval: 30
  value_template: '{{ value_json.temperature }}'
  unit_of_measurement: "°C"
- platform: template
  sensors:
    humidity:
      friendly_name: 'humidity'
      value_template: '{{ states.sensor.DHT22.attributes["humidity"] }}'
      unit_of_measurement: "%"

My DHT22 sensor is OK (I correctly get the temperature on HA).
However my humidity sensor always get the value “unavailable”.

I would be grateful if someone can help me.

Assuming your DHT22 rest sensor has the correct attribute ( look in developer tools / states menu to be sure):

- platform: template
  sensors:
    humidity:
      friendly_name: 'humidity'
      value_template: '{{ state_attr('sensor.dht22', 'humidity') }}'
      unit_of_measurement: "%"
1 Like

Hello.
Thank you for the answer.
The DHT22 is correct. In the devellopment tool / state menu, I have this :

humidity: 53.9
unit_of_measurement: °C
friendly_name: DHT22

So I have tried your code for the hudimity sensor : at configuration chek I have this error :

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/configuration.yaml", line 59, column 7
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 60, column 39

line 59, column 7 ==> correspond to the f of the line friendly_name
line 60, column 39 ==> correspond to the s in bold : sensor.dht22 of the line value_template.

Sorry, I stuffed the quotes up, it should be:

value_template: "{{ state_attr('sensor.dht22', 'humidity') }}"

Thank you ! It works now.

1 Like