How to "break" a running flow

Hello

I have a simple Nodered flow: when the motion sensor is triggered, a lamp turns on, then after 100 seconds it turns off.

hassio

The problem happens when:

  • I trigger the sensor for the first time, so the first flow starts: the lamp turns on
  • after 90 seconds (let’s say), I trigger the sensor a second time: the second flow starts
  • after 10 seconds the first flow ends and the lamp is turned off, even if the second flow is still running

So, when I trigger the sensor I need to “stop” the running flow (if any), in order to avoid the lamp to be turned off too early.

What is the best way to achieve this?

Does your motion sensor report the off state? if it does, then this:

[{"id":"e7c53191.24783","type":"server-state-changed","z":"937ef78.ec6b408","name":"Motion sensor off for 100s?","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.your_sensor","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"100","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"x":330,"y":720,"wires":[["37725ed5.caa4b2"],["37725ed5.caa4b2"]]},{"id":"37725ed5.caa4b2","type":"api-call-service","z":"937ef78.ec6b408","name":"light.turn_{{payload}}","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"light","service":"turn_{{payload}}","entityId":"light.your_light","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":580,"y":720,"wires":[[]]},{"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}]

If your motion sensor only reports one state, motion detected, then this:

[{"id":"f3594db2.4818c","type":"server-state-changed","z":"937ef78.ec6b408","name":"Motion sensor","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.your_sensor","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":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":210,"y":440,"wires":[["b2f7fc07.2fcd"],[]]},{"id":"b2f7fc07.2fcd","type":"api-call-service","z":"937ef78.ec6b408","name":"light on","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":390,"y":440,"wires":[["b946dd45.1e234"]]},{"id":"b946dd45.1e234","type":"trigger","z":"937ef78.ec6b408","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"100","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":540,"y":440,"wires":[["5cf7ab14.b3eb54"]]},{"id":"5cf7ab14.b3eb54","type":"api-call-service","z":"937ef78.ec6b408","name":"light off","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":700,"y":440,"wires":[[]]},{"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}]

Thank you! Your second solution seems obvious but I’m a newbie with Hassio :slight_smile:
I’ll give it try.

The real trick with the trigger node is checking Extend Delay If New Message Arrives.

Does your motion sensor send both on and off states?

Yes, I think it does the trick! I updated my configuration so I’ll test this solution for the next few days.

I think so, but as I’m almost a beginner I’d rather your second suggestion, that is more obvious and “readable” to me
Thank you!

This one is the same as the first one but more obvious. I leave it here for you to have more options

[{"id":"d450b0b0.8e603","type":"server-state-changed","z":"937ef78.ec6b408","name":"Motion on?","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.your_sensor","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":[],"x":160,"y":200,"wires":[["1c13743d.92bedc"],[]]},{"id":"1c13743d.92bedc","type":"api-call-service","z":"937ef78.ec6b408","name":"Light on","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.your_light","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":360,"y":200,"wires":[[]]},{"id":"883a5f49.bbf83","type":"server-state-changed","z":"937ef78.ec6b408","name":"Motion off for 100 seconds?","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.your_sensor","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"100","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[],"x":220,"y":280,"wires":[["6c2559c1.8c0f68"],[]]},{"id":"6c2559c1.8c0f68","type":"api-call-service","z":"937ef78.ec6b408","name":"Light off","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.your_light","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":440,"y":280,"wires":[[]]},{"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}]

which turns out to be the same as this:

[{"id":"1c13743d.92bedc","type":"api-call-service","z":"937ef78.ec6b408","name":"Light on","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.your_light","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":460,"y":300,"wires":[[]]},{"id":"883a5f49.bbf83","type":"server-state-changed","z":"937ef78.ec6b408","name":"Motion off for 100 seconds?","server":"9405c3fe.d0a6c","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.your_sensor","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"off","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"100","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"x":220,"y":280,"wires":[["6c2559c1.8c0f68"],["1c13743d.92bedc"]]},{"id":"6c2559c1.8c0f68","type":"api-call-service","z":"937ef78.ec6b408","name":"Light off","server":"9405c3fe.d0a6c","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.your_light","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":460,"y":260,"wires":[[]]},{"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}]

If you go one step further in compacting you end up with the first example I put you above. Using the state of the binary_sensor (payload) to set the light’s service. All three will work just as well though.

Hello,
I’m coming back after some times (and some testing) because I didn’t solve completerly my problem.
It seems that there’s some issue/bug related to HUE sensors, as other brands (IKEA, Xiaomi etc) work fine with your suggestions.
So I’m opening a separated thread…
In the meanwhile, thank you!