Node-Red Flow Samples/Sharing

Here is a simple subflow "wrapper" around the vari-timer delay node that allows for intermediate steps.. either seconds or minutes. You set the intervals via a delimited string in the subflow instance properties..

You could also do a similar thing via switch node and some additional logic but this is nice and encapsulated if you need it..

Delay with Intervals Subflow Example

[{"id":"85e4d659.1f2e28","type":"subflow","name":"Delay with Interval","info":"","category":"","in":[{"x":40,"y":80,"wires":[{"id":"ebdad02d.af581"}]}],"out":[{"x":1260,"y":60,"wires":[{"id":"907cab7d.d1bf58","port":0}]},{"x":1440,"y":180,"wires":[{"id":"709a5ed7.8312","port":0}]}],"env":[{"name":"DELAY","type":"num","value":"30"},{"name":"INTERVALS","type":"str","value":"10/20/30"},{"name":"USE_MINS","type":"bool","value":"true"}],"color":"#DDAA99","outputLabels":["Timer Completed","Interval Output"],"status":{"x":1440,"y":280,"wires":[{"id":"3bb0ab2a.dc0894","port":0},{"id":"709a5ed7.8312","port":1}]}},{"id":"907cab7d.d1bf58","type":"stoptimer-varidelay","z":"85e4d659.1f2e28","duration":"DELAY","durationType":"env","units":"Second","payloadtype":"num","payloadval":"0","name":"","reporting":"every_second","persist":false,"x":840,"y":60,"wires":[[],[],["efedccd6.62f1"]]},{"id":"ff01a84a.1f68f8","type":"change","z":"85e4d659.1f2e28","name":"set flow.intervals to array, set max = delay in secs.","rules":[{"t":"set","p":"intervals","pt":"flow","to":"$map(\t    $split($env('INTERVALS'),\"/\")\t    , function($v) {$number($v) * ( $env(\"USE_MINS\") ? 60: 1)\t})\t","tot":"jsonata"},{"t":"set","p":"max","pt":"flow","to":"$env('DELAY') * ( $env(\"USE_MINS\") ? 60: 1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":140,"wires":[["4ce3c5a6.88859c"]]},{"id":"7841b7c2.be35a8","type":"change","z":"85e4d659.1f2e28","name":"set flow.currSecs = max delay secs - timer secs","rules":[{"t":"set","p":"currSecs","pt":"flow","to":"$flowContext(\"max\") -\t(\t$number($split(payload,\":\")[0]) * 3600 +\t$number($split(payload,\":\")[1]) * 60 +\t$number($split(payload,\":\")[2])\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":220,"wires":[["709a5ed7.8312"]]},{"id":"ebdad02d.af581","type":"switch","z":"85e4d659.1f2e28","name":"payload = stop","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"stop","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":200,"y":80,"wires":[["907cab7d.d1bf58"],["ff01a84a.1f68f8"]]},{"id":"efedccd6.62f1","type":"switch","z":"85e4d659.1f2e28","name":"payload == stopped","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"stopped","vt":"str"},{"t":"neq","v":"stopped","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":850,"y":160,"wires":[["3bb0ab2a.dc0894"],["7841b7c2.be35a8"]]},{"id":"3bb0ab2a.dc0894","type":"change","z":"85e4d659.1f2e28","name":"format payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t    \"fill\":\"red\",\t    \"shape\":\"ring\",\t    \"text\": payload\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1100,"y":160,"wires":[[]]},{"id":"709a5ed7.8312","type":"function","z":"85e4d659.1f2e28","name":"output to current interval  when it occurs and currSecs in H:mm:ss format ","func":"var useMins = env.get(\"USE_MINS\");\nvar maxDelay = env.get(\"DELAY\") * (useMins ? 60 : 1);\nvar currSecs = flow.get(\"currSecs\");\nvar intervals = flow.get(\"intervals\");\nvar ret1;\nvar ret2 = msg;\n\nif (typeof intervals === \"number\") {\n    intervals = [intervals];\n}\n\n\nvar nextInterval = intervals.find(element => element > currSecs);\nif (currInterval = intervals.find(element => element == currSecs)) {\n        ret1 = { payload: currSecs / (useMins ? 60 : 1) };\n}\n\n//if (intervals.includes(currSecs)) {\n//        ret1 = { payload: currSecs / 60 };\n//}\n\nvar nextIntStr; \nif (maxDelay === currSecs) {\n    nextIntStr = \" / complete.\";\n} else {\n    nextIntStr = ( nextInterval ? \" / Next: \" + new Date(nextInterval * 1000).toISOString().substr(11, 8) :\" / No more intervals\")    \n}\n\nvar payLoad = new Date(currSecs * 1000).toISOString().substr(11, 8) + nextIntStr ;\n\nret2.payload = {fill:\"green\",shape:\"dot\",text:payLoad};\n\nreturn [ret1,ret2];\n\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":1020,"y":360,"wires":[[],[]]},{"id":"4ce3c5a6.88859c","type":"change","z":"85e4d659.1f2e28","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$env(\"USE_MINS\") ? $env(\"DELAY\") * 60 :  $env(\"DELAY\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":200,"wires":[["907cab7d.d1bf58"]]},{"id":"61a35e4.d8615a","type":"subflow:85e4d659.1f2e28","z":"af91369d.586338","name":"","env":[{"name":"INTERVALS","value":"1/5/8/13/22/25","type":"str"},{"name":"USE_MINS","value":"false","type":"bool"},{"name":"DELAY_MINS","value":"2","type":"num"}],"x":1130,"y":240,"wires":[["22944a1c.c26ce6"],["99348740.0576e8"]]},{"id":"99348740.0576e8","type":"debug","z":"af91369d.586338","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1370,"y":260,"wires":[]},{"id":"f42a81b.9bf2e8","type":"inject","z":"af91369d.586338","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":880,"y":220,"wires":[["61a35e4.d8615a"]]},{"id":"22944a1c.c26ce6","type":"debug","z":"af91369d.586338","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1370,"y":220,"wires":[]},{"id":"2fe82ee9.4f6162","type":"inject","z":"af91369d.586338","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"stop","payloadType":"str","x":870,"y":260,"wires":[["61a35e4.d8615a"]]}]
2 Likes