Set volume media player in automation

Hy guys,

I made an automatic for when my laundry is done. HA sends a message to me and my girlfriend’s phone, and a message plays through my google home speakers. Now I also want to add the volume in the automation. I have two questions:

  1. What code do I need for this and where do I put it?
  2. What a reasonable sound level that is audible but does not scare us? :slight_smile:

Thanks for your help!

alias: Wasmachine is klaar
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.wasmachine_smart_stekker
    for:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
    below: '4'
condition:
  - type: is_not_open
    condition: device
    device_id: d5c307599cbd71b6f3eb40e484f1ab42
    entity_id: binary_sensor.wasmachine_deursensor
    domain: binary_sensor
action:
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.wasmachine
  - repeat:
      count: '2'
      sequence:
        - service: notify.mobile_app_oneplus_van_elvira
          data:
            message: De was is klaar! :-)
        - service: notify.mobile_app_oneplus_van_rob
          data:
            message: De was is klaar!
        - service: tts.google_translate_say
          data:
            entity_id: >-
              media_player.keuken, media_player.mini, media_player.studeerkamer,
              media_player.woonkamer
            message: De was is klaar!
mode: single

Add this before the service tts.google.translate_say:

  - service: media_player.volume_set
    data_template:
      entity_id:
        - media_player.google_home
        - media_player.google_mini
      volume_level: 0.5

Thanks. Should I not name all my media.players?

Yes, the ones whose you want to change the volume,

  - service: media_player.volume_set
    data_template:
      entity_id:
        - media_player.keuken
        - media_player.mini
        - media_player.studeerkamer
        - media_player.woonkamer
      volume_level: 0.5
1 Like

As a side note to the answer already provided, you can always make speaker groups in google home app and then reference the one group entity for any automations. If you use the same speakers for multiple automations then it makes it a little easier…less typing anyway :wink:

1 Like

Thank you for your help! :pray:

Thanks for the tip. Very useful!

Two questions left.

When the command is given, all my Google home speakers are set at sound level 0.5. In addition, my speakers stop playing music when this is turned on.

  1. Is it possible after this automation is done, to reduce the sound level to a lower level?
  2. Is it possible to resume the music?

Thanks in advance!

Lower the volume is possible you just need to save the volumes to variables and after the messages, delay for two seconds then do a wait until media_player is idle (this is to make sure the message is complete before it lowers the volume).
Then lower it using the same as before but with the variable you saved.

Regarding 2, what is playing the music?
Is it a stream or is it Home Assistant?

Edit…
I now realize you asked for a “lower level” not the same as previous.
Lower level is just as before just change 0.5 to 0.3 or what you want.
But you should delay and wait until since actions are not waiting for the message to complete before lowering

How does that look in code?
Music can be Spotify for example.

I assume you have something like this now:

action:
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.wasmachine
  - repeat:
      count: '2'
      sequence:
        - service: notify.mobile_app_oneplus_van_elvira
          data:
            message: De was is klaar! :-)
        - service: notify.mobile_app_oneplus_van_rob
          data:
            message: De was is klaar!
        - service: media_player.volume_set
          data_template:
            entity_id:
              - media_player.keuken
              - media_player.mini
              - media_player.studeerkamer
              - media_player.woonkamer
            volume_level: 0.5
        - service: tts.google_translate_say
          data:
            entity_id: >-
              media_player.keuken, media_player.mini, media_player.studeerkamer,
              media_player.woonkamer
            message: De was is klaar!

then just add below:

        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0
        - wait_for_trigger:
            - platform: state
                entity_id: media_player.keuken
                to: idle
        - service: media_player.volume_set
          data_template:
            entity_id:
              - media_player.keuken
              - media_player.mini
              - media_player.studeerkamer
              - media_player.woonkamer
            volume_level: 0.3

Regarding resuming spotify. Not sure.
I don’t use spotify. But if the cast url is visible in the entity then you might be able to use the media extractor to cast it again.

Edit forgot the delay and wait for trigger

1 Like

Thank you. I will have a look into it!

For me when sending tts to my idle state google devices the state never changes from idle anyway so not sure if your wait in this case example would work ?

All my google home speakers change to “playing” when playing the TTS.
Then it goes to “idle” after the message, and then off after a few minutes.

How long was the message, perhaps the state didn’t have time to change?

I will have to revisit this and double check!

I realised that nearly 90% of my TTS automations were always playing the same message so ended up just renaming and moving the original cached tts files and referencing those mp3 files now instead of using cloud for the same message over and over.

Will check the above as I do have some bedtime routines that still use TTS as the message are dynamic and remember the timings being a pain due to what I thought was no state change from idle.