# Two inputs in a node function

**URL:** <https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891>\
**Category:** General\
**Created:** [31 March 2024 20:55 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891 "2024-03-31T20:55:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![satkey3](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@satkey3](https://discourse.nodered.org/u/satkey3)\
**Post date:** [31 March 2024 20:55 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/1 "2024-03-31T20:55:56Z")

</div>

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

 ![functie](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6e064a77b05c9278aa8beecad77c81ad39b5917.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 March 2024 21:31 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/2 "2024-03-31T21:31:01Z")

</div>

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](https://flowfuse.com/blog/2023/09/modernize-your-legacy-industrial-data-part2/)

---

<div class="post-metadata">

**Author:** ![satkey3](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@satkey3](https://discourse.nodered.org/u/satkey3)\
**Post date:** [1 April 2024 05:36 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/3 "2024-04-01T05:36:31Z")

</div>

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

 ![join](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f9ba5f243d1ed02d11286ccd0cc9903589ccea3.png)

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 ;}

 ![functie](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/429fa18062f4d89426ff0d5f05c80969e165aa13.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 April 2024 05:48 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/4 "2024-04-01T05:48:15Z")

</div>

> [@satkey3](#):
>
> But function still down work:  
> The fucntion code is oke?

Sorry, no, not at all.

> [@satkey3](#):
>
> var Grid = {payload: msg.payload};
> 
> var PV = {payload: msg.payload};

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`

> [@satkey3](#):
>
> { return 1;}  
> else {  
> return 0 ;}

An object **must** be returned (ideally the `msg` object)

> [@satkey3](#):
>
> if (PV.payload \>1.1 && Grid.payload \>-0.4)

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

---

<div class="post-metadata">

**Author:** ![satkey3](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@satkey3](https://discourse.nodered.org/u/satkey3)\
**Post date:** [1 April 2024 06:10 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/5 "2024-04-01T06:10:59Z")

</div>

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

 ![automatizatization](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/86bcb3652eae23afe7a14384768cb2c6f8e7aa42.png)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 April 2024 07:07 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/6 "2024-04-01T07:07:05Z")

</div>

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![satkey3](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@satkey3](https://discourse.nodered.org/u/satkey3)\
**Post date:** [1 April 2024 07:47 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/7 "2024-04-01T07:47:18Z")

</div>

Hi  
Thank you so much, works like a charm 🙏 😀

Here is my final work

 ![result](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/1819a8a6714cfa8520380400964d43de9d50d02d.png)

and swich

 ![swich](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/2/22050a6017e65ba70eabddce74f6dde87817d6b1.png)

and control device

 ![demisol-on](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/b/bb9344c9b95d51698ff4761c3c4d793d7eceb2fe.png)

Best regards guys !

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [1 April 2024 08:08 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/8 "2024-04-01T08:08:33Z")

</div>

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [1 April 2024 08:15 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/9 "2024-04-01T08:15:22Z")

</div>

> [@jbudd](#):
>
> `else if (PV > 2.1 && Grid > -0.4) { result = 3 }`  
> ...  
> `else if (PV > 2.8 && Grid > -0.4) { result = 5 }`

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [1 April 2024 08:24 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/10 "2024-04-01T08:24:44Z")

</div>

@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?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [1 April 2024 08:31 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/11 "2024-04-01T08:31:00Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac135b71108ce308920aa0c6596606b7321ac4e6.png)

and then for the pv nodes

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c32a4aca3933a903c5f340dd408a7a21e27ccd9d.png)

something along those lines.

---

<div class="post-metadata">

**Author:** ![satkey3](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@satkey3](https://discourse.nodered.org/u/satkey3)\
**Post date:** [2 April 2024 11:43 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/12 "2024-04-02T11:43:01Z")

</div>

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

Thank you guys!

 ![final copy](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b24bc7f5a65e81839ceb3fed5d39e693e73978e2.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 July 2024 11:43 UTC](https://discourse.nodered.org/t/two-inputs-in-a-node-function/86891/13 "2024-07-01T11:43:10Z")

</div>

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