Sensor to count open windows

I’m trying to setup a sensor to show the count of my open windows. I’ve setup a group with all my window binary sensors and am trying to count the ones that are “on” with the following sensor. I put this together after going through several iterations of examples on the forum but none are working for me. This current example will work with a count of up to “1” but even with more than one open it still just shows “1”.

Any help would be greatly appreciated as these templates are rather confusing to me.

window_count:
  value_template: >-
    {{ states | selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id')) | selectattr('state', 'eq', 'on') | list | count | int }}
1 Like

Add the entity_id field with all the entities from the group.

window_count:
  entity_id:
  - sensor.1
  - sensor.2
  - etc
  value_template: >-
    {{ states | selectattr('entity_id', 'in', state_attr('group.windows', 'entity_id')) | selectattr('state', 'eq', 'on') | list | count | int }}

This will cause your template to update when any sensor in the group changes. Otherwise it will only update when the GROUP state changes (which will be rare).

1 Like

This did the trick! I knew it was something obvious that I was missing.

It is now starting at 1 even when no sensors are on. I can always decrement by one but curious if you know why that would be.

lets try simplifying it first

    {{ expand('group.windows') | selectattr('state', 'eq', 'on') | list | count }}
4 Likes

FYI

There’s a useful enhancement in 0.110 that changes the way the expand function is handled in Template Sensors. Instead of monitoring the state of the group (the way it’s been done in all prior releases), it now monitors the states of the group’s members.

It means that in 0.110, this Template Sensor will update whenever one of the members of group.windows changes state.

  - platform: template
    sensors:
      open_windows:
        value_template: >
          {{ expand('group.windows')
             | selectattr('state', 'eq', 'on')
             | list | count }}

HOWEVER, I tried it and it didn’t work. I reported it as an Issue in the Core repo.

1 Like

That is much cleaner. It does still starts at “1” though

Put this into the template editor and you’ll see what entity is causing the problems

    {{ expand('group.windows') | selectattr('state', 'eq', 'on') | list }}
1 Like

Figured it out. I actually had a window sensor that had the magnet fall off while I was changing this so one of them was on. It’s working as expected. Thanks for all your help!

1 Like

Hi Petro

There is the possibility instead of the number to show the name of the opened window?

Thank You

I’m using the following template in order to get notified over TTS when I’m leaving:

{% set windowEntities = state_attr('group.windows', 'entity_id') %}   
{% set openWindows = states.binary_sensor 
  | selectattr('entity_id', 'in', windowEntities)
  | selectattr('state','eq','on') 
  | map(attribute='name')
  | list %}

{% if openWindows | length == 0 %}
  {% set windowMessage = '' %}
{% elif openWindows | length == 1 %}
  {% set windowMessage = 'The following window is open: ' + openWindows[0] + '.' %}
{% else %}
  {% set windowMessage = 'The following windows are open: ' + openWindows | join(', ')  + '.' %}
{% endif %}

{{ windowMessage }}
1 Like

If you are interested, here is a streamlined version of your template:

{% set openWindows = expand('group.windows')
  | selectattr('state','eq','on') 
  | map(attribute='name')
  | list %}
{% set qty = openWindows | length %}  
{% set p1 = ' is ' if qty == 1 else 's are ' %}
{{ '' if qty == 0 else 'The following window{}open: {}.'.format(p1, openWindows | join(', ')) }}
2 Likes

Thank you guys!!

What meens this error:

remove the entity_id field and the list of entities. entity_id is no longer valid.

1 Like

Thank you

now this:

what does the error message say?

Sadly I can not tell you that. The ad works in lovelace. Only comes this message

So you can’t tell me what this error message says yet you can read my replies:

image

Got it, sorry. I removed it. Still have to learn a lot. I am a Beginner

1 Like

Continue your journey by learning to use the „code“ button (</>) of this editor when posting configuration files or log outputs.

Bitte.

1 Like