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"