Count open state cover entities

The volunteers at our local community woodworking shop have built Arduino-based automated blast gates for our dust collection system. The gates publish their state via MQTT. We are successfully monitoring the gate state (open, closed, opening, or closing) using MQTT with Home Assistant.

Now we are attempting to create a template to count the total number of gates open.

I am a novice using HA and YAML. I have found several examples of counts of binary switches. Based on these examples, I created the template below. I always get a count of 0 no matter how many of entities are in an open state.

We use an RPI 4-64 running Home Assistant 2024.2.3 using HassOS image.

Can someone provide me some clues?

Here is the template:

template:
 -sensor:
   - name: open_gate_count
     unique_id: open_gate_count
        {% set entities = ['cover.m01', 'cover.m02', 'cover.m00', 'cover.M06', 'cover.m10', 'cover.m11', 'cover.m16', 'cover.m17', 'cover.m26', 'cover.m28', 'cover.m29', 'cover.m30', 'cover.m31', 'cover.m33', 'cover.m34', 'cover.m35', 'cover.m36', 'cover.m38'] %}
        {{ entities | selectattr('state','eq',"open") | list | count }}

Here is a sample of the entity configuration files:

mqtt:

  cover:
    - name: "M01"
      unique_id: M01
      state_topic: "home-assistant/blastgates/M01/state"
      availability:
        - topic: "home-assistant/blastgates/M01/availability"
          payload_available: "online"
          payload_not_available: "offline"
      qos: 0
      device_class: damper
      optimistic: false

    - name: "M02"
      unique_id: M02
      state_topic: "home-assistant/blastgates/M02/state"
      availability:
        - topic: "home-assistant/blastgates/M02/availability"
          payload_available: "online"
          payload_not_available: "offline"
      qos: 0
      device_class: damper
      optimistic: false

    - name: "M03"
      unique_id: M03
      state_topic: "home-assistant/blastgates/M03/state"
      availability:
        - topic: "home-assistant/blastgates/M03/availability"
          payload_available: "online"
          payload_not_available: "offline"
      qos: 0
      device_class: damper
      optimistic: false
  1. Your template sensor configuration is missing the value_template key.
  2. You need to use the proper select function and test to check the state value from a list of entity IDs… The function you are using, selectattr('state', 'eq', 'open'), needs a list of dictionaries/objects. Using it on a simple list of entity ID strings returns an empty list, hence the 0 count.
template:
  - sensor:
      - name: open_gate_count
        unique_id: open_gate_count
        value_template: |
          {% set entities = ['cover.m01', 'cover.m02', 'cover.m00', 'cover.M06', 'cover.m10', 'cover.m11', 'cover.m16', 'cover.m17', 'cover.m26', 'cover.m28', 'cover.m29', 'cover.m30', 'cover.m31', 'cover.m33', 'cover.m34', 'cover.m35', 'cover.m36', 'cover.m38'] %}
          {{ entities | select('is_state', 'open') | list | count }}

EDIT: :man_facepalming: Thanks @LiQuid_cOOled

1 Like

You mean 'open' not on' correct?

template:
  - sensor:
      - name: open_gate_count
        unique_id: open_gate_count
        value_template: |
          {% set entities = ['cover.m01', 'cover.m02', 'cover.m00', 'cover.M06', 'cover.m10', 'cover.m11', 'cover.m16', 'cover.m17', 'cover.m26', 'cover.m28', 'cover.m29', 'cover.m30', 'cover.m31', 'cover.m33', 'cover.m34', 'cover.m35', 'cover.m36', 'cover.m38'] %}
          {{ entities | select('is_state', 'open') | list | count }}

Thank You! That give me a clue where to look. I will research value templates.
I am beginning to understand how much I don’t know.

Thanks for asking. I may have this wrong.
My understanding is that the damper class within cover entities have four states

  • open
  • closed
  • opening
  • closing

on and off are not valid
Am I mistaken?

You can also do something like this, but it includes everything that is a cover device.

{{ states.cover | selectattr('state', 'eq', 'open') | list | count }}

That question was directed at Didgeidrew because he originally had the state as ‘on’ in the code he posted first. You would have still received 0 as a result. It was just a minor error that we all make.

He fixed the error in his post and gave me a shout out.

Thank You! Thank You!
I had spent hours trying solve this. It now works perfectly.
The suggestion to use {{ states.cover | selectattr(‘state’, ‘eq’, ‘open’) | list | count }} is also helpful. We want a count of all cover entities. This will eliminate the need to list them individually.

1 Like