Combining binary entities to a single integer value

Hello There

At the momemnt, i’m not very familiar with the more advanced functions of home assistant.

My current situation is, that I have 4 binary signals from a KNX binary input.
So there are 4 entities at home assistant which can be on or off.
With these 4 entites I would like to build an integer value which represents the values from 0 to 10.

Entity 1 = Bit 1 = Int 1
Entity 2 = Bit 2 = Int 2
Entity 3 = Bit 3 = Int 4
Entity 4 = Bit 4 = Int 8
When the signal is on I would like to sum up the values or use bitwise functions.

My aim is to display the value and also use the value for automations but right now I have no idea where I have to start from.

Maybe someone can give me a hint on this.
Thanks and kind regards
Alex

Maybe with a template sensor, which checks the state of each entity and gives it a value based on the bit and state of the entity and then sum up the values.

I don’t know if it can be done in native ha automation or in templates.

But for sure it can be done in NodeRed, for example using build-in JavaScript. Possible NR offers some fancy node which could do the same, but using JS will be efficient in this case too.
from implementation point, you have to monitor change of any of those sensors. When at least one changes, collect states of all of them and map into bits of integer value
Then result could be send to a sensor also created by NR

Try this (exchange the binary_sensor.entity1 etc. with the respective entities from your system):

sensor:
  - platform: template
    sensors:
      binary_input_bit:
        friendly_name: "Binary Input Bit"
        value_template: >-
          {% set bit1 = 1 if is_state('binary_sensor.entity1', 'on') else 0 %}
          {% set bit2 = 2 if is_state('binary_sensor.entity2', 'on') else 0 %}
          {% set bit3 = 3 if is_state('binary_sensor.entity3', 'on') else 0 %}
          {% set bit4 = 4 if is_state('binary_sensor.entity4', 'on') else 0 %} 
          {{ bit1 + bit2 + bit3 + bit4 }}

This will give you a sensor called sensor.binary_input_bit with a value from 0 - 10.

@maxym
Nodered and JS is way too overkill for this.

1 Like

Thanks a lot for your answers.

@maxym
This was my first approach, and worked very well.

@Burningstone
That’s the solution i was looking for. The bit values has to be 1,2,4,8 but it’s working as expected. Thank you for that.