🔹 Auto-entities - Automatically fill cards with entities

You need to use an attribute name which is displayed in Dev tools → set state.

forgot my brain fluke in the deleted post, so entangled in getting custom_template to work, I forgot we are seeking for entity_id’s only in auro-entities :wink:

why cant I do this:

    - type: custom:auto-entities
      card:
        type: entities
      filter:
        include:
          - integration: nederlandse_spoorwegen

while I can do

{{integration_entities('nederlandse_spoorwegen')}}

and:

      filter:
        template: >
          {{integration_entities('nederlandse_spoorwegen')}}

this is not a config_entry integration, still in yaml, but since the dev tools template shows fine, I would have expected auto-entities to list these too?

optimally, I would even set option on the entities

    - type: custom:auto-entities
      card:
        type: entities
      filter:
        include:
          - integration: nederlandse_spoorwegen
            options:
              type: custom:template-entity-row
              secondary: >
                Aankomst: {{state_attr(config.entity,'arrival_time_planned')}}

but first need to get the entities…

using a template filter is somewhat complexer:

      filter:
        template: >
          {%- for train in expand(integration_entities('nederlandse_spoorwegen')) -%}
          {{
            {
              'type': 'custom:template-entity-row',
              'entity': train.entity_id,
              'secondary': 'Aankomst: ' + train.attributes.arrival_time_planned
            }
          }},
          {%- endfor -%}
2 Likes

I know this is a year and a half old. But, I am currently in the same situation.

I have a need to pass attribute information of the object being passed to a decluttering card. I have been searching for days.

Were you able to get an answer on this? ID might be enough and I could possibly work with that. But, if I could go further here, it would simplify the other end :slight_smile:

1 Like

Hey Guys,
I am struggeling with the style of Mushroom Chips within the auto-entities card. Does anyone have any pointers as to how I can style the chips card? I’ve tried any combination of classes that I can think of (and Inspect in Chrome shows me).

Here is the code:

  - type: custom:auto-entities
    show_empty: false
    card:
      type: custom:mushroom-chips-card
      alignment: center
      card_mod:
        style: |
          ha-card {
          margin-top: 1px;
          margin-bottom: 1px;
          border: 0px !important;
          background: none !important;
          }
    card_param: chips
    sort: name
    filter:
      include:
        - options:
            type: light

Outcome is:
image

What I want it to look like:
image

What’s odd to me is that the style in working for margin but not for border och background. Mushroom Chips that are not within the auto-entities card I can style with the css above.

Thanks for all your ideas!
Rob

I have this code:

- type: 'custom:auto-entities'

    card:
      type: entities
    filter:
      include:
        - entity_id: sensor.recycleapp_vuilnis_restafval
        - entity_id: sensor.recycleapp_vuilnis_papier
        - entity_id: sensor.recycleapp_vuilnis_pmd
          options:
            type: custom:button-card
    sort:
      method: state

This is my result

How can I change pmd so it is like the other 2 ?

Hi @Ildar_Gabdullin,

i hope its ok to ping you. I see you are very advanced when it comes to templating.
I want to have an auto entities card which shows all turned on lights grouped by the area.

This is what i have so far:

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  template: |-
    {% set filter = ['light.light'] %}
    {% set area_filter = ['area'] %}
    {% set areas = states
      | selectattr('attributes.device_class', 'defined')
      | map(attribute='entity_id')
      | map('area_id') | unique | reject('none') | list %}
    {%- for area in areas -%}  
        {%- if area not in area_filter -%}{{
        { 'type': 'custom:mushroom-title-card',
          'title': area, }}},
      {%- endif -%}    
    {% set lights = expand(states.light) 
      | selectattr('state', 'eq', 'on')
      | selectattr('entity_id', 'in', area_entities(area)) 
      | rejectattr('entity_id', 'in',filter)       
      |list -%}   
    {%- for light in lights -%}
      {{{'type': 'custom:mushroom-light-card',
          'entity': light.entity_id,
          'use_light_color': 'true',
          'show_brightness_control': 'true',
          'show_color_control': 'true',
          'collapsible_controls': 'true',
          'show_color_temp_control': 'true', 
        }}},     
    {%- endfor %} {%- endfor %}

