Script to resume Google Cast devices after they have been interrupted by any action

Background

This package started somewhere in 2020 as a simple script to resume a Google Cast device after it was interrupted by a TTS message. During the years more functionality was added, the Cast devices can also be resumed after playing an audio file, or casting a Home Assistant Dasboard.

In the past years I also created others scripts using the Google Home Resume script. There is a script to determine on which device a question was asked and send a TTS or play a file on that specific device. Another script stores the state of the cast devices in a template sensor, so it can be resumed in later (for example store it when you leave home, and restore it when you arrive home again).

All these scripts are now combined in one package


Latest version

2022.12 Happy Holidays; let’s bring the family together

It’s December which is the time to come together with your family for the holidays. That is exactly what this version does, it brings all scripts together into one package!

:globe_with_meridians: GENERAL

:star2: Improvements

  • All scripts related to the Google Home devices are now combined in one package. This brings toghether the Google Home Resume, Google Home Voice and Google Home Event script. For me it makes it easier to update them, I can make more efficient usage of YAML anchors,so I don’t need to update templates and settings in 3 places.
  • Instructions on installation and usage of the package moved to GitHub
  • A lot of bigger and smaller template fixes
  • More usage of YAML anchors, which also reduces the total number of lines for the combined scripts

:recycle: RESUME SCRIPT

:star2: Improvements

  • Template to generate the target_list has been improved

  • Apparantly Google Home Speaker groups now show as playing when a member is playing media. Not only when the group has been targeted. The template to generate the player_data has been amended to avoid showing groups as playing when they are not.

  • (2022.12.1) New setting: max_runtime has been added. Here you can define a time period after which the scripts should be stopped, to avoid them waiting for ages. Take into account that this has to be longer than the interruption, so if you play a song of 10 minutes, the max_runtime has to be set longer than that

  • (2022.12.1) The spotcast.start service call is now issued twice, this should help in resuming Spotify

:bug: Bug fixes

  • There was bug when using the automation which would start the script for a group member when that was the target of a service call, but the group was playing. This caused the resume to fail.

How to install and use the package

As of version 2022.12 the instructions how to install the package are moved to my GitHub repository, so it is easier for me to keep them up to date.


Buy me a coffee

If you like this script, please feel free to buy me a coffee (I might spend it on another beverage though).
In case you decide to do so, thanks a lot!

Buy Me A Coffee

38 Likes

That looks great @TheFes . However it is not clear no me how to call the script. I have automation that use google say to broadcast a message to google nest hub everytime the garage door is opened. How could I use the script to resume spotify music or youtube video after the message is played?

@jpleite What is the current YAML code to perform that action? If it is service call (so service: some.service) you can use it directly with this script. If not, you need to create a script and call that.

If you want you can paste the YAML code here, and I can show how it should work in cooperation with the script.

BTW, if you are sending a TTS message, you can also use the TTS specific script linked in the Background section.

So is this script also usable without spotify?
I don’t use speakergroups, can is just delete those lines?

Hi @TheFes. Thanks for your prompt reply. As an example, when the garage door is opened I have an automation to notify several devices including google nest via tts.cloud_say. Bellow the automation

alias: Garage Door Open (Tileboard; TTS; iOS; WebOS)
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 127dea8fbf242d4f9a0f2b849474aec9
    entity_id: binary_sensor.garage_door_sensor
    domain: binary_sensor
condition: []
action:
  - event: tileboard
    event_data:
      command: open_garagedoorentry
  - service: notify.mobile_app_milkphone_13_pro_max
    data:
      title: Attention!
      message: Garage is door is open!
      data:
        entity_id: camera.garage
  - service: notify.milkhome
    data:
      message: O portão da garagem foi aberto!
  - service: notify.living_room_tv
    data:
      message: O portão da garagem foi aberto!
  - service: notify.basement_room_tv
    data:
      message: O portão da garagem foi aberto!
  - service: tts.cloud_say
    data:
      entity_id: media_player.kitchen_google_hub
      message: O portão da garagem foi aberto!
mode: single
initial_state: true

Yes, just remove the variables speaker_groups and primary_spotccast

@jpleite
As far as I can see, the only action which will stop Google Cast devices, and will need resuming, is the last one (the TTS).
You could also use the TTS specific script in that case, but this script will also work.
After you included the script in your HA setup, and amended the variables for your needs, you can amend the automation to this:

