# Calculate two variables

**URL:** <https://discourse.nodered.org/t/calculate-two-variables/67982>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [20 September 2022 15:26 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982 "2022-09-20T15:26:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)\
**Post date:** [20 September 2022 15:26 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/1 "2022-09-20T15:26:18Z")

</div>

I have two variables from an output of a smbs. I want to subtract them by function to be able to make an indication how much space is available on a disk (in percent).

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/ee6da131919c97536d1e1bfc4747239fdbf04b9a.jpeg)

```auto
"T1" looks like this
msg.topic="T1";
msg.payload = Number(msg.payload[0].value)
return msg;

```

The subtraction function looks like this

```auto
var numberone = msg.payload.T1;
var numbertwo = msg.payload.T2;

msg.payload = (numberone - numbertwo);
return msg;

```

But the output will look like this:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/1162dd9882936bc8e6d34e709ffee27e9e04981f.png)

Thank you in advance for your help

---

<div class="post-metadata">

**Author:** ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)\
**Post date:** [20 September 2022 15:27 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/2 "2022-09-20T15:27:29Z")

</div>

But if I debug the numbers directly then the following comes out

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82f5b7f66d44c7aef1aa3dfc8cdb3302effc7ee0.png)

---

<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:** [20 September 2022 15:42 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/3 "2022-09-20T15:42:50Z")

</div>

Hi @dereine

A flow based programming language requires a different way of looking at the flow of data and the code which is applied to it.

A message leaving your T1 function has a msg.payload value.  
A message leaving your T2 function has a **different** msg.payload value.

Each of these messages is processed **seperately** by the Subtraction node. It only knows of the msg.payload which it's processing, **with no memory of previous values**.

You need to pass your two messages through a Join node to combine them into one with both values.

---

<div class="post-metadata">

**Author:** ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)\
**Post date:** [20 September 2022 15:55 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/4 "2022-09-20T15:55:18Z")

</div>

@jbudd  
First of all, thanks for the fast answere

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/be23b2cf832bff157fb44c87b9c7e0e9b1f6652a.png)

Is the Output from

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/548e5e590040683aba2c3649722c98deee16f71f.jpeg)

i add the T1 and T2. is this wrong?

---

<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:** [20 September 2022 16:04 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/5 "2022-09-20T16:04:23Z")

</div>

Each message needs a distinct topic before the Join. In this example I use "T1" and "T2".

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/430661cfccdfbede742b10996167b603bf7ff45f.jpeg)  
In your T1 and T2 function nodes set msg.topic = "T1" or "T2".

This is the Join node.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/2/d229b53941e315b5c13aafd5428ac051d96bdb07.jpeg)

---

<div class="post-metadata">

**Author:** ![dereine](https://avatars.discourse-cdn.com/v4/letter/d/ee7513/32.png) [@dereine](https://discourse.nodered.org/u/dereine)\
**Post date:** [20 September 2022 16:08 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/6 "2022-09-20T16:08:57Z")

</div>

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3da569722a14386d3d7d7a4c7f7de44ccbd6532f.jpeg)

Perfect, this works fine now. Must find out, why they gife me double outputs, but i think this is a little thing.  
Thanks alot for the help and have a nice day

---

<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:** [20 September 2022 16:13 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/7 "2022-09-20T16:13:18Z")

</div>

Test it without "and evey subsequent message" ticked

---

<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:** [19 November 2022 16:13 UTC](https://discourse.nodered.org/t/calculate-two-variables/67982/8 "2022-11-19T16:13:50Z")

</div>

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