# Countdown Between Payloads

**URL:** <https://discourse.nodered.org/t/countdown-between-payloads/46815>\
**Category:** General\
**Created:** [7 June 2021 14:07 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815 "2021-06-07T14:07:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aa0z](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@aa0z](https://discourse.nodered.org/u/aa0z)\
**Post date:** [7 June 2021 14:07 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/1 "2021-06-07T14:07:36Z")

</div>

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?

Kyle

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 June 2021 20:04 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/2 "2021-06-07T20:04:33Z")

</div>

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

Here is an example you could use

```auto
Math.trunc(

```

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [7 June 2021 20:23 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/3 "2021-06-07T20:23:01Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [7 June 2021 21:55 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/4 "2021-06-07T21:55:47Z")

</div>

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 😆

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

```

---

<div class="post-metadata">

**Author:** ![aa0z](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@aa0z](https://discourse.nodered.org/u/aa0z)\
**Post date:** [8 June 2021 15:46 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/5 "2021-06-08T15:46:20Z")

</div>

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.

Kyle

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [8 June 2021 16:02 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/6 "2021-06-08T16:02:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aa0z](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@aa0z](https://discourse.nodered.org/u/aa0z)\
**Post date:** [8 June 2021 16:04 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/7 "2021-06-08T16:04:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [7 August 2021 16:04 UTC](https://discourse.nodered.org/t/countdown-between-payloads/46815/8 "2021-08-07T16:04:37Z")

</div>

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