How to create a boolean sensor and then set it's attributes based on other entities states? Create an "any window open" sensor that contains open windows as attributes

I’m attempting to create a single template binary_sensor that is true when any of my windows are open, and false when all of my windows are closed, AND that contains a list of all open windows as a list in it’s Attributes.

Right now, I have a binary_sensor for each individual window. I have all of those binary_sensors in an entity_filter card that only displays them when a window is open. This shows me at a glance that I have window(s) open. The problem is that when I have all my windows open, I have 10 windows icons showing up, cluttering my screen. I’m looking to condense that down to a single entity that is true when any window is open and contains a list of all open windows, and false when all windows are closed. This will help me de-clutter my screen, and inform me which window is still open.

The Sonarr sensor is a great example of what I’m looking to do. The main value is the number of upcoming TV episodes, and then the ‘attribute’ field contains the list of upcoming TV episodes.

Here’s an image of my entity_filter card that has too many ‘open window’ icons, and then an image of the Sonarr sensor that shows the list of upcoming TV shows in the ‘attribute’ accordion. Image of cluttered filter card and single Sonarr entity with attribute

So, in generic problem terms: I’m looking for how to set attribute fields of a boolean_sensor. Is that even possible?

Thanks!

1 Like

Try this:

  1. Create a group that contains all your windows, e.g. group.all_windows

  2. Create a template binary sensor:

template:
  - binary_sensor:
      - name: "Windows Open"
        device_class: window
        state: >
          {{ is_state('group.all_windows', 'on') }}
        attributes:
          open_windows: >
            {{ expand('group.all_windows')|selectattr('state','eq','on')|map(attribute='name')|list|join(',') }}
1 Like

I have a template sensor that is actually a tri-state:

- platform: template
  sensors:
    family_sensor:
      value_template: >- 
        {% set items = states.person %}
        {% set all = items|length %}
        {% set home = items|map(attribute="state")|select("equalto","home")|list|length %}
        {{ "none home" if home == 0 else "all home" if home == all else "some home" }}
      icon_template: >-
        {% if is_state('family_sensor', 'none') %}
          mdi:home-circle-outline
        {% else %}
          mdi:home-circle
        {% endif %}

As you can see, it’s using presence sensors but could be adapted for window sensors. That way you could have “none open”, “some open”, and “all open”.

If you’re interested, here’s a slightly more compact version of the template:

    family_sensor:
      value_template: >- 
        {% set home = states.person|selectattr('state','eq','home')|list|count %}
        {% set x = {0: 'none', states.person|count: 'all' } %}
        {{ x[home] if home in x.keys() else 'some' }} home

Boom, works perfect. Thank you!

Huh, I guess I missed them changing template sensors from being platforms inside sensor: and binary_sensor: to having their own top level integration. I guess I gotta spend some time figuring out how to convert to the new way of doing it.

Thanks again!

The new way of defining Template-based entities is the preferred way going forward but the old way (now referred to as ‘legacy’ Template Sensors) continues to work and is supported (so there’s no need to perform a mass conversion unless you really want to).

1 Like