How continue play the previous music after a tts voice announcement?

Hi @Bas81,

Before the alias in the script, you need to add a line with the script_id.
The scrip id is only low case letters and with no spaces.

Try adding the following line before the alias line:

tts_and_resume:

1 Like

Hi @Cosmin.FRC ,

Try to use the script version in post nr 19:

that was confirmed to work by @Jokerigno in post nr 20

It is a precondition that you install the official spotify component and spotcast, as described in post 15

Please let me know if this works for you

I already tried and it doesn’t work. I have previously installed the official spotify component and spotcast, this before creating your script. At first I thought maybe I missed something, but I started the whole process again, step by step and still without success.

I had some observations on different behaviors depending on how Spotify was started prior to the tts announcement.

  • By Spotify Connect from Spotify app on mobile or computer’
  • from the Spotify source with in the Sonos app
  • other

Could you please attach a screenshot of the your Sonos mediaplayer identity state from the developer tools before and after the TTS. Like that in post nr 40.

Spotify was started in Sonos app by shuffling a Spotify playlist.


Hi @ghassan ,

unfortunately I receive an error when the YAML-check is done:

A basic validation of the configuration is automatically done before restarting. The basic validation ensures the YAML configuration doesn’t have errors which will prevent Home Assistant or any integration from starting. It’s also possible to only do the basic validation check without restarting.
Configuration invalid!
Unexpected error calling config validator: ‘NoneType’ object is not iterable

script.yaml:

# melding doorgeven en verder gaan met muziek        
tts_and_resume:
alias: TTS and resume
variables:
  mediaplayer_State: '{{ states(tts_entity) }}'
  mediaplayer_media_content_id: '{{ state_attr(tts_entity,''media_content_id'') }}'
  mediaplayer_Source: '{{ state_attr(tts_entity,''media_channel'') }}'
  mediaplayer_volume_level: '{{ state_attr(tts_entity,''volume_level'') }}'
sequence:
  - service: media_player.volume_set
    data:
      entity_id: '{{ tts_entity }}'
      volume_level: 0.75
  - service: tts.google_translate_say
    data:
      entity_id: '{{ tts_entity }}'
      language: en
      message: '{{ msg }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: media_player.volume_set
    data:
      entity_id: '{{ tts_entity }}'
      volume_level: '{{ mediaplayer_volume_level }}'
  - service: media_player.play_media
    data:
      entity_id: '{{ tts_entity }}'
      media_content_id: '{{ mediaplayer_media_content_id }}'
      media_content_type: music
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ mediaplayer_Source != None }}'
        sequence:
          - service: media_player.select_source
            data:
              entity_id: '{{ tts_entity }}'
              source: '{{ mediaplayer_Source }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ mediaplayer_State != ''playing'' }}'
        sequence:
          - service: media_player.media_pause
            data:
              entity_id: '{{ tts_entity }}'
mode: single

I really don’t have any idea how to solve this…

1 Like

You have to indent all lines after tts_and_resume: by two spaces.

1 Like

@ghassan , that was the solution!
Me, YAML and spaces will never be friends I think :wink:

Thank you very much for your help and very nice script! I really appreciatie it!

1 Like

Hi @Cosmin.FRC ,

I see, some changes happened since last time I tested it .
Mainly in the script i test whether the application_id is Spotify but this is not how it works any more

I need to investigate it and hopefully make e new version to get it working.
I’ll get back during next week.

@ghassan, thanks, looking forward!

@ghassan, I upgraded my Spotify account to premium and now it works well with the sonos snapshot function and with your script it works to restore what was playing before the TTS but after the TTS is over Spotify starts playing again for 1 sec. the pauses 1 sec. and the starts playing again. The only minus is that after it resumes, it restarts the song from the beginning and doesn’t show anymore any info about the track, artist, etc.

Hi @Cosmin.FRC,

Sounds good :slight_smile:

Regarding starting the song from the beginning, I guess there is currently an error in the Sonos integration as to some reason the media_position attribute shows constantly 0 and therftre retrung that position is from the beginning.

image

And therefore restoring pisition zero starts from the beginning.

I guess It is possible to restore the media info. I’ll look into that and get back to you.

Hi,

thank you so much for this script. I have tried to implent it, and it works somehow, but not completly.

The playing music is stopped and the TTS kicks in, but then the music stays paused.

