# Subtraction within function node

**URL:** <https://discourse.nodered.org/t/subtraction-within-function-node/56586>\
**Category:** General\
**Tags:** node-red-dashboard, function-node, mqtt\
**Created:** [13 January 2022 11:18 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586 "2022-01-13T11:18:52Z")\
**Posts on this page:** 13\
**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:** [13 January 2022 11:18 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/1 "2022-01-13T11:18:52Z")

</div>

Hi all - Apologies in advance as I am new to Node-Red and this may be an easy fix, but after multiple attempts I cannot seem to find the fix.

I am using node-red version 2.1.3.

What I am trying to do is pull data into node red from MQTT.

Once the data arrives I am identifying two separate figures. One being current time and the other being time since last motion was detected.

What I want to do is **subtract** time since last motion, from current time.

Within my function node, this is what I have most recently tried;

```auto
msg.payload = msg.payload - msg.payload.tslm;
return msg;

```

The debug node gives me the output of 'NaaN'

Any help or guidance would be greatly appreciated.

Thanks in advance!

---

<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:** [13 January 2022 11:20 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/2 "2022-01-13T11:20:10Z")

</div>

What does the input object 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:** [13 January 2022 11:24 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/3 "2022-01-13T11:24:35Z")

</div>

The start of the flow is the MQTT in - this provides me with the data that I need.

I then filter this into two separate change nodes, to specify and pump out only the current time in one change node and time since last motion in the other. I have checked with a debug that these pump out the correct data which they do.

I then join both of the change nodes to the function node where I then attempt the subtraction.

Hope that makes sense?

---

<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:** [13 January 2022 11:25 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/4 "2022-01-13T11:25:44Z")

</div>

Please copy/paste the output of the join/debug node here.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 January 2022 11:28 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/5 "2022-01-13T11:28:25Z")

</div>

> [@JHScen](#):
>
> `msg.payload = msg.payload - msg.payload.tslm;`

That doesn't make sense.

Your "current time" must be msg.payload.something.  
Use a debug node to see the message coming out of your Join node.

---

<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:** [13 January 2022 11:32 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/6 "2022-01-13T11:32:35Z")

</div>

So in the change node for current time is;

```auto
Set msg.payload
to the value msg.payload.currentTime

```

And the change node for the time since last motion is

```auto
Set msg.payload.tslm
to the value msg.payload.motion.stats

```

The debug node gives me two messages:  
One being current time:

```auto
164206230

```

The second being time since last motion:

```auto
tslm: 1642061678

```

---

<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:** [13 January 2022 11:38 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/7 "2022-01-13T11:38:27Z")

</div>

Unless you share your flow or a screenshot we will struggle to help you.

> [@JHScen](#):
>
> `msg.payload = msg.payload - msg.payload.tslm;`

This makes no sense because an object property `msg.payload` cannot be a number and an object with a sub property at the same time.

At a guess, you should probably remove the change node and do this...

```auto
msg.payload = msg.payload.currentTime - msg.payload.motion.stats;
return msg;

```

but as i said, difficult to say without seeing the flow.

---

<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:** [13 January 2022 11:40 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/8 "2022-01-13T11:40:22Z")

</div>

![screenshotofflow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c034f3dede8a5d99b1eecbeeebb330dff6a887a8.png)

---

<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:** [13 January 2022 11:43 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/9 "2022-01-13T11:43:30Z")

</div>

> Please copy/paste the output of the join/debug node here.

_edit_ I concur with @Steve-Mcl , please follow the documentation.

---

<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:** [13 January 2022 11:43 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/10 "2022-01-13T11:43:54Z")

</div>

So now we can see the issue.

A basic understanding of node- red is that messages **NEVER** arrive from 2 different wires at the same time into a single node. You need to join the 2 messages using a `join` node.

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<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:** [13 January 2022 11:45 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/11 "2022-01-13T11:45:29Z")

</div>

I got rid of the change node and it worked! Thanks a lot!

---

<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:** [13 January 2022 11:46 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/12 "2022-01-13T11:46:16Z")

</div>

Actually, you also dont need 2 MQTT nodes if they are the same topic.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [27 January 2022 11:46 UTC](https://discourse.nodered.org/t/subtraction-within-function-node/56586/13 "2022-01-27T11:46:37Z")

</div>

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