How to stop a timestamp with delay

Hi to all,
i need help for a very simple code but i don't find a solution.
i've a RGB matrix display and a motion sensor in a room. I need to stop a timestamp with a delay 1 sec if if is motion sensor is safe. Can any one help me?

[{"id":"6b2c6ddb.cdfec4","type":"text-to-matrix","z":"9254773b.8426f8","name":"Clock 255","xOffset":"7","yOffset":"6","rgb":"","font":"10x20.bdf","prefix":"","source":"msg.payload","matrix":"341032d2.642a4e","zLevel":"","x":3480,"y":3420,"wires":[]},{"id":"49e60b56.7e4654","type":"switch","z":"9254773b.8426f8","name":"luminositĂ ","property":"payload","propertyType":"msg","rules":[{"t":"btwn","v":"06:45:00","vt":"str","v2":"21:00:00","v2t":"str"},{"t":"btwn","v":"21:00:01","vt":"str","v2":"23:59:59","v2t":"str"},{"t":"cont","v":"false","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":3,"x":3040,"y":3240,"wires":[["feabec13.cfad7"],["b2d8c21b.eeb5d"],[]]},{"id":"1da66f68.d493f1","type":"mqtt in","z":"9254773b.8426f8","name":"","topic":"home/status/122/value","qos":"2","datatype":"auto","broker":"af265a6b.1019a8","x":1780,"y":3420,"wires":[["cafa33a0.2342e"]]},{"id":"cafa33a0.2342e","type":"hc2-device","z":"9254773b.8426f8","name":"Motion (Cucina)","deviceID":"122","server":"f29d11a7.dd269","x":1980,"y":3420,"wires":[["b97a246f.5114b8"]]},{"id":"30f35f8d.34f78","type":"switch","z":"9254773b.8426f8","name":"","property":"payload.motion","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"jsonata"},{"t":"eq","v":"false","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":2,"x":2370,"y":3420,"wires":[["1279e607.349f3a"],["b2d8c21b.eeb5d"]]},{"id":"b97a246f.5114b8","type":"change","z":"9254773b.8426f8","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.motion","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2170,"y":3420,"wires":[["30f35f8d.34f78"]]},{"id":"1279e607.349f3a","type":"change","z":"9254773b.8426f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":2640,"y":3240,"wires":[["8655e4.6637ea2","4f22dbc8.f37564"]]},{"id":"4f22dbc8.f37564","type":"delay","z":"9254773b.8426f8","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":2640,"y":3300,"wires":[["1279e607.349f3a"]]},{"id":"3023fb77.92ab74","type":"debug","z":"9254773b.8426f8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":3490,"y":3240,"wires":[]},{"id":"8655e4.6637ea2","type":"moment","z":"9254773b.8426f8","name":"HH:mm:ss","topic":"","input":"data","inputType":"msg","inTz":"Europe/Rome","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"it_IT","output":"","outputType":"msg","outTz":"Europe/Rome","x":2850,"y":3240,"wires":[["49e60b56.7e4654"]]},{"id":"feabec13.cfad7","type":"function","z":"9254773b.8426f8","name":"on red","func":"var out = {\"data\":msg.payload,\n            \"rgb\":\"255,255,255\"\n}\nnode.send({\"payload\":out})","outputs":1,"noerr":0,"x":3250,"y":3240,"wires":[["3023fb77.92ab74","6b2c6ddb.cdfec4"]]},{"id":"b2d8c21b.eeb5d","type":"function","z":"9254773b.8426f8","name":"OFF","func":"var out = {\"data\":msg.payload,\n            \"rgb\":\"0,0,0\"\n}\nnode.send({\"payload\":out})","outputs":1,"noerr":0,"x":3250,"y":3420,"wires":[["3023fb77.92ab74","6b2c6ddb.cdfec4"]]},{"id":"341032d2.642a4e","type":"led-matrix","z":0,"height":"32","width":"64","chained":"2","parallel":"1","brightness":"100","refreshDelay":"500","mapping":"adafruit-hat","rgbSequence":"RGB","autoRefresh":true,"cmdArgs":""},{"id":"af265a6b.1019a8","type":"mqtt-broker","z":"","name":"Raspi3","broker":"192.168.0.75","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"f29d11a7.dd269","type":"hc2-server-config","z":"","name":"HC2","ipaddress":"192.168.0.163","output_topic":"home/status","globals_topic":""}]

Hello and welcome on the forum,
Can you please further explain, i don’t quite understand what you want to do?
Do I understand it right that you want to route the motion true message according to time of day and you want a timestamp to be shown every second? I see two errors right away.
Firstly you are creating an infinite loop as soon as motion is detected with no way to stop it. It will send the timestamp every second indefinitely.
Second the way you are trying to filter for a specific timeframe will not work. The is between option of the switch node is made for numeric ranges and not hh:mm:ss format time.
You will need some way to break a loop using something like a switch node that checks a flow variable that for example gets set when motion is false and breaks the loop. Than you will need to change the way you check for a timeframe. You could use https://flows.nodered.org/node/node-red-contrib-time-range-switch or use a function node if you feel fancy.
Have a look at this simplified version of your flow including the fancy way of doing it with a function node:


[{"id":"f954564f.03e718","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"e3c7173.52a81e8","type":"switch","z":"f954564f.03e718","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":1210,"y":540,"wires":[["97222aa9.a7ef18"],["f69e4f9e.604fb8"]]},{"id":"97222aa9.a7ef18","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":1400,"y":460,"wires":[["b98e34ee.cec3e8","320defd6.3ba2c"]]},{"id":"320defd6.3ba2c","type":"delay","z":"f954564f.03e718","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1340,"y":380,"wires":[["eafd92e2.844e1"]]},{"id":"8651e9ec.9e10d","type":"debug","z":"f954564f.03e718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":2210,"y":460,"wires":[]},{"id":"b98e34ee.cec3e8","type":"moment","z":"f954564f.03e718","name":"HH:mm:ss","topic":"","input":"data","inputType":"msg","inTz":"Europe/Rome","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"it_IT","output":"","outputType":"msg","outTz":"Europe/Rome","x":1610,"y":460,"wires":[["45834832.063e2"]]},{"id":"4e230c91.4bf314","type":"function","z":"f954564f.03e718","name":"on red","func":"msg.payload = {\n    data:msg.payload,\n    rgb:\"255,255,255\"\n};\nreturn msg;","outputs":1,"noerr":0,"x":1990,"y":380,"wires":[["8651e9ec.9e10d"]]},{"id":"f69e4f9e.604fb8","type":"function","z":"f954564f.03e718","name":"OFF","func":"msg.payload = {\n    data:msg.payload,\n    rgb:\"0,0,0\"\n};\nreturn msg;","outputs":1,"noerr":0,"x":1980,"y":540,"wires":[["8651e9ec.9e10d"]]},{"id":"cb405bde.f4bfa","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":830,"y":380,"wires":[["6b7cc586.aaa92c"]]},{"id":"937982fc.4270b","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":830,"y":540,"wires":[["6b7cc586.aaa92c"]]},{"id":"45834832.063e2","type":"function","z":"f954564f.03e718","name":"checktime","func":"const time = msg.payload;\nconst t1low = \"06:45:00\";\nconst t1up = \"21:00:00\";\nlet t1 = false;\nconst t2low = \"21:00:01\";\nconst t2up = \"23:59:59\";\nlet t2 = false;\nfunction tos(input){\n    let multi = [3600,60,0];\n    let result = input.split(\":\").map(item => parseFloat(item));\n    result = result.map((element, index) => element*multi[index]).reduce((a,b) => a+b);\n    return result;\n}\nlet t1s = tos(time);\nlet t1ls = tos(t1low);\nlet t1us = tos(t1up);\nif(t1ls>t1us){\n    if(t1s<t1ls) t1s += 86400;\n    t1us += 86400;\n}\nif(t1s>=t1ls && t1s<=t1us) t1 = true;\nlet t2s = tos(time);\nlet t2ls = tos(t2low);\nlet t2us = tos(t2up);\nif(t2ls>t2us){\n    if(t2s<t2ls) t2s += 86400;\n    t2us += 86400;\n}\nif(t2s>=t2ls && t2s<=t2us) t2 = true;\nif(t1){\n    return [msg, null];\n} else if (t2){\n    return [null, msg];\n} else {\n    return null;\n}","outputs":2,"noerr":0,"x":1810,"y":460,"wires":[["4e230c91.4bf314"],["f69e4f9e.604fb8"]]},{"id":"eafd92e2.844e1","type":"switch","z":"f954564f.03e718","name":"","property":"motion","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":1490,"y":380,"wires":[["97222aa9.a7ef18"]]},{"id":"6b7cc586.aaa92c","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"set","p":"motion","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":460,"wires":[["e3c7173.52a81e8"]]}]

i cleaned up your function nodes and wrote an additional function that replaces the switch. This function splits your hh:mm:ss into an array it than includes a function that reduces that array to the sum of the seconds since midnight to be able to compare it to your timeframes.
There is also now a flow variable motion which gets set to the value of the motion sensor and gets checked in the timestamp loop. As soon as the motion sensor goes to false the loop breaks.
Hope this might help a little as I was guessing as your description of your goal wasn’t very clear.
best regards Johannes

Hi Johannes,
thank you very much! Thanks to your changes I got exactly what I wanted.
To tell the truth, the switch I had inserted for selecting the time worked correctly, of course your solution is definitely cleaner. Instead, I couldn't find a way to break the infinite loop.
Now I can proceed with the full flow of my RGB clock matrix, I am attaching a photo so it is clearer what I am building.
Thank you again.
Lorenzo

Looks very nice :+1:.
I know now why the switch node wasn’t working for me. I was playing with it after midnight and the is between does handle time comparisons (which i honestly didn’t know) but only if they don’t cross over midnight.
I had changed your second time range to 21:00:01-02:00:00 so that i could test it and that doesn’t work it seams.
I thin what is happening is that it always assumes the earlier value is the first which means it will pass the inverted range for those time ranges. Edit, i had a look at the actual code of the switch node:( It only works in the first place for the other values because javascript does some funky type conversion in the background. It seams as the code in the actual switch node is very simplistic


'btwn': function(a, b, c) { return (a >= b && a <= c) || (a <= b && a >= c); },

it really just compares two values using the >= <= operators and tries it both ways which explains the inverted behaviour.)
The function i wrote actually checks for that and will work with those time ranges spanning across midnight.
Johannes

after further going down the rabbit hole what javascript does if you throw to strings like hh:mm:ss at a <= operator or similar it lexicographical compares the two, so it pretty much checks character by character.
https://javascript.info/comparison#string-comparison
so it will work in the constrains described above but it’s really not made for it i think.

thank you very much, your explanation is very clear.
I certainly need to study the basics.
For now, thanks to you, I have achieved my goal :+1:

1 Like

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