🔹 Auto-entities - Automatically fill cards with entities

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.

I think I might have just resolve the issue by just creating an actual list for the variables. Might have been obvious, but honestly I don’t know jinja all that well:

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

image

This is just an initial test, but it looks promising.
If anyone ever has a similar issue, I hope this can be found, otherwise at least you and me know now :wink:

If it turns out this doesn’t work after all, I’ll report back!

For the moment: thank you so much for your help, wouldn’t have gotten here without you!! Help from people like you is of so much value to beginners like me and is very much appreciated!

check this: you got to be sure you’re outputting actual entity_id’s to the template.

the above doesnt work, resulting in:

but expanding that same light group:

    - type: custom:auto-entities
      card:
        type: grid
        title: Test Lights populating cards
        columns: 4
      card_param: cards
      filter:
        template: >-
          {% for s in expand('light.alle_binnen_lampen')  -%}
              {{
                {
                  'type': 'custom:button-card',
                  'entity': s.entity_id,
                  'aspect_ratio': '1/1',
                  'show_state': 'true'
                }
              }},
          {%- endfor %}
      sort:
        method: state

actually populates the cards nicely

also not that the for template already is creating the list, so you shouldnt need to explicitly make that

edit

you can also make that work however, and still use the template filter, important detail is to put the comma in the right place:

    - type: entities
      title: Light groups auto
      card_mod:
        class: class-header-margin
      show_header_toggle: false
      entities:
        - type: custom:auto-entities
          card:
            type: entities
          filter:
            template: >
              {% for l in states.light|selectattr('attributes.entity_id','defined') %}
              {{
                {
                'type': 'custom:slider-entity-row',
                'entity': l.entity_id
                }
              }},
              {%- endfor %}

populating the cards using that:

    - type: custom:auto-entities
      card:
        type: grid
        title: Test Lights populating cards
        columns: 4
      card_param: cards
      filter:
        template: >-
          {% for l in states.light|selectattr('attributes.entity_id','defined')  -%}
              {{
                {
                  'type': 'custom:button-card',
                  'entity': l.entity_id,
                  'aspect_ratio': '1/1',
                  'show_state': 'true'
                }
              }},
          {%- endfor %}
      sort:
        method: state

individualLights isn’t a light group, it’s a list of entities, that’s why it’s working with the {% for e in individualLights %}

I also simplified the template a bit by using integration_entities('group') instead of checking if it has any entities in its attributes, also circumventing the issue here regarding groups with unavailable entities.

{% set groups = (states.light
  | selectattr('entity_id', 'in', integration_entities('group'))
  | rejectattr('entity_id', 'eq', 'light.presence_simulation')
  | 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
%}

Hi,
i’m getting the following error, can someone help me out?
It drives me crazy…

Invalid entity ID at position 0: {‘entity’:
entities:

  • entity: ‘{’‘entity’‘:’
  • entity:
type: custom:auto-entities
card:
  type: entities
  title: TGTG Surprise Bags
filter:
  template: >-
    {% for state in states.sensor if 'sensor.too_good_to_go_toogoodtogo_' in
    state.entity_id %}
         {% set entity_id = state.entity_id %}
         {% set s = states(entity_id) %}
         {%- if is_number(s) and s | int > 0 %}
           {% set pickup_start = state_attr(entity_id, 'pickup_start') %}
           {%- if pickup_start is not none -%}
             {{
               {
                 'entity': entity_id,
                 'name': state_attr(entity_id, 'friendly_name')[5:],
                 'type': "custom:multiple-entity-row",
                 'unit': false,
                 'secondary_info': 'Begin: ' + state_attr(entity_id, 'pickup_start_human') + ', End: ' + state_attr(entity_id, 'pickup_end_human') + ", €" + state_attr(entity_id, 'price') | string,
                 'tap_action': {
                   'action': 'url',
                   'url_path': state_attr(entity_id, 'url')
                 }
               }
             }},
           {%- endif -%}
         {%- endif -%}
       {%- endfor %}