thomasloven / lovelace-auto-entities

🔹Automatically populate the entities-list of lovelace cards
MIT License
1.2k stars 111 forks source link

Expand this.entity_id as well in the attributes section #325

Closed rklueber closed 1 year ago

rklueber commented 1 year ago

Having the possibility to reference the current entity_id in the options section is helpfull for service calls.

Is there a possibility to allow the replacement of the "this.entity_id" not only in options, but as well in the attributes section?

I am looking for an easy solution to show only the sonos media_players which play and arear not in a group ... or play and are master in their group.

This is the jinja2 code to identify those players. I would like to use auto-entities to get those done.

{% set ns = namespace(players = []) %}
{% for player in states.media_player 
     | selectattr('state', 'eq', 'playing') 
     | selectattr('attributes.group_members', 'defined')
     if player.attributes.group_members[0] == player.entity_id %} 
  {% set ns.players = ns.players + [player.entity_id] %}
{% endfor %}
{{ ns.players }}

I tried via the template: route with little success as I did not be able to use the mini-media-player card with auto-entities.

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

I tried a different approach ... but fail bc of this.entity_id is not being expanded in the attributes section. This is what I would like to achieve.

type: custom:auto-entities
card:
  type: grid
  columns: 1
  square: false
card_param: cards
filter:
  include:
    - entity_id: "/media_player\\.(ralf|kuche|bad)$/"
      state: playing
      attributes:
        group_members:0: this.entity_id
      options:
        type: custom:mini-media-player
        artwork: full-cover
ildar170975 commented 1 year ago

allow the replacement of the "this.entity_id" not only in options, but as well in the attributes section?

The attributes section supports a list of "attribute + value" options, the this.entity_id is meaningless here (templates are not supported for values). Your only solution could be a using the template option. Go to Community auto-entities thread for examples.

The issue is neither a bug nor a FR. Better to ask these questions in that thread.