Helper to Count True Binary Entities

Is there a simple helper to count the number of true (or on) binary entities? I’m looking for something similar to groups or counter, but can count the true values of various entities. Something similar to the way zones counts the number of people home.

For example:

  • Number of household reminders
  • Number of lights on in the house
  • Number of windows open in the house.
  • Number of trashcans full
  • Number of speakers playing music

With the template editor I’m looking for something more simple than an elongated if/else structure which becomes more cumbersome with increased entities. For example in an TTS greeting announcement, one could write:

    {% if is_state("input_boolean.dustbin_full", "on") and is_state("input_boolean.plants_need_water", "on") and is_state("input_boolean.washing_machine_full", "on") -%}
     You have 3 reminders.
    {% elif is_state("input_boolean.dustbin_full", "on") and is_state("input_boolean.plants_need_water", "on") and is_state("input_boolean.washing_machine_full", "off") -%}
     You have 2 reminders.
    {% elif is_state("input_boolean.dustbin_full", "on") and is_state("input_boolean.plants_need_water", "off") and is_state("input_boolean.washing_machine_full", "on") -%}
     You have 2 reminders.
    {% elif is_state("input_boolean.dustbin_full", "off") and is_state("input_boolean.plants_need_water", "on") and is_state("input_boolean.washing_machine_full", "on") -%}
     You have 2 reminders.
    {% elif is_state("input_boolean.dustbin_full", "on") and is_state("input_boolean.plants_need_water", "off") and is_state("input_boolean.washing_machine_full", "off") -%}
     You have 1 reminder.
    {% elif is_state("input_boolean.dustbin_full", "off") and is_state("input_boolean.plants_need_water", "on") and is_state("input_boolean.washing_machine_full", "off") -%}
     You have 1 reminder.
    {% elif is_state("input_boolean", "off") and is_state("input_boolean.plants_need_water", "off") and is_state("input_boolean.washing_machine_full", "on") -%}
     You have 1 reminder.
    {% else %}
     You have no reminders.
    {% endif %}

It would seem much more simple to write:

You have {{states('binary_counter.reminders')|float }} reminder(s).

try this to count them all:
{{ states.binary_sensor |list |count }}

and this to count those that are ‘on’
{{ states.binary_sensor |selectattr('state','equalto','on') |list |count }}


Edit: just realised you aren’t referring to binary_sensor - sorry. So try this instead:
{{ states.input_boolean |list |count }}
and
{{ states.input_boolean |selectattr('state','equalto','on') |list }}

1 Like
{% set qty = expand('input_boolean.dustbin_full', 'input_boolean.plants_need_water', 'input_boolean.washing_machine_full')
  | selectattr('state', 'eq', 'on') | list | count %}
 You have {{ iif(qty > 0, qty, 'no') }} reminder(s).
2 Likes

:thinking:

  - sensor:
    - name: Encje
      icon: mdi:counter
      availability: "{{states != unknown}}"
      state: "{{states | count}}"
      attributes:
        automation: "{{states['automation'] | count}}"
        binary_sensor: "{{states['binary_sensor'] | count}}"
        button: "{{states['button'] | count}}"
        device_tracker: "{{states['device_tracker'] | count}}"
        group: "{{states['group'] | count}}"
        input_boolean: "{{states['input_boolean'] | count}}"
        input_button: "{{states['input_button'] | count}}"
        input_datetime: "{{states['input_datetime'] | count}}"
        input_select: "{{states['input_select'] | count}}"
        input_text: "{{states['input_text'] | count}}"
        light: "{{states['light'] | count}}"
        media_player: "{{states['media_player'] | count}}"
        person: "{{states['person'] | count}}"
        remote: "{{states['remote'] | count}}"
        script: "{{states['script'] | count}}"
        select: "{{states['select'] | count}}"
        sensor: "{{states['sensor'] | count}}"
        sun: "{{states['sun'] | count}}"
        switch: "{{states['switch'] | count}}"
        update: "{{states['update'] | count}}"
        zone: "{{states['zone'] | count}}"

2 Likes

very clever!

I would never have thought of putting all the counts as attributes in a single sensor like that :+1:

Thanks for your solution! I finally got my reminder count set up. Took me a while to figure out (qty1)|int was necessary to do math.

    {% set qty1 = expand('binary_sensor.windows_should_be_open', 'input_boolean.dustbin_full', 'input_boolean.dryer_full', 'input_boolean.washing_machine_full')
  | selectattr('state', 'eq', 'on') | list | count %}
    {% set qty2 = iif(as_timestamp( now() - timedelta(days = 4)) > state_attr('input_datetime.last_watered_plants', 'timestamp'), '1', '0') %}
    {% set qty3 = iif(as_timestamp( now() - timedelta(days = 1)) > state_attr('input_datetime.last_fed_dog', 'timestamp'), '1', '0') %}
    {% set qty4 = iif(is_state("binary_sensor.windows_should_be_open", "off") and states('sensor.openweathermap_forecast_temperature')|float > 59, '1', '0') %}
    {% set qty5 = (qty1)|int + (qty2)|int + (qty3)|int + (qty4)|int %}
      I have {{ iif(qty5 > 0, qty5, 'no') }} {{ iif(qty5 == 1, 'reminder', 'reminders')}} for you.

If you’re interested, you can reduce the template to this:

    {% set qty1 = expand('binary_sensor.windows_should_be_open', 'input_boolean.dustbin_full', 'input_boolean.dryer_full', 'input_boolean.washing_machine_full')
     | selectattr('state', 'eq', 'on') | list | count %}
    {% set qty2 = as_timestamp(now() - timedelta(days = 4)) > state_attr('input_datetime.last_watered_plants', 'timestamp') %}
    {% set qty3 = as_timestamp(now() - timedelta(days = 1)) > state_attr('input_datetime.last_fed_dog', 'timestamp') %}
    {% set qty4 = is_state('binary_sensor.windows_should_be_open', 'off') and states('sensor.openweathermap_forecast_temperature') | float(0) > 59 %}
    {% set qty5 = qty1 + [qty2, qty3, qty4] | map('abs') | sum %}
    I have {{ iif(qty5 > 0, qty5, 'no') }} reminder{{ iif(qty5 == 1, '', 's')}} for you.
1 Like

Thank you!