Automation iterating the media player domain


I’m trying to do the following in an automation using templates but I’m not sure how exactly to do that :slight_smile: So any help would be appreciated.

Automation to stop all the media players

  1. If presence changes from home to not_home
  2. Loop the media_player domain and find the players that have state playing
  3. Stop one by one the players with state playing

I guess the step 2 I can do it with a template sensor that returns that at least one player is active.

And probably I can iterate the media_player domain in the action of the automation and stop the ones that have state playing?

Of course there is also the alternative to have all the media_player one by one in the automation and stop them even if they are not active. But I’m trying to find a way that I don’t have to update an automation every time that I’m adding a new media_player.

Put all your media players in a group? You can then control them all easily.

The state in the group is always off, I did tried that already.

Edit: The state is always off but it seems that I can use the service media_player.media_stop and the entity group.media_players to stop all the players.
It seems though that I can’t use the group for seeing if any player active but I have a template sensor already for that.

If it is already stopped I see no harm in sending the command again. It would just get ignored if it does not apply.

You mean not checking at all if something is playing? That could be a solution too :slight_smile:

The KISS principle - Keep it Simple.

1 Like

and short. :slight_smile:

1 Like

I’m looking for a similar solution, but have an added complexity. With Roku devices (similar too I assume), if you send a “Pause” command to a device that isn’t on, it turns it on. If the TV/Roku are connected with HDMI, it also turns the TV on. With this in mind, I have to check first to see if the device is playing, then pause it if needed.

As my list of devices has grown, my script is getting out out hand. Wondering if someone can help me simplify this process. Essentially, I’d like to look for ANY playing media devices, then send a Pause to just those devices that are playing.

This is what I’m using now, but with 14-15 devices, I’d like a better approach.

  - conditions:
      - condition: state
        entity_id: media_player.roku_family_room
        state: playing
      - service: media_player.media_pause
        data: {}
        entity_id: media_player.roku_family_room
default: []