How to handle switches that are accidentally turned off but involved in automations?

In Node-Red, I have a routine setup that automatically turns on the light for 5 mins when motion is detected, then turns it off. It’s set for 5 mins because that’s the cycle time of the GE motion sensor while it’s on battery power.

This is the node-red routine:

One problem I have is that the light is connected to a GE ZWave switch, which people mistakenly turn off sometimes. There are many reasons why I want to still keep the switch, but I want graceful recovery to get back into the right state when I ultimately turn the switch back on.

What’s the right way in Node-Red to handle this, so that if the switch is cycled, it will get back into the right state ready for my automation routine to be handled as-is? When the switch is turned off then back on, the light remains on so the automation is essentially broken.

I think what I want is when a switch is cycled (off then back on), there is a timer that automatically turns the light off so it is back in a good starting state.

Hello @tmchow, I have the exact setup as you, the only difference it the sensor. I just tested mine and if I turn off the switch GE switch on the wall that automation runs regardless.

Here is the snip, hope it helps.
[{"id":"29f88c6.8763174","type":"switch","z":"5717166.f78c0e8","name":"The Switch","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":120,"wires":[["dccd175b.be0ce8"],[]]},{"id":"53323b7.affa7c4","type":"server-state-changed","z":"5717166.f78c0e8","name":"Livingroom Motion Sensor","server":"f0aa0465.fa7ce8","entityidfilter":"binary_sensor.living_room_motion_sensor_sensor","entityidfiltertype":"substring","haltifstate":"","x":150,"y":120,"wires":[["ed621e7e.5545"]]},{"id":"10dc07aa.bb7068","type":"api-call-service","z":"5717166.f78c0e8","name":"Off","server":"f0aa0465.fa7ce8","service_domain":"switch","service":"turn_off","data":"{\"entity_id\":\"switch.entrance_light_switch\"}","mergecontext":"","x":1270,"y":120,"wires":[[]]},{"id":"60fef4a0.5e252c","type":"api-call-service","z":"5717166.f78c0e8","name":"On","server":"f0aa0465.fa7ce8","service_domain":"switch","service":"turn_on","data":"{\"entity_id\":\"switch.entrance_light_switch\"}","mergecontext":"","x":1270,"y":80,"wires":[[]]},{"id":"dccd175b.be0ce8","type":"api-current-state","z":"5717166.f78c0e8","name":"Foyer light","server":"f0aa0465.fa7ce8","halt_if":"on","override_topic":true,"override_payload":true,"entity_id":"light.livingroom_dimmer_level","x":850,"y":120,"wires":[["7a316926.df40b8","60fef4a0.5e252c"]]},{"id":"7a316926.df40b8","type":"stoptimer","z":"5717166.f78c0e8","duration":"59","units":"Second","payloadtype":"str","payloadval":"off","name":"","x":1070,"y":120,"wires":[["10dc07aa.bb7068"],[]]},{"id":"ed621e7e.5545","type":"time-range-switch","z":"5717166.f78c0e8","name":"Turn light on if movement","lat":"32.8614870","lon":"-97.2787370","startTime":"20:00","endTime":"06:30","startOffset":0,"endOffset":0,"x":410,"y":120,"wires":[["29f88c6.8763174"],[]]},{"id":"f0aa0465.fa7ce8","type":"server","z":"","name":"Home Assistant","url":"http:\\\\192.168.2.999:8123","pass":"PSW"}]

Thanks for the reply.

One thing I should’ve been clear on is that what I’m trying to resolve is when the switch is accidentally turned off, then I turn it back on, the light turns on and stays on.

I want it to go back to off, so the automation will run correctly.

Yes, what I posted does that. If I turn the light on before the automation, when the automation does run it shut off the light after 1 minute (in my case) and everything is back in sync. If I turn off the light manually and turn it back on, once the motion sensor is tripped again the light turns off a minute later.

Thus far I haven’t been able to make the light stay on all the time or stay off all of the time regardless of the switch position. Import my flow and take a look at the settings in several of the nodes. Or if you post your bits / flow I can take a look at it and see if figure out the difference.

Thanks! I’ll try it out.

This is the issue. I don’t want to have to trip the motion sensor again to reset things. I’m looking for a way to detect the switch was toggled off then on, and there was no motion, so turn the switch off say after 30s.

You can watch for event state change on the light and if it turns on then check the status of the motion sensor and see if it has detected motion in the last 10 seconds or some window that works for you. If it has not detected motion in that window turn off light in 30 seconds.

Another possible way is to still check for an event state change on the light and check the context of the event and if it’s null it should be the light being turned on manually and not from home assistant then you can set a timer to turn off in 30 seconds

You can reset the timer on trigger node when the switch turn on.

15s%20Trigger