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

Then that might be a dead end.

A somewhat workaround solution might be to send a text command from HA to Google Assistant speaker: ā€œplay the last song on youtube musicā€. Then it should at least restart the last played song. Could even try something like ā€œcontinue playing the last song from where it was last stoppedā€. Google Assistant is pretty smart and something like this might work, havenā€™t tested tho. I know there were some addons and ways to send text commands this way, donā€™t remember exactly. Something to do with webhooks and IFTTT. Maybe you get some good ideas through this angle :stuck_out_tongue:

Could also be done with something like Assistant Relay, but that will be difficult because people will use different languages for their Google Home. So you canā€™t just use a command in English.

You will also run in issues when there are different people in the same house all using their own account, how will you know which account was used to play it.

For YouTube Music the best solution at this moment is ytube_music_player. But then you canā€™t start it by voice command, youā€™ll have to use the media browser of HA.

Itā€™s unfortunate that the same information as is available though the Spotify integration is not visible.

I use your script, it works well! Great work, thak you! I have two small problems with it. I only use to resume to tunein, and the resume is a bit slow(start to conect to stream quickly, but the connecting is slow). Itā€™s a lot slower, like Iā€™m just starting tunein. Why? The other problem is that after resume, on my hub, the screen is blank, not showing what it is playing(not showing anything). Am i doing something wrong, or is it normal?

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