🔹 Auto-entities - Automatically fill cards with entities

Hi all!
I stack with as I supposed very trivial task: I have a number of vibration sensors and I want a card, showing me last 10 vibration detection events with timestamp.

I’ve got last 10 changed this way - it was easy, but it shows me last change to “not detected” - may be from “not available” or “unknown” and I have no idea how to get here only cases change from “not detected” to “detected” and how to get timestamps? Perfect solution would be: left part “friendly_name vibration detected”, right part - timestamp with date. Please help!

Worth a shot, thanks!
Doesn’t seem to accept it though, guess I’ll figure something out

Filtering by a state may be done by custom:logbook-card.
But actually for showing an only state (assuming “off”) with a customized “state” label may be achieved with an Entities card (instead of Logbook) + template-entity-row (for showing a timestamp instead of a state).

ChatGPT gave me several variants, using templates, none of them working:

type: custom:auto-entities
card:
  type: entities
  title: Вибрационные датчики с состоянием "Обнаружено"
filter:
  include:
    - domain: binary_sensor
      attributes:
        device_class: vibration
      state: "on"  # Показываем только датчики, которые находятся в состоянии "on" (вибрация обнаружена)
sort:
  method: last_changed
  reverse: true  # Сортировка по времени, начиная с последнего изменения
  count: 10  # Показываем только 10 последних датчиков

type: custom:auto-entities
card:
  type: entities
  title: Вибрационные датчики, которые срабатывали
filter:
  include:
    - domain: binary_sensor
      attributes:
        device_class: vibration
      state: "on"  # Показываем только датчики, которые когда-либо были в состоянии "on"
      options:
        type: custom:template
        template: >
          {% set last_changed = states[entity.entity_id].last_changed %}
          {% set time_diff = (as_timestamp(now()) - as_timestamp(last_changed)) %}
          {% if time_diff < 43200 %}
            {{ true }}
          {% else %}
            {{ false }}
          {% endif %}
      name: "{{ state_attr(entity.entity_id, 'friendly_name') }} - {{ states[entity.entity_id].last_changed.strftime('%Y-%m-%d %H:%M:%S') }}"
      secondary_info: last-changed
sort:
  method: last_changed
  reverse: true
  count: 10

Perhaps I shoul use “exclude”? Then what to exclude?

Now with help I came to this nice solution,

type: custom:auto-entities
card:
  type: logbook
  title: Вибрация
filter:
  include:
    - domain: binary_sensor
      attributes:
        device_class: vibration
sort:
  method: last_changed
  reverse: true

