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

It doesn’t show it like before, because it uses the default media player to resume the stream, and not the TuneIn cast functionality like Google does.

What do you exactly mean with slow? It tries to resume as soon as your player is idle or off, it could be that there is a delay in getting this state.

The resume is fast, but the connection to the stream is slow. But I think it’s because of the different application behavior (default media player vs tunein app)

Update

Version 1.2.0 - 13 February 2022

:red_circle: BREAKING

  • Made several changes in how data is sent from the Google Home Voice script to this one. So if you update this script, make sure to also update the other one

:star2: Improvements

  • When speaker groups are changed, the volume of the individual group members will now be restored, instead of the volume of the group.
  • Several changes in templates, and how data of cast entities is gathered and stored

:receipt: Docs

  • Added section on why this is not a blueprint
3 Likes

Sorry for all the updates :slight_smile:

Update

Version 1.2.1 - 14 February 2022 :heart_decoration:

:star2: Improvements

  • Added volume restore for ytube_music_player

:bug: Bug fixes

  • Fix for resuming ytube_music_player after Google Home Voice script
1 Like

I’m trying to implement your script to my install but it won’t resume the music after tts. Neither Tunein nor Spotify is working. No errors in logs.

Here is a trace from my last try.
The code I used to try it is the one pointed out by you above:

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

Any idea what I’m doing wrong?
Thanks in advance!

I don’t think you’re doing anything wrong. From the trace I can see that it determined that there was nothing to resume. But i can also see that the targeted media player was indeed playing.
So there might be something wrong in the template.

I’ll have a look tomorrow or Monday.

1 Like

Thanks! Take your time, nothing I need urgently. Just good to know there’s nothing I’m doing wrong :smiley:

Found the reason, however, you’re speaker groups are acting different then mine.
If my speaker group is playing, all members are also shown as playing in Home Assistant.
According to the data in the trace, your speaker group was indeed playing, but the members were idle.

This caused that the speaker group was not recognized as a player to resume.
However, in v1.2.2 this is now changed.

You are indeed right! Interestingly both of the members are stereo pairs, actually. If I select another group containing individual speakers too:
a) stereo paired speakers still show as idle
b) individual speakers show as playing

So seems like a pattern here. Anyone here can confirm this?

Anyway: thanks for implementing this edge use case!

Update

Version 1.2.2 - 19 February 2022

:bug: Bug fixes

  • Fixed unusual case (at least as far as I know) where a speaker group is playing, but the members are not shown as such. In case the speaker group was the target of the script, it would have caused the template to not recognize it as a player to resume

Version 1.2.3 - 19 February 2022

:bug: Bug fixes

  • Addition to v.1.2.2 to make sure the speaker group is also resumed when the target is not the group, but a member of the group

I added another addition to make sure the speaker group is also resumed when the target is not the complete group, but a group member.

That was amazingly fast :smiley: Guess I woke up your curiosity.

It works like a charm now. Thank you very much! Any way to buy you a coffee?

I just created an account for that :slight_smile:

1 Like

Great! Hope others will chime in and give something back!

Another feature request if I may? Ultimately I’d like to use your script to “save” currently playing media and resume same streams/Spotify later on. For example I could resume music at the exact same spot when leaving home and coming back later.

Also I could turn off the Google homes faster when leaving home. Currently I have a 10 minutes delay before turning music off in case I just went to bring out the trash. if I’m able to save current state I could turn everything off instantly and resume later in case I come back within minutes.

Is this possible with how the script works at the moment?

If this is not inside what you are planning for the script I can fully understand, just would find it useful :smiley:

Totally unrelated: regarding the last section of your intro post: wouldn’t this make perfectly sense as an integration? I have no idea of how different the programming would work, I just see that it’s more or less a integration from users perspective. It has a service call, it could generate entities (last playing, last playing entities) and so on.
This could be updated via hacs so the easier updating you wanted could be achieved. As I said: no idea of this would need complete rewrite. Just came to my mind reading what you said about blueprints. If it’s a lot of work, it’s probably better to stay with the current form as its working great!

The resume based on home status, or other events is actually a very good idea. I’ll look into that.

For the integration part, those are written in python, and that’s something I don’t have any experience with. Maybe somewhere in the future, but not in short or middle long term.

Thanks for the coffee!

1 Like

i’m using an automation with google tts and a camera feed when someone has pushed the doorbel. it first says someone is at the door and then pushes the camera feed onto the hubs. Because these are 2 actions with a delay in between, would this script still work to resume what was playing before? since the last action would be a camera stream and not google tts/spotify,etc…

alias: Deurbel TTS Camera Ellie Uitgeschakeld
description: ''
trigger:
  - event_data: {}
    event_type: doorbird_inkom_doorbell_1
    platform: event
condition:
  - condition: device
    type: is_off
    device_id: 56c4684283425a1d40cee71b8c6a370e
    entity_id: switch.camera_ellie_3
    domain: switch
action:
  - service: media_player.volume_set
    target:
      entity_id:
        - media_player.nest_hub
        - media_player.badkamer
        - media_player.google_opslagkamer
        - media_player.grote_slaapkamer_hub
    data:
      volume_level: 0.5
  - service: tts.google_translate_say
    data:
      entity_id: media_player.nest_hub
      message: DE BEL GAAT
      cache: true
  - service: tts.google_translate_say
    data:
      entity_id: media_player.grote_slaapkamer_hub
      message: DE BEL GAAT
      cache: true
  - service: tts.google_translate_say
    data:
      entity_id: media_player.badkamer
      message: DE BEL GAAT
      cache: true
  - service: tts.google_translate_say
    data:
      entity_id: media_player.google_opslagkamer
      message: DE BEL GAAT
      cache: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - data:
      media_player: media_player.nest_hub
    target:
      entity_id: camera.dome_camera
    service: camera.play_stream
  - data:
      media_player: media_player.grote_slaapkamer_hub
    target:
      entity_id: camera.dome_camera
    service: camera.play_stream
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - service: media_player.volume_set
    target:
      entity_id:
        - media_player.nest_hub
        - media_player.badkamer
        - media_player.grote_slaapkamer_hub
        - media_player.google_opslagkamer
    data:
      volume_level: 0.3
  - data: {}
    service: media_player.turn_off
    target:
      entity_id:
        - media_player.nest_hub
        - media_player.grote_slaapkamer_hub
