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

1 Like

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
1 Like

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:

2 Likes

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

3 Likes

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.

Hello Hellis81,

you wrote: “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.”

Can you give me the code? How can I save the volume level in an variable and how can I set volume level after speaking the message?

I would (in lack of knowing better) save it in an input_number.
Input Number - Home Assistant (home-assistant.io)

service: input_number.set_value
target:
  entity_id: input_number.volume
data:
  value: "{{ state_attr('media_player.something', 'volume') }}"  

Then after the message has been said:

service: media_player.volume_set
data:
  volume_level: "{{ states('input_number.volume') | float }}"
target:
  entity_id: media_player.something
1 Like

Hi Hellis81,
Thank you very mutch for your answer. I tried it, but
value: “{{ state_attr(‘media_player.box_buro’, ‘volume’) }}” give no result.
The result is “None” allways.
I think, “volume” is not an attributes of media_player?

Correct. It should be volume_level

Hi, sorry it dosen’t work


Edit:
I discovered, that with the service Turn_on you have to turn on the speaker first and then wait about 5 seconds. Then the volume is stored in the variable

This is my working script now:
alias: Test Lautstärke zwischenspeichern
sequence:

  • service: media_player.turn_on
    data: {}
    target:
    entity_id: media_player.box_buro
  • delay:
    hours: 0
    minutes: 0
    seconds: 5
    milliseconds: 0
  • service: input_number.set_value
    target:
    entity_id: input_number.v_volume
    data:
    value: ‘{{ state_attr(’‘media_player.box_buro’’, ‘‘volume_level’’) }}’
  • service: media_player.volume_set
    data:
    volume_level: 0.2
    target:
    entity_id: media_player.box_buro

Hier kommt noch die Ausgabe der mp3-Datei rein

Here comes the output MP3-file

  • delay:
    hours: 0
    minutes: 0
    seconds: 5
    milliseconds: 0

  • service: media_player.volume_set
    data:
    volume_level: “{{ states (‘input_number.v_volume’) | float }}”
    target:
    entity_id: media_player.box_buro

mode: single

Thank-you for your support.