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

How can you say if some device is offline?

As mentioned, I filter entities by last_seen: > 30m ago and want to convert a (long) list of entities to a (shorter) list of devices these entities belong to.

None of cards show a DEVICE. But you may try to get a device name for some entities belonging to some devices and then print these names in Markdown. How to get a device name - reed docs for Templating.

Maybe someone else has an idea how to use auto-entities to display the (deduplicated) device names.

So, getting a device name from an entity is easy.

{{ device_attr(entity, 'name_by_user') if device_attr(entity, 'name_by_user') else device_attr(entity, 'name') }}

If you named the device, this gives that otherwise, it gives the device name given by HA. But, for devices not named by you, the ā€˜nameā€™ seems to be the same for the same make and model.

For example, all of my zigbee door sensors are called ā€˜LUMI lumi.sensor_magnet.aq2ā€™ if not named. So, if you have two of them that are not named, they would be grouped together. So, you need to use ā€˜idā€™ for uniqueness.

Now, to get unique by id. This is specifically all of my zigbee entities.

{%- set ns = namespace(devices=[]) -%}
{%- for entity in integration_entities('zha') -%}
  {%- set ns.devices = ns.devices + [{"device_id": device_attr(entity, 'id'), "device_name": device_attr(entity, 'name_by_user') if device_attr(entity, 'name_by_user') else device_attr(entity, 'name'), "area_id": device_attr(entity, 'area_id')}] -%}
{%- endfor -%}
{%- for device in ns.devices|unique(attribute="device_id") %}
{{ device }}
{%- endfor -%}

I added ā€˜area_idā€™ to help differentiate devices that are not named by you.

But, you donā€™t really need these attributes here. You could just use the ā€˜idā€™ and in the second loop, use that ā€˜idā€™ to get any attributes you need for your output using device_attr.

What I donā€™t understand is where you are going to get ā€˜last_seenā€™. Also, know that auto_entities expects entities. Not devices. So, youā€™d probably need to do this in markdown anyway.

Thanks a lot, that helps ā€“ at least I hope it does. Iā€™ve named all devices, so thatā€™s not an issue, but itā€™s great that you also added the id.

Thereā€™s a thread from December 2022 where someone is trying to use auto-entities to list unavailable devices. He got rather far, but didnā€™t solve all issues. But at least he listed devices in the auto_entities entities card layout.

As for my naĆÆve attempt, I thought Iā€™d filter all entities for last_seen: > 30m ago and store their last-updated info, then replace all entity names with device_attr(entity, 'name_by_user'), remove duplicates from the list and use that list to feed the auto-entities list, which could then even sort by last-updated.

The goal behind this effort is to keep track of (passive) devices that might have a bad connection and therefore most of the updates they broadcast donā€™t make it to the server. I would then know where I should add another thread/zigbee router or change some placement ā€“ or replace a battery, even if itā€™s at 100%.

Again:

  • auto-entities cannot show devices since none card can show it (all cards - except markdown & stacks - are associated with some entity, not a device)
  • use Markdown and HA templating to generate a list of devices, assoc with entties which last-change>xxxx
1 Like

In the thread you mention they are using a sensor to create a list of unavailable ā€˜entitiesā€™ out of devices.

You cannot do it straight from devices as discussed here like @Ildar_Gabdullin has posted.

So, what I have discussed above would go into the sensor. Then, use the sensor in the auto_entities.

I am unsure of why they wanted to put ā€˜unavailableā€™ like a status in a list of unavailable devices. They are all unavailable.

Also, use this in Developer Tools | Template and see that, at least in my zha devices, there are not ā€˜last-updatedā€™ attributes.

Certainly play with others and see what you can find. You might need to look for unavailable like in the post you linked. Not sure you will get the > 30 minutes without a crapload of timers. Just saying:

{%- for entity in integration_entities('zha') %}
{{ entity }}
  {%- for attr in states[entity].attributes %}
  {{ attr }}-{{ state_attr(entity, attr) }}
  {%- endfor -%}
{%- endfor -%}

Any chance you could share your final code? I love where you have gone with this

Hi, I like your auto-entities card a lot. Is there a way to get tap_action : toggle working? That would improve usability a lot imho.

So, I donā€™t think you are understanding auto-entities. Auto-entities is not actually a ā€˜cardā€™.

