# IF time interval between events, THEN

**URL:** <https://discourse.nodered.org/t/if-time-interval-between-events-then/63065>\
**Category:** General\
**Created:** [25 May 2022 17:28 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065 "2022-05-25T17:28:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leppermesiah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leppermesiah/32/62103_2.png) [@leppermesiah](https://discourse.nodered.org/u/leppermesiah)\
**Post date:** [25 May 2022 17:28 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/1 "2022-05-25T17:28:00Z")

</div>

Hello geeks,

I'm trying to make another function for a Sonoff switch.  
Currently, if the switch entity state changes to ON, then Node-Red starts a timer for 1 hour. When it expires, it turns off the switch.  
What I need in addition to that, is IF I do ON-OFF-ON within 2 seconds, THEN start a separate timer for 3 hours. Is there a node or a script to do that?  
Any help or hint will be much appreciated.

All the best,  
Ivaylo

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [25 May 2022 17:52 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/2 "2022-05-25T17:52:31Z")

</div>

Hello @leppermesiah!  
Welcome to this forum.  
Do you have a flow to share ... as a starting point to support you?  
Regards, Ralph

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [25 May 2022 18:22 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/3 "2022-05-25T18:22:32Z")

</div>

I do something similar - however I count only if I switch 2 times within 1 or 2 seconds. Then you can use a aggregation node - together with a timer node.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/27b4778785eb5019057bc79b1cb7e8c1beb0a50e.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2022 18:31 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/4 "2022-05-25T18:31:44Z")

</div>

Here is a simple flow for detecting any sequence you wish (adjust it in the function node)

![chrome_DUEMJ7iNo8](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b51c81367ac76c301299b4348951619e40be0243.gif)

Demo flow (use `CTRL+I` to import)

