What is the best way to detect an open door after 5 minutes and closing

Hi everyone, quick question from a newbie, I try to learn. What would be the best way (if there is one) to detect that a door is still opened after 5min to send a warning message1 AND to send a second message2 that the door is closed only if the warning message1 was sent ?
I struggle on this simple one, finally I found that way but I’m sure there are other ones more elegant…
Thank you for your proposal.

 [{"id":"9aa5d26591f66160","type":"server-state-changed","z":"6e5bca89f5fb8327","name":"Door opened > 5 min","server":"a1e1bc7a.3b00d","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"binary_sensor.window_ts0203_iaszone","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":"4","forType":"num","forUnits":"seconds","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":"alarm","propertyType":"flow","value":"on","valueType":"str"},{"property":"payload.openTime","propertyType":"msg","value":"","valueType":"date"}],"x":140,"y":140,"wires":[["2ab1e73b50697cc8","87040a648fcbb94e"],[]]},{"id":"a1e1bc7a.3b00d","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":true,"heartbeatInterval":"10","statusSeparator":"","enableGlobalContextStore":false}]

You are going to LOVE this - already built into core and it is extremely configurable -

Home Assistant Alerts

Below is a snippet of a tiny part of configuration.yaml file - easy peasy:

alert:
  back_door_open:
    name: Back door open
    title: Ridgewood Estate Automation at {{ now().strftime('%-I:%M%p').replace('AM','am').replace('PM','pm') }}
    message: Back door has been open for (another) 5 minutes - close it?
    done_message: Back door was just closed.
    entity_id: binary_sensor.den_back_door_sensor_state
    state: 'on'
    repeat: 5
    skip_first: true
    notifiers:
      - Ridgewood_Estate_Text_Message
    can_acknowledge: false

Excellent, this shows me that there is still a lot to learn … I’ll try and use it.
I’m also still curious if there are other way to do this in Node Red in order to learn it.
Thanks @KruseLuds for your ultra fast answer !

1 Like

This one uses a group of doors/windows. After the group has been on for 5 minutes it will pull a list of which doors/windows are open and send the notification. If I were to re-do this today I’d drop the switch node. I would still use the timer over the built-in “For X minutes” because I want the action to fire right when the doors close not after they’ve been closed for 5 minutes.

For the second notification when they close only if a notification was sent, I’d probably create a helper entity here. Along with sending the first notification I’d turn that entity on. Then when the door closes, if that entity is on send the second notification and turn the entity off.

[{"id":"42442f06.7b14f","type":"server-state-changed","z":"5ddddd21.00d204","name":"Door/Window Open","server":"94fdcfdf.a00b","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"group.doors_windows","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,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":110,"y":1700,"wires":[["5cb1066a.159528"]]},{"id":"5cb1066a.159528","type":"switch","z":"5ddddd21.00d204","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":1700,"wires":[["92868919.54d7a8"],["a13066de.6decb8"]]},{"id":"92868919.54d7a8","type":"stoptimer","z":"5ddddd21.00d204","duration":"5","units":"Minute","payloadtype":"num","payloadval":"0","name":"5 Minutes","x":600,"y":1700,"wires":[["3b2966e7.564c1a"],[]]},{"id":"a13066de.6decb8","type":"change","z":"5ddddd21.00d204","name":"Stop Timer","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1760,"wires":[["92868919.54d7a8","a154c077.9126e"]]},{"id":"a154c077.9126e","type":"api-call-service","z":"5ddddd21.00d204","name":"Door/Window Closed","server":"94fdcfdf.a00b","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.door_open"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":800,"y":1760,"wires":[[]]},{"id":"3b2966e7.564c1a","type":"ha-get-entities","z":"5ddddd21.00d204","name":"Which Doors/Windows?","server":"94fdcfdf.a00b","version":0,"rules":[{"property":"entity_id","logic":"in_group","value":"group.doors_windows","valueType":"str"},{"property":"state","logic":"includes","value":"on, open","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":810,"y":1680,"wires":[["e0d33504.13ee68"]]},{"id":"e0d33504.13ee68","type":"template","z":"5ddddd21.00d204","name":"Format Msg","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.attributes.friendly_name}}","output":"str","x":1030,"y":1680,"wires":[["ce669e51.69747"]]},{"id":"ce669e51.69747","type":"join","z":"5ddddd21.00d204","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":", ","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1190,"y":1680,"wires":[["1c2550a2640ca9a1"]]},{"id":"1c2550a2640ca9a1","type":"function","z":"5ddddd21.00d204","name":"","func":"open = msg.payload\n\nmsg.payload = {\n    data: {\n        title: \"Temperature\",\n        message: open + \" left open for 5 minutes. Thermostats turned off\",\n        data: {\n            notification_icon: 'mdi:radiator-off',\n            tag: 'temperature',\n            channel: \"Thermostat\",\n            ttl: 0,\n            importance: 'default', \n            clickAction: 'dwains-dashboard/room_living_room_climate',\n        }\n    }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1340,"y":1680,"wires":[["f39269872647603d"]]},{"id":"f39269872647603d","type":"api-call-service","z":"5ddddd21.00d204","name":"Notify","server":"94fdcfdf.a00b","version":5,"debugenabled":false,"domain":"notify","service":"all_phones","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"json","mergeContext":"callServiceData","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1470,"y":1680,"wires":[[]]},{"id":"94fdcfdf.a00b","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":true,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

I looked at it. I like it. I was trying to do something similar with the switch (without success)

I guess that the issue during the import of your code “unknown stoptimer node” is to be installed with the Node Red > manage pallette > install

Thank you @MRobi

ahhh yes, stoptimer is no longer included in the base addon and has to be added.
I’d use node-red-contrib-stoptimer-varidelay
It’s got a few extra features over the regular stop timer node.

How are you detecting that the door is open?
If the door sensor is a Home Assistant entity, use the state node to detect the door opening. start a five minute timer, then check the state again when the timer expires.

Hi Stephen,
May be I was not clear enough in my initial post, I just pasted a screenshot and the code of one solution I found without explanation.

How are you detecting that the door is open?

I use the event:state node with if state is on for 5minutes settings.

If the door sensor is a Home Assistant entity, use the state node to detect the door opening. start a five minute timer, then check the state again when the timer expires

This part is OK, where I struggled is after. In the second part I want to use an AND condition:
How can I check the state again (this is OK) AND also check that a message1 was sent (how?).
@MRobi suggest to use a “helper entity” to record that the 1st notification was sent. This is what I want to try, but I’m wondering if there is another way to do it while staying completely in Node Red

Just to better learn Node Red (I also tried to used Node Red global context/global variables without success) … besides also learning how to use an “helper entity” from Home Assistant :slight_smile: .

The way to do it within node red itself is the use of flow/global variables. There’s really no benefit to this method over a helper.

I do not know it is the best way. But this is how I do it:
image

I the door is closed within 5 minutes the timer will stop
If you want to check the door again after 5 minutes place a current state to check and go back to the timer again for another 5 minutes (or earlier if the door closes_)

Thank you this is also an interesting way to interrupt the cycle. This shows that there is no single ideal best ways, all of us have a best way, I like creativity …

You are very welcome. As a new user of HA myself, I have alearned alot, however haven’t looked into Node-Red yet (but want to when I get the chance!)