Counting input_booleans which are ON

Hi

For learning/testing I created few ‘input_boolean’ helpers.
Assigned them to an area called ‘parter’.
I can create list of them using: area_entities(‘parter’)
I wanted to count those which are ‘on’ and came up with the following, not very elegant, ‘for’ loop:

{% set ns = namespace(count=0) %}
{% for temp in area_entities('parter') if is_state(temp, 'on') %}
  {% set ns.count = ns.count + 1 %}
{% endfor %}
Total: {{ ns.count }}

I was hoping to find a magic filter for a pipeline like this:

 {{area_entities('parter') | selectattr('state', 'eq', 'on') | list | count }}

(which doesn't work) and unfortunately couldn't find any.
Is there a nicer solution to count them?

Thanks
Chris

This should do it:

{{ area_entities('parter') | select('match', 'input_boolean') | select('is_state', 'on') | list | count }}
1 Like

I went few times through the “List of Builtin Filters” but missed the magic one, being next to “selectattr”, argh… :slight_smile:
Thanks a lot again!
Regards,
Chris