Problem with this is that the mushroom title card is always shown even when no light is turned on there. Of course because its not the same loop. i thought of creating some vertical stacks which contains the light cards. with this i could have a grouped card with titles which will hide when no light is turned on.

i tried many things and searched the whole topic here but couldnt find anything. is there a way to have nested cards in this templates?

big thanks!

Hi! Actually pinging is not allowed here in general.
Anyway, the template you created before the “{%- for light in lights -%}” part you may check in “Dev tools → Template”; about the “mushroom” part I cannot say a thing - never used them (((

What does that yield when no lights are on? Change that so it returns nothing if no lights are on. You are saying:

  1. Give me the areas and create a title card
  2. Now give me all lights “on” in that area and show them.

Of course areas that have no lights on will have titles shown.

Perhaps a better logic would be:

  1. Create a list of all lights on and their areas
  2. Create something for each unique area and show the lights on

The script itself works. But the problem is that I have two loops. One for areas and one for lights.

I think easiest way to fix this would be to have only one loop which gives me a vertical-stack-in-card which has other cards in it (doesn’t need to be a custom card like mushroom)

But I don’t know what the syntax is. I saw some posts here which have [{ in it to put other cards behind the stack card.

Any idea?

treid a bit and came to something new (still not perfect):

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  template: |-
    {% set filter = ['light.steckdose_wohnzimmer','light.hue_smart_plug_1'] %}
    {% set area_filter = ['wohnung','steckdose_wohnzimmer'] %}
    {% set areas = states
      | selectattr('attributes.device_class', 'defined')
      | map(attribute='entity_id')
      | map('area_id') | unique | reject('none') | list %}  
    {%- for area in areas -%}
    {% set lights = states.light
      | selectattr('state', 'eq', 'on')
      | selectattr('entity_id', 'in', area_entities(area)) 
      | rejectattr('entity_id', 'in',filter)     
      | list %}   
    {%- for light in lights -%}
       {{{ 'type': 'custom:vertical-stack-in-card',
          'title': area,
          'cards': 
            [ 
            { 'type': 'custom:mushroom-light-card',
              'entity': light.entity_id }]}}},   
    {%- endfor %} {%- endfor %}

i loop through all lights now so every light card is wrapped in its own vertical-stack-in-card. but i want one vertical-stack-card which shows the area title and then all assigned lights to it.
i dont know how to combine these two loops in this section:

       {{{ 'type': 'custom:vertical-stack-in-card',
          'title': area,
          'cards': 
            [ 
            { 'type': 'custom:mushroom-light-card',
              'entity': light.entity_id }]}}},   

After trying almost the whole day i think i got a step closer.
This is the code.

type: custom:auto-entities
title: test
show_empty: false
card:
  type: entities
filter:
  template: >-
    {% set areas = states.light | selectattr('state','eq', 'on')
    |map(attribute='entity_id') |map('area_name') | unique | reject('none')  
    | list %}   
    {%- for area in areas -%} 
       {{{ 'type': 'custom:auto-entities',
           'card':              
            {  'type': 'entities',
               'title': area},         
           'filter':            
            {  'template': "
    {% set lights = states.light
      | selectattr('state', 'eq', 'on')
      | selectattr('entity_id', 'in',area_entities('living-room'))     
      |list -%}  
    {%- for light in lights -%}
    {{{ 'type': 'custom:mushroom-light-card',
        'entity': light.entity_id }}}, 
     {%- endfor %} " }, }}},  {%- endfor %} 

Please see the lines

    {% set lights = states.light
      | selectattr('state', 'eq', 'on')
      | selectattr('entity_id', 'in',area_entities('living-room'))     
      |list -%}  

Here i have to define the are by hand. This is not what i want but

    {% set lights = states.light
      | selectattr('state', 'eq', 'on')
      | selectattr('entity_id', 'in', area_entities(area))    
      | list %}

also does not work because the variable area is empty. It seems that the variable isnt working in the second template of auto entities. any help?

Isn’t that ‘living-room’ just the variable area?

It should be. But only the string ‘living-room’ gives me output. Using the variable area isnt working.

Ok. I get it now. I have a sample like this I can get to in an hour or so. You need to probably loop and store the area list in a namespaced variable to make it available.

Similar to this python - Namespace variables and loops in jinja - Stack Overflow

would be great if you can share it.
also i think i merged all of my approaches and im very close to the perfect solution.
i loop through all areas which gives me a vertical-stack-in-card which has also an auto entity card inside.

with this the title is hidden when no entity is on. only problem is that i want to use the template filter (to get rid of entities i dont want) in the second one but syntax is breaking my brain right now.

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  template: |-
    {% set area_filter = ['wohnung','Steckdose Wohnzimmer','Allgemein'] %}
    {% set areas = states.light
      | selectattr('state','eq', 'on')
      | map(attribute='entity_id')
      | map('area_name') | unique | reject('none') | rejectattr('entity_id','in', area_filter) | list %}
    {%- for area in areas -%}  
        {%- if area not in area_filter -%}
           {{{ 'type': 'custom:vertical-stack-in-card',
          'title': area,
          'cards': 
            [{                     
               'type': 'custom:auto-entities',
               'show_empty' : 'false',
               'card' : 
               { 'type': 'entities'},
               'filter':
               { 'include': 
               [{ 'domain' : 'light', 
                  'state' : 'on',
                   'area' : area,
                   'options':
                   {'type' : 'custom:mushroom-light-card'}}],
                 'exclude': 
               [{  'name' : 'Schreibtisch 1' }]},            
               }]}}}, 
      {%- endif -%} 
      {%- endfor %}

this is what it looks for now:

I am missing it, I do not see “Schreibtisch 1” in the image … wasn;t it removed? Then again, I would think you would need to move that logic to the top level because what if the only thing on in some area is what you filter out later? Possibly this is handled by show_empty ?

Yes „Schreibtisch1“ is excluded. But it seems that I can’t put in multiple entities in the exclude tag.
I want to set an variable which has all entities in it which should be included like the area filter above.

Need to figure it out. Syntax drives me crazy.

EDIT: got everything in template working. now i only need to add the namespace beacuse " | selectattr(‘entity_id’, ‘in’, area_entities(area))" has no value… can you help me with that?

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  template: >-
    {% set area_filter = ['wohnung','Steckdose Wohnzimmer','Allgemein'] %}
    {% set areas = states.light
      | selectattr('state','eq', 'on')
      | map(attribute='entity_id')
      | map('area_name') | unique | reject('none') | rejectattr('entity_id','in', area_filter) | list %}
    {%- for area in areas -%}  
        {%- if area not in area_filter -%}
           {{{ 'type': 'custom:vertical-stack-in-card',
          'title': 'Test ' + area,
          'cards': 
            [{ 'type': 'custom:auto-entities',
               'show_empty' : 'false',
               'card' : 
               { 'type': 'entities'},
               'filter':
               { 'template': 
     "{% set filter = ['light.steckdose_wohnzimmer','light.hue_smart_plug_1'] %}  
      {% set lights = states.light
      | selectattr('state', 'eq', 'on') 
      | selectattr('entity_id', 'in', area_entities(area)) 
      | rejectattr('entity_id', 'in',filter) |list -%}   
      {%- for light in lights -%}
        {{{'type': 'custom:mushroom-light-card',
            'entity': light.entity_id,
            'use_light_color': 'true',
            'show_brightness_control': 'true',
            'show_color_control': 'true',
            'collapsible_controls': 'true',
            'show_color_temp_control': 'true', 
        }}},     
       {%- endfor %}"},}]}}}, 
      {%- endif -%} {%- endfor %}

I see you solved this, as an option you could also do like below. The YAML syntax to exclude multiple things by name would be something like this below. You use multiple name’s with numbers:

      exclude:
        - name 1: '*LEDs*'
        - area: 'Internet'
        - name 2: 'Keypad*'
        - name 3: 'FanLinc*'
        - name 4: '*shuffle switch'
        - name 5: 'Deck TV*'
        - name 6: '*normalization*'
        - name 7: 'Home Assistant*'
        - name 8: '*Roborock*'

But I am not sure you can combine template and exclude. If I read this, it should operate on template entities and work.

auto-entities creates a list of entities by:

  1. Including every entity given in entities: (this allow nesting of auto-entitiesif you’d want to do that for some reason…)
  2. Include every entity listed in a filter.template evaluation
  3. Include all entities that matches ALL options of ANY filter in the filter.include section. The same entity may be included several times by different filters.
  4. Remove all entities that matches ALL options on ANY filter in the filter.exclude section.

Just for your info i found out how to add multiple exclusions in the template section.

This is my completed card:

type: custom:mod-card
card_mod:
  style:
    .: |
      ha-card {
        --ha-card-border-width: 0;
        --ha-card-background: none; } 
card:
  type: custom:auto-entities
  show_empty: false
  card:
    type: entities
  filter:
    template: |-
      {% set area_filter = ['wohnung','Steckdose Wohnzimmer','Allgemein'] %}
      {% set filters = ['Schreibtisch 1','Schreibtisch 2'] %}    
      {% set areas = states.light
        | selectattr('state','eq', 'on')
        | map(attribute='entity_id')
        | map('area_name') | unique | reject('none') | rejectattr('entity_id','in', area_filter) | list %}
      {%- for area in areas -%}  
          {%- if area not in area_filter -%}
             {{{ 'type': 'custom:vertical-stack-in-card',
            'title': area,
            'cards': 
              [{ 'type': 'custom:auto-entities',
                 'show_empty' : 'false',
                 'card' : 
                 { 'type': 'entities'},
                 'filter':
                 { 'include': 
                 [{ 'domain' : 'light', 
                    'state' : 'on',
                     'area' : area,
                     'options':
                     {'type' : 'custom:mushroom-light-card',
                      'use_light_color' : 'true', 
                      'show_brightness_control': 'true',
                      'show_color_control': 'true',
                      'collapsible_controls': 'true',
                      'show_color_temp_control': 'true',}}],
                   'exclude':                
                 [{  'name' : 'Schreibtisch 1' },
                 {  'name' : 'Büro' },
                 {  'name' : 'Schreibtisch 2' },  
                 {  'name' : 'Schreibtisch 3' }],},}]}}}, 
        {%- endif -%} {%- endfor %} 

which leads to:

While testing i found out that i cant use variables which are assigned in the first auto entities template in the second auto entities template. I think the " is a break here but without " it does not work at all.
When i assign new variables after the " it works. So i think even namespace would not work for this.

Maybe there is also an easier way to achieve what i want.

I found another template which could work.

{% set filter = ['light.steckdose_wohnzimmer','light.hue_smart_plug_1'] %}
{% set ns = namespace(areas=[], lights= expand(
  states.light|selectattr('name','ne','All'))| selectattr("state","eq","on") | rejectattr('entity_id', 'in', filter)|  list) %}
{% for elm in ns.lights if not area_id(elm.entity_id) in ns.areas%}
   {% set ns.areas = ns.areas + [area_id(elm.entity_id)] %}
{%- endfor %}
{% for area in ns.areas -%}
Area:{{ area_name(area) }}
{% for elm in area_entities(area) if elm in ns.lights|map(attribute="entity_id") -%}
{{ state_attr(elm, "friendly_name") }},
{%- endfor %}
{%- endfor %}    

which give me:

Area:None
Area:Wohnzimmer
WLED,
Area:Küche
Küche Licht,
Area:Büro
Schreibtisch 1,Schreibtisch 2,Schreibtisch 3,Büro,Schreibtisch,Büro Schrank Licht,
1 Like

You second template is doing exact that, using namespaces.

{% set ns = namespace(areas=[], lights= expand(
  states.light|selectattr('name','ne','All'))| selectattr("state","eq","on") | rejectattr('entity_id', 'in', filter)|  list) %}

Variables defined inside a loop are not carried outside of the loop. By using namespaces ns.areas is still defined outside the endfor and hence can be used.

1 Like