# Check if at least two out of five booleans are true

**URL:** https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596
**Category:** General
**Created:** [11 August 2021 06:24 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596 "2021-08-11T06:24:18Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [11 August 2021 06:24 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/1 "2021-08-11T06:24:18Z")

</div>

Good morning just a quick question. How would I go about the following?  
Check if at least two out of five booleans are true. I am sure there must be a node for this.

thanx

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2021 06:28 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/2 "2021-08-11T06:28:36Z")

</div>

I guess a lot depends on how the states arrive: in one message or as 5 separate messages.

Could you elaborate on that part and what the messages (if multiple) are.

---

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [11 August 2021 06:39 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/3 "2021-08-11T06:39:31Z")

</div>

Thanks for your reply. I have a number of seperate outputs, if two of the payloads is true, the result must be true, otherwise false.

relay\_2 : msg : Object  
object  
topic: "relay\_2"  
payload: false  
bool: false

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 August 2021 07:06 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/4 "2021-08-11T07:06:07Z")

</div>

The first thing you will need to do is to combine all the payloads into one message. See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [11 August 2021 07:13 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/5 "2021-08-11T07:13:55Z")

</div>

Ok, thats not a problem, but then?

---

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [11 August 2021 07:18 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/6 "2021-08-11T07:18:46Z")

</div>

@Colin,

This is the result. So what I want now is to check that we have 2 X true and then the result should be true?

11-8-2021 09:15:36[node: 82b10c89.3acdf](https://unipit.energykonnex.energy/#)relay\_4 : msg : Object  
object  
topic: "relay\_4"  
payload: array[4]  
0: false  
1: false  
2: true  
3: true  
bool: true  
\_msgid: "ee1e011e.a532e"

---

<div class="post-metadata">

### Author: ![stefan24](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stefan24/32/23075_2.png) [@stefan24](https://discourse.nodered.org/u/stefan24)
#### Post date: [11 August 2021 07:21 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/7 "2021-08-11T07:21:39Z")

</div>

Change false to 0 and true to 1 and add all data to check whether the result is \> 1.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2021 07:28 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/8 "2021-08-11T07:28:13Z")

</div>

Just to add to the ideas:

The `JOIN` node may be of use.

But it has its own set of problems.

Unless you can guarantee that the 5 devices all send their messages at/around the same time, it can cause problems.

EG:  
device 1, 2, 3, 4 send their message and 5 doesn't. A bit later 1 sends its next message.  
That will then _activate_ the joining of 5 messages.

Anyway: good luck.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2021 07:31 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/9 "2021-08-11T07:31:59Z")

</div>

Oh, @Duncan

To follow up on what was suggested about making the message `0` and `1` rather than `false` and `true`.

You just stick a `function` node that looks at the incoming message and if it is `true` sends a `1` else it sends a `0`.

So that is 5 x `function` nodes at the input (or output of the device) to change the signals.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 August 2021 07:46 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/10 "2021-08-11T07:46:26Z")

</div>

You have not followed the example carefully enough. You should be using key/value pair mode in the Join node

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 August 2021 07:55 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/11 "2021-08-11T07:55:16Z")

</div>

Actually, since you are just interested in the count of how many true values there are then an array is may be the best way. How do you know when all the values are available? Do they all come at the same time with a gap between or is there some other way you can know when they are all there?

---

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [11 August 2021 08:00 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/12 "2021-08-11T08:00:48Z")

</div>

Hi Colin,

The values are sent every second. I have used the join node with a key, so no timing. Here is the result.  
Now i need the function to pick out the 2 X "1"  
payload: object  
relay\_1: 0  
relay\_2: 0  
relay\_3: 1  
relay\_4: 1

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2021 08:06 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/13 "2021-08-11T08:06:03Z")

</div>

So, you write some code in a `function` node **something like this**

