Using State_attr in node-red for Philips hue sensor. Reading light level

Hi

I’m completely stuck and out of ideas.
2 days trying to solve this issue!

I want to pull the attribute ‘lightlevel’ from a Philips hue motion sensor using node-red.

I want to do it right (judged by documentation) by using state_attr() but I’ve tried the template node and function node, with no luck.
Maybe the whole idea with template eludes me :frowning:

I wanted to paste what flow i tried but can’t copy from phone, sorry.

Can it be that state_attr isn’t working in node red, but only directly in configuration files?

This was the node trying to use

[{"id":"c6cca8c8.611828","type":"api-render-template","z":"b5a4f9b1.de8f78","name":"Kitchen Lux","server":"c07167d6.bc6238","template":"{ { state_attr('sensor.hue_motion_sensor_1_light_level', 'lightlevel') } };\n","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":410,"y":1180,"wires":[["97549345.1c8c3"]]},{"id":"c07167d6.bc6238","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

{{ state_attr('sensor.hue_motion_sensor_1_light_level', 'lightlevel') }}

This in a template node gives following in debug:

{"_msgid":"7f95d0f1.a7512","topic":"","payload":"{ { state_attr('sensor.hue_motion_sensor_1_light_level', 'lightlevel') } };","template":"{ { state_attr('sensor.hue_motion_sensor_1_light_level', 'lightlevel') } };\n"}

With nodes as following:

[{"id":"d4513e27.2e925","type":"inject","z":"b5a4f9b1.de8f78","name":"Test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":1720,"wires":[["76abf4e6.13464c"]]},{"id":"76abf4e6.13464c","type":"api-render-template","z":"b5a4f9b1.de8f78","name":"Kitchen Lux","server":"c07167d6.bc6238","template":"{ { state_attr('sensor.hue_motion_sensor_1_light_level', 'lightlevel') } };\n","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":350,"y":1720,"wires":[["ff943440.cd9d28"]]},{"id":"ff943440.cd9d28","type":"debug","z":"b5a4f9b1.de8f78","name":"Test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":530,"y":1720,"wires":[]},{"id":"c07167d6.bc6238","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

And when trying with

{ { states.sensor.hue_motion_sensor_1_light_level.attributes.lightlevel } };

Gives following

{"_msgid":"4bb44c23.7cfb34","topic":"","payload":"{ { states.sensor.hue_motion_sensor_1_light_level.attributes.lightlevel } };","template":"{ { states.sensor.hue_motion_sensor_1_light_level.attributes.lightlevel } };"}

By using

[{"id":"844971a0.a399f","type":"debug","z":"b5a4f9b1.de8f78","name":"Test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":510,"y":1840,"wires":[]},{"id":"a541eb94.72c1d8","type":"api-render-template","z":"b5a4f9b1.de8f78","name":"Kitchen Lux","server":"c07167d6.bc6238","template":"{ { states.sensor.hue_motion_sensor_1_light_level.attributes.lightlevel } };","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":330,"y":1840,"wires":[["844971a0.a399f"]]},{"id":"db8a21f5.29ccb","type":"inject","z":"b5a4f9b1.de8f78","name":"Test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":1840,"wires":[["a541eb94.72c1d8"]]},{"id":"c07167d6.bc6238","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

This group

[{"id":"e42c0edf.95743","type":"function","z":"b5a4f9b1.de8f78","name":"Lux","func":"msg.payload = { data: {'message': msg.data.attributes.lightlevel } };\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":1880,"wires":[["844971a0.a399f"]]},{"id":"844971a0.a399f","type":"debug","z":"b5a4f9b1.de8f78","name":"Test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":550,"y":1880,"wires":[]},{"id":"db8a21f5.29ccb","type":"inject","z":"b5a4f9b1.de8f78","name":"Test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":1880,"wires":[["b624e954.317bb8"]]},{"id":"b624e954.317bb8","type":"api-current-state","z":"b5a4f9b1.de8f78","name":"Get data","server":"468387c0.b24028","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.hue_motion_sensor_1_light_level","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":300,"y":1880,"wires":[["e42c0edf.95743"]]},{"id":"468387c0.b24028","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

Gave a new response

{"_msgid":"21fa2073.1cee8","topic":"","payload":{"data":{"message":1884}},"data":{"entity_id":"sensor.hue_motion_sensor_1_light_level","state":"1.54","attributes":{"battery_level":100,"lightlevel":1884,"daylight":false,"dark":true,"threshold_dark":16000,"threshold_offset":7000,"unit_of_measurement":"lx","friendly_name":"Hue motion sensor 1 light level","device_class":"illuminance"},"last_changed":"2019-09-29T17:33:47.028971+00:00","last_updated":"2019-09-29T17:33:47.028971+00:00","context":{"id":"4d5c27fe72d9400ebad1d455d10962ac","parent_id":null,"user_id":null},"timeSinceChangedMs":7445442}}

Now I got it to single out the data i wanted, just need correct formatting.
I will try to work with this tomorrow.

SUCCESS!!

Getting lightlevel attributes from Philips Hue Motion or PIR sensor using Node-red

[{"id":"12504ed6.e446e1","type":"change","z":"b5a4f9b1.de8f78","name":"Move Lightlevel to Payload","rules":[{"t":"move","p":"data.attributes.lightlevel","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1960,"wires":[["c0f62ca6.1295d"]]},{"id":"588c5f45.d8ce1","type":"api-current-state","z":"b5a4f9b1.de8f78","name":"Get data","server":"468387c0.b24028","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.hue_motion_sensor_1_light_level","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":300,"y":1960,"wires":[["12504ed6.e446e1"]]},{"id":"780c21a.1da0ee","type":"inject","z":"b5a4f9b1.de8f78","name":"Test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":1960,"wires":[["588c5f45.d8ce1"]]},{"id":"c0f62ca6.1295d","type":"debug","z":"b5a4f9b1.de8f78","name":"Test","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":690,"y":1960,"wires":[]},{"id":"468387c0.b24028","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

If it can be done in anyway simpler, I’m interested!

Result in:

{"_msgid":"59b0bb99.f98974","topic":"","payload":8619,"data":{"entity_id":"sensor.hue_motion_sensor_1_light_level","state":"7.27","attributes":{"battery_level":100,"daylight":false,"dark":true,"threshold_dark":16000,"threshold_offset":7000,"unit_of_measurement":"lx","friendly_name":"Hue motion sensor 1 light level","device_class":"illuminance"},"last_changed":"2019-09-30T16:39:12.029691+00:00","last_updated":"2019-09-30T16:39:12.029691+00:00","context":{"id":"5371ce146f144db39a4185cb792664d8","parent_id":null,"user_id":null},"timeSinceChangedMs":92720}}

Hi, is this the light level value or is it the lux value you get? I seem to be confused with these two…