```auto
[{"id":"9917b371dd14555c","type":"trigger","z":"b4132d0e1950d012","name":"timeout","op1":"reset","op2":"reset","op1type":"str","op2type":"str","duration":"2","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1580,"y":80,"wires":[["b4f1358385ad8b13"]]},{"id":"6f86f87ba4e65d92","type":"inject","z":"b4132d0e1950d012","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":1350,"y":80,"wires":[["32fee50d381b6260"]]},{"id":"012cfe0db8c94f9b","type":"inject","z":"b4132d0e1950d012","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":1350,"y":120,"wires":[["32fee50d381b6260"]]},{"id":"32fee50d381b6260","type":"change","z":"b4132d0e1950d012","name":"","rules":[],"action":"","property":"","from":"","to":"","reg":false,"x":1475,"y":100,"wires":[["9917b371dd14555c","cc884808c240dc27"]],"icon":"node-red/arrow-in.svg","l":false},{"id":"cc884808c240dc27","type":"delay","z":"b4132d0e1950d012","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1590,"y":140,"wires":[["b4f1358385ad8b13"]]},{"id":"b4f1358385ad8b13","type":"function","z":"b4132d0e1950d012","name":"SEQ monitor","func":"\nconst SEQ = [1,0,1]\nconst states = context.get(\"states\") || []\ncontext.set(\"states\", states)\n\nif(msg.payload === \"reset\") {\n states.length = 0\n node.status({fill:\"grey\",shape:\"ring\",text:\"\"});\n return null\n}\n\nif (states.length >= 3) {\n clear()\n return\n}\n\nstates.push(msg.payload)\nnode.status({ fill: \"yellow\", shape: \"ring\", text: states.map(e => e ? \"ON\" : \"OFF\").join(\"-\") });\n\nif(RED.util.compareObjects(states, SEQ)) {\n msg.payload = \"SEQ DETECTED\"\n clear()\n node.status({ fill: \"green\", shape: \"dot\", text: SEQ.map(e => e ? \"ON\" : \"OFF\").join(\"-\") });\n return msg;\n}\n\n\n\nfunction clear() {\n states.length = 0\n node.status({ fill: \"grey\", shape: \"ring\", text: \"\" });\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1770,"y":100,"wires":[["8f391f8531153b86"]]},{"id":"8f391f8531153b86","type":"debug","z":"b4132d0e1950d012","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1820,"y":180,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [25 May 2022 23:08 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/5 "2022-05-25T23:08:08Z")

</div>

Very nice Steve !

Craig

---

<div class="post-metadata">

**Author:** ![leppermesiah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leppermesiah/32/62103_2.png) [@leppermesiah](https://discourse.nodered.org/u/leppermesiah)\
**Post date:** [26 May 2022 08:14 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/6 "2022-05-26T08:14:15Z")

</div>

Thank you Steve. That's awesome solution, exactly what i needed. 👍

---

<div class="post-metadata">

**Author:** ![leppermesiah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/leppermesiah/32/62103_2.png) [@leppermesiah](https://discourse.nodered.org/u/leppermesiah)\
**Post date:** [26 May 2022 13:30 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/7 "2022-05-26T13:30:10Z")

</div>

So, that's what I ended with. I've also included a light blink to let me know that the separate workflow is in effect and not the 1 hour workflow.  
Again, thank you @Steve-Mcl

```auto
[{"id":"8d1931ef.fba7e","type":"stoptimer","z":"5855a9b0.f86128","duration":"1","units":"Hour","payloadtype":"bool","payloadval":"true","name":"","x":430,"y":80,"wires":[["96ff87cb.d9f8b8"],[]]},{"id":"96ff87cb.d9f8b8","type":"api-call-service","z":"5855a9b0.f86128","name":"Turn OFF Bathroom Fan","server":"f3fc7196.512e5","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.sonoff_bathroom_fan"],"data":"{ \"entity_id\": \"switch.sonoff_bathroom_fan\" }","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1010,"y":80,"wires":[[]]},{"id":"c4d786de.5eb878","type":"change","z":"5855a9b0.f86128","name":"STOP","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":200,"wires":[["8d1931ef.fba7e"]]},{"id":"52bd786d.357aa8","type":"server-state-changed","z":"5855a9b0.f86128","name":"Bathroom Fan","server":"f3fc7196.512e5","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.sonoff_bathroom_fan","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"300","forType":"num","forUnits":"milliseconds","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":70,"y":140,"wires":[["8d1931ef.fba7e"],["c4d786de.5eb878"]]},{"id":"9917b371dd14555c","type":"trigger","z":"5855a9b0.f86128","name":"timeout","op1":"reset","op2":"reset","op1type":"str","op2type":"str","duration":"2","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":400,"y":280,"wires":[["b4f1358385ad8b13"]]},{"id":"cc884808c240dc27","type":"delay","z":"5855a9b0.f86128","name":"","pauseType":"delay","timeout":"50","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":410,"y":340,"wires":[["b4f1358385ad8b13"]]},{"id":"b4f1358385ad8b13","type":"function","z":"5855a9b0.f86128","name":"SEQ monitor","func":"\nconst SEQ = [1,0,1]\nconst states = context.get(\"states\") || []\ncontext.set(\"states\", states)\n\nif(msg.payload === \"reset\") {\n states.length = 0\n node.status({fill:\"grey\",shape:\"ring\",text:\"\"});\n return null\n}\n\nif (states.length >= 3) {\n clear()\n return\n}\n\nstates.push(msg.payload)\nnode.status({ fill: \"yellow\", shape: \"ring\", text: states.map(e => e ? \"ON\" : \"OFF\").join(\"-\") });\n\nif(RED.util.compareObjects(states, SEQ)) {\n msg.payload = \"SEQ DETECTED\"\n clear()\n node.status({ fill: \"green\", shape: \"dot\", text: SEQ.map(e => e ? \"ON\" : \"OFF\").join(\"-\") });\n return msg;\n}\n\n\n\nfunction clear() {\n states.length = 0\n node.status({ fill: \"grey\", shape: \"ring\", text: \"\" });\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":300,"wires":[["f7523df7361fbdf9","c4d786de.5eb878","369918c40449fc6b"]]},{"id":"f7523df7361fbdf9","type":"stoptimer","z":"5855a9b0.f86128","duration":"3","units":"Hour","payloadtype":"bool","payloadval":"true","name":"","x":770,"y":300,"wires":[["e8f1e96bada124bc"],[]]},{"id":"e8f1e96bada124bc","type":"api-call-service","z":"5855a9b0.f86128","name":"Turn OFF Bathroom Fan","server":"f3fc7196.512e5","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.sonoff_bathroom_fan"],"data":"{ \"entity_id\": \"switch.sonoff_bathroom_fan\" }","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1010,"y":300,"wires":[[]]},{"id":"1a94494455f79651","type":"server-state-changed","z":"5855a9b0.f86128","name":"Bathroom Fan State","server":"4f302e90.f44de","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.sonoff_bathroom_fan","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":90,"y":320,"wires":[["7158afeb2d16bddc"]]},{"id":"7158afeb2d16bddc","type":"change","z":"5855a9b0.f86128","name":"Change","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"1","tot":"num"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":320,"wires":[["9917b371dd14555c","cc884808c240dc27"]]},{"id":"f8e85071d73dd4c0","type":"api-call-service","z":"5855a9b0.f86128","name":"Light Signal","server":"f3fc7196.512e5","version":5,"debugenabled":false,"domain":"switch","service":"toggle","areaId":[],"deviceId":[],"entityId":["switch.sonoff_bathroom_lights"],"data":"{ \"entity_id\": \"switch.sonoff_bathroom_lights\" }","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":630,"y":420,"wires":[[]]},{"id":"369918c40449fc6b","type":"trigger","z":"5855a9b0.f86128","name":"","op1":"1","op2":"1","op1type":"str","op2type":"str","duration":"250","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":420,"y":420,"wires":[["f8e85071d73dd4c0"]]},{"id":"f3fc7196.512e5","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},{"id":"4f302e90.f44de","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}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 June 2022 13:31 UTC](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/8 "2022-06-09T13:31:09Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
