Return a Light to a Prior (Prior) State

I have a flow:

[{"id":"8ff18cfd6c25cca5","type":"api-call-service","z":"2ce2e6f1666de406","name":"Light Off","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.backyard_house_sconce_1"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1480,"y":640,"wires":[[]]},{"id":"41a831c8a7d0cd82","type":"api-call-service","z":"2ce2e6f1666de406","name":"Restore Lights","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"scene","service":"turn_on","areaId":[],"deviceId":[],"entityId":["scene.before"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1300,"y":640,"wires":[["8ff18cfd6c25cca5"]]},{"id":"6c35f34cbb6727df","type":"trigger","z":"2ce2e6f1666de406","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"20","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1110,"y":640,"wires":[["41a831c8a7d0cd82"]]},{"id":"8112aa0d6c65d5a3","type":"api-call-service","z":"2ce2e6f1666de406","name":"Light Green","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["83b837db33f4f0f4658aceff6f445719"],"entityId":[],"data":"{\"color_name\":\"green\", \"brightness_pct\":100}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":890,"y":660,"wires":[["6c35f34cbb6727df"]]},{"id":"6db195116d1bcaf1","type":"api-call-service","z":"2ce2e6f1666de406","name":"Light Flash","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["83b837db33f4f0f4658aceff6f445719"],"entityId":[],"data":"{\t   \"flash\":\"long\"\t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1030,"y":580,"wires":[["6c35f34cbb6727df"]]},{"id":"8b29c66a0b3ce92b","type":"api-current-state","z":"2ce2e6f1666de406","name":"Gate Status","server":"6e751b1b.8f17c4","version":3,"outputs":2,"halt_if":"closed","halt_if_type":"str","halt_if_compare":"is_not","entity_id":"cover.gate","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":690,"y":640,"wires":[["13d97a6dfd9a6271"],["8112aa0d6c65d5a3"]]},{"id":"13d97a6dfd9a6271","type":"api-call-service","z":"2ce2e6f1666de406","name":"Light Red","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["83b837db33f4f0f4658aceff6f445719"],"entityId":[],"data":"{\"color_name\":\"red\",\"brightness_pct\":100}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":880,"y":600,"wires":[["6db195116d1bcaf1"]]},{"id":"478219914ae0c7fc","type":"api-call-service","z":"2ce2e6f1666de406","name":"Save Light State","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"domain":"scene","service":"create","areaId":[],"deviceId":[],"entityId":[],"data":"{\"scene_id\":\"before\",\"snapshot_entities\":[\"light.backyard_house_sconce_1\"]}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":440,"y":660,"wires":[["8b29c66a0b3ce92b"]]},{"id":"86df51de9e773b1b","type":"server-state-changed","z":"2ce2e6f1666de406","name":"Dining Room Door North Open","server":"6e751b1b.8f17c4","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.dining_room_door_north_contact","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":170,"y":660,"wires":[["478219914ae0c7fc"],[]]},{"id":"6e751b1b.8f17c4","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

The design of the flow is to capture the state of a single light and then change the color of the light based upon whether a gate is open/close and then restore the prior state.

Example: Door Opens and Gate is Open (light is off) → Light turns on/flashes red and then turns off.

If, however the door is opened a second time while the light is green or while flashing red that becomes the prior scene and now my light stays red/green for the rest of the day.

Can someone help me fix this?

Create a scene, then restore the scene you created.

Did that. This door constantly opens/closes.

Let’s say the automation takes 20 seconds to run and the light is white. The user opens the door, the automation captures the white light and turns the light red. However, the door is then opened again before the scene can restore. Since the light is now red (because the scene could not restore), red is captured as the new scene and when the automation completes, the light will restore to red.

Using scenes to capture state is still the easiest way to restore a situation (just too bad it does not work well with media players, because their attributes disappear when the media player is off. But that is another annoyance all together).

How to set and restore states is also separate from your question. To prevent accidentally capturing the state of the temporary change, you need to stop the automation from capturing a new state when the temporary change is running.

If you use an automation for that that has delays in it, you could add a condition that that automation must not be active (create a script for the temporary state, and check for that).

If you use timers for the temporary state, you could simply check that that timer should be idle.

By doing this the flow will only trigger once every cycle is completed

Or just rate limit your flow in some other way.

[{"id":"8ff18cfd6c25cca5","type":"api-call-service","z":"878e74c2.7f39c8","name":"Light Off","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"service":"turn_off","entityId":["light.backyard_house_sconce_1"],"data":"","dataType":"jsonata","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1440,"y":2100,"wires":[[]]},{"id":"41a831c8a7d0cd82","type":"api-call-service","z":"878e74c2.7f39c8","name":"Restore Lights","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"service":"turn_on","entityId":["scene.before"],"data":"","dataType":"jsonata","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1260,"y":2100,"wires":[["8ff18cfd6c25cca5"]]},{"id":"6c35f34cbb6727df","type":"trigger","z":"878e74c2.7f39c8","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"20","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1070,"y":2100,"wires":[["41a831c8a7d0cd82","96f201ec.b0b6f"]]},{"id":"8112aa0d6c65d5a3","type":"api-call-service","z":"878e74c2.7f39c8","name":"Light Green","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"service":"turn_on","entityId":[],"data":"{\"color_name\":\"green\", \"brightness_pct\":100}","dataType":"jsonata","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":850,"y":2120,"wires":[["6c35f34cbb6727df"]]},{"id":"6db195116d1bcaf1","type":"api-call-service","z":"878e74c2.7f39c8","name":"Light Flash","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"service_domain":"","service":"turn_on","entityId":[],"data":"{\t   \"flash\":\"long\"\t}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":990,"y":2040,"wires":[["6c35f34cbb6727df"]]},{"id":"8b29c66a0b3ce92b","type":"api-current-state","z":"878e74c2.7f39c8","name":"Gate Status","server":"6e751b1b.8f17c4","version":3,"outputs":2,"halt_if":"closed","halt_if_type":"str","halt_if_compare":"is_not","entity_id":"cover.gate","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":650,"y":2100,"wires":[["13d97a6dfd9a6271"],["8112aa0d6c65d5a3"]]},{"id":"13d97a6dfd9a6271","type":"api-call-service","z":"878e74c2.7f39c8","name":"Light Red","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"service":"turn_on","entityId":[],"data":"{\"color_name\":\"red\",\"brightness_pct\":100}","dataType":"jsonata","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":840,"y":2060,"wires":[["6db195116d1bcaf1"]]},{"id":"478219914ae0c7fc","type":"api-call-service","z":"878e74c2.7f39c8","name":"Save Light State","server":"6e751b1b.8f17c4","version":5,"debugenabled":false,"service":"create","entityId":[],"data":"{\"scene_id\":\"before\",\"snapshot_entities\":[\"light.backyard_house_sconce_1\"]}","dataType":"json","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":400,"y":2120,"wires":[["8b29c66a0b3ce92b"]]},{"id":"9b4f61ad.bcc81","type":"trigger-state","z":"878e74c2.7f39c8","name":"dining room on?","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.dining_room_door_north_contact","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on","propertyValue":"new_state.state"}],"inputs":1,"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","enableInput":true,"x":200,"y":2120,"wires":[["478219914ae0c7fc","2142b863.715a88"],[]]},{"id":"2142b863.715a88","type":"change","z":"878e74c2.7f39c8","name":"disable","rules":[{"t":"set","p":"payload","pt":"msg","to":"disable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":200,"y":2060,"wires":[["9b4f61ad.bcc81"]]},{"id":"96f201ec.b0b6f","type":"change","z":"878e74c2.7f39c8","name":"enable","rules":[{"t":"set","p":"payload","pt":"msg","to":"enable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":2220,"wires":[["9b4f61ad.bcc81"]]},{"id":"6e751b1b.8f17c4","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
1 Like