Countdown Between Payloads

I'm searching for a countdown timer that when it receives a payload, it starts at 0 and displays the negative time since the last payload is sent. The timer will reset to 0 when the next payload is sent and starts the timer again.

Anyone know of a node that does this?


I don't know of a node, but you could do this in a function node.

Here is an example you could use


Negative time is too strange thing to be real and it is not human readable. Should be avoided.
Time flows in one direction with constant speed.
The amount of time passed from last event has always represented with positive number.

You may find some clues from this:

[{"id":"23719e38.98c952","type":"function","z":"c0ddb509.b04f48","name":"","func":"let last = context.get('last') || new Date().getTime()\nlet now = new Date().getTime()\nlet delta = now -last\n\n\nfunction msToTime(s) {\n  function pad(n, z) {\n    z = z || 2;\n    return ('00' + n).slice(-z);\n  }\n\n  var ms = s % 1000;\n  s = (s - ms) / 1000;\n  var secs = s % 60;\n  s = (s - secs) / 60;\n  var mins = s % 60;\n  var hrs = (s - mins) / 60;\n\n  return pad(hrs) + ':' + pad(mins) + ':' + pad(secs) + '.' + pad(ms, 3);\n}\n\nif(msg.topic != 'tick'){\n    last = now\n    context.set('last',last)\n}\n\nlet result = 'Last seen: '+msToTime(delta) +' ago'\n\n\nnode.status({shape:'dot',fill:'red',text:result})\nmsg.payload = result\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":1700,"wires":[["6fe3c273.accbbc"]]},{"id":"7a14c895.1ef2f8","type":"inject","z":"c0ddb509.b04f48","name":"payload","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":1700,"wires":[["23719e38.98c952"]]},{"id":"74ce754b.ee8c4c","type":"inject","z":"c0ddb509.b04f48","name":"tick every second","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"tick","payload":"","payloadType":"date","x":210,"y":1740,"wires":[["23719e38.98c952"]]},{"id":"6fe3c273.accbbc","type":"debug","z":"c0ddb509.b04f48","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":1680,"wires":[]}]

Arugh...this is what I get for trying to do things with a head cold. Here is my example flow - very similar to @hotNipi's but his is much fancier :laughing:

[{"id":"2a147dbf.ee0a02","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"b3e47081.dc091","type":"inject","z":"2a147dbf.ee0a02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":80,"wires":[["eaa2ace9.4b093"]]},{"id":"eaa2ace9.4b093","type":"function","z":"2a147dbf.ee0a02","name":"","func":"// get current time\nvar d = new Date();\nvar current_time = d.getTime();\n\n// get the previous time - if non exists, use curent time\nvar previous_time = flow.get('previous_time')||current_time;\n\n//get the difference\nvar time_diff = current_time - previous_time\n//truncat the decimal portion and return it in msg.payload\nmsg.payload = Math.trunc(time_diff / 1000)\n\n//save current time as previous time\nflow.set('previous_time', current_time)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":80,"wires":[["b165801e.df7a38"]]},{"id":"b165801e.df7a38","type":"debug","z":"2a147dbf.ee0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":80,"wires":[]}]

Thanks for that code. It's exactly what I need but the only issue is it stops at 5 seconds. I am no programmer so I'm a bit lost with the code to figure out why it's stopping after 5 seconds.


Its stopping after 5 sec because the top inject is a demo inject & it is set to inject every 5 seconds.

Remove the interval from the top inject and manually trigger it to test it.

1 Like

Geez...I didn't look at the inject node. Good grief. Thanks.

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