Delay loop and reset

Hello Guys! I hope I am at the right place. I try to make a delay and loop. I have magnetic door lock with a door status sensor. I have mqtt message coming in saying "Door Opened" msg1 or "Door Closed" msg2 What I wish to achieve When we leave the house and open the door I wish to receive a notification if the door is opened longer than 2 minutes. and keep sending the notifications every 2 minutes until the node-red receive msg2 saying "Door Closed" So ideally the msg2 would kill all msg1 messages in the loop until a new msg1 arrive with the payload of "door Opened" and all starts over and over.
Some head up would be brilliant.
I started something but it does not work. I have incoming msg and feed into my functio node then the output is feed into the delay node. Tried all the settings in the delay node. I have msg1 door opened but I do not have msg2 in the output. I must doing something wrong.
Here is my function node:

var msg1 = { payload:0 };
var msg2 = { payload:0 };

if (msg.payload.door === "Door Open"){
msg1.payload = "open";
msg2.reset = "reset";
return [msg1,msg2];
} else {
msg.payload = "Door Closed";
return [null,msg2];
}

Thanks in advance.

1 Like

Hi.

Quick question: Why two returns? (msg1, msg2)

I may mess around with it but your flow would stop me going off in the wrong direction.

Ok, what I see/get/undertand:

Every 2 minutes you get a signal which is either Door open or Door closed.
If it is the former, it keeps sending a message every 2 minutes.
If, it the mean time, a Door closed message is received, the timer is canelled.

Right?

I think the way you are doing it may not be the best way.
There is no need (that I can see) for the delay node.

My take would be to generate a Door Status message every 2 minutes. (Basically how you detect the door status now.)

Send that through a SWITCH node and only pass on the ones which say DoorOpen.

As the door is ultimately going to be closed, those messages won't pass the SWITCH node.
Therefore there won't be a problem.

Hope that helps.

Just for the sake of it, here is basically my idea:

[{"id":"39a591f9.434fae","type":"inject","z":"9c1e5490.5d70b","name":"","topic":"","payload":"DoorOpen","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":990,"wires":[["e8be307f.9a699"]]},{"id":"9a7b290b.e9ef1","type":"inject","z":"9c1e5490.5d70b","name":"","topic":"","payload":"DoorClosed","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1040,"wires":[["e8be307f.9a699"]]},{"id":"e8be307f.9a699","type":"switch","z":"9c1e5490.5d70b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"DoorOpen","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":430,"y":1010,"wires":[["2ee8c961.011266"]]},{"id":"2ee8c961.011266","type":"debug","z":"9c1e5490.5d70b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":600,"y":1010,"wires":[]},{"id":"dff50d02.04e0f","type":"comment","z":"9c1e5490.5d70b","name":"Your flow here detecting the door OPEN or CLOSED","info":"","x":210,"y":940,"wires":[]}]

Hi!
I have tried the flow, but it is not the one I wish to achieve. Ideally I need some help with the function node java script.

At the moment I have two input door open and door closed. I wish to receive a notification every let's say 2 minutes until the door is open. So as soon as node-red receives a door closed msg, then it should cancel the the msg1 which is the door open message. So Ideally I use delay node and function node, which would check the value and if the value.

In my function above I need two return to see what is the output, msg1 and msg2, but I am maybe wrong. That is why here to gather more knowledge.

at the moment I am standing here I have managed to control the messages, but I was hoping to get rid of those nodes and use function node with a script in it. It would much nicer.

Thanks for your time.

