🔹 Auto-entities - Automatically fill cards with entities

You can use the group feature of auto-entities?

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  include:
    - domain: light
      integration: group

will give you all lights that are groups
(might have to check the Hue groups, but, for those you can add the other filter on the attribute)

ofc, also exclude with these

other syntax:

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  include:
    - domain: light
      not:
        integration: group

no need for a label at all…sorry for misleading you there

oh :astonished:

will set this up and report back!

Tested this but I don’t think this is entirely what I’m looking for.

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - domain: light
  exclude:
    - label: exclude
    - integration: group

As far as I can tell, integration: group replaces the following, right?

attributes:
        entity_id: $*

But what I am still missing is a way to exclude the entities that are members of groups. these still show up in the list of entities.

Let’s take the following example:

I have 5 light entities: light.entity1 through light.entity5

I also have 2 light groups:
light.group1:

  • light.entity1
  • light.entity2

light.group2

  • light.entity3
  • light.entity4

light.entity5 is in no group.

I am looking for a filter setting that ends up giving me just light.entity5

  • light.group1 and light.group2 get eliminated by integration: group (thanks for the hint!)
  • but how to eliminate light.entity1, light.entity2, light.entity3, and light.entity4?

reason for this: as I am displaying my grouped lights separately with the mentioned collapsable card:
image

I don’t want the lights to show up twice. without this filter I have them once as an individual card (see top) and once within the collapsable grouped card (see bottom).

Any idea how this could be achieved? :slight_smile:

Another issue I have noticed:
both cards are set-up with the auto-entities card. The top single card is just a basic auto-entities card. The bottom one is a nested auto-entities card using decluttering-cards. I am getting a list of all light groups and am creating the following card for this:

type: custom:auto-entities
filter:
  include:
    - domain: light
      integration: group
      options:
        type: custom:decluttering-card
        template: illumination_light-group
        variables:
          - Light Group: this.entity_id
          - Title Expand: Mein Test
  exclude:
    - label: exclude
    - area: system
show_empty: false

The Light Group: this.entity_id is then being used within the decluttering card again to get all the children of the group using:

include:
  - group: '[[Light Group]]'
    options:
      type: custom:mushroom-light-card
      use_light_color: true
      show_brightness_control: true
      collapsible_controls: false
exclude:
  - area: System
  - label: exclude

As you can see in the screenshot, this basically works fine. BUT: it takes a loooot of time to load the children elements into the collapsable card. However, as soon as I turn on one of these lights, the children elements are loaded immediately, no delay at all.

any idea what’s going on here and how to improve it?

Before anything else:

Test if taking out the label: filter improves speed

I can’t use it , as it completely grinds the view.

I have an open issue on that in the repo

@Mariusthvdb
What I have done is basically remove one layer of auto-entites by just setting it up manually for the two groups:

      type: horizontal-stack
      cards:
        - type: custom:decluttering-card
          template: illumination_light-group
          variables:
            - Light Group: light.group_dummy_light_group
        - type: custom:decluttering-card
          template: illumination_light-group
          variables:
            - Light Group: light.dummy_light_group_2

so basically auto-entities now doesn’t create cards for the groups, but only within the groups.

this loads immediately on page load. it is only when I create these cards using auto-entities (-> nested auto-entities: auto-entities creates cards for groups and then cards within each group), that the delay is introduced

Odd,
That exclude should exactly do what you want…

Both legacy group, and modern light groups…

Maybe it’s the ‘or’ construction . What happens if don’t use the - in front of those excludes, only use the - on the first

Meaning you ‘and’ all filters.

I have set up a test like this:

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - domain: light
  exclude:
    - integration: group

but this gives me a list of all entities that are not group entities. even the ones that ar part of a group.

both of these lights are part of the dummy group:
image

right, I get it… yes, its was outputting something else.

you could create a template filter, where you subtract 2 lists, one being all the lights, and the other the grouped lights and light groups, and then reject the ones in the grouped entities from the all lights?

