Show any currently active media player on default view?

I’m trying to figure out if there’s a simple way for me to display any media player that’s currently active on the default view? I’ve had a look at the group visibility stuff but from what I can see I’d have to make a separate group for each media player, include them all in the default view, and write an automation for each to show/hide based on it’s current status - which feels really messy.

Does anyone have a better solution?

3 Likes

Finally got this figured out

- platform: template
  sensors:
    media_players_active:
    friendly_name: 'Active Media Players'
    value_template: >
      {{ states.media_player
       | rejectattr('state', 'equalto', 'off')
       | rejectattr('state', 'equalto', 'idle')
       | map(attribute ='entity_id')
       | join(', ')
      }}

- alias: Media Update Active Group
  trigger:
  - entity_id: sensor.media_players_active
    platform: state
  action:
  - service_template: >
      {% if trigger.to_state.state != '' %}
        group.set
      {% endif %}
    data_template:
      object_id: media_players_active
      visible: true
      entities: '{{ trigger.to_state.state }}'
  - service_template: >
      {% if trigger.to_state.state == '' %}
        group.set
      {% endif %}
    data:
      object_id: media_players_active
      visible: false

Don’t manually create the group, just include it in whichever view you want to use it in.

4 Likes

This is great, thanks for posting. Could it be adapted to filter out certain types of media players?

Edit: Actually think I have worked it out myself…

| rejectattr('entity_id', 'equalto', 'media_player.sonos_bedroom')

I use another approach, with customizer and customUI.
This way, I can have dynamic attributes, including the “hidden” value for any object.

Create a group with all media_player of your interest (or anything), and put this group wherever you want.
Then, in the customize section add for each one the template status for “hidden” :

   media_player.3312:
    friendly_name: Denon
    templates:
      hidden: "return (state === 'off' );"

Will only display any media player that is turned on. You can use any other attribute of course or an attribute from another entity :

 templates:
      hidden: "return (entities['media_player.bluray'].state === 'off'); "

Will only display your amplifier media player if the bluray device is turned on.

1 Like

I’ve noticed this getting likes every so often, so thought it would be worth updating with my latest version. I’ve now got it set up as a package and the automation has been tidied up a bit. I found this version more reliable than the one I posted above.

https://github.com/danrspencer/hass-config/blob/master/configuration/package/active_media_players.yaml

1 Like

I tried this but its not working, i put the automation separately and the sensor in sensor yaml, the sensor shows up but still its not recognising, im using custom ui on hassio, any pointers for me? thanks

This has stopped working for me since I moved to 0.82.

Anyone get this working again?

You’ve deleted that link, can you edit to use this one: https://github.com/danrspencer/hass-config/blob/702cdace7c948480962a3b856c6e6c64b9e2673f/configuration/package/active_media_players.yaml

I have been looking at this and I have created the following template sensor, however it doesnt appear to be working.

Any help would be appreciated

  - platform: template
    sensors:
      media_players_playing:
          value_template: >
            {% set media_players = [
              media_player.spotify_andrew_vint,
              media_player.charlottes_bedroom,
              media_player.dressing_room,
              media_player.en_suite,
              media_player.hot_tub,
              media_player.katies_bedroom,
              media_player.kitchen,
              media_player.living_room,
              media_player.master_bedroom,
              media_player.office,
              media_player.dressing_room_tv,
              media_player.katies_fire_tv_stick,
              media_player.living_room_fire_tv,
              media_player.living_room_fire_tv_stick,
              media_player.living_room_tv,
              media_player.master_bedroom_tv,
              media_player.plex_living_room_tv,
              media_player.whole_house_fire_tv,
              media_player.whole_house_fire_tv_stick,
              media_player.xboxone,
              ] %}
            {{ media_players | selectattr('state','eq','playing') | list | count }}

The check config comes back as a pass but when looking at the state of the sensor it says unavailable

Regards

I use the Conditional Card - Home Assistant for this.

I think you’re just missing states before the media player entity.

The following works for me:

{% set media_players = [
  states.media_player.sonos_living_room,
  states.media_player.sonos_kitchen
] %}

{{ media_players | selectattr('state', 'eq', 'playing') | list | count }}

Don’t delay! Do yourself a favor and install the Auto-Entities card. It will change the way you do everything on the frontend and only for the better. Extremely powerful with filters and sorting plus it’s super easy to use.
I was using conditional cards for everything. Auto-Entities is so slick that I ended up tossing my whole frontend and went with Auto-Entities as the base for all my cards now.
It cut in half the number of cards I had and no more editing when I make changes to basic stuff. For example when adding a new media player to the network it just shows up, no editing required.
It’s not just for media players, it works on all entities, Lighting, door/windows sensors, etc. It’s a game changer. So get it sooner rather than later.

https://github.com/thomasloven/lovelace-auto-entities

This is the code I use to show my active echos and plex on the frontend:

type: 'custom:auto-entities'
card:
  type: entities
  show_header_toggle: false
filter:
  include:
    - domain: media_player
      state: playing
    - entity_id: sensor.plex_home_plex
      state: watching
  exclude: []
sort:
  method: none
show_empty: false
2 Likes

yup it was the states. that fixed it

Thanks

Hi,

Is it possible in the ‘custom:auto-entities’ to define the artwork for the domain currently playing ?
What I’m asking: is it possible to show the artwork from the mediabplayer (and eventually other attributes) in this script ?

Kr,

Bart

Thank you @PCRx
Auto-entities worked like a charm. New media players are auto added and the cover of media playing is also displayed.
That saved me a ton of work, I just copied your code and Boom, it worked. Thanks

1 Like

I have something super similar and copied your code… also works great…

Has anyone figured out how to eliminate the player controls like Volume and power? I do not want them showing.