Number of open windows and icon templating

Hi

I would like to count the number of open windows. I have a group “group.skal” which contains all the sensors that I would like to count (on/off), which works the following way

  - platform: template
    sensors:
      window_count:
        friendly_name: "Open Windows"
        entity_id: 
        - binary_sensor.openclose_12
        - binary_sensor.openclose_16
        - binary_sensor.openclose_20
        - binary_sensor.openclose_3
        - binary_sensor.openclose_2
        - binary_sensor.openclose_5
        - binary_sensor.openclose_4
        - binary_sensor.stue_vindue
        - binary_sensor.openclose_6
        - binary_sensor.openclose_15
        - binary_sensor.openclose_11
        - binary_sensor.openclose_13
        - binary_sensor.openclose_14   
        value_template: >-
            {{ states | selectattr('entity_id','in',state_attr('group.skal','entity_id')) | selectattr('state','eq','on') | list | count }}
        icon_template: >
          {% if states.group.skal.state == 'off' %}
            mdi:door-open
          {% elif states.group.skal.state == 'on' %}
            mdi:door-closed
          {% else %}
            mdi:help
          {% endif %}

What doesn’t work is the icon. I would like it to be an open door or window if the group.skal is on and a closed door or window if it is off. Also it would be cool if the open part could be a yellow colored icon as the rest of Home Assistant

here is a script I use for my lights if that helps:

{% set lights = [
      states.light.kitchen_lights,
      states.light.living_room_main,
      states.light.conservatory,
      states.light.staircase ] %}
{% set lights_on = lights | selectattr('state','eq','on') | list %}
{{ lights_on | length }} of {{ lights | length }} lights are on.

Your current icon_template is the opposite of what you want. It reports mdi:door-open when the group is off.

Change it to:

        icon_template: >
          {% if states.group.skal.state == 'off' %}
            mdi:door-closed
          {% elif states.group.skal.state == 'on' %}
            mdi:door-open
          {% else %}
            mdi:help
          {% endif %}

FYI
If you are using version 0.96 or higher, you can take advantage of the expand function to streamline the value_template:

     value_template: >-
       {{ expand('group.skal') | selectattr('state','eq','on') | list | count }}

To specify an icon color, I use the Custom UI component. There’s probably a way to do it using an appropriate Lovelace card (but I don’t know how).

1 Like

mmm, nice. TIL