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.
tom_l
February 15, 2023, 6:51pm
2
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.
tom_l
February 15, 2023, 7:13pm
4
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.
123
(Taras)
February 15, 2023, 7:27pm
6
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 }}
123
(Taras)
February 15, 2023, 8:01pm
8
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