```/
but it gives all changes of state for all recorded time, but I need only  "from off to on" and for last 12 hrs.

note that from 2024.12 there is

the addition of entity/device/area etc filtering in logbook.

not sure if this helps, but maybe you can use that and save yourself the trouble of another costly card

You mean “target selector is included”, right?
The person says they need particular states in UI. That is why I think they need not a Logbook but a simple Entities card.

Never ever mention chatgpt here ))). Avoid posting code it usually proposes because in many cases it is wrong and distracts users.

This code

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - domain: binary_sensor
      attributes:
        device_class: vibration
      state: on
      options:
        secondary_info: last-changed
sort:
  method: last_changed
  reverse: true
  count: 10

shows only “vibration” sensors which are ON.

This code

type: custom:auto-entities
card:
  type: entities
filter:
  include:
    - domain: binary_sensor
      attributes:
        device_class: vibration
      state: "on"
      options:
        type: custom:template-entity-row
        state: >-
          {{states[config.entity].last_changed.strftime("%H:%M:%S %d/%m/%Y")}}
        secondary: >-
          {{state_translated(config.entity)}}
sort: ...

shows same but in a different format (“ON” as a secondary-info, last-changed as “state label”).

2 Likes

Need a little help with a filter template…

I’m using auto-entities to display running Sonos zones as follows:

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  template: |
    [
    {% for player in states.media_player 
         | selectattr('state', 'eq', 'playing') 
         | selectattr('attributes.group_members', 'defined')
         if player.attributes.group_members[0] == player.entity_id %} 
         {{
            {
              'type': 'custom:sonos-card',
              'entity' : player.entity_id,
              'startSection' : 'player',
              'entityId' : player.entity_id,
            }
         }},
    {% endfor %}
    ]

However, I’d like to also specify that Sonos only display the player section:

sections:
  - player

But this doesnt work:


         if player.attributes.group_members[0] == player.entity_id %} 
         {{
            {
              'type': 'custom:sonos-card',
              'entity' : player.entity_id,
              'startSection' : 'player',
              'entityId' : player.entity_id,
              'sections' : {
                '- player'
              }
            }
         }},

I can’t find the correct syntax so that the template parses properly - can someone advise please?

untested:

              'sections' : [
                {'player'},
              ]

Many thanks, but unfortunately not

I know that this works:

'hide': {
  'power': true
},

…but the attribute using a hyphen means the other one doesn’t work for some reason

'sections' : [player]

Still no unfortunately:

Just to keep track of what I’ve tried:

Normal YAML syntax (each setting optional)
sections: # see explanation further up
  - volumes
  - groups
  - grouping
  - media browser
  - player
  - queue

Tried:

Not working at all (breaks)
'sections': [
  {'- player',}
]
'sections' : [
  {'player'},
]
'sections' : {
  '- player'
}

No effect 
'sections' : [player]

Changes to 'volumes" section
'sections' : ['- player'],
'sections' : ['- player',]
'sections' : [player]
'sections' : [{'xxx': player}]

These are both proper presentations of a list.
Show this sonos card w/o auto-entities.

This is all you need for the card to display wthout auto-entities

type: custom:sonos-card
startSection: player
sections:
  - player
entityId: media_player.kitchen

Ok, I’m thinking that there might be a better way around this:

Card-Mod works on the card:


card_mod:
  style:
    sonos-player$: |
      .controls {
        display: none !important;
      }
    sonos-player$ sonos-player-header$ sonos-progress$: |
      .progress-bar {
        display: none !important;
      }
      .progress {
        display: none !important;
      }

Is there a way to add this in the Auto-Entities template instead?

Then this should work

            'sections': [player]

like here:

type: custom:auto-entities
card:
  type: vertical-stack
card_param: cards
filter:
  template: |-
    {% for SENSOR in states.sensor|
            selectattr('entity_id','search','sensor.xiaomi_cg')|
            selectattr('attributes.device_class','defined')|
            selectattr('attributes.device_class','==','temperature')|
            map(attribute='entity_id')|
            list -%}
       {{
          {
            'type': 'custom:mini-graph-card',
            'entities': [SENSOR]
          }
       }},  
       {%- endfor %}

I guess your “template” does not work even w/o “sections” at all, check it.

card-mod thread → 1st post → link at the bottom → auto-entities

The template works fine - it only breaks when I try adding Sections.

Think probably card-mod is a better solution, I’ll see if I can figure it out.

This happens if a user uses same names for different things - you got 2 same “player” words with different purpose.
Bad practice.
This should work:

'sections' : ['player']

This happens if a user uses same names for different things - you got 2 same “player” words with different purpose.

There we go - yes, renamed the loop variable

Added card-mod to hide remaining items

Many thankls

type: custom:auto-entities
show_empty: false
card:
  type: entities
filter:
  template: |
    [
    {% for theplayer in states.media_player 
         | selectattr('state', 'eq', 'playing') 
         | selectattr('attributes.group_members', 'defined')
         if theplayer.attributes.group_members[0] == theplayer.entity_id %} 
         {{
            {
              'type': 'custom:sonos-card',
              'startSection' : 'player',
              'entityId' : theplayer.entity_id,
              'sections' : ['player'],
              "card_mod": {
                "style": {
                  "sonos-player$": ".controls { display: none !important;}",
                  "sonos-player$ sonos-player-header$ sonos-progress$": "|"
                  ".progress-bar { display: none !important;}" 
                  ".progress { display: none !important;}",    
                }          
              },
            }
         }},
    {% endfor %}
    ]