mode: single

Yes, you can sure make that work, however delays are not service calls, so you can not use them directly in the script. I think this will work.

Note that I have split it up into one automation and two scripts. One script could also work, but this will work better with the update I’m about to publish (hopefully later today).
It could be that the nest hubs are resumed before the camera feed starts, in that case you might need to increase the delay at the end of the first script.

# YOUR AUTOMATION, NOW CALLING A SCRIPT #
alias: Deurbel TTS Camera Ellie Uitgeschakeld
description: ''
mode: single
trigger:
  - event_data: {}
    event_type: doorbird_inkom_doorbell_1
    platform: event
condition:
  - condition: device
    type: is_off
    device_id: 56c4684283425a1d40cee71b8c6a370e
    entity_id: switch.camera_ellie_3
    domain: switch
action:
  - service: script.turn_on
    target:
      entity_id: script.google_home_resume
    data:
      variables:
        target:
          entity_id:
            - media_player.nest_hub
            - media_player.badkamer
            - media_player.google_opslagkamer
            - media_player.grote_slaapkamer_hub
      action:
        - service: script.turn_on
          target:
            entity_id: script.de_bel_gaat
### YOUR NEW "DE BEL GAAT" SCRIPT"
de_bel_gaat:
  alias: Script voor als de bel gaat
  sequence:
  - service: media_player.volume_set
    target:
      entity_id:
        - media_player.nest_hub
        - media_player.badkamer
        - media_player.google_opslagkamer
        - media_player.grote_slaapkamer_hub
    data:
      volume_level: 0.5
  - service: tts.google_translate_say
    data:
      entity_id: media_player.nest_hub
      message: DE BEL GAAT
      cache: true
  - service: tts.google_translate_say
    data:
      entity_id: media_player.grote_slaapkamer_hub
      message: DE BEL GAAT
      cache: true
  - service: tts.google_translate_say
    data:
      entity_id: media_player.badkamer
      message: DE BEL GAAT
      cache: true
  - service: tts.google_translate_say
    data:
      entity_id: media_player.google_opslagkamer
      message: DE BEL GAAT
      cache: true
  - service: script.camera_als_de_bel_gaat
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
### SEPARATE SCRIPT FOR THE CAMERA FEEDS, TO NOT DELAY THE OTHERS
camera_als_bel_gaat:
  alias: Script voor de camera feed als de bel gaat
  sequence:
  - wait_template: > 
      {{
        states('media_player.nest_hub') in [ 'idle', 'off']
        and states('media_player.grote_slaapkamer_hub') in [ 'idle', 'off']
      }}
  - data:
      media_player: media_player.nest_hub
    target:
      entity_id: camera.dome_camera
    service: camera.play_stream
  - data:
      media_player: media_player.grote_slaapkamer_hub
    target:
      entity_id: camera.dome_camera
    service: camera.play_stream
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - data: {}
    service: media_player.turn_off
    target:
      entity_id:
        - media_player.nest_hub
        - media_player.grote_slaapkamer_hub
1 Like

Just tested and it seem to be working very well, thank you!

I was wondering if there’s any way of stopping or ‘muting’ the google waking up sound along with your script. Since it seems to be generating a new group everytime, it seems to never stay ‘up’ so it’s doing a beep sound eveytime the script is used. It would be awesome to find a way to mute it.

That sound is the sound the Google Home makes when you start casting to it, it has never annoyed me much, so I didn’t look into it. But it is not related to my script, you will get the sound when you play anything using media_player.play_media or even when you cast something from your phone.

The generation of the groups is completely unrelated to this, this is just to store the entity_id’s of the speakers for which the script is active, they are not used to target or start the Google Homes.

If you search for it on the community forum or Google, one of the solutions seems to be to play a silent mp3 every few minutes to keep them awake.

UPDATE

Version 1.5.0 - 25 February 2022

:red_circle: BREAKING

  • This version brings quite some changes, which will require also to update the Google Home Voice script in case you use that one as well.
  • The script is now split into one main script (script.google_home_resume) and 3 helper-scripts (script.google_home_resume_perform_resume, script.google_home_resume_restore_non_playing and script.google_home_resume_ytube_seek). This allows the resume of different speakers to be ran in parallel, instead of that they will have to wait for each other. So in case you send a TTS and a video feed to one of your Nest Hubs, and only a TTS to a Nest Mini, the Nest Mini can already resume before the Nest Hub is finished. All the scripts are in one yaml-file, the one linked below, please make sure that you copy everything. The script for the resume of the ytube music player integration is at the bottom, if you don’t use that, you don’t need that script and you can leave it out of your configuration.

:star2: Improvements

  • The ytube music player integration will now resume at the right position in the track
  • Added a variable group_id which is just a random string to make sure no groups not belonging to this script are used.
  • Groups are now generated specifically for each script run by use of the the context.id which is used as a suffix of the script.
  • Lot’s of changes in the way data is stored and shared, also in preparation of a new script which can store the state when you eg leave your house, and resume when you come back
1 Like