# Linking two or more conditions in a function node - syntax?

**URL:** https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997
**Category:** General
**Created:** [13 October 2023 10:19 UTC](https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997 "2023-10-13T10:19:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pinie\_pinie](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pinie\_pinie](https://discourse.nodered.org/u/pinie_pinie)
#### Post date: [13 October 2023 10:19 UTC](https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997/1 "2023-10-13T10:19:26Z")

</div>

Hello,

I need the syntax for a function node to compare at least 2 conditions at the input and then generate a result accordingly.  
e.g. At the input the values 20 and 40 come from 2 nodes.  
Now the connection: If payload 1 = 20 and payload 2 \< 40 then  
output "Error".  
or:  
If payload 1 \>20 and payload 2 \<40 then  
output "Correct", etc.

Since I don't have any knowledge, I can't understand the required syntax in a function node.

Can anyone help?

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [13 October 2023 10:29 UTC](https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997/2 "2023-10-13T10:29:34Z")

</div>

Hi @pinie_pinie

There are a few ways to accomplish this.

One that springs to my mind is using the `join` node.  
you can set it to output a key:value pair after n payloads have been received.

The key will be the `topic` of each incoming payload.

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

```auto
[{"id":"d18ef770d90dd6f9","type":"inject","z":"5f4f6ef5a6e88063","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"M1","payload":"","payloadType":"date","x":2110,"y":215,"wires":[["11566532fd5b14da"]]},{"id":"fc46771fd736960d","type":"inject","z":"5f4f6ef5a6e88063","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"M2","payload":"","payloadType":"date","x":2115,"y":255,"wires":[["11566532fd5b14da"]]},{"id":"11566532fd5b14da","type":"join","z":"5f4f6ef5a6e88063","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":2325,"y":240,"wires":[["56aecb562d938a0e"]]},{"id":"56aecb562d938a0e","type":"debug","z":"5f4f6ef5a6e88063","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2535,"y":240,"wires":[]}]

```

In a `function` node after the `join` you can then do.

```auto
if(payload.M1 === 20 && payload.M2 < 40){
  // do stuff
}

```

---

<div class="post-metadata">

### Author: ![pinie\_pinie](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pinie\_pinie](https://discourse.nodered.org/u/pinie_pinie)
#### Post date: [13 October 2023 11:00 UTC](https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997/3 "2023-10-13T11:00:18Z")

</div>

Hello,

Thanks, I understood the first steps but how do you get the desired result?  
[flows(1).json](https://discourse.nodered.org/uploads/short-url/dNyC9yeQFSB5XmoRN4XN9YPt5KI.json) (1.4 KB)

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [13 October 2023 11:35 UTC](https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997/4 "2023-10-13T11:35:31Z")

</div>

Hi @pinie_pinie

Sorry - I missed `msg` in my example.

```auto
if(msg.payload.M1 === 20 && msg.payload.M2 < 40){
   msg.payload = "Error"
} else {
   msg.payload = "OK"
}
return msg;

```

`msg` is the entry point into the message flowing through everything

---

<div class="post-metadata">

### Author: ![pinie\_pinie](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@pinie\_pinie](https://discourse.nodered.org/u/pinie_pinie)
#### Post date: [13 October 2023 12:58 UTC](https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997/5 "2023-10-13T12:58:55Z")

</div>

Hello,

that's exactly how I wanted it. I tested it with three conditions and it worked. The “Join” node is the crucial point.  
Many thanks for the quick response.

---

<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: [12 December 2023 12:59 UTC](https://discourse.nodered.org/t/linking-two-or-more-conditions-in-a-function-node-syntax/81997/6 "2023-12-12T12:59:09Z")

</div>

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