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

Tags: #<Tag:0x00007fc411355f98>

Hello. when I listen music in my “google mini” and a automation execute and say a alert by tts.google_say , the music stop and not continue.

any idea ? how restart or continue in the same point of the music ?

thanks.
Regards
Ariel

you need to use a broadcast instead

thanks for you reply.

how I can broadcast a TTS , by example " the front door is open" and after this speech end continue listening the music.

regards.

I interesting. any idea?

Hey,

Maybe too late to give response but here is the way I do it

1: I get the source of the media player prior to sending the tts.

  MediaSource: '{{ state_attr(''media_player.alrum'',''media_channel'') }}'

After TTS I add a delay enough for google to be finished sayng the nmesage 3-5 sec.
then I set the source again to what it was before and it will start playing right away.

  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: media_player.select_source
    target:
      entity_id: media_player.alrum
    data:
      source: '{{ MediaSource }}'

hello, thanks for you help

my media player not have the state_attr media_channel

regards

Hi Ariel,

Ok, In my case I have a Sonos and the source have a value when you pick a source, like a radio station from the favorites. I did not check how it is on a Nest or other google devices yet.
but you can always use the content_Id.

I have actually just made an extended version of the code that handles various situations also if the device was not playing before the TTS.

I have it all in a script that I call from the automation:

Here you go, and please let me know if you have any questions or challenges:

alias: TTS and resume
variables:
  alrum_Source: '{{ state_attr(''media_player.alrum'',''media_channel'') }}'
  alrum_media_content_id: '{{ state_attr(''media_player.alrum'',''media_content_id'') }}'
  alrum_State: '{{ states(''media_player.alrum'') }}'
sequence:
  - service: tts.google_translate_say
    entity_id: media_player.alrum
    data:
      language: En
      message: this is your message, this is your message repeat!
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: media_player.play_media
    target:
      entity_id: media_player.alrum
    data:
      media_content_id: '{{ alrum_media_content_id }}'
      media_content_type: music
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ alrum_Source != None }}'
        sequence:
          - service: media_player.select_source
            target:
              entity_id: media_player.alrum
            data:
              source: '{{ alrum_Source }}'
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ alrum_State != ''playing'' }}'
        sequence:
          - service: media_player.media_pause
            target:
              entity_id: media_player.alrum
mode: single

great ! try the script , work fine !

but I have a problem, all my TTS alerts starts from a automation trigger , how i can run the script inside of automation and send “the text to play by tts” as parameter ?

regards

The HA documentation is really good if you want to look there

# Example configuration.yaml entry
automation:
  trigger:
    platform: state
    entity_id: light.bedroom
    from: "off"
    to: "on"
  action:
    service: script.turn_on
    target:
      entity_id: script.notify_pushover
    data:
      variables:
        title: "State change"
        message: "The light is on!"

You will need to modify the script to accept the parameter as per this on the linked page:

   ... 
    title: "{{ title }}"
    message: "{{ message }}"

Hi @ArielBaravalle,
I agree with @fanuch

Here is a modified version of the script that takes two parameters:

  • the tts message
  • the media player entity_Id so you ca use it with different media players in different cases.

and finally I added taking a back up of the volume prior the TTS, securing the TTS message is played at a given level example 75% and then restoring the original volume.

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

and here is an action example from an automation showing how to call the script and pass the parameters:

  - service: script.turn_on
    target:
      entity_id: script.tts_and_continue
    data:
      variables:
        msg: this is a test message, replace it by yours
        tts_entity: media_player.spisestue

Let me know if you have other challenges
good luck :slight_smile:

1 Like

Thanks for all !! great solutions !!

time now to change all my TTS alerts and informations :grimacing:

best regards

Sounds good
Pleas give men an update whether it works or if you have ideas for further optimization :slight_smile: :slight_smile:

Hi,

I tried with my setup (using spotify) and it doesn’t work. I think it’s pretty normal because spotify needs a custom component to allow HA to start playing music…

Hi,

I’ll take a look at it and get back.

I am not sure you do need the custom component to play spotify.
but have you tried with other sources like TuneIn for example?

Hi @Jokerigno,

Just wanted to give you an update.

I confirm that it works fine with Spotify when running on Sonos devices but, unfortunately not on Chrome cast or other google devices like the google mini or nest.
other streaming apps like TuneIn work fine on both Sonos and Google devices.

The reason basically is that calling the service media_player.play_media that is used in the script works on Sonos devices but not on google. Maybe we need the custom component you mentioned.

I’ll give it a try and look further for a solution

I’ll get back with an update

Kind regards, Ghassan

1 Like

Hi @Jokerigno ,

Good news, I found a solution :slight_smile:

If you first install and configure:

Then the updated version of the script will work with SpotifyConnect on Chrome cast devices like yours.
On Sonos it still works with Sonos if you start Spotify from the Sonos App but not if you choose the Sonos SpotifyConnect device from the the Spotify app.

On the other hand, other streaming services like TuneIn still works with both Chrome cast and Sonos devices.

Her is the updates version of the script:

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.75
  - service: tts.google_translate_say
    data:
      entity_id: '{{ tts_entity }}'
      language: da
      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 }}'
          - service: media_player.media_play
            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 }}'
  - service: media_player.media_pause
    data:
      entity_id: '{{ tts_entity }}'
  - delay:
      hours: 0
      minutes: 0
      seconds: 4
      milliseconds: 0
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ mediaplayer_State == ''playing'' }}'
        sequence:
          - service: media_player.media_play
            data:
              entity_id: '{{ tts_entity }}'

mode: single

As you ca see i introduced delays several places to make sure previous commands are finished executing before new ones are called. Maybe some of these delays can be made shorter. You can experiment finetuning these.

Please let med know if this works for you :slight_smile:

1 Like

Tomorrow in the morning I will test it and let you know asap the results! Can’t wait!

TY! :slight_smile:

So I tested out and in my case I don’t know why is not working.

First I already have spotcast and I tested it in dev tools with this simple command and it works:

service: spotcast.start
  entity_id: media_player.corridoio

What I’ve found is a simply copy paste of the script. What I’v e noticed and that my google mini make the blink after tts message but the music does not start. Moreover looking ath te spotcast docs maybe the second command of the script is not required?


      - choose:
          - conditions:
              - condition: template
                value_template: "{{ mediaplayer_app_name == 'Spotify' }}"
            sequence:
              - service: spotcast.start
                data:
                  entity_id: "{{ tts_entity }}"
##########THIS IS REQUIRED ? ###########
              - service: media_player.media_play
                data:
                  entity_id: "{{ tts_entity }}"
#######################################

Of course it is just a question. I just pasted the script as is.

I’ve checked also the logs but no error in there.

Hi @Jokerigno ,

Yes I also noticed some inconsistency too.
I don’t have a google mini but other Chinese products a Mi AI speaker and Mi TV Box box. with built in Chrome cast and these behave differently. So maybe the real Chrome cast behave in yet another way.

Try this a bit modifiede version:

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: da
      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

If it still don’t work, please send me the state and attributes values for your media player from the developer tools “states tab”, before and after you call the script?

This will help us figure out what is missing :slight_smile:

1 Like

it works!!! I need to make some other test but first impression is that it works on every condition!

1 Like