# Epoch Time subtraction

**URL:** https://discourse.nodered.org/t/epoch-time-subtraction/56872
**Category:** General
**Tags:** function-node, database, node-red-dashboard, mqtt
**Created:** [18 January 2022 17:09 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872 "2022-01-18T17:09:44Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [18 January 2022 17:09 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/1 "2022-01-18T17:09:44Z")

</div>

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:

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

```

The output I am getting is **NaN**.

Please see below for the flow.

 ![TSLM visual](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/8/2803fe542eace62c2e2b53e91291be24ababc932.png)

Any help would be greatly appreciated. Thanks in advance.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [18 January 2022 17:48 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/2 "2022-01-18T17:48:35Z")

</div>

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

> **[JavaScript Date Objects](https://www.w3schools.com/js/js_dates.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

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.

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [18 January 2022 18:00 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/3 "2022-01-18T18:00:03Z")

</div>

I shall give it a try!

Thank you!

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [18 January 2022 18:13 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/4 "2022-01-18T18:13:07Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 12:17 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/5 "2022-01-19T12:17:43Z")

</div>

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

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 12:19 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/6 "2022-01-19T12:19:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [19 January 2022 12:23 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/7 "2022-01-19T12:23:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 12:25 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/8 "2022-01-19T12:25:27Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [19 January 2022 12:33 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/9 "2022-01-19T12:33:37Z")

</div>

what does the function node code look like ?

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 12:38 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/10 "2022-01-19T12:38:26Z")

</div>

Within the function node I have the following:

```auto
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.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [19 January 2022 12:41 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/11 "2022-01-19T12:41:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 13:44 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/12 "2022-01-19T13:44:53Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 January 2022 13:49 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/13 "2022-01-19T13:49:32Z")

</div>

> [@bakman2](#):
>
> The join node will need to be configured properly

> [@JHScen](#):
>
> 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](https://cookbook.nodered.org/basic/join-streams) 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)

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [19 January 2022 13:53 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/14 "2022-01-19T13:53:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 14:21 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/15 "2022-01-19T14:21:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 January 2022 14:22 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/16 "2022-01-19T14:22:06Z")

</div>

> [@JHScen](#):
>
> 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?

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://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 14:23 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/17 "2022-01-19T14:23:01Z")

</div>

Thanks so much, that is a massive help!

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 January 2022 14:23 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/18 "2022-01-19T14:23:21Z")

</div>

If you havent already, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). 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.

---

<div class="post-metadata">

### Author: ![JHScen](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@JHScen](https://discourse.nodered.org/u/JHScen)
#### Post date: [19 January 2022 14:25 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/19 "2022-01-19T14:25:36Z")

</div>

Thanks, I will most certainly do that!

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

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [19 January 2022 14:26 UTC](https://discourse.nodered.org/t/epoch-time-subtraction/56872/20 "2022-01-19T14:26:36Z")

</div>

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.

[Next page](https://discourse.nodered.org/t/epoch-time-subtraction/56872.md?page=2)
