Binary_sensor group, add attributes to show which entities in the group are on resp. off

Would it make sense to add two attributes to a binary_sensor group that show which of the sensors in the group is on resp. off? Would come in handy in situations where you need to know which sensor(s) caused the group state to change (e.g. in an alarm system, which sensor caused the alarm to trigger).
Could either be expressed as a list or a string with comma separated names. Could also be a list of the entities (in a addition or instead).

Here’s how you could do it with a binary_sensor template instead of binary_sensor group, but well it’s yet another template that has to be written.

# create a binary_sensor group
binary_sensor:
- platform: group
  name: contact_sensors_inside
  device_class: opening
  entities:
  - binary_sensor.contact_sensor_a
  - binary_sensor.contact_sensor_b
  - binary_sensor.contact_sensor_c

# add a template binary_sensor with attributes for this group
template: 
  binary_sensor:
  - name: "Contact Sensors Inside With Attributes"
    state: "{{ states('binary_sensor.contact_sensors_inside') }}"
    device_class: "opening"
    attributes:
      sensors_on: "{{ expand('binary_sensor.contact_sensors_inside') | selectattr('state', 'eq', 'on') | map(attribute='name') | list | join(', ') }}"
      sensors_off: "{{ expand('binary_sensor.contact_sensors_inside') | selectattr('state', 'eq', 'off') | map(attribute='name') | list | join(', ') }}"

The more info card gives you this:

But that is only visual.
Perhaps an easier and more intuitive way of doing what you want is to have a template group?
That would give us the same flexibility of the group as we have with the sensor but still be able to group.
Actually a template sensor could be a group if you just coded the template for it.

A quick follow-up. I tried to create a template sensor w/o the need to also create an additional binary_sensor group. Here’s the result in case someone else has a similar need.

template:
  binary_sensor:
  - name: "Contact Sensors"
    # important to set a default, this.attributes.sensors_on may not yet be initialized when the state is evaluated
    state: "{{ (this.attributes.sensors_on | default([])) != [] }}" 
    attributes:
      entity_id: >
        {% set sensors = [ 
          "binary_sensor.contact_a_state",
          "binary_sensor.contact_b_state",
          "binary_sensor.contact_c_state"
        ] %}
        {{ sensors }}
        sensors_on: "{{ expand(this.attributes.entity_id | default([])) | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}"
        sensors_off: "{{ expand(this.attributes.entity_id | default([])) | selectattr('state', 'eq', 'off') | map(attribute='entity_id') | list }}"
    device_class: "opening"

NOTE: I did struggle a bit to find the right means to evaluate the state using this self-referencing. This way it seems to work. Also, I did not easily find out how to create an attribute of type list. I found an old post from @petro that lead me to this solution. Is there a better way to do this ? I would have liked to be able to used an anchored list as I need the same list multiple times in my configuration, I have not found a way to express that.

2 Likes