alias: Garage Door Open (Tileboard; TTS; iOS; WebOS)
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 127dea8fbf242d4f9a0f2b849474aec9
    entity_id: binary_sensor.garage_door_sensor
    domain: binary_sensor
condition: []
action:
  - event: tileboard
    event_data:
      command: open_garagedoorentry
  - service: notify.mobile_app_milkphone_13_pro_max
    data:
      title: Attention!
      message: Garage is door is open!
      data:
        entity_id: camera.garage
  - service: notify.milkhome
    data:
      message: O portão da garagem foi aberto!
  - service: notify.living_room_tv
    data:
      message: O portão da garagem foi aberto!
  - service: notify.basement_room_tv
    data:
      message: O portão da garagem foi aberto!
  - service: script.google_home_resume
    data:
      action:
        - service: tts.cloud_say
          data:
            entity_id: media_player.kitchen_google_hub
            message: O portão da garagem foi aberto!
mode: single
initial_state: true

Hi @TheFes . It worked like a charm! Thanks for your help and great work!

Added To Do section to first post

To Do

  • Dynamic creation of the group.resume_active, so it is possible to track for which entities the script is active
  • Make it possible to queue actions if the script is called multiple times for the same entity (this will requre the script to be cut into different scripts)
  • Add some information on how do the TTS actions like I did in the Google Home Say script which is no longer maintained. (basically it is calling this script as an action in the Google Home Resume script and provide the target (example)).

Update

1 February 2022

  • The script will now create a group (group.resume_active) and add the entties which are going to be resumed there. It will remove the entity from the group after it has been resumed. Entities which are listed in this group will be ignored, so only the original stream will be resumed.
  • Replaced persistant notifications with log messages. In case no valid service calls are provided, the script will abort and an error will be logged. In case thre are valid service calls, but also other invalid actions, a warning will be logged.
  • Adjusted some templates to avoid warnings in the log.

I’m trying to give this one a try. I’ve added the script in the scripts section of my Home Assistant and can call the script by a call. When I use the google_translate_say there, it works like a charm and tts my message. If I was playing a Youtube cast on my Nest Hub, will this resume afterwards or? That’s the thing I still can’t get to work.

1 Like

Well, not at this moment, I originally focused on audio (Spotify/TuneIn)
I can have a look, I think the most tricky part will be to get the the right moment the video was stopped.

That should be really awesome. I gave the broadcast function a try as well, so the TTS messages are just an ‘overlay’ of the content behind it. That is in my opinion the best approach, but I believe that’s even more difficult :wink: I tried it with: Assistant Relay

I’ve tried Assistant Relay, not it is not maintained anymore. And as Dutch is not supported, it was no solution for me.

Also it always sends the broadcast to all devices in your home, and it announced the broadcast, which can be annoying.

Hi, I’m having some issues implementing this script and getting the error ‘Failed to call service script/google_home_resume_wip. Error rendering data template: UndefinedError: ‘iif’ is undefined’.

No doubt it is something I have or haven’t done but any ideas where I’m going wrong, please? Would love to get this set up, my family get quite annoyed with my announcements interrupting their music and have to start the music from the beginning.

Oh, sorry, should have waited to after today to publish it. The iif function is new in 2022.2 which will be released today.
I also forgot to remove the _wip part

Yeah, got the same issue with that :wink: All devices & no dutch language. But the overlay was nice though. I’m interested if you can manage to let something like the youtube stream continue after TTS. Will follow this one :wink: Thanks for your effort!

Update

3 February 2022

  • Added requirements (HA Version 2022.2) to first post, also added requirements for Spotify resume there.
  • Removed WIP (work in progress) references in script object_id and alias.

The script now fires up with no errors after the HA core update but I’m still facing some issues.

if I fire up:

service: script.google_home_resume
data:
  action:
    - service: tts.cloud_say
      data:
        entity_id: media_player.dressing_room_speaker
        message: test

It plays the TTS announcement, makes a few google sounds but the music does not resume, any idea where I’m going wrong please?

I’m currently debugging a bit myself, but could you try if it works like this:

service: script.google_home_resume
data:
  target:
    entity_id: media_player.dressing_room_speaker
  action:
    - service: tts.cloud_say
      data:
        entity_id: media_player.dressing_room_speaker
        message: test
1 Like