Counting elapsed time

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

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

try something like this

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;
1 Like

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?

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;

if you want to reset the accumalated then

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

no need to reset start_time.

1 Like

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

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 :slight_smile:

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

2 Likes

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