🔹 Auto-entities - Automatically fill cards with entities

@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: []

That’s because if you have all lights in the group and then you take all your lights and remove all group members from the list you’re left with nothing.

you could also explicitly exclude this group either from set groups, similar to what I did with light.presence_simulation. Then you won’t have it as a group card either, which wouldn’t make sense anyways.

did some more testing on this: it seems that as soon as I remove layout_type: custom:grid-layout, the cards load immediately. It seems like these performance issues have something to do with adding the grid-layout.

anyone else noticed this?

I have also tried changing to built-in grid. No issues, but of course a lot of functionality is then missing. Same issue seemingly with horizontal-layout and vertical-layout

My takeaway from our exchanges so far was that light groups won’t work for this because I need to get a list of all the lights that are not part of any group, except the light.presence_simulation group, which is only possible via templates.

So what I currently have is this:

# Define list of all groups, whose members should be excluded from the list of ungrouped lights
{% 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
%}

# expand group to get the actual members
{% set members = groups | expand
  | map(attribute='entity_id')
  | list
%}

# from this list also exclude a couple of members due to my specific setup
{% 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 e in individualLights %}
  {
    "type": "custom:decluttering-card",
    "template": "illumination_light-card-single",
    "variables": {
      "  - Light": "{{ e }}" },
  },
{% endfor %}
]

This is basically an adaptation of what you’ve posted from @TheFes - thanks!

I’ve also tried to put this exact list into its own sensor to clean up the code, but that just adds unnecessary complication, so currently I’m working with the full code above.

The output of this in the template tester is the following:
image

But something is off… as soon as I put it into an actual template filter, this is the resulting card:

code of auto-entities card
type: custom:mod-card
card_mod:
  style:
    hui-grid-card $: |
      #root {
        grid-template-columns: 1fr !important;
      }
card:
  type: custom:auto-entities
  card:
    type: grid
    square: false
  card_param: cards
  show_empty: false
  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
        ) | 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 e in individualLights %}
        {
          "type": "custom:decluttering-card",
          "template": "illumination_light-card-single",
          "variables": {
            "  - Light": "{{ e }}" },
        },
      {% endfor %}
      ]
    include: []
    exclude: []

image

Result:

  • It finds the correct entities that should be displayed
  • YAML seems to be written almos correctly, theres just the quotes around the - Light and the indentation seems to be a bit off.

I’ve also tried switching the quotes up a bit:

      [
      {% for e in individualLights %}
        {
          "type": "custom:decluttering-card",
          "template": "illumination_light-card-single",
          "variables": {
            '  - Light': {{ e }} },
        },
      {% endfor %}
      ]

but this messes it up completely, as now every line gets rendered as an individual entity:

Do you have any idea how to fix this?

tbh, not sure no… I just tried my earlier suggestion again:

and the template itself revolves fine in dev tools (ofc I dont have that decluttering template so that remains to be seen…)

can you at least confirm your template itself resolves correctly in the dev tools?

and yes, you should Not quote the resulting value in the line

 '- Light': {{e}} 

as that would make it a string, and explains what you see when you did quote.