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

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.

I have exactly the same problem with my HomePod. Any ideas?

Music Assistant supports Spotify, Youtube, TuneIn (and others) and also resuming after TTS.

@larsnormark, @Schlichi83

Correct, but you’ll run in to a million other problems. It’s another layer of stuff that can break. In my experience, 1. It keeps crashing the APP on iPhone, 2. It’s not always resuming, 3. If you choose artist Guns n’ roses, it will play what it finds and then suddenly you have pop, country or something not in that genre. In the end I removed Music Assistant as it was causing more headaches than anything else.

Music Assistant 2.0 is just about out of beta. Many, many improvements. I’m using it with an array of google and other assorted media devices (including browser mod players) with local media, Spotify, YouTube, TuneIn, TTS. I’m not really experiencing very many bugs in my setup any longer. It’s not “perfect” yet but it’s getting much closer! I believe the creator of MASS is actually working for NabuCasa now which probably helps.

If it’s been a while since you’ve played around with MASS I recommend giving it another whirl!

Even in Music Assistant resume after TTS is not working

For those who prefer not to use Music Assistant the broadcast feature in the Google SDK integration is a good alternative to have announcements on google devices without stopping any media that is currently playing. I’ve been testing it out and works great.

1 Like