{% set groepen = states.light
|selectattr('attributes.entity_id','defined')
| map(attribute='entity_id')
|list %}

{% set leden = groepen| expand | map(attribute='entity_id')| list %}

{{ states.light
    | map(attribute='entity_id')
    | reject('in', groepen +leden)
    | list}}

thanks to @TheFes

is that what you are looking for?

I believe the only other way would be to include the domain light, and exclude all light groups manually

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - domain: light
  exclude:
    - group: light.terras_buiten
    - group: light.buiten_verlichting
    - group: light.plafond
    - group: light.primaire_binnen_lampen
    - integration: group
    - attributes:
        is_hue_group: true

note that group only iterates 1 level, so nested groups need to be added individually

update
btw, today I learned that lights groups consisting of only unavailable entities lose the entity_id attribute in the states machine, so can not be iterated over.

I found that while testing stuff for you, and kept noticing the same few lights returning, while being sure they were members of a group. Helper group that is.

Legacy groups dont have this problem, but I figure it wont be reason enough to start adding those back in your config :wink:

as a matter of fact, an old issue was raised on the matter, and closed because stale. I re-opened a new one, if only to get some response from the dev team on this behavior.

it would be helpful if you added some comment on your usecase, so the dev team is aware this is not a single user problem…

Hello, I m trying to use auto entities with custom card but I don’t understand how to input values:

  - type: custom:auto-entities
    card:
      type: entities
      title: Docker compose
    filter:
      include:
        - entity_id: sensor.omv_compose_*
          options:
            type: custom:button-card
            template: sensor_small
            entity: this.entity_id
            variables:
              name: |
                [[[
                  return `this.entity_id.split('_').pop()`
                ]]]
              icon: mdi:server-network-outline
              background: var(--popupBG)
              label: |
                [[[
                  return 'this.state'
                ]]]

Actual result: Pasteboard - Uploaded Image

If anyone now how to achieve it I will be glad :slight_smile:

Thanks

Wow, thank you so much @Mariusthvdb! This is amazing and with some tinkering leads exactly to the list of lights that I was hoping to get!

I have added a couple of additions:

  • I rejected light.presence_simulation (dummy group for presence simulation add-on), as this isn’t really a group in the normal sense
  • I additionally added a second list getting all the entities that contain .group_ in the entity_id. Just as a back-up to the issue you mentioned with unavailable groups, so that I can rename the group if it becomes necessary. The lists have some overlap, but this doesn’t matter as it is only used for the exclusions, excluding an entity twice doesn’t matter.
{% set groups = (states.light
    | selectattr('attributes.entity_id','defined')
    | rejectattr('entity_id', 'eq', 'light.presence_simulation')
    | map(attribute='entity_id')
  |list) + (states.light
    | selectattr('entity_id', 'search', '.*\.group_.*')
    | map(attribute='entity_id')
    | list
  ) | list
%}

{% set members = groups | expand
  | map(attribute='entity_id')
  | list
%}

{{ states. Light
  | rejectattr('entity_id', 'in', label_entities('exclude'))
  | rejectattr('entity_id', 'in', area_entities('System'))
  | map(attribute='entity_id')
  | reject('in', groups +members)
  | list
}}

Now this brings me to the issue that I can’t add options to template filters. I tried to follow this post to include the options right into the template and ended up with this:

type: custom:auto-entities
card:
  type: entities
filter:
  template: |
    [
      {% for e in state_attr('sensor.individual_lights','entities') %}
        {
          "type": "custom:decluttering-card",
          "template": "illumination_light-card-single",
          "variables": "{{ 
                - Light: this.entity_id }}",
          }, 
      {% endfor %} ]
  include: []
  exclude: []

But that doesn’t really work yet. It just gives me no entities at all and honestly, I’m a bit out of my depth here. My sensor.individual_lights looks like this: ['light.dummy_light_1', 'light.sunricher_on_off_2ch_licht', 'light.sunricher_on_off_2ch_licht_2', 'light.dummy_light_group_2']

