Script to resume radio (TuneIn) and Spotify after TTS on Google Home speakers

That’s what prerequisite 8 states :wink:

  1. I’m using Google Cloud TTS, check the TTS service call to make sure this matches your setup. It could be that your TTS service also requires service data like language (lines 265 - 269 in the script).

But I’m also planning to put the TTS-service in a variable, to make it more easy to change it.

Thanks, this now works correctly. Only thing I am running into now is that if my google hub is not playing anything, the volume is set to default (=0.25) instead of the old volume. Any idea where this issue is caused and what I should change?

You could try to increase the delay a bit (to 3 seconds for example) after playing the silent mp3
Last line of this part:

    - alias: "Play silent MP3 if volume of non playing targets should be restored"
      choose:
        - conditions: "{{ volume_restore_required }}"
          sequence:
            - alias: "Play silent MP3"
              service: media_player.play_media
              target:
                entity_id: "{{ tts_target_not_playing }}"
              data:
                media_content_id: "{{ silent_mp3 }}"
                media_content_type: "audio/mp3"
            - delay: 2.5 # change it here

In my latest version (which I still have to place here) I’ve also added it to the variables to make it easier to configure.

For me 2 also seems to short (the Hub’s seem to be a bit slower) and 3 seems to work fine.

Thanks, I will try. The only thing still bothering me is the sound each google device makes if it was in the off state. Not sure however if this can be solved when you want to return to the old volume.

It does not bother me that much, but you can try to keep them awake by sending a silent mp3 every few minutes (to all non active players).

Hi TheFes,

Love this script and implemnted it in my set-up.
Did find a bug though. Not sure if it’s an Spotify API issue, but while playing a podcast, I got an error:

Executed: 1 december 2021 10:01:34
Error: TypeError: 'NoneType' object is not iterable

With the following being feed back from Spotify API:

players_to_resume:
  - entity_id: media_player.living_group
    media_content_id: spotify:episode:4lJrP8lm5uULHjzQTwuUSW
    media_title: 'Daan Roosegaarde: kunst als redmiddel voor de toekomst?'
    media_artist: NRC Future Affairs
    media_content_type: no type
    app_name: Spotify
    entity_picture: https://i.scdn.co/image/ab6765630000ba8a83a356a105529f1eee416986
    volume_level: 0.49

As you can see it does not hold a media_type and thus the script fails to run.
Also I found the script not resuming the music on the correct speaker group, although I have defined them in the script. What happens when playing music on media_player.living_group, the automation first resumes on living_room_speaker and then switches to office_speaker.

  speaker_groups:
    media_player.home_group:
      - media_player.kitchen_speaker
      - media_player.living_room_speaker
      - media_player.office_speaker
    media_player.living_group:
      - media_player.living_room_speaker
      - media_player.office_speaker
    media_player.kitchen_group:
      - media_player.bedroom_speaker
      - media_player.kitchen_speaker

Automation:

- service: script.google_home_say
          data:
            tts_message: Hello, you left the fridge door open!
            tts_target:
              - media_player.home_group
            tts_volume: 40
            speaker_group_split: false

Thanks for reporting it, I will have a look.

Could you set debug: True and post the output from the debug log action in the script (you can eiter copy it from the log if your log level is set to info, or copy it from the script trace).

The part you posted seems to be the players_to_resume variable from the script. Why should the Spotify API return this? However, the media_content_type should return music. But for Spotify this should not be relevant, as it just uses the spotcast service, and only uses the app_name to determine if Spotify was playing.

Could you also show a screenshot of the trace, or let me know in which action the script stopped because of this error (as shown in the trace).
If you want, you can also send me a direct message, or find me on Discord (same username). We can talk in Dutch then :slight_smile:

Also I found the script not resuming the music on the correct speaker group, although I have defined them in the script. What happens when playing music on media_player.living_group, the automation first resumes on living_room_speaker and then switches to office_speaker.

This is fixed now

Executed: 1 december 2021 10:01:34
Error: TypeError: ‘NoneType’ object is not iterable

And I think this is solved as well, however it was not related to Spotify.

Thanks for the fix I check out the revisions and copied your new script. Checked if the error appeared again with a different podcast on Spotify and seems to be working fine!
However, the bug off not resuming music to correct speaker group still seems to be apparent. I have added the full trace file below for you reference.

Cheers!

Your paste stops after the TTS, so I can’t see the resume action.