This is my script.yaml:

 
tts_and_resume:
  alias: TTS and resume
  variables:
    mediaplayer_State: '{{ states(tts_entity) }}'
    mediaplayer_volume_level: '{{ state_attr(tts_entity,''volume_level'') }}'
    mediaplayer_media_content_id: '{{ state_attr(tts_entity,''media_content_id'') }}'
    mediaplayer_app_name: '{{ state_attr(tts_entity,''app_name'') }}'
    mediaplayer_Source: '{{ state_attr(tts_entity,''media_channel'') }}'
  sequence:
    - service: media_player.volume_set
      data:
        entity_id: '{{ tts_entity }}'
        volume_level: 0.15
    - service: tts.google_translate_say
      data:
        entity_id: '{{ tts_entity }}'
        language: de
        message: '{{ msg }}'
    - delay:
        hours: 0
        minutes: 0
        seconds: 7
        milliseconds: 0
    - service: media_player.volume_set
      data:
        entity_id: '{{ tts_entity }}'
        volume_level: '{{ mediaplayer_volume_level }}'
    - choose:
        - conditions:
            - condition: template
              value_template: '{{ mediaplayer_app_name == ''Spotify'' }}'
          sequence:
            - service: spotcast.start
              data:
                entity_id: '{{ tts_entity }}'
      default:
        - service: media_player.play_media
          data:
            entity_id: '{{ tts_entity }}'
            media_content_id: '{{ mediaplayer_media_content_id }}'
            media_content_type: music
    - delay:
        hours: 0
        minutes: 0
        seconds: 8
        milliseconds: 0
    - choose:
        - conditions:
            - condition: template
              value_template: '{{ mediaplayer_Source != None }}'
          sequence:
            - service: media_player.select_source
              data:
                entity_id: '{{ tts_entity }}'
                source: '{{ mediaplayer_Source }}'
            - delay:
                hours: 0
                minutes: 0
                seconds: 4
                milliseconds: 0

    - service: media_player.media_pause
      data:
        entity_id: '{{ tts_entity }}'
    - choose:
        - conditions:
            - condition: template
              value_template: '{{ mediaplayer_State == ''playing'' }}'
          sequence:
            - delay:
                hours: 0
                minutes: 0
                seconds: 4
                milliseconds: 0
            - service: media_player.media_play
              data:
                entity_id: '{{ tts_entity }}'
  mode: single

and my automation action:

service: script.turn_on
target:
  entity_id: script.tts_and_resume
data:
  variables:
    msg: test
    tts_entity: media_player.gastezimmer
enabled: true

Hopefully someone can help! Thanks!

For anyone using Sonos, this is now offered automatically for TTS announcements in HA 2023.5. It will overlay the sound on top of currently playing music without the need to snapshot/resume. It’s also available for custom alert sounds but using the announce parameter with media_player.play_media service calls.

More info is here in the docs if you’re interested.

3 Likes

Hello everyone! Please tell me, I am using MPD player to send TTS notifications to it. Is there any idea how can I snapshot/resume audio playback on MPD player after TTS announcement?

Thats great news for Sonos, but a generic solution would be ever better. I use Google home devices and trying to get a nest doorbell home assistant chime to work, while resuming music, doesn’t yet look possible… :thinking::sob:

That’s awesome, made my life a lot easier haha, thx!

Has anyone gotten this working of 2023.7 with homepods? I previously used the script below for my TTS messages, but this no longer resumes what was previously playing - instead it just shuffles music in my library as if I pressed play on the homepod with no music selected.

(To be honest, I’m not exactly sure when this stopped working as Piper TTS was broken in the Apple TV integration for a few releases.)

alias: TTS and resume
fields:
  tts_entity:
    description: Media player to play TTS message on.
    required: true
    example: media_player.homepods
    selector:
      entity:
        domain: media_player
  tts_volume_level:
    description: Volume for TTS message.
    required: false
    example: 0.75
    selector:
      number:
        min: 0
        max: 1
        step: 0.05
  msg:
    description: Message to speak
    required: true
    selector:
      text: null
variables:
  mediaplayer_State: "{{ states(tts_entity) }}"
  mediaplayer_media_content_id: "{{ state_attr(tts_entity,'media_content_id') }}"
  mediaplayer_Source: "{{ state_attr(tts_entity,'media_channel') }}"
  mediaplayer_volume_level: "{{ state_attr(tts_entity,'volume_level')|float(0.5) }}"
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mediaplayer_State == 'playing' }}"
        sequence:
          - service: media_player.media_pause
            data:
              entity_id: "{{ tts_entity }}"
  - service: media_player.volume_set
    data:
      entity_id: "{{ tts_entity }}"
      volume_level: "{{ tts_volume_level|default(mediaplayer_volume_level) }}"
  - service: tts.speak
    data:
      cache: true
      media_player_entity_id: "{{ tts_entity }}"
      message: "{{ msg }}"
    target:
      entity_id: tts.piper
  - service: media_player.volume_set
    data:
      entity_id: "{{ tts_entity }}"
      volume_level: "{{ mediaplayer_volume_level }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mediaplayer_State == 'playing' }}"
        sequence:
          - service: media_player.media_play
            data:
              entity_id: "{{ tts_entity }}"
mode: queued
max: 3

Hello! I am facing the same problem. I want to announcements to be played over the HomePod Mini. Mostly we listen to radio programs with tune in over the HomePod. I have a script that pauses the HomePod, plays the announcement and instead of playing the radio program again, it just plays something out of my library.

It seems like this is never going to be easy - The whole Spotify, Google Speaker mixed with notifications and resuming whatever was playing.

I do see the reasons why, but I also feel like a smart developer should be able to solve this (I am not one of those and even though i would love to dig in to Yaml, variables and templates, I simply can’t dedicate the time to do so.

Therefore I suggest (Admin; Sorry if this is not proper procedure and if not please delete) a reward to anyone who can develop a working solution.

This is my version of a working solution:

  • Save states for volume on each device (Google Nest Speaker)
  • Save states for source (What ever is playing on the device)
  • Set the volume (Volume should be based upon time of day and pressence)
  • Run the announcement
  • Set the volume back (Recorded state before the announcement)
  • Set the speakers to play whatever was playing before (Recorded state)

This is an Open Source project and I’ve been working with Open Source since the age of 18, so I know how this works, but I also wanna be a part of the evolution, especially fixing or optimizing the basic stuff Home Assistant should be capable of.