Do you have any pointers on how I can add the options to the template filter? Or is there a way to use sensor.individual_lights as part of a normal include filter? Other than that it seems to work great!

I’ve also added a comment to the issue you have opened. This would definitely be more logic!

you’re making life difficult…
you are aware you can test these templates in the dev tools ? that should provide the complete output you are throwing at auto_entities

"variables": "{{ 
                - Light: this.entity_id }}",

seems to not have quotes on the - light , but I really am not sure decluttering card will be able to do this.

did you try a manually edited config first? so you know what will work before trying to figure that out with aut–entities.

sorry :frowning:

Yes, works without any issues:
image
This is even generated using auto-entities to create the children elements.

Code for this card
type: custom:decluttering-card
template: illumination_light-group
variables:
  - Light Group: light.group_dummy_light_group

I’ve also managed to set up the entire card with auto-entities (the one where I mentioned before that loading times suck with the nested auto-entities)


All of these cards are completely auto-generated.

Code for this card
type: custom:auto-entities
filter:
  include:
    - domain: light
      integration: group
      options:
        type: custom:decluttering-card
        template: illumination_automatic-light-group
        variables:
          - Light Group: this.entity_id
          - Title Expand: Mein Test
  exclude:
    - label: exclude
    - area: system
show_empty: false
card:
  type: custom:layout-card
  layout_type: custom:grid-layout

So I can confirm that the setup with decluttering card works. Aside from the loading times, as mentioned.

I am now just struggling to pass the options in the template filter, which I so far passed through the include filter. Or to somehow use my sensor.individual_lights within the include filter if at all possible.

I’m aware of the template section in the developer tools. It throws the following error: TemplateSyntaxError: expected token 'end of print statement', got ':'

Unfortunately, I don’t know how to fix this. putting the ‘Light’ part in quotes like this throws a different error:

[
  {% for e in state_attr('sensor.individual_lights','entities') %}
    {
      "type": "custom:decluttering-card",
      "template": "illumination_light-card-single",
      "variables": "{{ 
            '- Light: this.entity_id' }}",
      }, 
  {% endfor %} ]

TypeError: 'NoneType' object is not iterable

sensor.individual_lights looks like this at the moment:
['light.dummy_light_1', 'light.sunricher_on_off_2ch_licht_2', 'light.dummy_light_group_2']

shouldn’t that be something lik this

[
  {% for e in state_attr('light.alle_binnen_lampen','entity_id') %}
    {
      "type": "custom:decluttering-card",
      "template": "illumination_light-card-single",
      "variables": "{ 
            '- Light': {{e}} }",
      }, 
  {% endfor %} ]

I’ve been playing around with this a bit (with the help of ChatGPT, this post and this post) and ended up with this:

[
{% set entities_str = states('sensor.individual_lights') %}
{% set entities = entities_str[1:-1].replace("'", '"').split(', ') %}
{% for e in entities %}
  {
    "type": "custom:decluttering-card",
    "template": "illumination_light-card-single",
    "variables": "{
      '- Light': {{ e.strip('"') }} }",
  },
{% endfor %}
]

the reason for all the code at the beginning is because state_attr('sensor.individual_lights') is None.

the outcome in developer tools Template looks very similar to yours:

But when I put this into my card, auto-entities sees each line as a separate entity.

My Auto entities card
type: custom:auto-entities
filter:
  template: |
    [
    {% set entities_str = states('sensor.individual_lights') %}
    {% set entities = entities_str[1:-1].replace("'", '"').split(', ') %}
    {% for e in entities %}
      {
        "type": "custom:decluttering-card",
        "template": "illumination_light-card-single",
        "variables": "{
          '- Light': {{ e.strip('"') }} }",
      },
    {% endfor %}
    ]
  include: []
  exclude: []
show_empty: false
card:
  type: custom:layout-card
  layout_type: custom:grid-layout

I feel like we’re suuuuper close, but something isn’t working out yet :confused:

tbh, I dont think you are close now, as your template is using

{% set entities_str = states('sensor.individual_lights') %}
{% set entities = entities_str[1:-1].replace("'", '"').split(', ') %}

