Read state of a hue lamp, and use this state later in the flow

Hi, I’m a new user. For years I’ve used Pimatic but it’s important that development continues and it didn’t. So I started to look further and took a shoot with Home Assistant. I’ve choosed Hass.io because of the easy setup and, very important for me, Node-Red.

I’ve got almost everything running. The same as on Pimatic.
Almost. I have a sensor and if it’s activated i want to turn on my Hue lamp to the max for 20 seconds. After the 20 seconds I want it to return to it’s original state. If it was turned off, then it must be turned on. If it was orange on 60% brightness then I want that setting.

How can I read the current state of the Hue lamp and return to that state after the delay?

Hope someone can help me!

You can use flow context to store the state. Now the output of the light state contains attributes that cannot be passed as a service call so you have to pick, also sometimes depending on the lights you cannot combine parameters (color_temp vs rgb_color, etc). This is very basic but the idea is that the call_service node accepts an input to fill all the fields payload.service, payload.domain and payload.data. So store flow.lightState the whole payload that is gonna be used to set the light back.

[{"id":"47d61b16.4bfe14","type":"inject","z":"4bad5756.743d58","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":200,"wires":[["d1c28401.6845f8"]]},{"id":"d1c28401.6845f8","type":"api-current-state","z":"4bad5756.743d58","name":"","server":"e0d90e7a.95517","halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":true,"override_payload":true,"override_data":true,"entity_id":"light.dining_table","state_type":"str","outputs":1,"x":430,"y":200,"wires":[["c67b9c40.1e714"]]},{"id":"6ff82db6.68c944","type":"debug","z":"4bad5756.743d58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1050,"y":200,"wires":[]},{"id":"c67b9c40.1e714","type":"switch","z":"4bad5756.743d58","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":200,"wires":[["1430417e.d4fe3f"],["af1d4ce8.60667"]]},{"id":"1430417e.d4fe3f","type":"change","z":"4bad5756.743d58","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"{ \"service\" : \"turn_on\",\t  \"data\" : { \t  \"entity_id\" : $string(topic),\t  \"brightness\" : $number(data.attributes.brightness),\t  \"rgb_color\" : data.attributes.rgb_color\t  }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":160,"wires":[["6ff82db6.68c944"]]},{"id":"af1d4ce8.60667","type":"change","z":"4bad5756.743d58","name":"","rules":[{"t":"set","p":"lightState","pt":"flow","to":"{ \"service\" : \"turn_off\",\t  \"data\" : { \t  \"entity_id\" : $string(topic)\t  }\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":220,"wires":[["6ff82db6.68c944"]]},{"id":"6d14209d.f12ff","type":"inject","z":"4bad5756.743d58","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":340,"wires":[["561b95ab.7dca2c"]]},{"id":"561b95ab.7dca2c","type":"change","z":"4bad5756.743d58","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"lightState","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":340,"wires":[["c659f32a.951b","16a34f34.316cd1"]]},{"id":"c659f32a.951b","type":"api-call-service","z":"4bad5756.743d58","name":"","server":"e0d90e7a.95517","service_domain":"light","service":"turn_on","data":"","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":650,"y":340,"wires":[[]]},{"id":"16a34f34.316cd1","type":"debug","z":"4bad5756.743d58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":550,"y":400,"wires":[]},{"id":"e0d90e7a.95517","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open"}]

Change the entity_id of course, in the get state node.

turn on your light via ha, change brightness or color. Then press the first inject, turn off now your lamp then press the bottom inject.

1 Like

Thanks for your response. I’m gonna try this.

This just saved me hours of searching and frustration lol. Thank you so much!