Sensor to list entities in certain state

Hi Team,

I am having trouble getting my head around a configuration for my window and door sensors…

  • Right now, each windows has a sensor showing open/close state.
  • I created a helper for the two windows in one room to see if a window is open
  • If I open a window a, the state gets change from closed to open and in the log it shows that window a is open
  • when I open window b now, it doesn’t state that window b was opened
  • when I close window a, state is still opened
  • if I close window b, it logs that window b was closed (but never logged that it was opened) and changes the helper state to closed

I like to achieve:

  • Group for windows
  • Logs all changes of the included windows
  • create either a new sensor or entity that reads all open windows and makes a list for me

Is this by any means achievable? :slight_smile:

Thanks a lot and warm regards
Sebastian

You could show a count of the number of currently open windows and doors. So it’s 1 when ‘a’ is open, and changes to ‘2’ if ‘b’ is opened, and then back to 1 if ‘a’ is then closed. This would give you that count:

{{ states.binary_sensor
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.device_class', 'eq', 'opening')
  | selectattr('state', 'eq', 'on')
  | list
  | count }}

And if you want a sorted list (up to 255 chars) of open devices:

{{ states.binary_sensor
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.device_class', 'eq', 'opening')
  | selectattr('state', 'eq', 'on')
  | map(attribute='attributes.friendly_name')
  | sort
  | join(', ') }}
1 Like

Thats great thanks a lot :slight_smile:

Could I take this one step further saying:

  • Create a sensor, that both counts and lists entities that have a the open state
  • Like a and b is open
  • Shows “2 open”
  • When I click on it, I get the “more information” dialogue with a list of “HPB window east” & “HPB window south”

Is that possible or to complex? :slight_smile:

Yes it’s possible and it’s only a little more complex than what @michaelblight has already provided. The following assumes you have created a helper group called binary_sensor.kitchen_window_group.

template:
  - sensor:
      - name: Open Kitchen Windows
        availability: "{{ has_value(`binary_sensor.kitchen_window_group`) }}"
        state: >
          {{  state_attr(`binary_sensor.kitchen_window_group`, 'entity_id')
          | select('is_state', 'on') | list | count }} open
        attributes:
          open_windows: >
            {{  expand(state_attr(`binary_sensor.kitchen_window_group`, 'entity_id')
            | select('is_state', 'on') | list) | map(attribute='name') | list | default("None", 1)}}
1 Like

Thats amazing - thanks a lot for the help, works like a charme :slight_smile:

One final question:

How can I overwrite the text shown like:

  • if 0 => “all closed”
  • if >0 => “1 open”

Thanks again :slight_smile:

        state: >
          {% set open_count =  state_attr('binary_sensor.kitchen_window_group', 'entity_id')
          | select('is_state', 'on') | list | count %} 
          {{ iif( open_count == 0, 'all closed', open_count~' open' ) }}
2 Likes

Amazing, thanks a lot, you are awesome :slight_smile:

Hello, how can I select only the shutter whose current position is more than 10%?

You can use the expand filter to access the whole state object of your cover entities, then select by the attribute using comparisons. The template to just list the selected entities’ IDs would be:

{{ state_attr('cover.my_cover_group', 'entity_id')
| expand | selectattr('attributes.current_position', '>', 10) | map(attribute = 'entity_id') | list }}

To use it like we did for the OP of this thread it would be:

{% set open_covers =  state_attr('cover.my_cover_group', 'entity_id')
| expand | selectattr('attributes.current_position', '>', 10) | list | count %}
{{ iif( open_covers == 0, 'all closed', open_covers~" open" ) }}
1 Like