🔹 Auto-entities - Automatically fill cards with entities

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 %}

posted this is Discord too, but since many here wont use that, I’ll repost here:

using auto-entities in a sections view, leaves a gap when the (entities) card is empty (even when the show_empty: false is set. Is there a generic solution/trick for this, or is the auto-entities card simply not yet fully compatible (bewerkt)

the exact same cards configuration in a type: custom:vertical-layout has no gap. so it might be a section thing after all? (using yaml mode btw) (bewerkt)

example to test:

  - type: custom:auto-entities
    card:
      type: entities
    show_empty: false
    filter:
      include:
        - entity_id: media_player.plex_*
          state: /playing|paused|'on'/

thanks if you have any hint how to prevent this in the new Sections

update

missed his issue show_empty in Sections view ¡ Issue #433 ¡ thomasloven/lovelace-auto-entities ¡ GitHub yesterday. hope auto-entities will see an update fixing this then

Any ideas on how to get this to work? I want to have it filter based on the input_select helper that would contain the room name (i.e. “Kitchen”)

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - area: '{{ states.input_select.room_selection.state }}'
  exclude: []

Can be only done by a “template” option:

filter:
  template: ... build a list of entries here using jinja ...

Thanks. I got it to work with this:

card:
  type: entities
filter:
  template: >-
    {{states.light | selectattr('entity_id', 'in',
    area_entities(states.input_select.room_selection.state)) |
    map(attribute='entity_id') | list }}

BUT I cannot get custom:bubble-card to populate for entities as this doesn’t work properly, it shows ALL lights. Any ideas? Is there a way to use template without it being overriden/ignored by include?

card:
  type: grid
  columns: 2
  square: false
card_param: cards
filter:
  template: >-
    {{states.light | selectattr('entity_id', 'in',
    area_entities(states.input_select.room_selection.state)) |
    map(attribute='entity_id') | list }}
  include:
    - domain: light
      state: 'on'
      options:
        type: custom:bubble-card
        card_type: button
        button_type: slider
        show_attribute: false
        show_last_changed: true
        show_state: true
  exclude: []

You can use any of these combinations:

  • include
  • include + exclude
  • template

Means - all “options” must be a part of a template.
Search here, there are ready examples.

Yeah, I think me adding the “domain: light” overrides my template to select only lights with the matching area name. I cannot get the options to work in template

Got it to work! It’s beautiful. That wasn’t as simple as I thought it would be.

card:
  type: grid
  columns: 2
  square: false
card_param: cards
filter:
  template: >-
    {%- for s in states.light|selectattr('entity_id', 'in',
    area_entities(states.input_select.room_selection.state)) -%}
      {{
        {
          'type': 'custom:bubble-card',
          'card_type': 'button',
          'entity': s.entity_id,
          'button_type': 'slider',
          'show_last_changed': 'true',
          'show_state': 'true'
        }
      }},
    {%- endfor %}
sort:
  method: state
  numeric: true

I’ve got a card counting down our birthdays (it’s kinda thing within our household). got a very simple piece of code, but would like to see the whitespaces between the entities to be less.

Code:

type: custom:auto-entities
show_empty: false
card:
  title: Verjaardagen
  type: entities
entities: null
filter:
  include:
    - entity_id: sensor.verjaardag*
    - entity_id: sensor.anniversar*
sort:
  method: state
  ignore_case: false
  reverse: false
  numeric: true

image

I have searched this topic and the github page, but can’t seem to find it, or I don’t know what i’m looking for. Does anyone know how to do this?

All you need to do is target your card element with card_mod:

card:
  title: Verjaardagen
  type: entities
  card_mod:
    style: |
      #states > * {
        margin: 0 !important;
      }

You’ll need to have card_mod installed for this:

2 Likes

Try something like this:

type: custom:auto-entities
show_empty: false
card:
  type: custom:layout-card
  layout_type: grid
  layout:
    grid-template-columns: repeat(auto-fit, minmax(185px,1fr))
card_param: cards
filter:
  include:
    - domain: switch
      area: escritorio
      options:
        type: tile
        color: red
        show_entity_picture: false
        tap_action:
          action: toggle
        icon_tap_action:
          action: more-info
  exclude:
   

Change the filter for your needs.

I am trying to replicate sub_button and grids within grids. Any ideas how to do that with auto-entities?

Here’s an example:

square: false
type: grid
cards:
  - type: custom:bubble-card
    card_type: separator
    name: ROOM1
    sub_button:
      - name: '1'
        show_name: true
        icon: mdi:lightbulb
        show_background: false
        tap_action:
          action: none
  - type: custom:bubble-card
    card_type: separator
    name: ROOM2
  - square: false
    type: grid
    cards:
      - type: custom:bubble-card
        card_type: button
        entity: light.living_room_floor_lamp
      - type: custom:bubble-card
        card_type: button
        entity: light.living_room_floor_lamp
    columns: 2
columns: 1

Here is what I can do now (I have it all in 1 column, but it does populate every light assigned to each room)

type: custom:auto-entities
card:
  type: grid
  columns: 1
  square: false
card_param: cards
filter:
  template: |-
    {% for option in state_attr('input_select.room_selection','options') -%}
      {%- if states.light|selectattr('entity_id', 'in',area_entities(option))|list|count -%}
        {{
          {
            'type': 'custom:bubble-card',
            'card_type': 'separator',
            'name': option,
            'card_layout': 'normal',
          }
        }},{%- for s in states.light|selectattr('entity_id', 'in',
      area_entities(option)) -%}
        {{
          {
            'type': 'custom:bubble-card',
            'card_type': 'button',
            'entity': s.entity_id,
            'button_type': 'slider',
            'show_last_changed': 'true',
            'show_state': 'true'
          }
        }},
      {%- endfor %}{% endif -%}
    {%- endfor %}
view_layout:
  position: main