and no longer iterates the attributes of the lights in the sensor.individual_lights ? not sure how that sensor is configured, but why not use a light group?
(or labels for that matter… but we’ve been over that before…)

As you can see, the template in my suggestion above works just fine (and you’ve basically copied that in your later test), no requirement for stripping at all.
the crux is the injection into the decluttering card

I also try things out.
Now I just need a template for all lamps that are not in any group.

type: custom:auto-entities
card:
  type: entities
filter:
  template: |
    {% set groups = (states.light
        | selectattr('attributes.entity_id','defined')
        | rejectattr('entity_id', 'eq', 'light.presence_simulation')
        | map(attribute='entity_id')
      | list) + (states.light
        | selectattr('entity_id', 'search', '.*\.group_.*')
        | map(attribute='entity_id')
        | list)
    %}

    {% set members = groups | expand
      | map(attribute='entity_id')
      | list
    %}
    [
      {% for group in groups %}
        {
          "type": "custom:expander-card",
          "title-card": {
            "type": "custom:mushroom-light-card",
            "entity": "{{ group }}"
          },
          "cards": [
            {% for light in state_attr(group, 'entity_id') %}
              {
                "type": "custom:mushroom-light-card",
                "entity": "{{ light }}"
              }{% if not loop.last %},{% endif %}
            {% endfor %}
          ]
        }{% if not loop.last %},{% endif %}
      {% endfor %}
    ]
include: []
exclude: []


1 Like

please read the last 50 posts or so… that is what we have been trying to do, and established it cant be done, unless using legacy style groups

@Mariusthvdb and @Christian-1982
This actually seems to work:

I’m just struggling to get the card_mod styles in there. How would I format this with all the quotes and stuff?

"type": "custom:collapsable-cards",
"title": "Einzelne Lampen",
"card_mod":
  {
    style: |
      ha-card button { 
        font-family: "Kollektif", sans-serif !important;
      }
      ha-card button:focus {
        background: none !important;
      }
  },

It also seems to be a lot faster than using the nested filters I’ve had before.

@Christian-1982, you should be able to get the individual lights that are not part of any group like this, right? In my case this basically returns all the lights that are not part of any groups.

{% set individualLights = states.light
  | rejectattr('entity_id', 'in', label_entities('exclude'))
  | rejectattr('entity_id', 'in', area_entities('System'))
  | map(attribute='entity_id')
  | reject('in', groups +members)
  | list
%}

card_mod is for another thread… :wink:

not even sure what you’re asking exactly, and I dont know collapsible card… so hop over the card_mod thread for that

1 Like

I think I got what I wanted
However, I had to delete my lights group with all the lights, otherwise it somehow didn’t work.

All lights:

type: custom:stack-in-card
mode: vertical
cards:
  - type: custom:auto-entities
    card:
      type: entities
    filter:
      template: |
        {% set groups = (states.light
            | selectattr('attributes.entity_id','defined')
            | rejectattr('entity_id', 'eq', 'light.presence_simulation')
            | map(attribute='entity_id')
          | list) + (states.light
            | selectattr('entity_id', 'search', '.*\.group_.*')
            | map(attribute='entity_id')
            | list)
        %}

        {% set members = groups | expand
          | map(attribute='entity_id')
          | list
        %}
        {% set individualLights = states.light
          | rejectattr('entity_id', 'in', label_entities('exclude'))
          | rejectattr('entity_id', 'in', area_entities('System'))
          | map(attribute='entity_id')
          | reject('in', groups + members)
          | list
        %}

        [
          {% for group in groups %}
            {
              "type": "custom:expander-card",
              "title-card": {
                "type": "custom:mushroom-light-card",
                "entity": "{{ group }}"
              },
              "cards": [
                {% for light in state_attr(group, 'entity_id') %}
                  {
                    "type": "custom:mushroom-light-card",
                    "entity": "{{ light }}"
                  }{% if not loop.last %},{% endif %}
                {% endfor %}
              ]
            }{% if not loop.last %},{% endif %}
          {% endfor %}
          

        ]
    include: []
    exclude: []
  - type: custom:auto-entities
    card:
      type: entities
    filter:
      template: |
        {% set groups = (states.light
            | selectattr('attributes.entity_id','defined')
            | rejectattr('entity_id', 'eq', 'light.presence_simulation')
            | map(attribute='entity_id')
          | list) + (states.light
            | selectattr('entity_id', 'search', '.*\.group_.*')
            | map(attribute='entity_id')
            | list)
        %}

        {% set members = groups | expand
          | map(attribute='entity_id')
          | list
        %}
        {% set individualLights = states.light
          | rejectattr('entity_id', 'in', label_entities('exclude'))
          | rejectattr('entity_id', 'in', area_entities('System'))
          | map(attribute='entity_id')
          | reject('in', groups + members)
          | list
        %}

        [
          
          {% for light in individualLights %}
            {
              "type": "custom:mushroom-light-card",
              "entity": "{{ light }}"
            }{% if not loop.last %},{% endif %}
          {% endfor %}

        ]
    include: []
    exclude: []

