šŸ”¹ Auto-entities - Automatically fill cards with entities

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


Could you add a way to template the name? Itā€™s very very often that when I have a group of entities that requires using this custom card, that they all share something in the name that I want to take out.

1 Like

Just wanted to share this little thing i came up with:

Auto entities lists all my areas and creates a area-card with temp, humidity and luxā€¦ still working on how to get a different icon for each area, dont wanna rename my rooms, but could be a possibilityā€¦

type: custom:auto-entities
filter:
  template: |
    [ {% for omrade in areas()|sort %}
      {{ 
        {
        'type': 'custom:streamline-card', 
        'template': 'klimakort',
        'variables': [
        {'entry': omrade}
          ]
        } 
      }} 
      , 
    {% endfor %}  ]
  include: []
  exclude: []
card:
  square: false
  type: grid
  columns: 2
show_empty: true
card_param: cards
sort: {}

and a decluttering card (streamline-cardā€¦ because that has a gui-editor lol)

streamline_templates:
  klimakort:
    card:
      type: custom:mushroom-template-card
      primary: '{{area_name(''[[entry]]'') | title }}'
      secondary: >-
        {% set temp = (area_entities('[[entry]]') | select('is_state_attr',
        'device_class', 'temperature') | map('states') | select('is_number') |
        map('float') | list or [0]) | average | round(1) %}

        {% set hum = (area_entities('[[entry]]') | select('is_state_attr',
        'device_class', 'humidity') | map('states') | select('is_number') |
        map('float') | list or [0]) | average %}

        {% set lux = (area_entities('[[entry]]') | select('is_state_attr',
        'device_class', 'illuminance') | map('states') | select('is_number') |
        map('float') | list or [0]) | average | round(1) %}

        {{ [ ('šŸŒ”ļø' ~ temp|int ~ 'Ā°C') if temp, ('šŸ’§' ~ hum | int ~ '%') if hum,
        ('šŸ”†' ~ lux | int ~ 'lx') if lux] | select() | join(' ') }}
      icon: hue:room-office
      entity: '[[entry]]'
      fill_container: true
      tap_action:
        action: navigate
        navigation_path: '#popup-[[entry]]'
      layout: vertical

2 Likes