Setting media volume for sonos tts

I have an automation that when the patio door is opened it says “Patio Door Opened” via tts on my sonos speaker.

It takes a snapshot if there is music playing, plays the notification and then resumes the snapshot so my music keeps playing.

However - I don’t want the “patio door opened” to play at the same volume level as the music was, but quieter …

I’m not understanding volume_level and how to set this … i keep getting errors.

Any ideas?

service: media_player.play_media
target:
  entity_id: media_player.living_room
data:
  media_content_id: media-source://tts/google_translate?message=Patio+Door+Opened
  media_content_type: provider
metadata:
  title: Patio Door Opened
  thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
  media_class: app
  children_media_class: null
  navigateIds:
    - {}
    - media_content_type: app
      media_content_id: media-source://tts
    - media_content_type: provider
      media_content_id: media-source://tts/google_translate?message=Patio+Door+Opened

To be clear, the above code runs, just i don’t have the volume level set as whereever i try i get the error.

Set the desired volume level using media_player.volume_set prior to calling media_player.play_media.

A little new to all this…I usually use the visual editor. So does that mean I call the service to set the volume and then call the service again to send the tts?

You’re not calling it “again”. You’re calling two different service calls, one after another.

Set the volume then play the media.

If you need further clarification, post the automation that you are using in YAML format.

Thanks… Here it is…

alias: Notification - Patio Door Opened
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.patio
    to: "on"
    from: "off"
condition: []
action:
  - if:
      - condition: state
        entity_id: media_player.living_room
        state: playing
      - condition: state
        entity_id: media_player.living_room
        state: paused
    then:
      - service: sonos.snapshot
        data:
          entity_id: media_player.living_room
          with_group: false
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - service: input_boolean.turn_on
        data: {}
        target:
          entity_id: input_boolean.sonos_snapshot_and_resume
  - service: media_player.play_media
    target:
      entity_id: media_player.living_room
    data:
      media_content_id: media-source://tts/google_translate?message=Patio+Door+Opened
      media_content_type: provider
    metadata:
      title: Patio Door Opened
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: media-source://tts/google_translate?message=Patio+Door+Opened
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
    enabled: true
  - if:
      - condition: state
        entity_id: input_boolean.sonos_snapshot_and_resume
        state: "on"
    then:
      - service: sonos.restore
        data:
          entity_id: media_player.living_room
          with_group: false
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.sonos_snapshot_and_resume
mode: single

Is there a reason why you added the additional complexity of creating a snapshot only if the media_player is playing/paused (as opposed to simply creating a snapshot regardless of the media_player’s current state)?

Not really, I guess I just assumed …why run unnecessary code and to run less… But no reason other then that so I could change it…

I can highly recommend Sonos Cloud. It solves all of this.

The TTS can play at a specific volume, and it reduces the volume of whatever is playing automatically, returning to normal after.

This allows playback of short clips (e.g., alert sounds, TTS messages) on Sonos speakers without interrupting playback

1 Like
alias: Notification - Patio Door Opened
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.patio
    to: "on"
    from: "off"
condition: []
action:
  - service: sonos.snapshot
    data:
      entity_id: media_player.living_room
      with_group: false
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room
    data:
      volume_level: 0.3
  - service: media_player.play_media
    target:
      entity_id: media_player.living_room
    data:
      media_content_id: media-source://tts/google_translate?message=Patio+Door+Opened
      media_content_type: provider
    metadata:
      title: Patio Door Opened
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: media-source://tts/google_translate?message=Patio+Door+Opened
  - delay:
      seconds: 2
  - service: sonos.restore
    data:
      entity_id: media_player.living_room
      with_group: false
mode: single
1 Like

Much appreciated, this did exactly what i needed. I’m going to have to study this now :slight_smile: thanks!

1 Like

There is a better solution using the newish announce feature. That doesn’t require stashing the state and restoring it.

    - service: media_player.play_media
      data:
        entity_id: media_player.living_room
        media_content_id: >
           media-source://tts/google_translate?message="{{ trigger.to_state.state }}"
        media_content_type: music
        announce: true
    - delay: "00:00:05"

The problem with that announce feature “which i hate” it makes a very LOUD ding before announcing the tts

It doesn’t do that for me.

1 Like

Hahaa! Glad to know I’m not going mad, and it seems I’m not alone here, my friend. I recently added the Sonos Roam 2 to my network, which is how I ended up on this thread. I already have the Move 2, but the Roam 2 is behaving differently and has brought up two issues:
1. It plays this sharp “PING” sound before the announcement.
2. I can’t seem to lower the volume, so the announcements are blasting at 100%.

My neighbours really don’t need to know that my jocks and socks are ready to be hung out on the washing line! :sweat_smile:

FYI this is my speech engine script, which handles TTS’s to single devices, multiple devices and a mix of smart speakers (Nest, AirPod, Move, Roam)

speech_engine:
    sequence:
      - repeat:
          for_each: "{{ who | list }}"  # Ensure 'who' is treated as a list
          sequence:
            - choose:
                - conditions:
                    # Check if the current item is move_2 or roam_2
                    - condition: template
                      value_template: >
                        {{ repeat.item in [
                            'media_player.move_2',
                            'media_player.roam_2'
                          ] }}
                  sequence:
                    - service: media_player.play_media
                      target:
                        entity_id: "{{ repeat.item }}"
                      data:
                        announce: true
                        media_content_id: >
                          media-source://tts/amazon_polly?message=<speak><break time="0.1s"/><prosody rate="125%">{{ message }}</prosody></speak>
                        media_content_type: "music"
                        extra:
                          volume: 50
              default:
                - service: media_player.volume_set
                  data:
                    entity_id: "{{ repeat.item }}"
                    volume_level: 0.7

                - service: tts.amazon_polly_say
                  data:
                    entity_id: "{{ repeat.item }}"
                    message: >
                      <speak>
                        <break time="0.1s"/>
                        <prosody rate="125%">
                          {{ message }}
                        </prosody>
                      </speak>
                    cache: true