All lights in room (‘Schlafzimmer’):

type: custom:stack-in-card
mode: vertical
cards:
  - type: custom:auto-entities
    card:
      type: entities
    filter:
      template: |
        {% set groups = (states.light
            | selectattr('attributes.entity_id','defined')
            | rejectattr('entity_id', 'eq', 'light.presence_simulation')
            | selectattr('entity_id', 'in', area_entities('Schlafzimmer'))
            | map(attribute='entity_id')
          | list) + (states.light
            | selectattr('entity_id', 'search', '.*\.group_.*')
            | selectattr('entity_id', 'in', area_entities('Schlafzimmer'))
            | map(attribute='entity_id')
            | list)
        %}

        {% set members = groups | expand
          | map(attribute='entity_id')
          | list
        %}
        {% set individualLights = states.light
          | rejectattr('entity_id', 'in', label_entities('exclude'))
          | rejectattr('entity_id', 'in', area_entities('System'))
          | selectattr('entity_id', 'in', area_entities('Schlafzimmer'))
          | map(attribute='entity_id')
          | reject('in', groups + members)
          | list
        %}

        [
          {% for group in groups %}
            {
              "type": "custom:expander-card",
              "title-card": {
                "type": "custom:mushroom-light-card",
                "entity": "{{ group }}"
              },
              "cards": [
                {% for light in state_attr(group, 'entity_id') %}
                  {
                    "type": "custom:mushroom-light-card",
                    "entity": "{{ light }}"
                  }{% if not loop.last %},{% endif %}
                {% endfor %}
              ]
            }{% if not loop.last %},{% endif %}
          {% endfor %}
          

        ]
    include: []
    exclude: []
  - type: custom:auto-entities
    card:
      type: entities
    filter:
      template: |
        {% set groups = (states.light
            | selectattr('attributes.entity_id','defined')
            | rejectattr('entity_id', 'eq', 'light.presence_simulation')
            | selectattr('entity_id', 'in', area_entities('Schlafzimmer'))
            | map(attribute='entity_id')
          | list) + (states.light
            | selectattr('entity_id', 'search', '.*\.group_.*')
            | selectattr('entity_id', 'in', area_entities('Schlafzimmer'))
            | map(attribute='entity_id')
            | list)
        %}

        {% set members = groups | expand
          | map(attribute='entity_id')
          | list
        %}
        {% set individualLights = states.light
          | rejectattr('entity_id', 'in', label_entities('exclude'))
          | rejectattr('entity_id', 'in', area_entities('System'))
          | selectattr('entity_id', 'in', area_entities('Schlafzimmer'))
          | map(attribute='entity_id')
          | reject('in', groups + members)
          | list
        %}

        [
          
          {% for light in individualLights %}
            {
              "type": "custom:mushroom-light-card",
              "entity": "{{ light }}"
            }{% if not loop.last %},{% endif %}
          {% endfor %}

        ]
    include: []
    exclude: []