Automation to Change Volume on All Media Players with Template

Hello! I’ve bee spinning my wheels on this a bit this morning. Figured I’d make my first forum post.

The goal is to change all media players volume to 40% at night so Google doesn’t give me a headache when I ask it for something.

I have it working without a template but I want to learn how to call all entities of a certain type. Here is my current automation yaml:

alias: Testing Automation 1
description: ""
trigger:
  - platform: time
    at: "23:00:00"
condition: []
action:
  - service: media_player.volume_set
    target:
      entity_id: "{{ targets }}"
    data:
      volume_level: 0.4
  - service: automation.turn_on
    target:
      entity_id:
        - automation.netflix_start_sync_automation
        - automation.sync_box_automation
        - automation.stop_sync_automation
        - automation.stop_netflix_sync_automation
    data: {}
mode: single
variables:
  targets: |
    {{ states.media_player | map(attribute='entity_id') | list}}

I tested by setting all my speakers to 100% and then running the automation. It “runs” but no volume change.

The trace step details show me this (redacted some) which… looks right. But alas, no volume change :’( :

Executed: March 2, 2023 at 08:48:32
Result:
params:
  domain: media_player
  service: volume_set
  service_data:
    volume_level: 0.4
    entity_id:
      - media_player.all_speakers
      - media_player.art_room_speaker
  target:
    entity_id:
      - media_player.all_speakers
      - media_player.art_room_speaker
running_script: false
limit: 10

One of the issues with blanket commands like this is that many entity domains contain entities that you might not expect or entities that are generated by integrations that do not support all the available services for a given domain. Are you sure that all your media_player entities accept the volume_set service?

FWIW, the automation.turn_on service does not trigger automations… it simply enables them if they are disabled. If your goal was to have your automations’ actions execute, you need to use the automation.trigger service. Automation Services

I know a subset of those media players contain it as I have a working automation which provides a list of specific entities. So even if some of them don’t, I am still expecting a successful volume set on the media players which do.

As for the automation turn on, that one is intended. I have a button in my living room to disable the automation and reset the lights. The automation I show above is like… “a nightly reset.” Getting devices back to a “default state” if that makes sense.

It doesn’t work that way… the service will error out before it sends the command. You need to reject any media player that doesn’t accept volume_set.

service: media_player.volume_set
data:
  volume_level: 0.4
target:
  entity_id: >
    {{ states.media_player | map(attribute='entity_id')  
    | reject('search', 'b_mod') 
    | reject('in', ['media_player.x ,'media_player.fire_tv']) 
    | list}}

If you use the Services tool in the Dev. Tools menu, you can test your service call with the template and it will tell you which media_player entity is causing the error.

1 Like

This is the perfect answer to my problem. Thank you so much. Thanks for even showing me a better way to test.

Are there so many media_players that it’s easier to repeatedly filter them with a template rather than explicitly list them? Do you have dozens of media_players?