```auto
total = payload.relay_1 + payload_relay_2 + payload.relay_3 + payload.relay_4
if total > 1
{
    send message
}

```

# **BUT**

Sorry, you said five messages. This is only showing FOUR.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 August 2021 08:09 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/14 "2021-08-11T08:09:12Z")

</div>

It will be easier to go back to the array version of the join. Set it to output a value on a timeout of 0.5 seconds (so it well send the array when there has been a gap of 0.5 seconds, so you know they are all there), then you can add up the number of true values using, in a function node  
`const total = msg.payload.reduce((accumulator, currentValue) => accumulator + currentValue)`

Then you can test total for \>=2 and take whatever action is required.

That code will work with true/false or 1/0 values.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 August 2021 08:24 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/15 "2021-08-11T08:24:01Z")

</div>

Or if you want to keep it as key/values in an object then

```auto
let total = 0
for (const [key, value] of Object.entries(msg.payload)) {
    total += value
}

```

Which adds up the values of all keys in msg.payload

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [11 August 2021 08:24 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/16 "2021-08-11T08:24:44Z")

</div>

That seems a better way.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 August 2021 08:31 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/17 "2021-08-11T08:31:11Z")

</div>

Or use JSONata and count the true values. Join the relays manually, using key value, and set the count to 5 and ever subsequent message.

```auto
[{"id":"e8b2339b.a97c6","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"relay1","payload":"true","payloadType":"bool","x":170,"y":2840,"wires":[["a6dcf54.cb62488"]]},{"id":"a6dcf54.cb62488","type":"join","z":"c74669a0.6a34f8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"5","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":420,"y":2960,"wires":[["b95420ef.cc09e8"]]},{"id":"dfc39466.b8114","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"relay2","payload":"false","payloadType":"bool","x":180,"y":2880,"wires":[["a6dcf54.cb62488"]]},{"id":"ac03c7de.383bc8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"relay3","payload":"true","payloadType":"bool","x":170,"y":2920,"wires":[["a6dcf54.cb62488"]]},{"id":"8045dd02.c4ae9","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"relay4","payload":"false","payloadType":"bool","x":180,"y":2960,"wires":[["a6dcf54.cb62488"]]},{"id":"b95420ef.cc09e8","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$count($$.payload.*[$=true]) >= 2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":2960,"wires":[["38ffe006.30b6e8"]]},{"id":"e4fbe3c3.48595","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"relay5","payload":"false","payloadType":"bool","x":180,"y":3000,"wires":[["a6dcf54.cb62488"]]},{"id":"fc513290.4edac","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"relay1","payload":"false","payloadType":"bool","x":360,"y":2840,"wires":[["a6dcf54.cb62488"]]},{"id":"38ffe006.30b6e8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":710,"y":2880,"wires":[]}]

```

```auto
$count($$.payload.*[$=true]) >= 2

```

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 August 2021 08:36 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/18 "2021-08-11T08:36:35Z")

</div>

I think it would be better to set the count in the Join node to 2 (and every subsequent message). Otherwise, if one or more channels is not working, then it will never output anything.

---

<div class="post-metadata">

### Author: ![Duncan](https://avatars.discourse-cdn.com/v4/letter/d/ecc23a/32.png) [@Duncan](https://discourse.nodered.org/u/Duncan)
#### Post date: [11 August 2021 08:41 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/19 "2021-08-11T08:41:41Z")

</div>

Wow, 3 different approaches and I get them all to work. Thanks for the great support, I am learning every day.  
Thanks!!!

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 August 2021 08:44 UTC](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596/20 "2021-08-11T08:44:40Z")

</div>

As long as the 5 initial count has happen then the subsequent will work, 2 would work to. The first count of 5 has to happen then everything will be ok. But you could possibly do it all in the join node to with a reduce sequence.

[Next page](https://discourse.nodered.org/t/check-if-at-least-two-out-of-five-booleans-are-true/49596.md?page=2)
