IF time interval between events, THEN

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