Help with data_template using dynamic entity names question

Hi, you suggestions to others where of great help to me.

I’ve tried a bunch of code to be able to feed to the option image a variable instead of a url path

its supposed to be be like this:

image: "image/path"

I have a template variable that saves the changing url for the media for Sonos

also I can get the url from media_player.living_room.attributes.entity_picture

sensor.sonos_media

with a state :
/api/media_player_proxy/media_player.living_room?token=1124256fa1cae8e1be0c476c2ce881aaf523ff4cfeaba2b1866759033a944f8b&cache=d05f973fec3e2c7b

The token changes every time there is a new image cover

How do I get that url into the image option?

thanks in advance

ok let me elaborate:

-I have an entity called media_player.living_room_2

-One of its attributes is entity_picture

-The content of that attribute is a url with a token that changes depending on the art cover

This works all fine and dandy in the media-control type card, because it pulls the url on its own. But im setting up another Lovelace with HomeKit styled cards an the code goes as follows:

 - type: 'custom:mini-media-player'
                sync_volume: true
                entity: media_player.living_room
                name: Music
                image: >-
                  {{ state(media_player.living_room.attributes.entity_picture)
                  }}
                speaker_group:
                  platform: sonos
                  show_group_count: true
                  artwork: full-cover
                  sync_volume: true
                  expanded: false
                  entities:
                    - entity_id: media_player.Living_Room
                      name: Sonos Play
                    - entity_id: media_player.Living_Room_2
                      name: Sonos SL
                      sync_volume: true

there in the image option lies the issue, its expecting a string and it works when its provided

What I want is to pass the attribute “entity_picture” instead of the string

Ihope this is clearer

The correct method to get a state out of an entity is states(), however you’re trying to get an attribute, which is state_attr().

Also, you need to wrap the entity_id in quotes as well as the attribute.

Thats the thing Petro, I have done all that and still nothing, check this out:

I’ve correctly templated this data into a sensor entity called sensor.sonos_media. And I can see its taking the url:

 sonos_media:
        friendly_name: "sonos_media"
        value_template: "{{ (state_attr('media_player.living_room', 'entity_picture')) }}"

I’ve also passed it the way I’ve stated and the way you have stated (the correct way), I’ve ALSO tried to pass the sensor “sensor.sonos_media” that has the url

like this

image: {{ states('sensor.sonos_media') }}

I get

                image:
                  '[object Object]': null

but it can’t be null because im seeing its value right nou in developer tools!

Screen Shot 2021-01-25 at 9.59.53 AM

This is the card:

Screen Shot 2021-01-25 at 10.01.08 AM

And when I get long press to go to the more info pop up, the image I want is there!!! OMG and can’t get it out!!!

Screen Shot 2021-01-25 at 10.01.13 AM

/api/media_player_proxy/… is not the full path and that’s most likely your issue.

When I hard code the value of the attribute or the templated sensor (see link) the picture works

/api/media_player_proxy/media_player.living_room?token=1124256fa1cae8e1be0c476c2ce881aaf523ff4cfeaba2b1866759033a944f8b&cache=d05f973fec3e2c7b

See picture when I hard code url

Screen Shot 2021-01-25 at 10.09.48 AM

put the template in double quotes or use the multiline indicator > and move the template to the next line.

This
Screen Shot 2021-01-25 at 10.22.37 AM

turns into this after I save

Screen Shot 2021-01-25 at 10.23.20 AM

still no image

Please reread this

I don’t mean to be dense but even tho I didn’t provide a picture I also used the multilane indicator and got the same result.

when you say put the template on double quotes, did I do that right?
what would be the notation when I use the multiline indicator?

Thanks a lot for the help

No this is what a template wrapped in double quotes would look like:

image: "{{ states('sensor.sonos_media') }}"

I provided it already

image: >
  {{ states('sensor.sonos_media') }}

Double quotes result in this

Screen Shot 2021-01-25 at 10.32.03 AM

Doesn’t work

(edit) Multiline turns into something else (sorry had an indentation issue before):

Screen Shot 2021-01-25 at 10.41.24 AM

What doesn’t work about the first image?

In the multiline indicator, the template needs to be indented by 2 spaces from image, like my example.

Result for both is no image

Screen Shot 2021-01-25 at 10.42.26 AM

instead of using that template to grab the state, just use the state_attr template in it instead, see if that works.

Ok I don’t know if this plays a role in the behavior im seeing, but all of this is wrapped in a fullscreen card add-on called home kit that makes rows and columns so I can organize mi view like so:

The code I use to prepare this Lovelace view is:

  - title: Tiles
    background: center / cover no-repeat fixed url("/local/bg2.jpg")
    path: Tiles
    panel: true
    icon: 'hass:tablet'
    style:
      opacity: 50
    badges: null
    cards:
      - type: 'custom:homekit-card'
        title: tablet
        style: |
          :host {
            font-family: -apple-system, BlinkMacSystemFont, sans-serif;
            }
        useBrightness: false
        useTemperature: false
        tileHoldAnimation: true
        titleColor: '#FFF'
        enableColumns: true
        statePositionTop: true
        rows:
          - row: 1
            columns:
              - column: 1
                tileOnRow: 3
                entities:

I have no idea, I don’t use that card. Does that card support images?

Does that card support templates for any field?

Yes, in that little circle it has an option called image that receives a url as string and displays it there

What im beginning to think is it doesn’t play well with gettin a variable instead of the string.