Epoch Time subtraction

I am trying to do a simple subtraction of time in a function node.

The elements of time include; current time, minus 'time since last motion'. Both of these are formatted in epoch time.

The issue that I am having is that 'time since last motion' - only sends a piece of data every time motion is sensed and the time is obviously a constant, therefore the flow gets thrown off as there is never a cross over with data being received.

I am using MQTT to pull in the 'time since last motion' and I am using a simple inject node for the current time.

The function node consists of:

msg.payload = msg.time - msg.payload.mot;
return msg;

The output I am getting is NaN.

Please see below for the flow.

Any help would be greatly appreciated. Thanks in advance.

As messages arrive at different times you would have to join the node
https://cookbook.nodered.org/basic/join-streams
you could also access the current time in the function node

Thinking about your question, you could also do this in a single wire, just move the incoming time to another property. Then format the current time. Then in the function use the two properties to do your calculation.

I shall give it a try!

Thank you!

basically you want to calculate the elapsed time between two mqtt msgs coming from the "Pull" node ?

a single Function node can remember the last timestamp by saving it in Context and then doing a calculation

Here is an example:

[{"id":"1e385cb4f3e69b70","type":"inject","z":"54efb553244c241f","name":"mqtt","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":300,"y":1080,"wires":[["ae5f9ce4c2ca41b1"]]},{"id":"ae5f9ce4c2ca41b1","type":"function","z":"54efb553244c241f","name":"Calculate elapsed time","func":"let lastMotion = context.get(\"lastMotion\") ||  Date.now()\nlet now = Date.now()\n\nlet elapsed = now - lastMotion\n\ncontext.set(\"lastMotion\", now)\n\nmsg.payload = elapsed;\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":1080,"wires":[["b0647e2c628fdda4"]]},{"id":"b0647e2c628fdda4","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":1080,"wires":[]}]
1 Like

One of the msgs is MQTT the other is just a time stamp in Epoch format..

Still seems to give me a NaN reading when I put them through the function node to do the subtraction

NaN means 'not a number' is it arriving as a string ?

It is - if I put it straight to a debug node, it comes through as expected. It just seems to be when it goes through the function node to do the subtraction that something unexpected happens

what does the function node code look like ?

Within the function node I have the following:

msg.payload.timeconvert = (msg.payload.time / 1000) - msg.payload.tsml;
return msg;

I have tried using the join node as suggested above, but I am having an issue using the join node as it doesn't seen to want to connect the data as one is from MQTT and the other is from an Inject node.

The join node will need to be configured properly, ie. number of expected input messages and how they will be combined.

Think I have done that, however when I run it - there is no error, but there is also no output.

Then you dont have it set up correctly.

Do both streams going into join have a unique topic?
Have you set the join node to Key/Value mode and enters 2 as the count?
Did you follow this article in the cookbook for an example of how to join messages into one object?

Share your flow if you are are still stuck & show us the value that comes from both legs of the flow before the join node (use debug nodes and screenshot the 2 output values)

1 Like

An example of what you have tried would help, Can not help with no information.
export your flow and show us the incoming payload/s and the output you require.

I have now managed to join them. The issue I had was that I had called the different inputs msg.payload.x and msg.payload.y

I have now managed to get the output I wanted - however I'm now not sure how I would do the subtraction of msg.payload - msg.payload?

Apologies if I'm being dumb - I'm new to Node-Red.

Use the debug panel to copy the path of each value & do your math / logic in a function node, setting msg.payload to the new value.

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

1 Like

Thanks so much, that is a massive help!

If you havent already, I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like

Thanks, I will most certainly do that!

I have just copied the 'path' - both of which are just coming back as 'payload'.

yes, they do - in a function node, just prepend msg.

(the copy path is really intended for regular nodes - but it gets you the most important part - the subpath)

so... "copy path", type msg., paste.