Auto-entities is used to select a ā€˜groupā€™ of entities to build cards. It is the actual cards that need the tap_action. Not auto-entities.

So, yes, auto-entities can build cards that have tap actions.

1 Like

Suggest you to describe your use case with that ā€œtap-actionā€ issue. Probably it is possible by a target cardā€™s settings.

1 Like

I would echo this - please could you share the finalised code. Excellent usage of this card and exactly what Iā€™m trying to achieve.

I have several cards filled with auto-entities. With some toggle did not work. I went through them again because with some it worked. Turns out that on a type: glance card I had to put
options:
tap_action:
action: toggle
In the include section to get it to work. On other cards it is put in the entity: section but since with auto-entities you do not specify entities I did not know how to get it working. Now it works.

List of area cards:

  - type: custom:auto-entities
    card:
      type: grid
      columns: 2
      square: false
    card_param: cards
    filter:
      template: |-
        {% for AREA in areas() -%}
          {{
            {
              'type': 'area',
              'area': AREA
            }
          }},
        {%- endfor %}


other examples

1 Like

Iā€™m seeking some help ā€¦ I have a fairly complex card. I am using a vertical stack card with a series of conditional cards after a selector card - the selector picks the room, the conditional card then populates all the scenes for that room. The room selection and conditionally picking the auto entity card works great but as soon as I try to use any custom card in place of the build-in ā€˜entitiesā€™ card it seems to fall apart.

Hereā€™s the YAML Iā€™ve got so far, with no chips card:

type: vertical-stack
cards:
  - type: conditional
    conditions:
      - condition: state
        entity: input_select.room_selector
        state: Living Room
    card:
      type: custom:auto-entities
      filter:
        include:
          - domain: scene
            area: Living Room
        exclude: []
      card:
        type: entities

On my phone, it looks something like this

Anybody been able to make a setup like this work with the auto-entities card putting stuff into a mushroom chips card?

Assume you need to show entities associated with some Area.
And this area is selected by ā€œinput_selectā€.
Should be:

  1. Vertical stack: Entities card with input_select + Entities card filled by auto-entities.
  2. Auto-entities with a ā€œtemplateā€ filter (not ā€œincludeā€, ā€œexcludeā€) - learn docs & examples.
  3. This template filter takes a state of ā€œinput_selectā€ and uses as an area.
1 Like

please have a look what I am not seeingā€¦ trying to add a card_mod to a template filter is always a bit tricky, but somehow I cant find the error, and the output in dev template seems ok tooā€¦

    filter:
      template: >
        [{% set threshold = states('input_number.power_threshold')|float(0) %}
        {% set ns = namespace(above_threshold=[]) %}
        {% for s in states.sensor
         |rejectattr('entity_id','search','solaredge')
         |rejectattr('entity_id','eq','sensor.alle_schakelaars_samen_actueel')
         |selectattr('entity_id','search','_actueel|_current_power')
         if s.state|float(0) > threshold %}
           {{
              {
                'entity': s.entity_id,
                'options':
                  {'card_mod': '!secret power_color'}
              }
            }},
          {%- endfor %}]

does create the correct list of filtered entities and shows those, but the card_mod option is not applied, meaning the icons arenā€™t colored as expected.

if I do a single entity like this:

  - entity: sensor.vijverpomp_links_actueel
    card_mod: !secret power_color

it does work properly ( icon is colored following state), and attaches the secret which I saved as

power_color:
  style: |
    :host {
      --paper-item-icon-color:
        {% set state = states(config.entity)|int(-5) %}
        {% if state > 2000 %} purple
        {% elif state > 1000 %} maroon
        {% elif state > 450 %} darkred
        {% elif state > 300 %} firebrick
        {% elif state > 250 %} crimson
        {% elif state > 150 %} darkorange
        {% elif state > 70 %} orange
        {% elif state > 10 %} lightsalmon
        {% elif state > 0 %} gold
        {% else %} var(--no-power-color)
        {% endif %}
    }

not sure what to check next. is it a dom path that changed because of the auto-entities, and should I change the mod itself? (Inspector seems to show the identical pathā€¦)

or, is it a syntax problem after all, and should I change the template filter.

the result is a list alright, as seen here:

and the output is like this:

