Sensor Template Entity_id Wildcard For Loop

I have Xiaomi door/window sensors and I would like to count how many of these are currently Open.

The entity_id for these looks like this:

binary_sensor.door_window_sensor_158d0001d68293

Is it possible to narrow down the entity_id to just include:

states.binary_sensor.door_window_sensor*

instead of:

states.binary_sensor

in the following example below:

- platform: template
    sensors:
      doors_open:
        friendly_name: "Doors Open"
        value_template: >-
          {% for state in states.binary_sensor if state.state == 'off' %}{% if loop.last %}{{loop.index}}{% endif %}{% endfor %}

Thank you!

Did you ever get this to work?

Yes and no - I didn’t use a wildcard in the end but instead listed all the items I wanted to count, using the code below:

sensor:
  - platform: template
    sensors:
      windows_open:
        friendly_name: "Windows Open"
        value_template: >-
          {%- set windows = [
          "binary_sensor.door_window_sensor_158d0001d68293",
          "binary_sensor.door_window_sensor_158d0001ab4089",
          "binary_sensor.door_window_sensor_158d0001ab38a0",
          "binary_sensor.door_window_sensor_158d0001ab73fa",
          "binary_sensor.door_window_sensor_158d0001ab73f1",
          "binary_sensor.door_window_sensor_158d0001dad608",
          "binary_sensor.door_window_sensor_158d0001e44e11"] -%}
          {%- for window in windows if is_state(window, 'on') -%}
            {% if loop.last -%}
              {{ loop.index }}
            {% endif %}
            {% else %}
              0
          {%- endfor -%}

I then just reference the sensor as - sensor.windows_open

2 Likes

Awesome! I’ll give this a try tonight. Thanks

Anyone ever figure out how to do this with wildcards?

Was looking for something like this, sadly i cant get it to work:

This is my template sensor:

sensor:
  - platform: template
    sensors:
      lights_on:
        friendly_name: "Lights on"
        value_template: >-
          {%- set lights = [
          "light.beistelltisch",
          "light.kitchen_lights",
          "switch.fibaro_01_esstisch",
          "switch.sonoff01",
          "switch.sonoff02_power",
          "switch.sonoff04",
          "switch.zb_osram_plug_01"] -%}
          {%- for light in lights if is_state(light, 'on') -%}
            {% if loop.last -%}
              {{ loop.index }}
            {% endif %}
            {% else %}
              0
          {%- endfor -%}

But its stuck at “0” all the time even if some of the switches or lights are turned on.
Any idea what could be the problem? Im running 0.86.2 atm and tried the sensor in a package as well as the main configuration.yaml.

Ah yes, a change in later versions of HA meant that entities need to be specified when using templates. In your case, the following should work:

sensor:
  - platform: template
    sensors:
      lights_on:
        entity_id:
          - light.beistelltisch
          - light.kitchen_lights
          - switch.fibaro_01_esstisch
          - switch.sonoff01
          - switch.sonoff02_power
          - switch.sonoff04
          - switch.zb_osram_plug_01
        friendly_name: "Lights on"
        value_template: >-
          {%- set lights = [
          "light.beistelltisch",
          "light.kitchen_lights",
          "switch.fibaro_01_esstisch",
          "switch.sonoff01",
          "switch.sonoff02_power",
          "switch.sonoff04",
          "switch.zb_osram_plug_01"] -%}
          {%- for light in lights if is_state(light, 'on') -%}
            {% if loop.last -%}
              {{ loop.index }}
            {% endif %}
            {% else %}
              0
          {%- endfor -%}
1 Like

Thanks for the quick reply.
It is working no with referencing the entity ids!