Setting up a door and window group

Dear all,

I’m trying to set up an entity that summarizes the state of all my door and window entities. I put them all in a group entity.

This basically works fine, but there is one issue: The critical information is whether all doors and windows are closed. Now if a device is unknown or unavailable, the group entity still reports “closed” if all others are closed. So this might not be true. But this is the intended behavior according to the documentation for groups of binary sensors.

What is the most elegant way to ensure that really all group elements reported “closed” and otherwise the error state becomes obvious?

Thanks,
Tobias

1 Like

One option would be to use a template like:

{% set obj_list = states.binary_sensor 
| selectattr('attributes.device_class', 'defined') 
| selectattr('attributes.device_class', 'in', ['door','window'])
| list %}
{% if obj_list | rejectattr('state', 'in', ['on','off']) | list %}
  ERROR
{% else %}
  {{ 'on' if obj_list|selectattr('state', 'eq', 'on') | list else 'off' }}
{% endif %}

Since you already have them in a group you could use:

{% set ents = state_attr('binary_sensor.all_windows_and_doors', 'entity_id')%}
{% if ents | reject('has_value') | list %}
  ERROR
{% else %}
  {{ 'on' if ents | select('is_state', 'on') | list else 'off' }}
{% endif %}

Thanks a lot.
As I’m still not so familiar with yaml: How would these look like as part of configuration.yaml? It would start with template: and then…?

You should be able to use either of those templates in the Template Helper Settings… no need to add anything directly in the configuration.

I did this


but this first lead to an exclamation mark at the helper entry. I opened again, saved again, but now it always reports “closed” even if a group member is open.

Is ERROR maybe just a placeholder I should fill? I would simply expect that the helper goes to “unknown” state or “unavailable” in case one member is not working properly.

Sorry in case I’m not understanding the fundamentals, but templating is beyond my experiences…

No. Though you can change it to whatever makes sense to you, it is meant to be a state specific to issues with upstream sensors. That is what you asked for in your original post. This requires you to switch to a Template sensor helper, not a binary sensor, because you have more than 2 possible output values.

If you want a binary sensor and to use unavailable for the purpose of showing upstream errors; you can do that by moving to a YAML-configured Template binary sensor. Template Helpers do not allow defining an availability, but YAML configuration does.

In general, it is not advisable to use “unknown” and “unavailable” as user-configured states. Those value already have specific meanings within HA. If you use them for other purposes, it can complicate debugging if a problem arises.

Okay, thanks. So I should bei more precise:

I am looking for a piece of code that works similar as a group of door and window elements (which I already created).

The code should create a binary door sensor with the following behavior:

If at least one element is in an error state (such as unknown or unavailable), the sensor should also have an error state, ideally the same one.

If at least one door or window (in the group) reports “open”, the sensor should be “open”.

Otherwise (when all group members are confirming to be closed), the sensor should be “closed”.

In contrast to a simple group, this will reliably report the closure of all its members!

Thanks again for your help!

Best wishes,
Tobias

That would be the previously provided template, but it needs to be setup as a sensor not a binary sensor:

{% set ents = state_attr('binary_sensor.turen_und_fenster_gruppe', 'entity_id')%}
{% if ents | reject('has_value') | list %}
  Error
{% else %}
  {{ 'Open' if ents | select('is_state', 'on') | list else 'Closed' }}
{% endif %}

OK, thanks. Can I somehow enforce that it copies the dynamic icon behavior of the binary door sensor?

Just like the binary sensor group does?

Not using the UI Template Helper editor.

Configuring in YAML does allow templating icons.

Would you have a solution for a YAML code at hand?

template:
  - sensor:
      - name: Fenster und Türen
        state: |
          {% set ents = state_attr('binary_sensor.turen_und_fenster_gruppe', 'entity_id')%}
          {% if ents | reject('has_value') | list %}
            Problem
          {% else %}
            {{ 'Aus' if ents | select('is_state', 'on') | list else 'Geschlossen' }}
          {% endif %} 
        icon: >
          {% set d = {
          'Problem': 'mdi:alert-circle',
          'Aus': 'mdi:door-open',
          'Geschlossen': 'mdi:door-closed'} %}
          {{ d.get(this.state, 'mdi:alert-circle') }}

That seems to work perfectly. Thanks a lot!

In case someone (from Germany) needs a translated code with the correct states:

  - sensor:
      - name: Türen und Fenster
        unique_id: turen_und_fenster
        state: |
          {% set ents = state_attr('binary_sensor.turen_und_fenster_gruppe', 'entity_id')%}
          {% if ents | reject('has_value') | list %}
            Sensorausfall
          {% else %}
            {{ 'Offen' if ents | select('is_state', 'on') | list else 'Geschlossen' }}
          {% endif %} 
        icon: >
          {% set d = {
          'Problem': 'mdi:alert-circle',
          'Offen': 'mdi:door-open',
          'Geschlossen': 'mdi:door-closed'} %}
          {{ d.get(this.state, 'mdi:alert-circle') }}

You need to replace Problem with Sensorausfall in the icon template