Time subtraction in a function node

Hi All,

I'm not a programmer by any means. I'm trying to figure out how to measure the seconds/milliseconds between two different node events which should end up around 20.xxx seconds. I'm experimenting with a simpletime node as a trigger, mysecond/my millis checked, then passed to the function. I have divided the seconds by 1 to force it to a number, and the millis by 1000 to get a decimal (and force a number). While watching the debug node, it occurred to me that the seconds wrap over after 59. Now I'm stuck handling this phenomenon. Would I be better off with a simple timestamp?

Russ

Yes. No need for any contrib nodes.

var ts1 = msg.payload; // some other time
var diffMS = ts1 - Date.now();
var diffS = diffMS / 1000;
msg.payload = diffS;
return msg;

Alternatively if you look on the Flows site you will find at least one node that measures the times between messages sent to it, so if you send it a start and stop message it will tell you the time between.

Colin,

I actually found that flow by accident yesterday morning, imported it & piddled with it, then moved on. After your mention, I went back and tried to find it with no luck. In the meantime, I found Bart B's "Interval" node which works just like I'd hoped. I really wanted to accomplish something on my own, but I'm not quite ready yet :weary:

Oh, Steve Mcl, I tried your code and it threw an "NaN". I've seen that before on some of my number puzzle sites, what does that mean?

Russ

NaN = Not a Number

it probably means means msg.payload didnt have a timestamp in it

LIeUPDt7M8

This is easy to confirm be using a debug node.

PS, the code i posted does work. Demo...

[{"id":"e34dd9b5.008118","type":"debug","z":"b83d9b41.9c80c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1290,"y":240,"wires":[]},{"id":"7abe0182.47052","type":"inject","z":"b83d9b41.9c80c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1140,"y":180,"wires":[["d6daa69e.7b89e8"]]},{"id":"d6daa69e.7b89e8","type":"function","z":"b83d9b41.9c80c8","name":"","func":"var ts1 = msg.payload; // some other time\nvar diffMS = ts1 - Date.now();\nvar diffS = diffMS / 1000;\nmsg.payload = diffS;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1300,"y":180,"wires":[["e34dd9b5.008118"]]},{"id":"15997279.16c56e","type":"inject","z":"b83d9b41.9c80c8","name":"not a timestamp","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"i am not a number","payloadType":"str","x":1120,"y":140,"wires":[["d6daa69e.7b89e8"]]}]
1 Like

Tried to find what?

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