Template binary sensor does not work for a group of sensors

I have a list of some window sensors, door sensors and motion sensors. I collected these sensors in three groups so to have a generic idea if a window or a door in my place is open, or there is some motion detected.

The following is from my groups.yaml file:

doors:
    name: Main Doors
    entities:
      - binary_sensor.window_door_sensor_2
      - binary_sensor.openclose_9

motion_sensors:
    name: Motion Sensors
    entities:
        - binary_sensor.motion_sensor_2
        - binary_sensor.presence_17
        - binary_sensor.presence_18

windows:
    name: Windows
    entities:
        - binary_sensor.openclose_13
        - binary_sensor.openclose_12
        - binary_sensor.openclose_14

When showing the corresponding groups on lovelave I can see the status changing from off to on when an activity is triggered, as I would expect.

However, I wanted to set up a binary template sensor to have a more personalized experience, like “open/closed” or “detected/clear” text. Additionally, the template sensor should come handy because I would like to have a different icon according to the sensor status.

This is how I set up the binary template sensors in my configuration.yaml:

binary_sensor:
  - platform: template
    sensors:
        windows:
            friendly_name: "Windows"
            device_class: window
            value_template: >-
                {% if group.windows == 'on' %}
                     Open
                 {% elif group.windows == 'off' %}
                   Closed
                 {% else %}
                    n/a
                  {% endif %}
            icon_template: >
                 {% if group.windows == 'on' %}
                    mdi:window-open
                 {% elif group.windows == 'off' %}
                    mdi:window-closed
                 {% else %}
                    mdi:help
                 {% endif %}
  - platform: template
    sensors:
        doors:
            friendly_name: "Doors"
            device_class: door
            value_template: >-
                {% if group.doors == 'on' %}
                     Open
                 {% elif group.doors == 'off' %}
                   Closed
                 {% else %}
                    n/a
                  {% endif %}
            icon_template: >
                 {% if group.doors == 'on' %}
                    mdi:door-open
                 {% elif group.doors == 'off' %}
                    mdi:door-closed
                 {% else %}
                    mdi:help
                 {% endif %}
  - platform: template
    sensors:
        motion_sensors:
            friendly_name: "Motion Sensors"
            entity_id:
            - binary_sensor.motion_sensor_2
            - binary_sensor.presence_17
            - binary_sensor.presence_18
            value_template: >-
              {% if is_state("binary_sensor.motion_sensor_2", "on")
              or is_state('binary_sensor.presence_17', 'on')
              or is_state('binary_sensor.presence_18', 'on') %}
                Clear
              {% else %}
                Detected
              {% endif %}
            icon_template: >-
              {% if is_state("binary_sensor.motion_sensor_2", "on")
              or is_state('binary_sensor.presence_17', 'on')
              or is_state('binary_sensor.presence_18', 'on') %}
                mdi:human-male
              {% else %}
                mdi:human-handsup
              {% endif %}

The door and window sensors always show closed with the “closed-icon”, even when something is open (and I can see the group status going correctly from off to on). While the motion sensor is always showing off, so not even the text changes to “Clear”.

Do you know what I am doing wrong? I guess it should be an easy thing to implement :confused:

Try using this format:

{% if states('group.windows') == 'on' %}

As @tom_l says.

Still, the template OP uses shouldn’t produce an icon at all, since the template editor errors out stating:

Error rendering template: UndefinedError: ‘group’ is undefined.

this returning mdi:window-closed is rather surprising…if anything, it should return the else clause, mdi:help ?

I am not at home now and thus cannot test any trigger but just tried to invert the on/off texts and icons to have a hint if it’s working. This is now the code:

binary_sensor:
  - platform: template
    sensors:
        windows:
            friendly_name: "Windows"
            device_class: window
            value_template: >-
                 {% if states('group.windows') == 'off' %}
                   Open
                 {% elif states('group.windows') == 'on' %}
                   Closed
                  {% endif %}
            icon_template: >-
                 {% if states('group.windows') == 'off' %}
                    mdi:window-open
                 {% elif states('group.windows') == 'on' %}
                    mdi:window-closed
                 {% endif %}

So now I can see the icon of the open window (I have all my windows closed but on purpose I set up that “off” should have the icon of an open window). However, the text is “Closed” while it should say “Open”. So it is still not working fully :frowning:

4

All you should need is this (the device class will take care of the state values):

  - platform: template
    sensors:
      windows:
        friendly_name: "Windows"
        device_class: window
        value_template: "{{ is_state('group.windows', 'on') }}"
        icon_template: >-
          {% if is_state('group.windows', 'on') %}
            mdi:window-open
          {% elif is_state('group.windows', 'off') %}
            mdi:window-closed
          {% else %}
             mdi:help-box
          {% endif %}
1 Like

Tom I found the solution! It’s all wrong from the beginning as I have been using a binary sensor template as specified in my code and title. That is wrong because the binary template can only show on or off. This is why the icon was working but not the text. I should just use the template sensor to solve the issue. So it would have worked with your final code proposal under sensors (while I was using the binary_sensor: initially).

1 Like

@tom_l’s solution is better than using a sensor instead of a binary sensor. When you use a device class for a binary sensor like window, on means open, off means closed. So the UI will show ‘Open’, and ‘Closed’ as text. The icon will also change for you, so there really is no need for an icon_template, but you can use that if you don’t like the supplied icons. As it stands, the mdi:window-open and mdi:window-closed are the default icons. So the simpliest solution for you to implement is:

binary_sensor:
  - platform: template
    sensors:
      windows:
        friendly_name: "Windows"
        device_class: window
        value_template: "{{ is_state('group.windows', 'on') }}"

This will create a window binary sensor that displays Open/Closed in the UI. The icon will change between mdi:window-open/mdi:window-closed. The under the hood states will be on/off for automations.

Device classes were created so that users didn’t need to create icon templates or translate their text into their native language. You should use these built in’s to make life easier for yourself.

I thought it might just be a colour change (which was why the template was needed) but you’re right. I should have checked.