Cancel Automation if State Goes Back to What It Was Within Timeframe

Hi all! Any thoughts on how to cancel an automation in Node-RED if state goes back to what it was within a two minute time frame?

This automation takes care of my office door opening and closing. Notifies my wife when I am available and not available. Sometimes the door opens for less than two minutes while I use the bathroom, but I don’t want to notify that I’m available.

Below is what I have currently. This will trigger the same notification though, rather than not sending the notification if the state goes back to what it was within a two minute period.

Couldnt dou just set a „for…minutes“ in the first node?

I think I’d still have the same issue though–once the sensor resets, it would send the notification again.

You only want the notification to trigger if the door is opened for more than 2 minutes?

Not exactly. If the door opens and the state changes from “closed” to “open”, but then the state changes BACK to “closed” again within two minutes, then there should be no notification.

I believe we are saying the same thing. This should work, you might be overdoing it.

Put an event state node like this one. It will only trigger if it’s on for 2 minutes or more. If during this two minutes, the door is closed, it notification won’t trigger.

Appreciate your help @obaldius. I must not be doing a good job at explaining.

Here’s the scenario:

  1. Door is closed while in meeting (state closed)

  2. Open the door to use bathroom (state changes to open)

  3. Back in less than two minutes, door closes (state is back to closed)

  4. Because the state changed twice in less than two minutes, ignoring the state change when “current state equals previous state” will not solve the problem. A notification will still be sent.

In my last example, the notification will only be sent if the door is opened for 2 or more minutes. If during those 2 minutes you close the door, nothing will be sent. Try it out.

This should do it too, just in case you don’t have the “For” label in your even state node. They both achieve the same.

[{"id":"6bb95ee8.cac6b","type":"server-state-changed","z":"d4450f17.60952","name":"Door","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.your_door","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":130,"y":440,"wires":[["23f9ab8b.377824"]]},{"id":"23f9ab8b.377824","type":"trigger","z":"d4450f17.60952","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"2","extend":false,"overrideDelay":false,"units":"min","reset":"off","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":440,"wires":[["b6c86e54.d57e1"]]},{"id":"b6c86e54.d57e1","type":"api-call-service","z":"d4450f17.60952","name":"Notify","server":"9405c3fe.d0a6c","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":440,"y":440,"wires":[[]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","addon":true}]

Hmm, played around with both scenarios but the issue here is that I have two different notifications being sent. One says I’m available, the other says I’m unavailable. But one or the other should only trigger IF the state of the door has changed for two minutes, otherwise it should cancel the notification.

In the scenarios you’ve laid out above, an additional notification will be sent.

Here’s an example: An unavailable notification is sent when I close the door for a meeting, I open the door to go use the restroom, I come back in less than two minutes and door closes again. Another unavailable notification will be sent. The reality though is I was unavailable the whole time, I just had to use the bathroom haha.

May seem like I’m straining at gnats here, but these notifications are going to my wife who is not technologically savvy. Less notifications the better :laughing:

[{"id":"8cb4dbed.f28268","type":"server-state-changed","z":"b241aa40.ce5ee8","name":"Door","server":"2ee251ef.2ba05e","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.office_door","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":170,"y":1140,"wires":[["2325790c.08bcd6"]]},{"id":"2325790c.08bcd6","type":"trigger","z":"b241aa40.ce5ee8","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"off","bytopic":"all","topic":"topic","outputs":1,"x":340,"y":1140,"wires":[["8f4d124a.827f2"]]},{"id":"8f4d124a.827f2","type":"api-call-service","z":"b241aa40.ce5ee8","name":"available","server":"2ee251ef.2ba05e","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_pixel_5","entityId":"","data":"{\"message\":\"Available\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":580,"y":1140,"wires":[[]]},{"id":"c27e040b.65dde8","type":"server-state-changed","z":"b241aa40.ce5ee8","name":"Door","server":"2ee251ef.2ba05e","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.office_door","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":170,"y":1240,"wires":[["323c8fe6.b4bc2"]]},{"id":"323c8fe6.b4bc2","type":"trigger","z":"b241aa40.ce5ee8","name":"","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"s","reset":"on","bytopic":"all","topic":"topic","outputs":1,"x":340,"y":1240,"wires":[["58ed8352.05f39c"]]},{"id":"58ed8352.05f39c","type":"api-call-service","z":"b241aa40.ce5ee8","name":"unavailable","server":"2ee251ef.2ba05e","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_pixel_5","entityId":"","data":"{\"message\":\"unavailable\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":590,"y":1240,"wires":[[]]},{"id":"2ee251ef.2ba05e","type":"server","name":"Home Assistant","addon":true}]

Ok so it sounds like what you want is this:

  1. If the door was closed and is now open for at least 2 minutes, send a notification
  2. If the door was open for at least 2 minutes and is now closed, send a notification

What I would do then is this. Start with the node @obaldius showed earlier:

Connect the top output of this node to your “Notify I’m available” node as they are showing. This completely covers the “I’m available” notification as it is only going to fire if the door is opened for at least two minutes.

For the “I’m no longer available” notification we have to get a bit fancy. We’re going to use the lower output of this node. A message will be sent from this anytime anytime the door was closed no matter how long it was open for. So we need to filter this to ignore any messages where data.old_state.last_changed was less then 2 minutes ago. Because in this message old_state.last_changed will be set to the date/time when the door’s state was changed to open so we need to make sure that was at least 2 minutes in the past before sending out the “I’m no longer available” message.

We can do that by passing that into a switch node with this:

Here’s the stub you can import and flesh out from there:

[{"id":"50b6f08e.d72e38","type":"switch","z":"a74fee2d.ac9068","name":"Door was open for 2 min","property":"$toMillis(data.old_state.last_changed)","propertyType":"jsonata","rules":[{"t":"lt","v":"$millis() - 120000","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":1250,"y":1080,"wires":[["2b9eb7ac.07b7d8"]]},{"id":"9d2f4bd9.a81b5","type":"server-state-changed","z":"a74fee2d.ac9068","name":"Door open for 2 minutes","server":"cc03735a.94933","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.your_door","entityidfiltertype":"exact","outputinitially":false,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"2","forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":true,"ignorePrevStateUnavailable":true,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"x":940,"y":1060,"wires":[["71da7e36.7f24a8"],["50b6f08e.d72e38"]]},{"id":"71da7e36.7f24a8","type":"api-call-service","z":"a74fee2d.ac9068","name":"Notify 'I'm available'","server":"cc03735a.94933","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1240,"y":1020,"wires":[[]]},{"id":"2b9eb7ac.07b7d8","type":"api-call-service","z":"a74fee2d.ac9068","name":"Notify 'I'm not available'","server":"cc03735a.94933","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"jsonata","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1510,"y":1080,"wires":[[]]},{"id":"cc03735a.94933","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Note that since we are using the lower output of the events: state node now I checked all the boxes for state changes to ignore. We want to make sure you don’t get random notifications whenever HA restarts since then it goes from null or unknown to whatever its current state is, that’s not a change we’re interested in.

1 Like

That did it, @CentralCommand! Thank you. Tonight I will go over this so that I can understand it :smiley:

Thank you as well, @obaldius!

2 Likes