Template 'states' out of a rest sensor

Trying to rebuild the hue sensors via rest sensors, Im running into this brain-lock how to template these ‘states’ out of the main sensor.

sensor.attic_sensor_state so using

{{states.sensor.attic_sensor_state.state}} to show the above

{{states.sensor.attic_sensor_state.state.lightlevel}} reveals an empty window…


{{ states.attic_sensor_state.attributes.lightlevel }}

{{ states.sensor.attic_sensor_state.attributes }}


as was already shown in the first screen… thats no surprise. Strange I cant filter out the states-map though

for completeness sake, this is the sensor in the Hue-api:

“12”:{“state”:{“presence”:false,“lastupdated”:“2018-06-28T09:27:15”},“swupdate”:{“state”:“noupdates”,“lastinstall”:null},“config”:{“on”:true,“battery”:100,“reachable”:true,“alert”:“none”,“ledindication”:false,“usertest”:false,“sensitivity”:2,“sensitivitymax”:2,“pending”:[]},“name”:“Attic sensor”,“type”:“ZLLPresence”,“modelid”:“SML001”,“manufacturername”:“Philips”,“productname”:“Hue motion sensor”,“swversion”:“”,“uniqueid”:“unique_id”,“capabilities”:{“certified”:true}},

“13”:{“state”:{“lightlevel”:13604,“dark”:true,“daylight”:false,“lastupdated”:“2018-06-28T11:32:04”},“swupdate”:{“state”:“noupdates”,“lastinstall”:null},“config”:{“on”:true,“battery”:100,“reachable”:true,“alert”:“none”,“tholddark”:16000,“tholdoffset”:7000,“ledindication”:false,“usertest”:false,“pending”:[]},“name”:“Hue ambient light sensor 8”,“type”:“ZLLLightLevel”,“modelid”:“SML001”,“manufacturername”:“Philips”,“productname”:“Hue ambient light sensor”,“swversion”:“”,“uniqueid”:“unique_id”,“capabilities”:{“certified”:true}}


  - platform: rest
    resource: !secret motion_sensor_12
    method: GET
    name: Attic sensor config
    scan_interval: 1
    value_template: >

  - platform: rest
    resource: !secret ambient_light_sensor_13
    method: GET
    name: Attic sensor state
    scan_interval: 1
    value_template: >

Have you tried:

{{ states.sensor.attic_sensor_state.state['lightlevel'] }}

yess… showing null…

I’ll bet the issue is, even though the state looks like a dictionary, it’s probably a string. Try this:

{{ states.sensor.attic_sensor_state.state is mapping }}

If that returns False, then that confirms my suspicion. In which case, I’m not sure how to turn it back into a dictionary. But, you may be able to use a regex to extract the piece of data you want. Something like:

{{ states('sensor.attic_sensor_state')|regex_findall_index("'lightlevel': [0-9]*")|replace("'lightlevel': ","") }}
1 Like

I don’t think you can turn it back into a dictionary, just grab what you need w/ regex and go! Or even a simple search using “‘lightlevel’: .*,”

Yep, I suppose there’s several patterns that could work. Just be careful, if the order changes, there might not be a comma at the end. So "'lightlevel': [^,}]*" might be better, assuming, of course, the value isn’t a string that might contain those characters. :wink:

1 Like

well thats sounds all very complicated, tbh, we should really be able to do just this, but then with a rest sensor. Create 1 base sensor containing all sensor info from the Hub and then template the rest out of it:

  - platform: jsonrest
    resource: !secret hue_sensors_resource
    method: GET
    name: Philips Hue Sensors
    scan_interval: 1

  - platform: template

        friendly_name: 'Update Dimmer switch'
        value_template: "{{states.sensor.philips_hue_sensors.attributes['2'].state.lastupdated | timestamp_custom ('%H:%M')}}"
        friendly_name: 'Battery Dimmer switch'
        value_template: "{{states.sensor.philips_hue_sensors.attributes['2'].config.battery}}"
        unit_of_measurement: '%'

        friendly_name: 'Temperatuur Corridor'
        value_template: "{{states.sensor.philips_hue_sensors.attributes['4'].state.temperature | float / 100}}"
        unit_of_measurement: °C
        friendly_name: ' Battery Corridor'
        value_template: "{{states.sensor.philips_hue_sensors.attributes['4'].config.battery}}"
        unit_of_measurement: '%'
        friendly_name: 'Motion sensor Corridor'
        value_template: "{{states.sensor.philips_hue_sensors.attributes['5'].state.presence}}"
#        unit_of_measurement: 'Name'


had to take the rest sensors out again, because of the system strain it causes… up to spontaneous reboots.

I know this is an old thread, but I learned something recently about the RESTful Sensor. Although the state will always be a string (even if it looks like a dictionary), the same is not true for json_attributes. So, e.g., for your ambient_light_sensor_13, you could do this instead:

  - platform: rest
    resource: !secret ambient_light_sensor_13
    method: GET
    name: Attic ambient light
    scan_interval: 1
    value_template: "{{ value_json.state.lightlevel }}"
      - state
      - swupdate
      - config

Then the state of sensor.attic_ambient_light would be lightlevel. But, in addition, you could get any of the other attributes - e.g., {{ states.sensor.attic_ambient_light.attributes.state.lastupdated }} or {{ states.sensor.attic_ambient_light.attributes.config.battery }}.


thank you! very elegant indeed.
will certainly set this up and see if it is worth the effort replacing the Hue CC. Hue sensors were supposed to be natively supported anytime soon, but is has been very silent on that department.

hope it wont add to the issues many other rest sensors result in in my setup.