Efficiently filtering area_entities()

Hi,

I have a template, working, which selects and sums all the Power entities in a given Area.

In the template helper the preview shows all the entities the template will initially look for state changes on, which is all entities in the Area - binary_sensor, switch, light etc as well as all sensors

My question is, is there a way to make it more efficient, so it only looks for state changes in the subset of entities of interest (based on attribute in my case)

{% set checker = area_entities('bedroom main') | 
   select('is_state_attr', 'device_class', 'power') |
   select('is_state_attr', 'unit_of_measurement', 'W') |
   list %}
   {% set ns = namespace(states=[]) %}
        {% for stu in checker %}
        {% set ns.states = ns.states + [ states(stu) | float(0) ] %}
        {% endfor %}
        {{ ns.states | sum | round(2) }}

Thanks

Untested, smth like this:

{{
  states.sensor |
  selectattr('entity_id','in',area_entities('xxx')) | 
  selectattr('attributes.device_class','defined') |
  selectattr('attributes.device_class','eq','xxx') |
  map(attribute='state') |
  map('float',0) |
  sum
}}

Thanks.

My assumption is that the key to the efficiency of the template (i.e. the resources it consumes) is the first selection, the quantity of items it contains? Good assumption? Are there any profiling functions built in to accurately measure/compare equivalent solutions for their efficiency?

so in our two solutions it’s the entities in an area (bedroom main) versus the entities in a domain (sensor). The latter contains more entities (in my HA install, it’s 79 v. 1,144) , but perhaps HA can more efficiently monitor a domain as a single/whole thing compared to a list of specific entities?

I had another thought, requires a bit more upfront admin, perhaps assigning a Label to all the Power/W entities. Then the initial selection could be label_entities(‘power_label’), and within that a filter by Area.

could maintain the label assignment by a regular script. Actually, the script could assign area-specific power labels, then we wouldn’t need filtering in the template. Just need to run the label-applying script frequent enough to keep the entities each label includes current, while not running too often to negate the efficiency gain. Perhaps triggered by the addition of a new device.

Cheers