Hi,
I’m trying to put a card in my main dashboard to show the entity_picture of the last active media player (last one in playing state, and if not last one in paused state).
I was able to get it working using the code below and I could put this on a Mushroom Template card, but I’m trying to just show the picture on a big custom:button-card so you can just click on it to get you to the media view in the dashboard (like in the screenshot).
Working code with Mushroom Template Card (I have a long list of media players, I’m just including 3 as an example):
{% set players = [ states.media_player.family_room_sonos,
states.media_player.sonos_roam,
states.media_player.roku_family_room ] %}
{% set last_played = players
| selectattr('state', 'eq', 'playing')
| sort(reverse=true, attribute='last_changed')
| list %}
{% set last_paused = players
| selectattr('state', 'eq', 'paused')
| sort(reverse=true, attribute='last_changed')
| list %}
{% if last_played |count > 0 -%}
{{ last_played[0].attributes.entity_picture }}
{% elif last_paused |count > 0 -%}
{{ last_paused[0].attributes.entity_picture }}
{%- else -%}
{{ states.media_player.spotify.attributes.entity_picture }}
{%- endif %}
This is what I have so far, but I cannot find a way to navigate the array to pick up the ones playing/paused and sorting it out (the “set last_played and set last_paused” portions):
type: custom:button-card
entity: media_player.family_room_sonos
show_name: false
show_entity_picture: true
entity_picture: |
[[[
const players = [ states['media_player.family_room_sonos'],
states['media_player.sonos_roam'],
states['media_player.roku_family_room'] ];
set last_played = players
| selectattr('state', 'eq', 'playing')
| sort(reverse=true, attribute='last_changed')
| list
set last_paused = players
| selectattr('state', 'eq', 'paused')
| sort(reverse=true, attribute='last_changed')
| list
if (last_played |count > 0)
return last_played[0].attributes.entity_picture;
elif (last_paused |count > 0)
return last_paused[0].attributes.entity_picture;
else
states['media_player.spotify'].attributes.entity_picture;
]]]
tap_action:
action: navigate
navigation_path: media
styles:
icon:
- width: 100%
- height: 100%
I’ve also thought about creating a media_player template (last active), but that’d open another can of worms, but I’m open to ANY suggestions at this time!
Thanks!
German