# Calculate the seconds between two timestamps

i need a function to calculate the difference time in seconds between two clicks, On click and Off click

You aren't going to believe this.

I just wrote one.

``````[{"id":"93846123e59e8ce9","type":"function","z":"7e987ddf260bdf0d","name":"Time between message arrival","func":"////////////////////////////////////////////////////////////////////////////////\n//\n//                  How to use\n//\n//  `msg.payload` **MUST** be timestanps.   So at worst, add a `change` node\n//  to set the message as `timestamp`\n//\n////////////////////////////////////////////////////////////////////////////////\n\nvar a = context.get(\"A\") || 0;\n//var b = context.get(\"B\") || 0;\nvar c;\n\nif (a == 0)\n{\n    //  A\n    a = msg.payload;\n//    node.warn(\"A = \" + a);\n    context.set(\"A\",a);\n    return;\n} else\nif (msg.topic == \"B\")\n{\n    //  B\n    b = msg.payload;\n//    node.warn(\"B = \" + b);\n}\n\na = context.get(\"A\");\nc = b - a;\n\ncontext.set(\"A\",0);\n\nmsg.payload = c;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1140,"y":490,"wires":[[]]}]
``````

But it will give you the number of MILLISECONDS... So just `/1000` for seconds.

1 Like

There is a nice re-usable subflow: flow-timer. A simple subflow to measure time taken in parts of your flows. (flow) - Node-RED

Example...

Demo flow (use CTRL+I to import)

``````[{"id":"9a693308.7ebaf","type":"subflow","name":"flow-timer","info":"","category":"","in":[{"x":80,"y":100,"wires":[{"id":"7fc82258.93e36c"}]}],"out":[{"x":440,"y":100,"wires":[{"id":"7fc82258.93e36c","port":0}]}],"env":[{"name":"name","type":"str","value":"measure","ui":{"icon":"font-awesome/fa-tag","label":{"en-US":"Timer Name"},"type":"input","opts":{"types":["str","env"]}}},{"name":"operation","type":"str","value":"start","ui":{"icon":"font-awesome/fa-cog","label":{"en-US":"Operation"},"type":"select","opts":{"opts":[{"l":{"en-US":"start"},"v":"start"},{"l":{"en-US":"stop"},"v":"stop"},{"l":{"en-US":"msg.topic"},"v":"msg.topic"},{"l":{"en-US":"msg.operation"},"v":"msg.operation"},{"l":{"en-US":"msg.payload"},"v":"msg.payload"}]}}}],"meta":{"module":"node-red-contrib-flow-performance","type":"flow-performance","version":"1.0.1","author":"Steve-Mcl","desc":"Inline flow performance measure node","keywords":"node-red performance","license":"MIT"},"color":"#DAEAAA","icon":"node-red/timer.svg","status":{"x":280,"y":160,"wires":[{"id":"7fc82258.93e36c","port":1}]}},{"id":"7fc82258.93e36c","type":"function","z":"9a693308.7ebaf","name":"do operation","func":"// @ts-ignore\nvar name = msg.perfName || env.get(\"name\");\n// @ts-ignore\nvar operation = msg.perfOperation || env.get(\"operation\");\nvar measures = global.get(\"flow_timers\") || {};\nvar measure = measures[name] || {};\n\nfunction doOp(measure, op){\n    if(operation === \"start\"){\n        measure.start = Date.now();//change to process.hrtime\n        measure.stop = null;\n        measure.durationMs = null;\n    } else if(operation === \"stop\") {\n        measure.stop = Date.now();//change to process.hrtime\n        measure.durationMs = measure.start ? measure.stop - measure.start : null;\n        msg._performance = measure;\n    }\n}\n\n\nif(operation === \"start\"){\n    doOp(measure, operation);\n} else if(operation === \"stop\") {\n    doOp(measure, operation);\n    node.send([null, { payload: { text: name + \": \" + measure.durationMs + \"ms\" }}]);\n} else if(operation === \"msg.topic\") {\n    operation = msg.topic;\n    doOp(measure, operation);\n} else if(operation === \"msg.operation\") {\n    operation = msg.operation;\n    doOp(measure, operation);\n} else if(operation === \"msg.payload\") {\n    operation = msg.payload;\n    doOp(measure, operation);\n} else {\n    return [msg, null];\n}\nmeasures[name] = measure;\nglobal.set(\"flow_timers\", measures);\n\nreturn [msg, null];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":100,"wires":[[],[]]},{"id":"3863b16330f8d504","type":"inject","z":"79d198b88749c016","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":610,"y":864,"wires":[["6f1a2bc49f1ca8ee"]]},{"id":"7548488f56eb733b","type":"inject","z":"79d198b88749c016","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OFF","payloadType":"str","x":610,"y":912,"wires":[["364744c8a7cc9c30"]]},{"id":"6f1a2bc49f1ca8ee","type":"subflow:9a693308.7ebaf","z":"79d198b88749c016","name":"on_off_time start","env":[{"name":"name","value":"on_off_time","type":"str"}],"x":786,"y":864,"wires":[[]]},{"id":"364744c8a7cc9c30","type":"subflow:9a693308.7ebaf","z":"79d198b88749c016","name":"on_off_time stop","env":[{"name":"name","value":"on_off_time","type":"str"},{"name":"operation","value":"stop","type":"str"}],"x":790,"y":912,"wires":[["1bef0bef8f6a22af"]]},{"id":"1bef0bef8f6a22af","type":"debug","z":"79d198b88749c016","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":960,"y":912,"wires":[]}]
``````
1 Like

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