# How to use function node for 1+1=2

**URL:** <https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749>\
**Category:** General\
**Created:** [28 July 2022 11:58 UTC](https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749 "2022-07-28T11:58:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelvin](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kelvin](https://discourse.nodered.org/u/kelvin)\
**Post date:** [28 July 2022 11:58 UTC](https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749/1 "2022-07-28T11:58:11Z")

</div>

Hi everyone,

May i know how to use a function node for simple mathematics, or can use others node to do that.  
thanks

---

<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:** [28 July 2022 12:08 UTC](https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749/2 "2022-07-28T12:08:19Z")

</div>

```auto
msg.payload = 1 + 1;
return msg;

```

It might be wise to do some basic JS tutorials and watch the official node-red videos...

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![kelvin](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kelvin](https://discourse.nodered.org/u/kelvin)\
**Post date:** [28 July 2022 12:15 UTC](https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749/3 "2022-07-28T12:15:17Z")

</div>

thank you for your reply, but my issue is when inject 2 node i will received = 2.  
meaning that each inject node is 1

---

<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:** [28 July 2022 12:54 UTC](https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749/4 "2022-07-28T12:54:48Z")

</div>

Hi Kelvin,

Your problem is that a function node, or any other node, only knows about the single message which is currently passing through it.

If your data comes as two separate messages, you have to either join the two into a single message or else save the values as "context" variables.

Here is a flow that illustrates the two approaches:

 ![Untitled 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/524ca7922ba774dac15e84d2d6804632c78dbb92.jpeg)

```auto
[{"id":"54ef561233e67d08","type":"tab","label":"Flow 11","disabled":false,"info":"","env":[]},{"id":"5b2f60cf5fdf2a78","type":"inject","z":"54ef561233e67d08","name":"1st input","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":120,"y":40,"wires":[["2678bffc8fd6f767"]]},{"id":"bae2837ac3810d3e","type":"inject","z":"54ef561233e67d08","name":"2nd input","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":120,"y":80,"wires":[["2678bffc8fd6f767"]]},{"id":"2678bffc8fd6f767","type":"function","z":"54ef561233e67d08","name":"Can access only the current mesage","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":60,"wires":[["032be0c96ab4a79f"]]},{"id":"f7dc4e8a6ef97d50","type":"inject","z":"54ef561233e67d08","name":"1st input, topic one","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"one","payload":"3","payloadType":"num","x":150,"y":160,"wires":[["10507078a2aaa36b"]]},{"id":"6a830b0a7160c99d","type":"inject","z":"54ef561233e67d08","name":"2nd input, topic two","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"5","payloadType":"num","x":150,"y":200,"wires":[["10507078a2aaa36b"]]},{"id":"778644e5490eaa07","type":"function","z":"54ef561233e67d08","name":"Can access values as msg.payload.one and msg.payload.two","func":"msg.payload = msg.payload.one + msg.payload.two;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":740,"y":180,"wires":[["5ee102f0be05cdb8"]]},{"id":"10507078a2aaa36b","type":"join","z":"54ef561233e67d08","name":"Join (Manual mode)","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":380,"y":180,"wires":[["778644e5490eaa07"]]},{"id":"4aafd88c9cabbccd","type":"inject","z":"54ef561233e67d08","name":"1st input","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":120,"y":260,"wires":[["0c794c9cc70c0225"]]},{"id":"3a0a68248717bed1","type":"inject","z":"54ef561233e67d08","name":"2nd input","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":120,"y":300,"wires":[["45df97c32728132d"]]},{"id":"980af9debe89b93f","type":"function","z":"54ef561233e67d08","name":"Can access values as flow.one and flow.two","func":"let vone = flow.get('one') | 0;\nlet vtwo = flow.get('two') | 0;\nmsg.payload = vone + vtwo;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":280,"wires":[["cca0879b263a84e8"]]},{"id":"0c794c9cc70c0225","type":"change","z":"54ef561233e67d08","name":"","rules":[{"t":"set","p":"one","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":260,"wires":[["980af9debe89b93f"]]},{"id":"45df97c32728132d","type":"change","z":"54ef561233e67d08","name":"","rules":[{"t":"set","p":"two","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":300,"wires":[["980af9debe89b93f"]]},{"id":"032be0c96ab4a79f","type":"debug","z":"54ef561233e67d08","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":100,"wires":[]},{"id":"5ee102f0be05cdb8","type":"debug","z":"54ef561233e67d08","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":860,"y":220,"wires":[]},{"id":"cca0879b263a84e8","type":"debug","z":"54ef561233e67d08","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":780,"y":320,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![kelvin](https://avatars.discourse-cdn.com/v4/letter/k/7ba0ec/32.png) [@kelvin](https://discourse.nodered.org/u/kelvin)\
**Post date:** [28 July 2022 13:10 UTC](https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749/5 "2022-07-28T13:10:04Z")

</div>

Thanks a lot for helping me

---

<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:** [26 September 2022 13:10 UTC](https://discourse.nodered.org/t/how-to-use-function-node-for-1-1-2/65749/6 "2022-09-26T13:10:21Z")

</div>

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