REST not extracting keys with 'JSON_Attributes'

Can anyone tell me why I can’t seem to get the following set as attributes?

curl output:

{“temperature”:{“poolTemp”:75,“spaTemp”:75,“airTemp”:77,“solarTemp”:0,“freeze”:0,“poolSetPoint”:55,“poolHeatMode”:0,“poolHeatModeStr”:“OFF”,“spaSetPoint”:98,“spaManualHeatMode”:“On”,“spaHeatMode”:1,“spaHeatModeStr”:“Heater”,“heaterActive”:1}}

yaml code:

sensor:
  - platform: rest
    name: Pool Temperature
    json_attributes:
      - poolTemp
      - spaTemp
      - spaSetPoint
    resource: http://192.168.2.124:3000/temperature
    value_template: '{{ value_json.temperature }}'

result:

Because the RESTful Sensor only allows top-level keys to be called out under json_attributes. So, in your case, that would be temperature. You might want to do this instead:

sensor:
  - platform: rest
    name: Pool Temperature
    json_attributes:
      - temperature
    resource: http://192.168.2.124:3000/temperature
    value_template: '{{ value_json.temperature.poolTemp }}'

Then you can get at the other values like this:

{{ state_attr('sensor.pool_temperature', 'temperature.spaTemp') }}
1 Like

Thanks. Still having issues.

The only way it will handle the yaml is if I wrap quotes around it… but that results in an empty sensor with no value. Am I doing this wrong?

  - platform: rest
    name: Pool Temperature
    json_attributes:
      - temperature
    resource: http://192.168.2.124:3000/temperature
    value_template: '{{ value_json.poolTemp }}'
  - platform: template
    sensors:
      pool_temp:
        friendly_name: 'Pool Temperature'
        value_template: "{{ state_attr('sensor.pool_temperature', 'temperature.poolTemp') }}"

Try:

value_template: "{{ state_attr('sensor.pool_temperature', 'temperature').poolTemp }}"
1 Like

Thanks, that worked. I assume there is, but is there any rhyme or reason as to the placement of the parenthesis? Never would have figured that one out.

The state_attr function takes two parameters - the first is the entity_id, and the second is the name of the attribute. I wasn’t sure if the latter would accept an expression (my first suggestion) or just the name of an attribute (the one that worked.) Once state_attr returns the attribute you can use it “normally”. Since this particular attribute has “attributes” itself, I then accessed poolTemp by using the dot operator. Generally I recommend using the state_attr function, but in this case it could also reasonably be written this way:

value_template: "{{ states.sensor.pool_temperature.attributes.temperature.poolTemp }}"

Two days to find the correct answer. This post. Thanks @pnbruckner

1 Like