How to save light state, then restore it after an action?

I have the following flow setup for my garage doors.

When the door opens, it grabs the state and attributes of my WLED lights, then changes them to blinking red.

I’d like it so when the garage door closes, it returns the effect to what it was previously, but this is not working.

I feel I’m about 90% there, but I need help getting that last 10%.

Any thoughts on what I’m doing wrong?

[{"id":"c2489f41.8e4aa","type":"switch","z":"583ad8e1.7bd1f8","name":"open closed","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"open","vt":"str"},{"t":"eq","v":"closed","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":470,"y":1300,"wires":[["901ed353.78035","7480ee41.862a"],["a2fca8b3.279828"]]},{"id":"901ed353.78035","type":"api-call-service","z":"583ad8e1.7bd1f8","name":"Red blinking","server":"bf8f8406.230b48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"{\"effect\":\"blink\",\"brightness\":\"38\",\"rgb_color\":[255,0,0]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":690,"y":1280,"wires":[[]]},{"id":"2350c89b.370688","type":"api-call-service","z":"583ad8e1.7bd1f8","name":"Lights to previous state","server":"bf8f8406.230b48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"\"{{msg.data.attributes}}\"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":890,"y":1400,"wires":[["97e01587.e801d8"]],"inputLabels":["msg.data"]},{"id":"98f40a73.754b48","type":"server-state-changed","z":"583ad8e1.7bd1f8","name":"Garage Doors Group","server":"bf8f8406.230b48","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"group.garage_doors","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":160,"y":1300,"wires":[["c2489f41.8e4aa"]]},{"id":"652aab2f.868504","type":"inject","z":"583ad8e1.7bd1f8","name":"open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"open","payloadType":"str","x":290,"y":1240,"wires":[["c2489f41.8e4aa"]]},{"id":"c7077d.8ad0c88","type":"inject","z":"583ad8e1.7bd1f8","name":"closed","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"closed","payloadType":"str","x":290,"y":1380,"wires":[["c2489f41.8e4aa"]]},{"id":"a2fca8b3.279828","type":"change","z":"583ad8e1.7bd1f8","name":"set msg.data to flow.data","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":1400,"wires":[["2350c89b.370688"]]},{"id":"7480ee41.862a","type":"api-current-state","z":"583ad8e1.7bd1f8","name":"","server":"bf8f8406.230b48","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.wled","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":660,"y":1200,"wires":[["ac5ba275.bdbb8"]]},{"id":"97e01587.e801d8","type":"debug","z":"583ad8e1.7bd1f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"data.attributes","targetType":"msg","statusVal":"","statusType":"auto","x":1190,"y":1400,"wires":[]},{"id":"ac5ba275.bdbb8","type":"change","z":"583ad8e1.7bd1f8","name":"","rules":[{"t":"set","p":"data","pt":"flow","to":"data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":890,"y":1200,"wires":[[]]},{"id":"2576530e.33b8ac","type":"debug","z":"583ad8e1.7bd1f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1090,"y":1200,"wires":[]},{"id":"bf8f8406.230b48","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I do this exact thing, just with a different trigger and WAY more actions in between :slight_smile:

Here’s the code. Make sure you click into the README comment for tiny instructions. This also assumes you are using MQTT for WLED, since that’s the only way to set all the attributes at once:

[{"id":"af9a73b1.1a138","type":"api-current-state","z":"4bd0ade.d5fdf54","name":"Light current state","server":"74d5538b.e0092c","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.cabinet_leds","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":610,"y":1900,"wires":[["7ea06828.0abf48"]]},{"id":"a5a69eb1.a78c2","type":"api-call-service","z":"4bd0ade.d5fdf54","name":"Reset lights","server":"74d5538b.e0092c","version":1,"debugenabled":true,"service_domain":"mqtt","service":"publish","entityId":"","data":"{\"topic\":\"wled/kitchen/api\",\"payload\":\"{{{payload}}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1210,"y":1920,"wires":[[]]},{"id":"7ea06828.0abf48","type":"function","z":"4bd0ade.d5fdf54","name":"Set payload","func":"const { state, attributes: attr } = msg.data;\n\nif (state === 'off') {\n    return [msg, null];\n}\n\nconst effect = attr.effect_list.indexOf(attr.effect);\nmsg = {\n    payload: `FX=${effect}&IX=${attr.intensity}&SX=${attr.speed}&R=${attr.rgb_color[0]}&G=${attr.rgb_color[1]}&B=${attr.rgb_color[2]}&A=${attr.brightness}`\n};\n\nreturn [null, msg];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1010,"y":1900,"wires":[["69c4f767.30b5d8"],["a5a69eb1.a78c2"]]},{"id":"69c4f767.30b5d8","type":"api-call-service","z":"4bd0ade.d5fdf54","name":"Turn off","server":"74d5538b.e0092c","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.cabinet_leds","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1200,"y":1880,"wires":[[]]},{"id":"ddaa7cb2.bbc7","type":"inject","z":"4bd0ade.d5fdf54","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":410,"y":1900,"wires":[["af9a73b1.1a138"]]},{"id":"1b9e8725.33c6d9","type":"comment","z":"4bd0ade.d5fdf54","name":"README!","info":"Place your actions where this comment appears.\n\n\nThe `Light current state` node grabs the light's current state by passing `msg.data` the entity. This includes all of the information that HA has about the wled entity.\n\nFrom there, you can add a bunch of actions where this comment appears. Make sure none of your actions override the `msg.data` of the message flow!\n\nThe next node sets the MQTT API payload up together using a JavaScript string template, built from the entity information that we stored in `msg.data` in that first node.\n\nFrom there, we decide if we should turn the light off or if we should pass the entity data to MQTT and turn the light on.","x":830,"y":1900,"wires":[]},{"id":"74d5538b.e0092c","type":"server","name":"Home Assistant","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

edit: You will probably also want some sort of delay in between your actions and the Set payload function, otherwise everything might just flash quickly.

I would use create scene service with scene called something like door_opened or whatever and then after closing door apply this scene

Problem with this is that my WLEDs could be on any effect, and not a static one like a scene would be configured for

This looks promising! I’ll try this one out

After a lot of trial and error, I was able to get it working with the below config:

[{"id":"af9a73b1.1a138","type":"api-current-state","z":"583ad8e1.7bd1f8","name":"Light current state","server":"bf8f8406.230b48","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.wled","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":670,"y":1380,"wires":[["b892fa37.3eef48","335d66e9.30ce2a"]]},{"id":"6ea614e7.1268dc","type":"server-state-changed","z":"583ad8e1.7bd1f8","name":"Garage Doors Group","server":"bf8f8406.230b48","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"group.garage_doors","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":160,"y":1380,"wires":[["f32c55f8.6ac4e8"]]},{"id":"f32c55f8.6ac4e8","type":"switch","z":"583ad8e1.7bd1f8","name":"open closed","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"open","vt":"str"},{"t":"eq","v":"closed","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":1380,"wires":[["af9a73b1.1a138"],["9a819db8.f17eb"]]},{"id":"b892fa37.3eef48","type":"api-call-service","z":"583ad8e1.7bd1f8","name":"Red blinking","server":"bf8f8406.230b48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"{\"effect\":\"blink\",\"brightness\":\"38\",\"rgb_color\":[255,0,0]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":970,"y":1380,"wires":[[]]},{"id":"5bf88cea.d77a34","type":"inject","z":"583ad8e1.7bd1f8","name":"open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"open","payloadType":"str","x":190,"y":1300,"wires":[["f32c55f8.6ac4e8"]]},{"id":"63a295c.922136c","type":"inject","z":"583ad8e1.7bd1f8","name":"closed","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"closed","payloadType":"str","x":190,"y":1440,"wires":[["f32c55f8.6ac4e8"]]},{"id":"335d66e9.30ce2a","type":"change","z":"583ad8e1.7bd1f8","name":"save entity data to flow.data","rules":[{"t":"set","p":"data","pt":"flow","to":"data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1020,"y":1320,"wires":[[]]},{"id":"9a819db8.f17eb","type":"change","z":"583ad8e1.7bd1f8","name":"switch flow.data back to msg.data","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1460,"wires":[["912665d2.ce1ac8"]]},{"id":"912665d2.ce1ac8","type":"api-call-service","z":"583ad8e1.7bd1f8","name":"Reset Lights","server":"bf8f8406.230b48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"{\t   \"effect\": data.attributes.effect,\t   \"brightness\": data.attributes.brightness,\t   \"rgb_color\":[\t       data.attributes.rgb_color[0],\t       data.attributes.rgb_color[1],\t       data.attributes.rgb_color[2]\t   ]\t}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":970,"y":1460,"wires":[[]]},{"id":"bf8f8406.230b48","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Actually found a bug that if the light was off, this wouldn’t complete because of an API error. After some more testing, this seems to be working well:

Hope this helps someone else in the future!

[{"id":"af9a73b1.1a138","type":"api-current-state","z":"583ad8e1.7bd1f8","name":"Light current state","server":"bf8f8406.230b48","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.wled","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":670,"y":1380,"wires":[["b892fa37.3eef48","335d66e9.30ce2a"]]},{"id":"6ea614e7.1268dc","type":"server-state-changed","z":"583ad8e1.7bd1f8","name":"Garage Doors Group","server":"bf8f8406.230b48","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"group.garage_doors","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":160,"y":1380,"wires":[["f32c55f8.6ac4e8"]]},{"id":"f32c55f8.6ac4e8","type":"switch","z":"583ad8e1.7bd1f8","name":"open closed","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"open","vt":"str"},{"t":"eq","v":"closed","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":1380,"wires":[["af9a73b1.1a138"],["9a819db8.f17eb"]]},{"id":"b892fa37.3eef48","type":"api-call-service","z":"583ad8e1.7bd1f8","name":"Red blinking","server":"bf8f8406.230b48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"{\"effect\":\"blink\",\"brightness\":\"38\",\"rgb_color\":[255,0,0]}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":970,"y":1380,"wires":[[]]},{"id":"5bf88cea.d77a34","type":"inject","z":"583ad8e1.7bd1f8","name":"open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"open","payloadType":"str","x":190,"y":1320,"wires":[["f32c55f8.6ac4e8"]]},{"id":"63a295c.922136c","type":"inject","z":"583ad8e1.7bd1f8","name":"closed","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"closed","payloadType":"str","x":190,"y":1440,"wires":[["f32c55f8.6ac4e8"]]},{"id":"335d66e9.30ce2a","type":"change","z":"583ad8e1.7bd1f8","name":"save entity data to flow.data","rules":[{"t":"set","p":"data","pt":"flow","to":"data","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1040,"y":1320,"wires":[[]]},{"id":"9a819db8.f17eb","type":"change","z":"583ad8e1.7bd1f8","name":"switch flow.data back to msg.data","rules":[{"t":"set","p":"data","pt":"msg","to":"data","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":1460,"wires":[["5d4261e3.45b2d"]]},{"id":"912665d2.ce1ac8","type":"api-call-service","z":"583ad8e1.7bd1f8","name":"Reset Lights","server":"bf8f8406.230b48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"{\t   \"effect\": data.attributes.effect,\t   \"brightness\": data.attributes.brightness,\t   \"rgb_color\":[\t       data.attributes.rgb_color[0],\t       data.attributes.rgb_color[1],\t       data.attributes.rgb_color[2]\t   ]\t}","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1290,"y":1440,"wires":[[]]},{"id":"5d4261e3.45b2d","type":"switch","z":"583ad8e1.7bd1f8","name":"previously on or off?","property":"data.state","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1000,"y":1460,"wires":[["912665d2.ce1ac8"],["dd50ad4e.d19a4"]]},{"id":"dd50ad4e.d19a4","type":"api-call-service","z":"583ad8e1.7bd1f8","name":"turn off light","server":"bf8f8406.230b48","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.wled","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1290,"y":1500,"wires":[[]]},{"id":"bf8f8406.230b48","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

I’m new to HA. Just a question: how did you create these state machine graphical views? Thanks!

There’s an addon you can install called Node-RED that uses this visual/flow programming style for automations. It is a full separate app from Home Assistant and can do a lot more stuff, but the HA integration is really good.

thanks a lot!