IF time interval between events, THEN

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

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

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

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

chrome_DUEMJ7iNo8

Demo flow (use CTRL+I to import)

[{"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":[]}]
3 Likes

Very nice Steve !

Craig

1 Like

Thank you Steve. That's awesome solution, exactly what i needed. :+1:

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

[{"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}]

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