# Stopwatch on node-red?

**URL:** <https://discourse.nodered.org/t/stopwatch-on-node-red/18518>\
**Category:** General\
**Created:** [27 November 2019 15:50 UTC](https://discourse.nodered.org/t/stopwatch-on-node-red/18518 "2019-11-27T15:50:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikza5566](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nikza5566/32/26570_2.png) [@nikza5566](https://discourse.nodered.org/u/nikza5566)\
**Post date:** [27 November 2019 15:50 UTC](https://discourse.nodered.org/t/stopwatch-on-node-red/18518/1 "2019-11-27T15:50:38Z")

</div>

I want to make a stopwatch for some condition in my project.  
for ex. if my node get data(something like "START") from mqtt so that will make a time count too, time will count untill my node get an another data(like "STOP") and I'll record that time between start and stop (ex. 01:32:24 ).  
Thank you.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 November 2019 20:02 UTC](https://discourse.nodered.org/t/stopwatch-on-node-red/18518/2 "2019-11-27T20:02:40Z")

</div>

Well, one way of doing this is when you send the "Start" and "Stop" signals, you include a time stamp with them.

Then you simply subtract them.

Rather than sending ONLY a "message", send it with a TOPIC which will be used to determine if it Stops or Starts.

Eg:  
(Two buttons)  
Both inject the "time stamp" - which is the default in the `inject` node.  
Set the `topic` for on as START and the `topic` for the other as STOP.

Use a `function` node.  
Something like:

```auto
let difference = 0;
let start = 0;
if (msg.topic == "START")
{
    context.set("START",msg.payload)
}
else
if (msg.topic == "STOP")
{
    start = context.get("START");
    difference = msg.payload - start;
    msg.payload = difference
}
return msg;

```

Something LIKE that.

I'm not a coder, and I may have made a mistake in there, but I hope you can see the concept of what I am meaning.

Then send that node to another node (who's name I can't remember) to format it into a time format.

---

<div class="post-metadata">

**Author:** ![nikza5566](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nikza5566/32/26570_2.png) [@nikza5566](https://discourse.nodered.org/u/nikza5566)\
**Post date:** [28 November 2019 12:38 UTC](https://discourse.nodered.org/t/stopwatch-on-node-red/18518/3 "2019-11-28T12:38:33Z")

</div>

Thank you for reply me. 😀  
I'll try your suggestion and I have few question but let me try this first.
