Proceed only if sensor state remains unchanged for specified time

I’m trying to figure out how to trigger a flow and send a notification if the triggering sensor state does not change for 5 seconds. This use case is for a sonoff that sometimes stars to go unavailable for 1 second and back to normals… which could continue for a few minutes. I want to get the notification in the following scenarios:

Changed to Unavailable > 5-second wait > Unavailable
Changed to Available > 5-second wait > Available

No notification if:
Changed to Unavailable > 5-second wait > Available
Changed to Available > 5-second wait > Unavailable

Are there existing nodes that can be used for these or does this call for a function?

Thanks!

You can use the ha websocket node and the basic delay node. This is very basic, see if it works for you

[{"id":"1a38bb36.5d85b5","type":"server-state-changed","z":"9ac58995.e96338","name":"","server":"d5ce5cec.92bc8","entityidfilter":"light.dining_table","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"unavailable","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":330,"y":1540,"wires":[["56e92bd6.e2b6b4"],[]]},{"id":"56e92bd6.e2b6b4","type":"delay","z":"9ac58995.e96338","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":560,"y":1540,"wires":[["76c310ee.abd18"]]},{"id":"76c310ee.abd18","type":"api-current-state","z":"9ac58995.e96338","name":"","server":"d5ce5cec.92bc8","outputs":2,"halt_if":"unavailable","halt_if_type":"str","halt_if_compare":"is_not","override_topic":false,"entity_id":"light.dining_table","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":790,"y":1540,"wires":[["d8721c92.ea1bd"],[]]},{"id":"d8721c92.ea1bd","type":"api-call-service","z":"9ac58995.e96338","name":"notifySrv","server":"d5ce5cec.92bc8","service_domain":"","service":"","data":"","mergecontext":"","output_location":"","output_location_type":"none","x":1020,"y":1540,"wires":[[]]},{"id":"d5ce5cec.92bc8","type":"server","z":"","name":"Home Assistant","legacy":false}]
1 Like

Thanks @subzero79, that works. Looks like I’m going to have to just clone that one to as many states as I know there will be for that entity and make sure it’s not jumping back to the original state.

If anyone has some ideas about how to use one process where the second state node after 5 seconds compares it to the result of the first (without knowing all the states beforehand)… like storing first one into a variable and then referencing it if it’s still the same, that would be helpful in future.

I may misunderstand your need but to me it sounds like using a switch,delay and state node would do what you want?
[{"id":"7c312acb.5290c4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"ee5e415c.f67bc","type":"server-state-changed","z":"7c312acb.5290c4","name":"Event State","server":"74cbfde1.f316f4","entityidfilter":"light.bedroom","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":128.10000610351562,"y":136.00000095367432,"wires":[["3c16e83e.51c408"]]},{"id":"3c16e83e.51c408","type":"switch","z":"7c312acb.5290c4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"availible","vt":"str"},{"t":"eq","v":"unavailible","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":321.1000061035156,"y":136.00000190734863,"wires":[["610e9ee7.ff40a"],["1c06ffd7.447db"]]},{"id":"1c06ffd7.447db","type":"delay","z":"7c312acb.5290c4","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":470.8500633239746,"y":153.7500057220459,"wires":[["cfae8f1f.c9499"]]},{"id":"cfae8f1f.c9499","type":"api-current-state","z":"7c312acb.5290c4","name":"Current State","server":"74cbfde1.f316f4","outputs":2,"halt_if":"availible","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.bedroom","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":642.8501129150391,"y":156.0000171661377,"wires":[["de777c67.5067b"],[]]},{"id":"de777c67.5067b","type":"debug","z":"7c312acb.5290c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":887.6251258850098,"y":132.50001525878906,"wires":[]},{"id":"610e9ee7.ff40a","type":"delay","z":"7c312acb.5290c4","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":470.00001525878906,"y":117.50000190734863,"wires":[["1bcfc11c.ae48af"]]},{"id":"1bcfc11c.ae48af","type":"api-current-state","z":"7c312acb.5290c4","name":"Current State","server":"74cbfde1.f316f4","outputs":2,"halt_if":"unavailible","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.bedroom","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":643.7500152587891,"y":118.75000190734863,"wires":[["de777c67.5067b"],[]]},{"id":"74cbfde1.f316f4","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

1 Like

again i say, NR needs a “watchdog” node.

test a condition, wait X amount of time, test again, same result?, continue.

i’m not JS savvy enough to make it, or i would.

You can use flow context(variables, objects or arrays) to store and use them to compare it in state nodes.

[{"id":"5ef2c9dc.514b78","type":"server-state-changed","z":"9ac58995.e96338","name":"","server":"d5ce5cec.92bc8","entityidfilter":"light.dining_table","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":210,"y":1680,"wires":[["c7d57a93.41b6a8","616a0679.028f78","d5cf92fc.76567"]]},{"id":"c7d57a93.41b6a8","type":"delay","z":"9ac58995.e96338","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":520,"y":1680,"wires":[["4c49440d.731d2c"]]},{"id":"4c49440d.731d2c","type":"api-current-state","z":"9ac58995.e96338","name":"","server":"d5ce5cec.92bc8","outputs":2,"halt_if":"light_state","halt_if_type":"flow","halt_if_compare":"is_not","override_topic":false,"entity_id":"light.dining_table","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":750,"y":1680,"wires":[["c8071a18.83a578"],[]]},{"id":"616a0679.028f78","type":"change","z":"9ac58995.e96338","name":"","rules":[{"t":"set","p":"light_state","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":1740,"wires":[[]]},{"id":"1edb904f.c5d34","type":"inject","z":"9ac58995.e96338","name":"","topic":"","payload":"light_state","payloadType":"flow","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":1820,"wires":[["d5cf92fc.76567"]]},{"id":"d5cf92fc.76567","type":"debug","z":"9ac58995.e96338","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":350,"y":1820,"wires":[]},{"id":"c8071a18.83a578","type":"debug","z":"9ac58995.e96338","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1010,"y":1760,"wires":[]},{"id":"d5ce5cec.92bc8","type":"server","z":"","name":"Home Assistant","legacy":false}]
1 Like

Nice, this is EXACTLY what I was trying to do! The only problem is that it’s not working as I expected.

If I flip the switch again within 5 seconds, the flow.light_state will get updated again with the newer state and it will still always match the second sensor reading after 5 seconds. Also, if I flip the switch multiple times within that 5 seconds it will send all of those messages. So there are a few things to work out, but it’s definitely a great start.