Create Template Sensor for Specific Devices in Specific State

Hey All, the title pretty much says what I want to accomplish. I have multiple sensors that will show one of 3 states but I can’t figure out how to write a template for it.

They are team sensors and the states will either be PRE, IN or POST.

I’ve looked online and tried manipulating some code but I’m not that familiar with it, yet.

Thanks for your time.

You have not said what you want the template to do or show.

Sorry, I just want it to count the devices in a specific state.

And again, do we have to guess which state?

I was going to have one for each state. If you can point me in the right direction I can hopefully figure it out.

Do you mean you want something like this?

template:
  - sensor:
      - name: Quantity PRE
        state: >
          {{ expand('sensor.first', 'sensor.second', 'sensor.third')
            | selectattr('state', 'eq', 'PRE') | list | count }}
      - name: Quantity IN
        state: >
          {{ expand('sensor.first', 'sensor.second', 'sensor.third')
            | selectattr('state', 'eq', 'IN') | list | count }}
      - name: Quantity POST
        state: >
          {{ expand('sensor.first', 'sensor.second', 'sensor.third')
            | selectattr('state', 'eq', 'POST') | list | count }}

EDIT

Correction. Overlooked to wrap the entity_ids in quotes.

1 Like

I tried this in the template editor but it didn’t count anything. It says ‘sensor’ is undefined. Should it work there?

{{ expand(sensor.tracker_lakers, sensor.tracker_kings, sensor.tracker_lafc) | selectattr('state', 'eq', 'PRE') | list | count }}

That’s due to my mistake. I overlooked to wrap each entity_id in quotes. I’ve corrected the example I posted above.

Try this in the Template Editor:

{{ expand('sensor.tracker_lakers', 'sensor.tracker_kings', 'sensor.tracker_lafc') | selectattr('state', 'eq', 'PRE') | list | count }}
1 Like

Thank you so much!

1 Like