# Calculate with function node

**URL:** <https://discourse.nodered.org/t/calculate-with-function-node/73322>\
**Category:** General\
**Created:** [7 January 2023 10:53 UTC](https://discourse.nodered.org/t/calculate-with-function-node/73322 "2023-01-07T10:53:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![F11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/f11/32/73913_2.png) [@F11](https://discourse.nodered.org/u/F11)\
**Post date:** [7 January 2023 10:53 UTC](https://discourse.nodered.org/t/calculate-with-function-node/73322/1 "2023-01-07T10:53:29Z")

</div>

Hello,  
I am a newbie with node red and have a little problem.  
I want to add two values in a function.  
One value comes via the payload, the other is a flow variable.  
I can display the values, but I can't calculate them.  
Unfortunately, I can't do it.

Here is the code in the function node. I will leave the countless attempts in

```auto
var msgtemp = { payload: msg.payload };
var msghumi = { payload: msg.humidity };
var f_temp1 = flow.get('f_temp1');
var msgtemp1 = {payload: f_temp1};
var msgtemp2 = Math = Number({ payload: msg.payload }) + Number({ payload: f_temp1 });
//var msgtemp2 = Math = Number(msgtemp) + Number(msgtemp1);
var msgtemp3 = {payload: msgtemp2};

//msg.payload = Math = Number(msgtemp) + Number(msgtemp1);
//var msgtemp3 = {payload: msgtemp2};
return [msgtemp, msghumi, msgtemp2];
//return [msgtemp, msghumi, msgtemp1];
//return msg;

```

I only want

24.3 + (-0.6) = 23.7  
msgtemp + f\_temp1 = msgtemp1  
calculate

if this works, another flow variable is added  
msghumi + f\_feuch1 = msghumi1

many thanks already  
Greetings  
Roland

---

<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:** [7 January 2023 11:03 UTC](https://discourse.nodered.org/t/calculate-with-function-node/73322/2 "2023-01-07T11:03:22Z")

</div>

Do not create your payload objects till after you have done all your calcs  
e.g.

```auto
var msgtemp = msg.payload ;
var msghumi = msg.humidity ;
var f_temp1 = flow.get('f_temp1') ?? 0;
var msgtemp2 = Number(msgtemp) + Number(f_temp1);
msghumi = {payload: msghumi};
msgtemp2 = {payload: msgtemp2};
msgtemp = {payload: msgtemp};
return [msgtemp, msghumi, msgtemp2];

```

[edit] fixed typo

---

<div class="post-metadata">

**Author:** ![mudwalker](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mudwalker](https://discourse.nodered.org/u/mudwalker)\
**Post date:** [7 January 2023 11:08 UTC](https://discourse.nodered.org/t/calculate-with-function-node/73322/3 "2023-01-07T11:08:58Z")

</div>

Welcome to the Forum!

@E1cid beat me to it and has provided the answer.

Here was my response and it will show your problem....

Are you working on numbers? It may be that one or both of your variables are NOT numbers.

Add a debug node configured to show All Messages (see below) after your function node and pass the variables as messages to this.

 ![2023-01-07_11-04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/20308f94f616017d80525dde3a24d20b7f435322.png)

---

<div class="post-metadata">

**Author:** ![F11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/f11/32/73913_2.png) [@F11](https://discourse.nodered.org/u/F11)\
**Post date:** [7 January 2023 11:56 UTC](https://discourse.nodered.org/t/calculate-with-function-node/73322/4 "2023-01-07T11:56:49Z")

</div>

Hello E1cid,

that was really fast.  
Now it works! I have already tested it 🙂  
But a small mistake has crept into your work

Now I just have to integrate it into my project.  
it should not be

> [@E1cid](#):
>
> `msgtemp = {payload: msg.temp};`

```auto
msgtemp = { payload: msgtemp };

```

just for the sake of completeness, in case someone wants to recreate this.

@mudwalker  
yes I am working with numbers at this point.

thank you very much!  
until the next problem

Roland

---

<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:** [21 January 2023 11:57 UTC](https://discourse.nodered.org/t/calculate-with-function-node/73322/5 "2023-01-21T11:57:48Z")

</div>

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