# Function node - simple addition of two values

**URL:** <https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690>\
**Category:** General\
**Created:** [25 April 2021 10:37 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690 "2021-04-25T10:37:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rantanplan](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@rantanplan](https://discourse.nodered.org/u/rantanplan)\
**Post date:** [25 April 2021 10:37 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/1 "2021-04-25T10:37:27Z")

</div>

Hi all,  
I'm struggeling with an easy addtition of two valueas in a function node. I'm able to handover flow-variables and send it to a debug node but I'm too stupid to add this two values and hand it over to a dashboard text node. can someone please help me out?

```auto
[{"id":"84614ad9.405f98","type":"function","z":"79a8ef90.aeb6f","name":"","func":"var f_pv = flow.get('f_pv');\n\nmsg.pv = f_pv;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":600,"wires":[["864775b5.916fa8"]]},{"id":"13f3836a.accb4d","type":"function","z":"79a8ef90.aeb6f","name":"","func":"var f_stromzaehler = flow.get('f_stromzaehler');\n\nmsg.zaehler = f_stromzaehler;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":660,"wires":[["36174598.6cb99a"]]},{"id":"864775b5.916fa8","type":"debug","z":"79a8ef90.aeb6f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"pv","targetType":"msg","statusVal":"payload","statusType":"auto","x":480,"y":600,"wires":[]},{"id":"36174598.6cb99a","type":"debug","z":"79a8ef90.aeb6f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"zaehler","targetType":"msg","statusVal":"payload","statusType":"auto","x":490,"y":660,"wires":[]},{"id":"ca0ddf76.42082","type":"inject","z":"79a8ef90.aeb6f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":620,"wires":[["84614ad9.405f98","13f3836a.accb4d"]]}]

```

---

<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:** [25 April 2021 11:02 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/2 "2021-04-25T11:02:18Z")

</div>

Hi, because you get the flow vars in separate functions you cannot add them together without re-joining them (a join node would do this)

HOWEVER, why bother doing the `flow.get` twice in two functions - just do it all in one function...

```auto
var f_pv = flow.get('f_pv'); 
var f_stromzaehler = flow.get('f_stromzaehler');
msg.pv = f_pv;
msg.zaehler = f_stromzaehler;
msg.payload = f_pv + f_stromzaehler;
return msg;

```

demo...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/6180aa95fc9631d65154d2745d4d2978f7e3ffaa.png)

```auto
[{"id":"84614ad9.405f98","type":"function","z":"dd03fa8d.2f4be8","name":"","func":"var f_pv = flow.get('f_pv'); \nvar f_stromzaehler = flow.get('f_stromzaehler');\nmsg.pv = f_pv;\nmsg.zaehler = f_stromzaehler;\nmsg.payload = f_pv + f_stromzaehler;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1220,"y":780,"wires":[["864775b5.916fa8","9851a4ff.e91c18","95da2f8e.6a351"]]},{"id":"864775b5.916fa8","type":"debug","z":"dd03fa8d.2f4be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"pv","targetType":"msg","statusVal":"payload","statusType":"auto","x":1420,"y":720,"wires":[]},{"id":"ca0ddf76.42082","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1040,"y":780,"wires":[["84614ad9.405f98"]]},{"id":"9851a4ff.e91c18","type":"debug","z":"dd03fa8d.2f4be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"zaehler","targetType":"msg","statusVal":"payload","statusType":"auto","x":1430,"y":780,"wires":[]},{"id":"95da2f8e.6a351","type":"debug","z":"dd03fa8d.2f4be8","name":"to Dashboard","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1440,"y":840,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![rantanplan](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@rantanplan](https://discourse.nodered.org/u/rantanplan)\
**Post date:** [25 April 2021 11:31 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/3 "2021-04-25T11:31:42Z")

</div>

Hi Steve,

many thanks for your input, I've tried it but I'm getting this one:  
msg.payload : string[30]  
"[object Object][object Object]"

 ![2021-04-25 13_30_00-Window](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b1fe3bae773c62c2f7ebdb9c27aded9930383987.png)

---

<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:** [25 April 2021 11:33 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/4 "2021-04-25T11:33:08Z")

</div>

show me what you have stored in the flow context for those values....  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/3501efb520a7cd12c90ea93b8eb1e6b73257de35.png)

it looks like you have stored the FULL msg instead of just the payload.

---

<div class="post-metadata">

**Author:** ![rantanplan](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@rantanplan](https://discourse.nodered.org/u/rantanplan)\
**Post date:** [25 April 2021 11:51 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/5 "2021-04-25T11:51:09Z")

</div>

![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b2e6e42895413db4b958a9f650d5340755e1d98.png)

---

<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:** [25 April 2021 11:52 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/6 "2021-04-25T11:52:41Z")

</div>

so yes, you are storing the full `msg` object in flow variables.  
This is not really desirable for your requirements.

If you save just `msg.payload` in the flow variable, it will hold the actual value (not the msg) & the function I wrote will work.

---

<div class="post-metadata">

**Author:** ![rantanplan](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@rantanplan](https://discourse.nodered.org/u/rantanplan)\
**Post date:** [25 April 2021 11:57 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/7 "2021-04-25T11:57:36Z")

</div>

aaaaaawesome, thats it.  
thanks a lot your glory, have a nice day.

---

<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:** [9 May 2021 11:58 UTC](https://discourse.nodered.org/t/function-node-simple-addition-of-two-values/44690/8 "2021-05-09T11:58:12Z")

</div>

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