Adding

Whilst I was hunting around the internet, trying to find out how to add two numbers  together, I stumbled across a smart way of getting both numbers into a Function node effectively at the same time.

This was achieved by loading msg.payload with an array containing the numbers that are needed for the calculation.  It is a generalised solution that I suspect may be used for many a problem.

So we create an array by sending the various parts of the calculation to a ‘join’ node, then when all the elements of the array have been received, it is sent on to a change node.

The flow looks like this:

Flow for the array technique

Put one value into msg.parts. We need two of these, one for each number to be added

Join node configuration – makes the array of two values.

The change node that does the calculation

This method of doing simple arithmetic can be extended  by using other JASONata expressions in the change node.

Here is a collection of these ‘other’ expressions to have a play with in your sandbox.

https://flows.nodered.org/flow/29fd01f8a62fec86d875ecbd68001cb0

Join nodes are good for making arrays, see https://cookbook.nodered.org/basic/join-streams