Guidance with Groups of Binary Sensors

Currently, that is not possible.

On startup, Home Assistant inspects the Template Sensor’s template and attempts to identify it’s entities. For each identifiable entity it creates a “listener”. The listener is responsible for detecting the entity’s state changes. If it cannot find any entities, no listeners are created. The result is the Template Sensor’s template is evaluated once on startup and never again (until the next restart). The only way to ensure Home Assistant can create listeners is to include the desired entities within the template, or separately in the entity_id option (which supercedes the template).

The automation you linked to works with events, not entity states, and is not applicable to Template Sensors.

1 Like

Ok, thanks for point out! :+1:

you could create an automatic group with the conditions you like, and then listen to the expanded group. I do something like that with my batteries

script:
  group_battery_sensors_low_create:
    alias: Group battery sensors low create
    sequence:
      service: group.set
      data_template:
        object_id: battery_sensors_low
        entities: >-
          {%- for s in states.sensor
            if ('battery_level' in s.entity_id or
                'motion_sensor_battery' in s.entity_id)
             and s.state|int < states('input_number.battery_alert_level')|int %}
            {{s.entity_id}}{% if not loop.last %}, {% endif %}
          {%- endfor %}

call this script using an automation which can be triggered at startup and other triggers.

or use template sensors like this;

      low_level_batteries:
        friendly_name: Low level batteries
        value_template: >
          {%- set ns = namespace(below=[]) %}
          {%- for s in states.sensor
            if ('battery_level' in s.entity_id or 'motion_sensor_battery' in s.entity_id)
                and s.state|int < states('input_number.battery_alert_level')|int
                and s.state != 'unknown' %}
              {%- set ns.below = ns.below + [s] %}
          {%- endfor %}
            {{ns.below|count}}
        attribute_templates:
          List: >
            {%- set ns = namespace(below=[]) %}
            {%- for s in states.sensor
             if ('battery_level' in s.entity_id or 'motion_sensor_battery' in s.entity_id)
                  and s.state|int < states('input_number.battery_alert_level')|int
                  and s.state != 'unknown' %}
                {%- set ns.below = ns.below + [s.name + ': (' + s.state + '%)'] %}
            {%- endfor %}

            {% set count_below = ns.below|count|int %}
            {% set list = ns.below %}
            {%- if count_below == 0 %} All batteries are fine
            {%- elif count_below == 1 %}
              {{list[0]}} is low!
            {%- elif count_below == 2 %}
              {{list|join(' and ') }} are low!
            {%- else %}
              {{list[:-1]|join(', ')}}, and {{list[-1]}} are low!
            {%- endif %}

binary_sensor:
  - platform: template
    sensors:
      battery_alert:
        friendly_name: Battery alert
        device_class: safety
        value_template: >
          {{states('sensor.low_level_batteries')|int > 0}}
        attribute_templates:
          Count: >
           {{states('sensor.low_level_batteries')|int }}
          List: >
           {{state_attr('sensor.low_level_batteries','List')}}
1 Like

There is work underway to improve Home Assistant’s ability to identify entities in a template. In some future release, listeners for each binary_sensor will be created for the following:

states.binary_sensor

expand (‘group.my_binary_sensors’)

1 Like

Wow, thanks. I need to sit down for this one and try this out.

That sounds great! Thanks for the heads up.

What are the identifiable entities in sensor.low_level_batteries?

The reason why I ask is because I tried to use this in a Template Sensor in 0.114 (as an experiment) and it was reported in the log to have no identifiable entities.

{{ states.light | selectattr('state', 'eq', 'on') | list | count }}

In other words, states.light was not assigned any listeners so, by extension, nor would any for states.sensor.

if I do that, is counts the entities nicely in my setup, currently on 114.1


as does

replacing the last line of the value_template to use this:

{{ns.below|map(attribute = 'entity_id')|join(',')}}

shows that even better: (sorry but it wont fit on a single screenshot…)

It has always worked in the Template Editor and only because you are refreshing the template manually.

It won’t work in a Template Sensor because no listeners will be created for states.light. This ability is currently being developed but doesn’t yet exist in any release version.


      # This one doesn't listen to anything and is flagged in the log
      v114_states:
        friendly_name: 114 States
        value_template: >
          {{ states.light | selectattr('state','eq', 'on') | list | length }}

Sorry, I misunderstood your question, thought the template itselfdidnt work in your setup, even in the template editor.

You’re right, that’s why I also use some automations in this setting:

  - alias: Update sensor battery alert
    trigger:
      platform: time_pattern
      minutes: '/15'
      seconds: 00
    action:
      - service: homeassistant.update_entity
        entity_id:
          - sensor.low_level_batteries
          - sensor.count_low_level_batteries
          - sensor.low_level_batteries_group
      - service: automation.trigger
        entity_id: automation.create_battery_low_group

  - alias: Update battery low group
    trigger:
      platform: state
      entity_id: input_number.battery_alert_level
    action:
      service: automation.trigger
      entity_id: automation.create_battery_low_group

  - alias:  Alert when batteries are below alert level
    trigger:
      platform: state
      entity_id: binary_sensor.battery_alert
      to: 'on'
    action:
      service: notify.system
      data_template:
        title: Battery alert
        message: >
          {{state_attr('binary_sensor.battery_alert','List')}}

depending on the needs of the poster, he might be able to use the group’s all: functionality to his advantage, and automate based on the state of that group, instead of all individual entities.

If these need to be available to use {{trigger.to_state.state}} for now there’s no other option than to list them in the trigger: platform: state as individual entities.

I’d hope using that group as a triggering entity would someday have the option to trigger on all containing entities in that group.

Originally (back in version 0.110) the new functionality was simply to assign listeners to all members of a group when used with expand. So if the following template were used in a Template Sensor, Home Assistant would listen to all entities within the group (and not only the group itself):

{{ expand('group.my_sensors') | etc }}

Unfortunately, it didn’t work. Changes were made in 0.114 but those also failed to make it work.

There’s a completely separate initiative underway to fundamentally change the way Home Assistant assigns listeners to a Template Sensor. It’s goal is not only to handle expand correctly, it will also support states.light, states.sensor, etc. In theory, it should be able to handle something tricky like this:

{{ expand(states.binary_sensor, states.sensor) | etc }}