Two inputs in a node function

Hello. I have two inputs in a node function that input random numerical data read from a solar inverter. PV is the instant production of the inverter and Grid is the consumption from the network.
Through that function, I want to do the following::

var Grid = {payload: msg.payload};

var PV = {payload: msg.payload};

if (PV >1.1 && Grid >-0.4)

{ return 1;}

if (PV <0.7 && Grid <-0.4)

{ return 2; }

if (PV >2.1 && Grid >-0.4)

{ return 3; }

if (PV <1.7 && Grid <-0.4)

{ return 4; }

if (PV >2.8 && Grid >-0.4)

{ return 5; }

if (PV <2.5 && Grid <-0.4)

{ return 6; }

else {

return 0 ;}

data 1,2,3 4,5,6 would be sent to a node switch that controls some switches

Unfortunately, I have nothing on the output, something is not right. I'm at the beginning and I can't figure it out.
Maybe someone can help me with the code from the node function. Thank you

Messages from multiple wires will never arrive at the input of a node at the same time.

You have 2 options here. 1. Connect everything in series and use alternative msg properties e.g msg.result1 & msg.result2 or use a join node to combine the multiple messages into one.

For your particular scenario, I recommend the series approach.

Note: there are more (and better) approaches however it depends on the modbus addresses you are reading. Here is a document that touches on what I am inferring: Modernize your legacy industrial data. Part 2. • FlowFuse

Hi, Thank you for the answer. I cannot connect in series the 2 modbus getter node because the huawei inverter dont respond at 2 requests for 2 differently internal address in the same time . So, i used join node to combine the 2 values : PV and grid and it works.- i have the result in one messages debug4

But function still down work:
The fucntion code is oke?

var Grid = {payload: msg.payload};

var PV = {payload: msg.payload};

if (PV.payload >1.1 && Grid.payload >-0.4)

{ return 1;}
else {
return 0 ;}

Sorry, no, not at all.

Why are you creating 2 objects from the same variable? Your screenshot clearly shows the values of interest are in msg.payload.PV and msg.payload.Grid

An object must be returned (ideally the msg object)

This is the only logic part of your function and it can be done in regular nodes (no need for function)

Use 2 switch nodes in series instead:

Join --> switch (msg.payload.PV > number: 1.1) --> switch (msg.payload.Grid > number: -0.4) --> next node

Yes, i will try with 2 switch but i have 6 contidions and i will need 12 switch

var Grid = {payload: msg.payload};

var PV = {payload: msg.payload};

if (PV.payload >1.1 && Grid.payload >-0.4)

{ return 1;}

if (PV.payload <0.7 && Grid.payload <-0.4)

{ return 2; }

if (PV.payload >2.1 && Grid.payload >-0.4)

{ return 3; }

if (PV.payload <1.7 && Grid.payload <-0.4)

{ return 4; }

if (PV.payload >2.8 && Grid.payload >-0.4)

{ return 5; }

if (PV.payload <2.5 && Grid.payload <-0.4)

{ return 6; }

else {

return 0 ;}

I need a function with one output result ( 1or 2 or 3 or 4 or 5 or6) because after function I have a single switch that controls 3 wireless switches- ex if the function output is 1 first wireless switch is open, if the result is 2 first wireless switch is closed .
So, i need a working function code

Does this code work for you, assuming that the two values are in msg.payload.PV and msg.payload.Grid?

const PV = msg.payload.PV
const Grid = msg.payload.Grid
let result = 0
if (PV > 1.1 && Grid > -0.4) { result = 1 }
else if (PV < 0.7 && Grid < -0.4) { result = 2 }
else if (PV > 2.1 && Grid > -0.4) { result = 3 }
else if (PV < 1.7 && Grid < -0.4) { result = 4 }
else if (PV > 2.8 && Grid > -0.4) { result = 5 }
else if (PV < 2.5 && Grid < -0.4) { result = 6}
msg.payload = result
return msg;

Hi
Thank you so much, works like a charm :pray: :grinning:

Here is my final work

and swich

and control device

Best regards guys !

I think it takes to test that function before use.

Do some simple test cases like that and see if the output matches your expectations

const PV = 3.2
const Grid = -0.2
//testing for (PV > 2.8 && Grid > -0.4) so expect 5
const expected = 5

let result = 0
if (PV > 1.1 && Grid > -0.4) { result = 1 }
else if (PV < 0.7 && Grid < -0.4) { result = 2 }
else if (PV > 2.1 && Grid > -0.4) { result = 3 }
else if (PV < 1.7 && Grid < -0.4) { result = 4 }
else if (PV > 2.8 && Grid > -0.4) { result = 5 }
else if (PV < 2.5 && Grid < -0.4) { result = 6 }
node.warn("expected: "+expected+' result: '+result)
//msg.payload = result
//return msg;

Although it works, PV 2.9 would validate to true for both - it is only due to the order that it does not fall through.

@hotNipi and @bakman2 are right to query that chain of if statements.

@satkey3's original version used return statements, so only one if (... && ...) statement would be evaluated true.
In getting rid of the return statements I used else if (... to hopefully achieve the same result.

A chain of compound if statements like that can quickly become impenetrable.
If I were writing the code myself I'd probably split it into two blocks for Grid > -0.4 and Grid < -0.4
And what happens if Grid === -0.4?

I would use 2 switch nodes, makes it more clear (in my mind at least)

and then for the pv nodes

something along those lines.

1 Like

Hi
Yes, the solution with 2 switch nodes is more easy and clear

Thank you guys!

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