[
  {
    "entity": "sensor.patchboard_zolder_actueel",
    "options": {
      "card_mod": "!secret power_color"
    }
  },
  {
    "entity": "sensor.vijverpomp_links_actueel",
    "options": {
      "card_mod": "!secret power_color"
    }
  }
]

tried it like this too:

        filter:
          include:
            - entity_id: sensor.*_actueel
               options:
                 card_mod: !secret power_color

which also works correctly showing all nicely colored icons.
So, my first bet would be the template filter has a syntax issueā€¦

would appreciate a look, thanks

btw nevermind the namespace, it is a remnant of my other template, and does nothing in this config.

update

some progressā€¦ I should not use the option: in the template filterā€¦

I can. are things happen when spelling the mod out verbosely (and not inserting the secret)

      template: >
        [{% set threshold = states('input_number.power_threshold')|float(0) %}
        {% for s in states.sensor
         |rejectattr('entity_id','search','solaredge')
         |rejectattr('entity_id','eq','sensor.alle_schakelaars_samen_actueel')
         |selectattr('entity_id','search','_actueel|_current_power')
         if s.state|float(0) > threshold %}
           {{
              {
                'entity': s.entity_id,
                'card_mod':
                  {'style':
                      ':host {
                        --paper-item-icon-color:
                          {% set state = states(config.entity)|int(-5) %}
                          {% if state > 2000 %} purple
                          {% elif state > 1000 %} maroon
                          {% elif state > 450 %} darkred
                          {% elif state > 300 %} firebrick
                          {% elif state > 250 %} crimson
                          {% elif state > 150 %} darkorange
                          {% elif state > 70 %} orange
                          {% elif state > 10 %} lightsalmon
                          {% elif state > 0 %} gold
                          {% else %} var(--no-power-color)
                          {% endif %}
                      }'
                  }
              }
           }},
          {%- endfor %}]

dont think we an import the !secret inside the template filter hereā€¦

So, this has been burning my ass. Because this should be doable.

This can actually be done in a markdown card without using a sensor.

views:
  - title: Test Unavailable
    path: test_unavailable
    subview: false
    badges: []
    cards:
      - type: markdown
        content: >
          |Device

          |-

          {%- set ns = namespace(devices=[]) -%}

          {%- set entities = states | selectattr('state', 'in', ['unavailable', 'none']) | map(attribute='entity_id') | list -%}

          {%- for entity in entities -%}
            {%- set ns.devices = ns.devices + [{"device_id": device_attr(entity, 'id')}] -%}
          {%- endfor -%}

          {%- for device in ns.devices | unique(attribute="device_id") %}
            | {{ device_attr(device.device_id, 'name_by_user') if device_attr(device.device_id, 'name_by_user') else device_attr(device.device_id, 'name') }}
          {%- endfor -%}

But, that shows devices that have any entity that is unavailable. And, that doesnā€™t really fit the question. My phone shows unavailable because Android Auto has an entity (car_ev_connector_type) that is unavailable since my truck is not an EV.

You can exclude certain devices. But, what if they really go unavailable? You can exclude certain entities though if you want.

This one will show a list of devices and the entities that are not available.

views:
  - title: Test Unavailable
    path: test_unavailable
    subview: false
    badges: []
    cards:
      - type: markdown
        content: >
          |Device|Entity

          |-|-

          {%- set ns = namespace(devices=[]) -%}

          {%- set entities = states | selectattr('state', 'in', ['unavailable', 'none']) | map(attribute='entity_id') | list -%}

          {%- for entity in entities -%}
            {%- set ns.devices = ns.devices + [{"unique_id": device_attr(entity, 'id') ~ entity, "device_id": device_attr(entity, 'id'), "entity_id": entity}] -%}
          {%- endfor -%}

          {%- for device in ns.devices | unique(attribute="unique_id") %}
            {%- set device_name = device_attr(device.device_id, 'name_by_user') if device_attr(device.device_id, 'name_by_user') else device_attr(device.device_id, 'name') -%}
            {%- if not loop.first -%}
              {%- if loop.previtem.device_id == device.device_id -%}
                {%- set device_name = '' -%}
              {%- endif -%}
            {%- endif %}
            | {{ device_name }} | {{ device.entity_id }}
          {%- endfor -%}

If you want something other than a table in a markdown card, you could certainly use auto-entities, build a list of cards in the template filling in the data wanted, and use:

card_param: cards