Template "All windows" closed

Hey,

For each window I create sensor template which show the state open/tilt/closed. This works fine.
Now, I´d like to create an similar template which shows that all windows closed, if they are all closed.

template.yaml

sensor:

  - name: "Alle Fenster"
    state: >
      {% if is_state('binary_sensor.fenster_eltern_offen', 'on') and
      is_state('binary_sensor.fenster_ankleide_offen', 'on') and
      is_state('binary_sensor.fenster_bad_og_offen', 'on') %}
      geschlossen
      {% endif %}

    icon: >
      {% if is_state('binary_sensor.fenster_eltern_offen', 'on') and
      is_state('binary_sensor.fenster_ankleide_offen', 'on') and
      is_state('binary_sensor.fenster_bad_og_offen', 'on') %}
      mdi:window-closed-variant
      {% endif %}

with the above code there is no content in the entity, see below.
fenster

Thanks for your help.

Maybe creating a groups is less of a hassle?

1 Like

I´ve already done that. but that just show on/off and not “geschlossen”.
Or is possible to remane the group state, as well?

If the type of the sensor is set right and your locale is set right, you should get translated texts for all entities (Dutch in my case). Don’t you? Creating entities for everything ou wat translated is not maintainable in the long run.

don´t get that, sorry.

name of the binary_sensor are correct.
state: “on” or “1” means, window is closed, “off” or “0” means, window is closed.

If all are state “1” → all windows are closed.

edit: and I would like to show the mdi:window-closed-variant

Greate a group helper in the helpers section of the settings and add the window or door sensors. In the configuration options, tell if it is a door or a window to get the right icons and translations. The door or window group should show translations for open and closed, even though in developer options, the real values of the sensor are on / off. 0 and 1 are not values that should be used for door/windows sensors., even kf they are called binary_sensors.

If all your window sensors have the correct device_class, you can do it with a simpler template for state and icon.

Something like this:

{{ states.binary_sensor 
  | selectattr('attributes.device_class', 'defined') 
  | selectattr('attributes.device_class', 'search', 'window') 
  | selectattr('state', 'eq', 'on') 
  | map(attribute="name")
  | list }}

What you do here, is taking all binary_sensors, select if they have device_class definied, if so, take only the device_class “window”, where the state is ‘on’ and display the names as a list.

You can as well use it a little more “displayable”, with something like this:

{%- for entity in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', 'search', 'window') | selectattr('state', 'eq', 'on') | list -%}
  {{ entity.attributes.friendly_name }}{% if not(loop.last) %}, {% endif %}
{% endfor %}

No need for a group, just make sure, your actual window sensors have the correct device class, and future additions do to.

thanks.

Something like this:

{{ states.binary_sensor 
  | selectattr('attributes.device_class', 'defined') 
  | selectattr('attributes.device_class', 'search', 'window') 
  | selectattr('state', 'eq', 'on') 
  | map(attribute="name")
  | list }}

if I understand correct the code it search and displays the open window?! Isn´t it?
I just want to show in dash, “all windows closed”. thats it :slight_smile:

The problem with groups is, it´s activated if one of the members state “on”. the binary window sensors are state “on” when windos is closed…
I need to invert the sensor. May I need to configure the group I a yaml?

There is an all option in the docs.

Got it. thanks for the help.

Group works well. I had an issue with an sensor. Now it´s fixed.

Oh, you want it the other way round, sorry, didn’t pick up on that. :laughing:

Than a group is perfect, as you already found the all: true is your friend. :wink:

1 Like

Hey Patrick, but I use your code anyway, to send me a notification on my mobile which window is open. :wink:

edit: But can you help me with the follwing code:

I´d like to get a notificaion
front door open → notification “door open”
front door closed → notification “door closed”

(same I´d use for few garage doors. Maybe it´s possible just with one code? front door and garage doors? )

1 Like

Hey,
Just trying to notify the name and the state of the my doors, like mentioned above:

the binary sensors are:

  - name: "Garagetor rechts"
    state_address: "5/2/71"
    device_class: garage_door

  - name: "Garagentor links"
    state_address: "5/2/72"
    device_class: garage_door

my autmation code:
trigger

entity_id:
  - binary_sensor.alle_turen_toren
from: "on"
to: "off"
enabled: true
entity_id:
  - binary_sensor.alle_turen_toren
from: "off"
to: "on"
enabled: true

then

service: notify.mobile_app_pixel_7
data:
  message: " {{ expand('binary_sensor.alle_turen_toren') 
                      | selectattr('state', 'eq', 'on') 
                      | map(attribute='name') 
                      | map(attribute='state') 
                      | list | join (', \n ') }} "

and then

service: notify.mobile_app_pixel_7
data:
  message: " {{ expand('binary_sensor.alle_turen_toren') 
                      | selectattr('state', 'eq', 'off') 
                      | map(attribute='name') 
                      | map(attribute='state')  
                      | list | join (', \n ') }} "

It doesn´t work.

  message: >
    {{ expand('binary_sensor.alle_turen_toren') 
                      | selectattr('state', 'eq', 'off') 
                      | map(attribute='name') 
                      | list | join (', \n ') }}

Thanks Petro,
but with your code it just send me the name.
Im my case, if I open the garage I receive the notification:
“Garagentor links” (only the name of the entity)

I would like to receive the state as well.
“Gargentor links offen”
Garagentor links geschlossen"

Just add it. :slight_smile:

message: >
    {{ expand('binary_sensor.alle_turen_toren') 
                      | selectattr('state', 'eq', 'off') 
                      | map(attribute='name') 
                      | list | join (', \n ') }} geschlossen! 

You select upfront that only “closed” are shown. :slight_smile:

Just add the state manually, as paddy said, it’s only searching for off so the list will only contain the ones that are off.