But I do see something fishy here:

      "sequence/2": [
        {
          "path": "sequence/2",
          "timestamp": "2021-12-06T08:41:01.934390+00:00",
          "changed_variables": {
            "players_to_resume": [
              {
                "entity_id": "media_player.living_room_speaker",
                "media_content_id": "spotify:track:4QF8w44G6fU4VNxQJK1OJn",
                "media_title": "King of a One Horse Town",
                "media_artist": "Dan Auerbach",
                "media_content_type": "music",
                "app_name": "Spotify",
                "entity_picture": "https://i.scdn.co/image/ab67616d0000b273b2d7362dc639856d9d046205",
                "volume_level": 0.41
              },
              {
                "entity_id": "media_player.office_speaker",
                "media_content_id": "spotify:track:4QF8w44G6fU4VNxQJK1OJn",
                "media_title": "King of a One Horse Town",
                "media_artist": "Dan Auerbach",
                "media_content_type": "music",
                "app_name": "Spotify",
                "entity_picture": "https://i.scdn.co/image/ab67616d0000b273b2d7362dc639856d9d046205",
                "volume_level": 0.45
              }
            ]
          }
        }
      ],

It determines that those two speakers should be resumed, where that should be the speaker group.

I think I did not take into account your use case where you send the tts to the house group, in which a speaker group is playing. I will do some tests.

Thanks for the info.

Should be fixed now, see the changelog in the start post.
An additional variable sub_groups has been added, and the template to determine which entities to resume has been changed to take these sub_groups into account.

1 Like

Thank you so much TheFes, will test and get back to you with results!

Cheers

Was hoping that this was the solution I needed, however too confusing for me as newbie to understand what part needs to go where (configuration.yaml, scripts.yaml, GUI, etc…)…

Without some more detailed questions I’m afraid I can not help you much.

The script itself (the one on Github) can be placed in scripts.yaml

But for further answers I need to know how you are planning to use it. Do you want to use it in automations? Or do you want to have it on your dashboard?

I’m absolutely loving this script, however only managing to get it to work about 25% of the time.
Not entirely sure if my use-case is just bizarre, but I keep getting errors. I’ll keep an eye on your updates, and after digging some more, I’ll pester you with some logs :slight_smile:
Thanks for sharing this with us - can’t believe it’s not something already built into the default media players/tts!

Okay, I will wait for the logs so I can try to fix it.
Can you elaborate a bit more on the specific use-case?

OK, so I dropped the script from Github into scripts.yaml, and changed my variables like this:

variables:
tts_service: tts.google_cloud_say
service_data:
players_screen:
- media_player.googlehome6664
- media_player.googlemini
spotify_media_players:
- media_player.spotify_nonkelsue
speaker_groups:
media_player.huis_groep:
- media_player.googlehome6664
- media_player.googlemini
primary_spotcast: “nonkelsue”
silent_mp3: “media-source://tts/10-seconds-of-silence.mp3”
default_volume_restore: True
delay_volume_restore: 3
default_volume_level: 0.25
default_group_split: False

However, each time there is a tts message, my Spotify music stops and does not resume. What am I missing or overlooking here?

The indentation of your code is incorrect. Is this because of your copy/paste or is it like this in your script.yaml? Becasue in that case that could explain some issues.

Could you go to Configuraton > Automations & Scenes (assuming you are on HA 2021.12) > Scripts
Then find the Google Home Say script on this page, and press the debug button (the one with the clock and the arrow around it).
In the trace, there is a download button. Can you share that with me (maybe not in a post here, share the contents on something like https://www.codepile.net/)

Last check, did you install spotcast, and is it working?

So I have tested your latest script but somehow it still resumes the music on one of the speakers in the group first and then it stops on that device because it then starts on another speaker in that group.

I did put in every speaker separately in the automation, script from sequence: onwards is an exact copy from yours. My automation is:

alias: Fridge Door Left Open
description: Alert to notify the fridge door is left open for 1 minute, repeat until closed
trigger:
  - platform: state
    entity_id: binary_sensor.aqara_fridge_sensor_contact
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition: []
action:
  - repeat:
      until:
        - condition: state
          entity_id: binary_sensor.aqara_fridge_sensor_contact
          state: 'off'
      sequence:
        - service: notify.mobile_app_oneplus_a5000
          data:
            message: You left my door open!
            title: The Fridge
        - service: script.google_home_say
          data:
            tts_message: Hello, you left the fridge door open!
            tts_target:
              - media_player.living_room_speaker
              - media_player.office_speaker
              - media_player.kitchen_speaker
            tts_volume: 35
            restore_volume_all: true
            speaker_group_split: false
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
            milliseconds: 0
mode: single
max: 10

Hi @tivoo

Could you upload the trace for script.google_home_say to pastebin like you did last time?
Of course I need a script run when the resuming part did not properly work.

Hey yes, should be this one: { "trace": { "last_step": "sequence/10/choose/0/sequence/1/choose/0/cond - Pastebin.com

Edit: I can see that was when it triggered but nothing was playing on my speakers. Will manually trigger one when I get home while Spotify is playing on my speakers.