Media Player condition card

Hi All,

I’m looking for some help please on how to achieve something. On my new dashboard I’m wanting to have conditional tiles pop up only when needed to provide a counter / overview. This is working great for everything but I can’t think of a way to set the media card conditional.

I have all my media players grouped into a group called all media players, the problem is some media players report different state, off, standby / on, playing. I’d like to show the below media card only if any media player is playing or on.

I think the correct way to do this is probably to create a new sensor and use that sensor for the conditional card but I’m struggling to understand the yaml to do this and only have it pick up state playing or on.

image

          - type: conditional
            conditions:
              - entity: media_player.all_media_players
                state: 'on'
            card:
              type: custom:stack-in-card
              cards:
                - type: custom:mushroom-template-card
                  primary: >-
                    Media: {{ expand(states.media_player.all_media_players) |
                    selectattr('state','in',['on','playing']) | list | count }}
                  secondary: ''
                  icon: mdi:cast
                  entity: media_player.all_media_players
                  hold_action:
                    action: toggle
                  tap_action:
                    action: navigate
                    navigation_path: /home-v2/media
                  icon_color: grey
                  fill_container: false
                  layout: vertical
                  multiline_secondary: false
                  card_mod:
                    style:
                      mushroom-state-info$: |
                        .primary {
                          #text-shadow: 1px 1px 1px black;
                          --card-primary-font-weight: 450;
                          --primary-text-color: black;
                          --card-primary-font-size: 13px;
                          #align-items: center;
                          margin-left: -auto;
                          margin-top: -10px;
                        }
              card_mod:
                style: |
                  ha-card {
                    border-radius: none;
                    background-color: white;
                    border: none;
                    box-shadow: none;
                    align-left: auto;
                    align-right: auto;
                  }

If I understand your request correctly you want to show ONLY the media players of the group ‘all_media_players’ which are in state ‘on’ or ‘playing’ - is this correct?

in this case my recommendation would be the lovelace-auto-entites card with a configuration like the following (probably needs to be adjusted for you):

  - type: custom:auto-entities
    card: custom:mushroom-template-card
	filter:
	  template: {{ expand('group.all_media_players') | selectattr('state','in',['on','playing']) | map(attribute='entity_id') | list }}

It’s the condition for displaying the card I’m struggling with.

So I only want the card to show if media players within the group are playing or on. At the moment it doesn’t work because some media players report status as standby or idle so the group status is on all the time. The actual card and card count is working fine with the code I posted above.

          - type: conditional
            conditions:
              - entity: media_player.all_media_players
                state: 'on'

The conditional card does not allow you to filter on specific entities
It just allows you to make a simple “show the following configuration” or “do not show the following configuration” decision.

What you are experiencing is absolutely correct looking into how the media player group works - Group - Home Assistant (home-assistant.io)
“… the group state is playing if all group members are playing .”
“… the group state is on if at least one group member is not off , unavailable or unknown .”

and as you state correctly some members of the group are not off, unavailalbe or unknown - they are standby and as such the group is ON

That is fine and I understand that is how groups work but there must be a way to get it to show up by creating a custom sensor that only monitors for on or playing and then use that in the conditional card.

what you can do is create an independent template binary_sensor which is on only if one entity in the group is in state “on” or “playing” (igorning all other states) and use this sensor as conditional option:

so this goes into your yaml configuration:

template:
  - binary_sensor:
      - name: "any_media_players_on"
        state: {{ expand('media_player.all_media_players') | selectattr('state','in',['on','playing']) | list | count > 0 }}

and this into your lovelace:

          - type: conditional
            conditions:
              - entity: binary_sensor.any_media_players_on
                state: 'on'

That is exactly what I was after and is now working perfectly. I just couldn’t get the format right for the state on the binary sensor and was missing the > 0

Thanks very much.