Template to count entities with value true

Hello,

I would appreciate some help with a helper template sensor. I’m trying to count the number of devices with an entity that has the Value ‘True’ or ‘1’. This is to see how many thermostats are demanding heat and bring a second boiler online if it exceeds a certain threshold.

I’m new to Home Assistant but not to programming in general (C/C++, Java etc.). I’ve managed to count the number of devices that have this entity, but I can’t figure out how to select the ones that have that value set as true. I’ve tried all sorts of combinations and the template just gives the result ‘unavailable’.

Any help greatly appreciated.

Lee

{{ states | selectattr('entity_id', 'search', 'thermostatic_valve_provide_heat') | list  | count }}

What is the domain of the entities in question? Are they sensor or binary_sensor, something else or a mix? If it is only one domain you should add it to states to narrow the search and make the template more efficient i.e. states.sensor instead of states.

The general function you want to use is selectattr() with an in test since you are testing against multiple values:

{{ states.sensor
| selectattr('entity_id', 'search', 'thermostatic_valve_provide_heat')
| selectattr('state', 'in', ['True','1'])
| list  | count }}

Yes it’s sensor. Many thanks, that’s working perfectly!

Any hints on the best place to find documentation/references for templates?