Calculate with function node

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

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

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

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

2 Likes

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.

Hello E1cid,

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

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

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

1 Like

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