You make a good point. The template I suggested, although clearly not trivial, I think at least, is pretty simple to use. However, it probably does require some explanation. Once you understand the pieces, it actually is pretty straightforward.
{{ states|selectattr('entity_id','in',state_attr('group.electrics','entity_id'))
|selectattr('state','eq','on')|list|count }}
First, states
is all the entity state objects in the system. (Basically each state object contains information about a given entity - its entity_id, its current value - i.e., its âstateâ - its other attributes, such as friendly_name, etc.)
That collection of state objects is then âfilteredâ (thatâs what the |
symbol means) down by a function that selects objects by their attributes. In this case, I use selectattr('entity_id','in',XXX)
, which only accepts state objects whose entity_id
is in
a list of entity_idâs.
The list is the XXX
part, which in this case is state_attr('group.electrics','entity_id')
. That is a function that provides the list of entity_id
's that the group group.electrics
contains.
So, putting these two together, weâre filtering the list of all state objects down to just the state objects that correspond to the entities defined by group.electrics
.
Next we use the selectattr
filter again, but this time weâre selecting state objects whose state
attribute is eq
(i.e, equal to) on
.
Next we turn that collection into a proper list using the |list
filter. So, at this point we have a list of state objects that correspond to entities that are defined by group.electrics
that also happen to be currently on
. Lastly we use the |count
filter to get a count of the objects in the resulting list. I.e., the number of power switches that are on.