[{"id":"9415995b.c09f98","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"6d0485d1.402e5c","type":"ui_toast","z":"9415995b.c09f98","position":"dialog","displayTime":"10","highlight":"","outputs":1,"ok":"OK","cancel":"","topic":"Warning! Activity at the gate!","name":"","x":1009,"y":191,"wires":[]},{"id":"41f4466c.786458","type":"delay","z":"9415995b.c09f98","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"5","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":818,"y":140,"wires":[["6d0485d1.402e5c"]]},{"id":"86d161bc.8aeb4","type":"function","z":"9415995b.c09f98","name":"User","func":"msg.payload = msg.payload.username + " Arrived home at:
"\nmsg.topic = "Warning! The gate is open!"\n\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+"/"+(dt.getMonth() + 1)+"/"+dt.getDate()+" - "+(dt.getHours()+0)+":"+dt.getMinutes(); + msg.payload + msg.topic\nreturn msg;","outputs":1,"noerr":0,"x":483,"y":188,"wires":[["6d0485d1.402e5c"]]},{"id":"787b437f.4392ec","type":"mqtt in","z":"9415995b.c09f98","name":"","topic":"status","qos":"1","datatype":"auto","broker":"3ad182b8.a268fe","x":82,"y":92,"wires":[["98898ecc.af1cf"]]},{"id":"98898ecc.af1cf","type":"json","z":"9415995b.c09f98","name":"","property":"payload","action":"","pretty":false,"x":214,"y":92,"wires":[["46580809.0dfaf8"]]},{"id":"65ff230a.b53e9c","type":"function","z":"9415995b.c09f98","name":"Open","func":"msg ={\n payload: "Warning the gate left open for more than 2 miniutes at!
",\n topic: "Warning! Activity at the gate!"\n};\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+"."+(dt.getMonth() + 1)+"."+dt.getDate()+" "+(dt.getHours()+0)+":"+dt.getMinutes();\nreturn msg;","outputs":1,"noerr":0,"x":852,"y":246,"wires":[["6d0485d1.402e5c"]]},{"id":"278d23b6.d0547c","type":"function","z":"9415995b.c09f98","name":"Closed","func":"msg ={\n payload: "The gate has been closed at!
",\n topic: "Warning! The activity has ended !"\n};\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+"."+(dt.getMonth() + 1)+"."+dt.getDate()+" "+(dt.getHours()+0)+":"+dt.getMinutes();\nreturn msg;","outputs":1,"noerr":0,"x":633,"y":138,"wires":[["41f4466c.786458"]]},{"id":"46580809.0dfaf8","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.type","propertyType":"msg","rules":[{"t":"eq","v":"door","vt":"str"},{"t":"eq","v":"access","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":343,"y":92,"wires":[["3e7512c0.019ede"],["86d161bc.8aeb4"]]},{"id":"3e7512c0.019ede","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.door","propertyType":"msg","rules":[{"t":"eq","v":"Door Closed","vt":"str"},{"t":"eq","v":"Door Open","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":483,"y":86,"wires":[["4e225111.d3bd4","278d23b6.d0547c"],["4e225111.d3bd4"]]},{"id":"f8c7cf01.dd0b8","type":"change","z":"9415995b.c09f98","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Status","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":275.9999694824219,"y":246.66665649414062,"wires":[["7f12a25d.0e0abc"]]},{"id":"4e225111.d3bd4","type":"change","z":"9415995b.c09f98","name":"","rules":[{"t":"set","p":"Status","pt":"flow","to":"payload.door","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":652.9999389648438,"y":87.66665649414062,"wires":[],"outputLabels":["2"]},{"id":"1f765554.07a9eb","type":"inject","z":"9415995b.c09f98","name":"2 min","topic":"Warning! Activity at the gate!","payload":"","payloadType":"date","repeat":"120","crontab":"","once":false,"onceDelay":0.1,"x":108.44439697265625,"y":246,"wires":[["f8c7cf01.dd0b8"]]},{"id":"7f12a25d.0e0abc","type":"switch","z":"9415995b.c09f98","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Door Open","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":482,"y":246,"wires":[["65ff230a.b53e9c"]]},{"id":"3ad182b8.a268fe","type":"mqtt-broker","z":"","name":"mqtt","broker":"192.168.1.71","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

well a trigger node may be a better place to start. It can detect the open - wait 2 mins then send the door open alert - and be cancelled by the DoorClosed.

[{"id":"e8f2916b.a3d","type":"inject","z":"de9a4bc7.709688","name":"","topic":"","payload":"DoorOpen","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":380,"wires":[["228a0bb8.349124"]]},{"id":"fda5147.011bce8","type":"inject","z":"de9a4bc7.709688","name":"","topic":"","payload":"DoorClosed","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":430,"wires":[["228a0bb8.349124"]]},{"id":"228a0bb8.349124","type":"trigger","z":"de9a4bc7.709688","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"2","extend":false,"units":"min","reset":"DoorClosed","bytopic":"all","name":"","x":470,"y":400,"wires":[["3d958daf.852712"]]},{"id":"3d958daf.852712","type":"debug","z":"de9a4bc7.709688","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":400,"wires":[]}]
1 Like

Hang on.

That's different to what I read originally.

The word until is the sticking point.
Should that be while?

What was it about mine that was wrong? I'm curious.

That's interesting.

I just checked what is listed as what I posted in that flow.
I am sure that when I posted it the switch node was a trigger one.
I don't have the flow any more so can't really say.

After a bit of looking, I found that exact flow but in another thread:
reply #3

How weird.

(Redacted)
(How do you get the "strike through" style letters?)
Errr, isn't that 99% what I posted?

Fair is fair.

Thanks for all the help!
At the moment I partly solved the flow, but I am not happy with it. It should be much nicer to write a javascript for it and that would do the work instead of using so many nodes. It is clearly down to my lack of javascript programming skills, which I wish to take some courses to improve it because until I have no idea about the basic rules and the purposes of signs in the script, I feel I am walking in a pitch-black room.
I am only using node-red for hobby purposes and never learned programing or even about computers. All I know is from self-learning.

Regarding the flow I have just store the value into a flow context, then a inject node keep checking the value, a switch node let through only the door open message. So ass soon as the value changed to door closed there will be no messages going further than the switch node. Pretty simple, but can be much nicer and no need for storing the value into a context.

[{"id":"9415995b.c09f98","type":"tab","label":"RF id test flow","disabled":false,"info":""},{"id":"b77b440.3e4b4c","type":"ui_toast","z":"9415995b.c09f98","position":"dialog","displayTime":"10","highlight":"","outputs":1,"ok":"Ok","cancel":"","topic":"Warning! Activity at the gate!","name":"","x":1114,"y":179,"wires":[[]]},{"id":"64694e3d.14e74","type":"delay","z":"9415995b.c09f98","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"5","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":914,"y":130,"wires":[["b77b440.3e4b4c"]]},{"id":"1ee18c18.366e44","type":"function","z":"9415995b.c09f98","name":"User","func":"msg.payload = msg.payload  +   \"  Arrived home at: <br>\"\nmsg.topic = \"Warning! The gate is open!\"\n\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+\"/\"+(dt.getMonth() + 1)+\"/\"+dt.getDate()+\" - \"+(dt.getHours()+0)+\":\"+dt.getMinutes(); + msg.payload + msg.topic\nreturn msg;","outputs":1,"noerr":0,"x":698,"y":184,"wires":[["b77b440.3e4b4c"]]},{"id":"c3e51b73.17e318","type":"mqtt in","z":"9415995b.c09f98","name":"","topic":"status","qos":"1","datatype":"auto","broker":"3ad182b8.a268fe","x":110,"y":129,"wires":[["9bd02c8e.0dda8"]]},{"id":"9bd02c8e.0dda8","type":"json","z":"9415995b.c09f98","name":"","property":"payload","action":"","pretty":false,"x":242,"y":129,"wires":[["7f90d7d4.012598"]]},{"id":"b0521fd1.1db6","type":"function","z":"9415995b.c09f98","name":"Open","func":"msg ={\n    payload: \"Warning the gate left open for more than 2 miniutes at! <br>\",\n    topic: \"Warning! Activity at the gate!\"\n};\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+\".\"+(dt.getMonth() + 1)+\".\"+dt.getDate()+\"  \"+(dt.getHours()+0)+\":\"+dt.getMinutes();\nreturn msg;","outputs":1,"noerr":0,"x":700,"y":233,"wires":[["b77b440.3e4b4c"]]},{"id":"2602d81b.dbfa78","type":"function","z":"9415995b.c09f98","name":"Closed","func":"msg ={\n    payload: \"The gate has been closed at! <br>\",\n    topic: \"Warning! The activity has ended !\"\n};\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+\".\"+(dt.getMonth() + 1)+\".\"+dt.getDate()+\"  \"+(dt.getHours()+0)+\":\"+dt.getMinutes();\nreturn msg;","outputs":1,"noerr":0,"x":701,"y":130,"wires":[["64694e3d.14e74"]]},{"id":"7f90d7d4.012598","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.type","propertyType":"msg","rules":[{"t":"eq","v":"door","vt":"str"},{"t":"eq","v":"access","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":371,"y":129,"wires":[["ac7a5bec.247788"],["38426c07.ccd344"]]},{"id":"ac7a5bec.247788","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.door","propertyType":"msg","rules":[{"t":"eq","v":"Door Closed","vt":"str"},{"t":"eq","v":"Door Open","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":516,"y":85,"wires":[["477c92aa.73ec2c","2602d81b.dbfa78"],["477c92aa.73ec2c"]]},{"id":"bfdb74d8.a346f8","type":"change","z":"9415995b.c09f98","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Status","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":311.9999694824219,"y":232.66665649414062,"wires":[["24ce1afa.53bd26"]]},{"id":"477c92aa.73ec2c","type":"change","z":"9415995b.c09f98","name":"","rules":[{"t":"set","p":"Status","pt":"flow","to":"payload.door","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":718.9999389648438,"y":87.66665649414062,"wires":[[]],"outputLabels":["2"]},{"id":"1f0ee1d0.33e90e","type":"inject","z":"9415995b.c09f98","name":"2 min","topic":"Warning! Activity at the gate!","payload":"","payloadType":"date","repeat":"10","crontab":"","once":false,"onceDelay":0.1,"x":135.44439697265625,"y":234,"wires":[["bfdb74d8.a346f8"]]},{"id":"24ce1afa.53bd26","type":"switch","z":"9415995b.c09f98","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Door Open","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":515,"y":233,"wires":[["b0521fd1.1db6"]]},{"id":"38426c07.ccd344","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.username","propertyType":"msg","rules":[{"t":"cont","v":"Detty","vt":"str"},{"t":"cont","v":"Pisti","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":515,"y":181,"wires":[["1ee18c18.366e44"],["1ee18c18.366e44"]]},{"id":"3ad182b8.a268fe","type":"mqtt-broker","z":"","name":"mqtt","broker":"192.168.1.71","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Export your flow again and format it here as:

```
your code
```

Unfortunately your flow isn't currently importable.Please read the following post How to share code or flow json and then edit the above message.
After you have posted it, try copying and importing it. If it works for you it will work for others...

1 Like
[{"id":"9415995b.c09f98","type":"tab","label":"RF id test flow","disabled":false,"info":""},{"id":"b77b440.3e4b4c","type":"ui_toast","z":"9415995b.c09f98","position":"dialog","displayTime":"10","highlight":"","outputs":1,"ok":"Ok","cancel":"","topic":"Warning! Activity at the gate!","name":"","x":1114,"y":179,"wires":[[]]},{"id":"64694e3d.14e74","type":"delay","z":"9415995b.c09f98","name":"","pauseType":"rate","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"5","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":914,"y":130,"wires":[["b77b440.3e4b4c"]]},{"id":"1ee18c18.366e44","type":"function","z":"9415995b.c09f98","name":"User","func":"msg.payload = msg.payload  +   \"  Arrived home at: <br>\"\nmsg.topic = \"Warning! The gate is open!\"\n\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+\"/\"+(dt.getMonth() + 1)+\"/\"+dt.getDate()+\" - \"+(dt.getHours()+0)+\":\"+dt.getMinutes(); + msg.payload + msg.topic\nreturn msg;","outputs":1,"noerr":0,"x":698,"y":184,"wires":[["b77b440.3e4b4c"]]},{"id":"c3e51b73.17e318","type":"mqtt in","z":"9415995b.c09f98","name":"","topic":"status","qos":"1","datatype":"auto","broker":"3ad182b8.a268fe","x":110,"y":129,"wires":[["9bd02c8e.0dda8"]]},{"id":"9bd02c8e.0dda8","type":"json","z":"9415995b.c09f98","name":"","property":"payload","action":"","pretty":false,"x":242,"y":129,"wires":[["7f90d7d4.012598"]]},{"id":"b0521fd1.1db6","type":"function","z":"9415995b.c09f98","name":"Open","func":"msg ={\n    payload: \"Warning the gate left open for more than 2 miniutes at! <br>\",\n    topic: \"Warning! Activity at the gate!\"\n};\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+\".\"+(dt.getMonth() + 1)+\".\"+dt.getDate()+\"  \"+(dt.getHours()+0)+\":\"+dt.getMinutes();\nreturn msg;","outputs":1,"noerr":0,"x":700,"y":233,"wires":[["b77b440.3e4b4c"]]},{"id":"2602d81b.dbfa78","type":"function","z":"9415995b.c09f98","name":"Closed","func":"msg ={\n    payload: \"The gate has been closed at! <br>\",\n    topic: \"Warning! The activity has ended !\"\n};\nvar dt = new Date();\nmsg.payload = msg.payload + dt.getFullYear()+\".\"+(dt.getMonth() + 1)+\".\"+dt.getDate()+\"  \"+(dt.getHours()+0)+\":\"+dt.getMinutes();\nreturn msg;","outputs":1,"noerr":0,"x":701,"y":130,"wires":[["64694e3d.14e74"]]},{"id":"7f90d7d4.012598","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.type","propertyType":"msg","rules":[{"t":"eq","v":"door","vt":"str"},{"t":"eq","v":"access","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":371,"y":129,"wires":[["ac7a5bec.247788"],["38426c07.ccd344"]]},{"id":"ac7a5bec.247788","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.door","propertyType":"msg","rules":[{"t":"eq","v":"Door Closed","vt":"str"},{"t":"eq","v":"Door Open","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":516,"y":85,"wires":[["477c92aa.73ec2c","2602d81b.dbfa78"],["477c92aa.73ec2c"]]},{"id":"bfdb74d8.a346f8","type":"change","z":"9415995b.c09f98","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"Status","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":311.9999694824219,"y":232.66665649414062,"wires":[["24ce1afa.53bd26"]]},{"id":"477c92aa.73ec2c","type":"change","z":"9415995b.c09f98","name":"","rules":[{"t":"set","p":"Status","pt":"flow","to":"payload.door","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":718.9999389648438,"y":87.66665649414062,"wires":[[]],"outputLabels":["2"]},{"id":"1f0ee1d0.33e90e","type":"inject","z":"9415995b.c09f98","name":"2 min","topic":"Warning! Activity at the gate!","payload":"","payloadType":"date","repeat":"10","crontab":"","once":false,"onceDelay":0.1,"x":135.44439697265625,"y":234,"wires":[["bfdb74d8.a346f8"]]},{"id":"24ce1afa.53bd26","type":"switch","z":"9415995b.c09f98","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Door Open","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":515,"y":233,"wires":[["b0521fd1.1db6"]]},{"id":"38426c07.ccd344","type":"switch","z":"9415995b.c09f98","name":"","property":"payload.username","propertyType":"msg","rules":[{"t":"cont","v":"Detty","vt":"str"},{"t":"cont","v":"Pisti","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":515,"y":181,"wires":[["1ee18c18.366e44"],["1ee18c18.366e44"]]},{"id":"3ad182b8.a268fe","type":"mqtt-broker","z":"","name":"mqtt","broker":"192.168.1.71","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
1 Like

Hi Pisti, you could try https://flows.nodered.org/node/node-red-contrib-looptimer2. I think it does what you need.