# Counting elapsed time

**URL:** <https://discourse.nodered.org/t/counting-elapsed-time/59432>\
**Category:** General\
**Created:** [6 March 2022 10:39 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432 "2022-03-06T10:39:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![PanK](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@PanK](https://discourse.nodered.org/u/PanK)\
**Post date:** [6 March 2022 10:39 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/1 "2022-03-06T10:39:14Z")

</div>

Hi, I need some help working on a project since I am not so familiar with JavaScript.

What I need is the time between when the user on the emergency slider ON and OFF. The time also has to accumulate every time the status change.

The situation that will happen is like at 8:30 Emergency is on till 8:45 then Emergency on again at 9:20 then off at 9:25. The payload I need is 20.

I can get the time different use the below flow but not really sure on how to accumulate them

Thank you in advance

```auto
[{"id":"dd743d7cea6578f9","type":"function","z":"49e7cb4bbb6a8c74","name":"start counting stop","func":"var resume = flow.get(\"Resume\")||0\n\nif (msg.payload == \"STOP\"){\n var stop = new Date(); // current time\n flow.set(\"stopTime\", stop);\n} else {\n var running = new Date();\n resume = (running - flow.get(\"stopTime\"))/1000;\n}\n\nflow.set(\"Resume\", resume)\n \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":180,"wires":[[]]},{"id":"5eb25248627ebf72","type":"ui_switch","z":"49e7cb4bbb6a8c74","name":"","label":"Emergency","tooltip":"","group":"455218286d580613","order":3,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"status","topicType":"str","style":"","onvalue":"STOP","onvalueType":"str","onicon":"","oncolor":"","offvalue":"RUNNING","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":210,"y":180,"wires":[["dd743d7cea6578f9","2c116880aaeadb6e"]]},{"id":"2c116880aaeadb6e","type":"ui_text","z":"49e7cb4bbb6a8c74","group":"455218286d580613","order":4,"width":0,"height":0,"name":"","label":"STATUS","format":"{{msg.payload}}","layout":"row-center","className":"","x":400,"y":240,"wires":[]},{"id":"455218286d580613","type":"ui_group","name":"Button control","tab":"5e6283e6bfa1daa4","order":1,"disp":true,"width":"4","collapse":false,"className":""},{"id":"5e6283e6bfa1daa4","type":"ui_tab","name":"SIIT","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 March 2022 11:06 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/2 "2022-03-06T11:06:52Z")

</div>

try something like this

```auto
if (msg.payload == "RUNNING"){
    let accumalated = flow.get("accumalated") || 0;
    let stop = new Date().valueOf(); // current time
    let start = flow.get("start_time") || stop;
    msg.payload = accumalated + (stop - start)/1000 // accumalated in seconds
    flow.set("accumalated", msg.payload);
} else {
    flow.set("start_time", new Date().valueOf());
    msg = null 
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![PanK](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@PanK](https://discourse.nodered.org/u/PanK)\
**Post date:** [6 March 2022 11:31 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/3 "2022-03-06T11:31:10Z")

</div>

Thankyou so much, it's work!

I forgot to mention that I also need a way to reset everything too. If I do sth like this would it be fine?

```auto
if (msg.payload == "reset"){
    accumalated = 0
    start_time = 0
} else if (msg.payload == "RUNNING"){
    let accumalated = flow.get("accumalated") || 0;
    let stop = new Date().valueOf(); // current time
    let start = flow.get("start_time") || stop;
    msg.payload = accumalated + (stop - start)/1000
    flow.set("accumalated", msg.payload);
} else {
    flow.set("start_time", new Date().valueOf());
    msg = null 
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 March 2022 11:40 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/4 "2022-03-06T11:40:16Z")

</div>

if you want to reset the accumalated then

```auto
if (msg.payload == "reset"){
    flow.set("accumalated", 0)
    msg = null //so flow stops there.
}

```

no need to reset start\_time.

---

<div class="post-metadata">

**Author:** ![WBr](https://avatars.discourse-cdn.com/v4/letter/w/b3f665/32.png) [@WBr](https://discourse.nodered.org/u/WBr)\
**Post date:** [6 March 2022 11:47 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/5 "2022-03-06T11:47:43Z")

</div>

Maybe this could be of additional help  
[https://flows.nodered.org/flow/07570367cf2ccf79344c5f336004d9d9](https://flows.nodered.org/flow/07570367cf2ccf79344c5f336004d9d9)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 March 2022 11:55 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/6 "2022-03-06T11:55:02Z")

</div>

I feel I have to point out, just to save any other pedants here from further mental anguish, that that is not how to spell accumulated. To save typing and brain strain I normally use `total` 🙂

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [6 March 2022 12:11 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/7 "2022-03-06T12:11:53Z")

</div>

Apologisez to all Grammer nazi's, I will insure to cause more mentol anguesh.

---

<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:** [20 March 2022 12:12 UTC](https://discourse.nodered.org/t/counting-elapsed-time/59432/8 "2022-03-20T12:12:28Z")

</div>

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