Cannot calculate date difference in NodeRed?!

#1

Hi,

I'm writing a function where I want to calculate the difference between when current message was received and a previous message. The previous date timestamp is stored in a context variable.

When I do:

var previous = context.get('prev') || 0;
var now = new Date().getTime();
var since = now - previous;

The since is always equal to now. It is not performing the substration --> huh??
I have tried with Date's before, which should also just work, but it also did not.
Also tried Math.abs(now - previous) but it gives the same result.

If, on the same spot, I do a simple var since = 10 - 9 then the result is 1.
I have checked the types. Both now and previous are of type number.

Is this causing an issue because the timestamp value is too large? But why would that be an issue in NodeJS and not in a browser? I use Chrome Console for trying stuff out and there new Date() - new Date() just works as expected.
Is this a known problem with NodeJS/NodeRed?
How can I calculate the time difference?

Cheers

0 Likes

#2

Somewhere you'll need to store this previous value in context. If it is not stored, the value will be always 0 (zero) and your calculation does always the now - 0 = now

0 Likes

#3

previous is stored in context and when I log the previous value, I see it has the right value.
So it's value not 0.

0 Likes

#4

Can you show whole function you have written then?

0 Likes

#5

Hi @diversit
you don't need a function for that.

Try this and just replace the "Trigger" buttons by your inputs:

[{"id":"60a4cc8c.fbd7a4","type":"inject","z":"95476864.681758","name":"previous ","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":140,"wires":[["cddeddfc.246d5"]]},{"id":"16558fab.c3191","type":"inject","z":"95476864.681758","name":"Trigger","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":210,"y":200,"wires":[["7f4f5da8.834c14"]]},{"id":"cddeddfc.246d5","type":"change","z":"95476864.681758","name":"Save previous time","rules":[{"t":"set","p":"previous_timestamp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":140,"wires":[[]]},{"id":"7f4f5da8.834c14","type":"change","z":"95476864.681758","name":"Calculate","rules":[{"t":"set","p":"payload","pt":"msg","to":"$millis()-$flowContext(\"previous_timestamp\")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":200,"wires":[["66684540.e4291c"]]},{"id":"66684540.e4291c","type":"debug","z":"95476864.681758","name":"Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":630,"y":200,"wires":[]}]
0 Likes

#6

I think @diversit wants the time between 2 sequential messages in the same flow - not the time between two messages in different flows

0 Likes

#7

Sorry guys, my function was a bit more complex as I described here and I just found the problem. All my bad. I reset the previous value to 0 in a function before doing the calculation. Oepsedaisy.
Thanks for helping out though!

0 Likes