Create a group with either dynamic or with static members

I like the idea here, but your code is poorly optimized. It would be a resource hog. There’s loops and loops and loops. With jinja, you want to take advantage of the filters because they use generators to handle the heavy load. Filters like select, reject, selectattr, rejectattr, and unique.

This replicates your code but it executes extremely fast, even with 1000s of entities and it achieves the same result.

{%- set entities = states | selectattr('domain','in', domains) | list if domains else states | list %}
{%- set ns = namespace(include=[], exclude=[]) %}
{%- for item in includes %}
  {%- set ns.include = ns.include + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
{%- endfor %}
{%- for item in excludes %}
  {%- set ns.exclude = ns.exclude + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
{%- endfor %}
{%- set entities = entities | map(attribute='entity_id') | select('in', ns.include) | reject('in', ns.exclude) | list %}
{%- set entities = (entities + add) | reject('in', rem) %}
{{ entities | unique | sort }}
1 Like

Thank you! That is much more efficient! Still learning filters and their order of operation. Updated the GIST with your code and a few changes.

Just verify it works for all your use cases, I tried it a bunch and it appeared to do what yours did. Either way, its much faster and it should be the same.

EDIT: speaking of which, found some errors. This should fix em.

        {%- set entities = states | selectattr('domain','in', domains) | list if domains else states | list %}      
        {%- set ns = namespace(include=[], exclude=[]) %}
        {%- for item in includes %}
          {%- set ns.include = ns.include + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
        {%- endfor %}
        {%- for item in excludes %}
          {%- set ns.exclude = ns.exclude + entities | selectattr('object_id', 'search', item) | map(attribute='entity_id') | list %}
        {%- endfor %}
        {%- set entities = entities | map(attribute='entity_id') %}
        {%- set entities = entities | select('in', ns.include) if ns.include else entities %}
        {%- set entities = entities | reject('in', ns.exclude) if ns.exclude else entities %}
        {%- set entities = (entities | list + add) | reject('in', rem) %}
        {{ entities | unique | sort }}

Here’s what I came up with earlier. Only minor changes from what you posted before. Tested it pretty thoroughly and it appeared to work. Did I miss something?

{%- set entities = states | selectattr('domain','in', domains) | list if domains else states | list %}
{%- set ns = namespace(include=[], exclude=[]) %}
{%- for item in includes %}
  {%- set ns.include = ns.include + entities | selectattr('object_id', 'search', item) | list %}
{%- endfor %}
{%- for item in excludes %}
  {%- set ns.exclude = ns.exclude + entities | selectattr('object_id', 'search', item) | list %}
{%- endfor %}
{%- set entities = entities | select('in', ns.include if includes else entities ) | reject('in', ns.exclude) | map(attribute="entity_id") | list %}
{%- set entities = (entities + add | list ) | reject('in', rem) %}
{{ entities | unique | sort }}

If domain is listed with nothing else it fails, that’s at least what I fixed in the one I just posted

I just tested the blueprint with only two static entities and it added absolutely all of the entities in home assistant to the group. Something’s not working.

Thanks for letting me know. I’ll have it fixed shortly.

1 Like

Need to wrap if statement around includes and excludes sets, I’ll post tomorrow morning if ya don’t post anything

1 Like

Made changes and thoroughly tested. I believe it’s fixed but please let me know if you find anything.

1 Like

It works great now! thank you so much for taking the time to fix it.

@mwolter Do you think this would also enable me to create a group from en existing sensor that contains multiple sensors called out as entities.

This is the entity (example)

And if I use (for instance) this template below I get the list of entities.

It would be great if your blue print would allow me to create a real ‘group’ from this and keep it in ‘sync’ with the fake group sensor powercalc generates.

This only works with old school groups as the new school groups do not have the ability to adjust their members.

Short answer: Not possible.

Is it possible to also work with friendly names instead of object IDs?

I have tons of Zigbee devices and I normally do not care their object ID, instead I simply rename them to what they should be used for instead (as any normal user would also do). But instead I use a specific naming concept in my friendly names that would be very beneficial to have such dynamic groupings.

Example:
‘Bathroom Window1 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_80e6df01_on_off
‘Bathroom Window2 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_80e9df04_on_off
‘Livingroom Window1 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_81e9da01_on_off
‘Livingroom Window2 on_off’ = binary_sensor.lumi_lumi_sensor_magnet_aq2_86e96f01_on_off

… which are not unique in general.

No objection in this but still, as in my setup they are not, i´d love this option. So would it be possible?

Just rename your entity_ids

OT then here, but:

Let´s leave it like this

It takes less than 5 seconds to do a find in files/replace with a text editor. Notepad++, VSCode, or the vscode addon.

I would like to set the ‘all_entities’ option. Is there any way to do this?

Not sure what you are referring to. Please provide more detail.