# How to sum the quantity of binary inputs

**URL:** <https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [21 May 2023 11:10 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573 "2023-05-21T11:10:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Momo1](https://discourse.nodered.org/u/Momo1)\
**Post date:** [21 May 2023 11:10 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/1 "2023-05-21T11:10:48Z")

</div>

Hi there,

I would like to analyze, how many of the inputs are true and generate the quantity of the true ones.

Example:

- Binary A
- Binary B
- Binary C

If A and B have the Status "True", the Status should be "2.  
If only C is "true", the status should be "1".

How can I realize it?

Working with Homeassistant-entities if this makes a effect.

Sorry, I'm absolutely new to NodeRed.

Thank you!

Cheers

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 May 2023 11:12 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/2 "2023-05-21T11:12:06Z")

</div>

If you share a typical input message, we will be able to better help.

---

<div class="post-metadata">

**Author:** ![Momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Momo1](https://discourse.nodered.org/u/Momo1)\
**Post date:** [21 May 2023 11:18 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/3 "2023-05-21T11:18:19Z")

</div>

As I'm working with HomeAssistand, I have the entities of HomeAssistant which came from KNX.

So on KNX the datatype is just 1/0 or true/false.  
The KNX-Addon of Homeassistant translates it to the entity and I would like to analyze, how many entities are true.

In this case I would like to count the number of open windows.

And Iam absolutely newbie - so I have absolutely no Idea where to start 🫤

---

<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:** [21 May 2023 11:27 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/4 "2023-05-21T11:27:55Z")

</div>

If you join your values into an object, you can then use Javascript or JSONata to sum the values  
JSONata Example.

```auto
$sum($$.payload.*.$number($))

```

```auto
[{"id":"519ff94a2e8374aa","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"a","payload":"true","payloadType":"bool","x":110,"y":3140,"wires":[["e337904c45b25764"]]},{"id":"e337904c45b25764","type":"join","z":"b9860b4b9de8c8da","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":250,"y":3180,"wires":[["60cab298a57210e3"]]},{"id":"96935e53f65c9842","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"b","payload":"false","payloadType":"bool","x":110,"y":3180,"wires":[["e337904c45b25764"]]},{"id":"e72de01a45294348","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"c","payload":"true","payloadType":"bool","x":110,"y":3220,"wires":[["e337904c45b25764"]]},{"id":"60cab298a57210e3","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum($$.payload.*.$number($))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":3180,"wires":[["c896bb713eec327c"]]},{"id":"c896bb713eec327c","type":"debug","z":"b9860b4b9de8c8da","name":"debug 297","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":470,"y":3080,"wires":[]}]

```

[edit] if the incoming messages are objects you can still join all objects and output the sum with

```auto
$sum($$.payload.*.$number($.status))

```

where `status` is the name of object property holding the boolean.

---

<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:** [21 May 2023 11:34 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/5 "2023-05-21T11:34:11Z")

</div>

It is not clear what datatype you are working with  
.  
You name the inputs "Binary A" to "Binary C" but you say that Binary A has "Status", suggesting an object; the value of Status is a string "True".

Can you, as already requested, show us an actual example of your data to clarify this?

---

<div class="post-metadata">

**Author:** ![Momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Momo1](https://discourse.nodered.org/u/Momo1)\
**Post date:** [21 May 2023 11:43 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/6 "2023-05-21T11:43:44Z")

</div>

To be honest, I don't really know, how to find it out.

In HomeAssistant it looks like this (in the list of entities):  
"binary\_sensor.eg\_bad\_wc\_fensterkontakt" state "off"

I'm using the node-red add-on for homeassistant with "node-red-contrib-home-assistant-websocket", which should make it able to read out the entities of HomeAssistant.

So my way would be:  
"off" = 0  
"on" = 1  
-\> for every entity and sum all the entities will result in the number of open windows

But I have no idea, how to realize it.

---

<div class="post-metadata">

**Author:** ![Momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Momo1](https://discourse.nodered.org/u/Momo1)\
**Post date:** [21 May 2023 11:46 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/7 "2023-05-21T11:46:13Z")

</div>

Looks like you know what you are doin' 😃 Can you please precise what I would have to do in a little example with my entity "binary\_sensor.eg\_bad\_wc\_fensterkontakt" ?  
How would the flow look like?

---

<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:** [21 May 2023 11:49 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/8 "2023-05-21T11:49:00Z")

</div>

No i can not as i do not use HA or their nodes, but if you supply an export of your flow i may be able to manually edit it to show you.  
[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)  
You can capture the outputs of your HA nodes using the debug panel, then add those to inject nodes along side the HA nodes, so we know what your Nodes output, use the copy value icon/button, it appears when you hover mouse over property name.

P.s. Most people replying to you know what they are doing to.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 May 2023 12:04 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/9 "2023-05-21T12:04:01Z")

</div>

> [@Momo1](#):
>
> As I'm working with HomeAssistand, I have the entities of HomeAssistant which came from KNX.

You need to share some actual data, most of us don't use Home Assistant or KNX.

Put a debug node on the input and share the msg output.

---

<div class="post-metadata">

**Author:** ![Momo1](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Momo1](https://discourse.nodered.org/u/Momo1)\
**Post date:** [21 May 2023 12:26 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/10 "2023-05-21T12:26:42Z")

</div>

Ok, I got it with this but maybe it could be shorter

```auto
[
    {
        "id": "99709123402ec96f",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "5bdabc111aa09c67",
        "type": "server-state-changed",
        "z": "99709123402ec96f",
        "name": "Essen 2",
        "server": "60b53dbd.05c354",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "binary_sensor.eg_wohnen_mitte_fensterkontakt",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "outputs": 1,
        "output_only_on_state_change": true,
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "eventData"
            },
            {
                "property": "topic",
                "propertyType": "msg",
                "value": "",
                "valueType": "triggerId"
            }
        ],
        "x": 130,
        "y": 360,
        "wires": [
            [
                "1dce25f77035aec9"
            ]
        ]
    },
    {
        "id": "1dce25f77035aec9",
        "type": "change",
        "z": "99709123402ec96f",
        "name": "on = 1, off = 0",
        "rules": [
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "on",
                "fromt": "str",
                "to": "1",
                "tot": "num"
            },
            {
                "t": "change",
                "p": "payload",
                "pt": "msg",
                "from": "off",
                "fromt": "str",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 360,
        "y": 360,
        "wires": [
            [
                "d9c6f5d0dd395d47"
            ]
        ]
    },
    {
        "id": "d9c6f5d0dd395d47",
        "type": "debug",
        "z": "99709123402ec96f",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 560,
        "y": 360,
        "wires": []
    },
    {
        "id": "60b53dbd.05c354",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    }
]

```

---

<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:** [21 May 2023 13:10 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/11 "2023-05-21T13:10:26Z")

</div>

> [@Momo1](#):
>
> I'm absolutely new to NodeRed.

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<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:** [4 June 2023 13:10 UTC](https://discourse.nodered.org/t/how-to-sum-the-quantity-of-binary-inputs/78573/12 "2023-06-04T13:10:45Z")

</div>

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