How to use function node for 1+1=2

Hi everyone,

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

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. 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.

1 Like

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

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:

[{"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":[]}]

Thanks a lot for helping me

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