How to sum the quantity of binary inputs

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

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

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 :face_with_diagonal_mouth:

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

$sum($$.payload.*.$number($))
[{"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

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

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

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?

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.

Looks like you know what you are doin' :smiley: 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?

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

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.

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

[
    {
        "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
    }
]

I recommend watching this playlist: Node-RED Essentials. 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.

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