Consume an array into function to do math

This likely seems simple but after hours of different attempts, I am unable to resolve. I have an array of two values that I wish to do math on in a function node but don't know how to set the variables within the function so I can operate on each value. Here is the debug of the array...thank you in advance
3

msg.payload[0] is 14.44
and msg.payload[1] is 0
so
msg.payload[0] * msg.payload[1] would multiply 14.44 by 0.
Or
let myvar = msg.payload[0] set myvar to 14.44
There are other methods but you do not mention what calculation you wish to do.

Thank you...and yes simple multiplication is all I am looking for. However the syntax in your example does not appear to work, and I fully admit that I am a noob. Here is what is in my function node:

var volts = msg.payload[0];
var amps = msg.payload[1];
var watts = volts * amps;
return watts;

And this is the debug message:
4

You always have to return an object.
So it would be.

var volts = msg.payload[0];
var amps = msg.payload[1];
var watts = volts * amps;
msg.payload = watts;
return msg;

or any msg property you wish
msg.watts = watts; for example.

2 Likes

Worked perfectly...I truly appreciate your assistance and very quick reply!!

1 Like

Hey thanks for the super helpful reply. I'm not sure how I missed that thread. I haven't quite mastered the search function on here. I think I'll pass with him this time around. If i want any further guideline we will contact you here https://discourse.nodered.org/t/consume-an-array-into-function-to-do-math/61374.

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