Multiply two variables

I have two random payloads, First, I assign the variables with "change note" then want to multiply them but I am getting NaN in debug.

You need to use JOIN Node.

Hi @usmanEmder

it looks like you have two different flows coming into the Function node. That means each will arrive as a separate event into the Function - one will have msg.variable1 set and the other will have msg.variable2 set.

If you want to add values from two different messages then you need some way of getting them combined into a single message with both values present.

There are two ways of doing that.

One is to use the Join node (as I see @softy2k has just suggested). That can be used to combine messages into a single message before passing it on.

The other is to use Context. That would let you store the two values in context outside of the flow, and then if you have both values you can then perform the calculation.

For using a Join node see this example in the cookbook. https://cookbook.nodered.org/basic/join-streams

Now, I attached the function node and to define payloads.(For 1st: msg.payload = msg.payload ; & for 2nd: msg.payload = msg.payload1; ) and the join node setting as shown in picture below

And the result shows

Will I need to multiply by using the following code in the function node.

msg.payload = msg.payload1 * msg.payload ; return msg;

Expand the payload object in the debug to see the data. Then if you don't know how to get it out have a look at the node-red guide Working with Messages. In fact it would be worth your while working right through the node-red essentials videos linked from the docs. That should only take an hour or so and will be time well spent.

Hi,
I have the same Problem multiplying two variables. I get 24 values from my electricity meter via some transmitting protocol. After defining each of these 24 numbers as variables i like to start some calculations and create new variables. A simple example would be the power of one phase which is the product of the voltage and current of that phase. In my example v5 and v8.
I know absolutely nothing about programming. Can anyone look at the image in line 27 and tell me why the return of v25 is undefined.
What code would multiply 2 of those 24 variables.

Thank you for your help.

Please copy/paste the code from your function here so that we can read it easily and if necessary test it. When pasting, go to a new line and click the </> button at the top of the forum text entry window and paste it in.

[Edit] Also please enable the debug node showing what is going into the function node and fully expand it in the debug window. Then click the little Copy button next to it (in the debug window) and paste that here too. Again use the </> button.

v5 and v8 are objects.
if you want to do any maths on them, you need to access the object properties that hold the value (not the object itself)

e.g.

var v25 = { payload: v5.payload * v8.payload }

Thank you so much Steve.
It worked! Now I have the power of every Phase in my dashboard. That is awesome :slight_smile:
As you can see I struggle with this payload stuff. It is not very intuitive for a beginner.
Is it possible to give the properties of the object a different name in the first place like:

var v25 = { PowerL1: v5.voltage* v8.current};

Can you recommend any JavaScript literature that teaches this very basic stuff with focus on electronics and control engineering?

Yes you can use (just about) any names you like for variables as long as you remember to change them everywhere... There are some reserved characters that are best avoided like space and _ and punctuation - (and starting with a number) - but in general simple text and numerics should serve you well.

You might like to try this js tuturial from the "Learn enough to be dangerous" guy. I haven't worked through it but it looks as if it may be good, and I do know that some of his other courses are good and it does talk about using it with nodjs whereas many tutorials assume you are using it for web development. It is free to use online, but you don't get the videos and some other stuff for free.
Let us know if you find it any good for a beginner so we know whether to recommend it to others.