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

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

Thanks for the quick answer! I have looked into it. I was just wondering since if you send more than one TTS message back to back to any google home device, it will make the beep twice using your code. If you use the standar TTS feature, you will hear it the first time, then the second TTS message wont have a beep until it automatically goes off after 4-5 mins of inactivity.

I tested it very quickly last night, Iā€™ll have to do some more testing.

My script doesnā€™t do anything different to cause the bleep, if it is already awake, it wonā€™t bleep.
It could be that what you are hearing is the volume adjust sound, it adjust the volume before the TTS starts, and then back to the original value after the TTS.
That also causes some (maybe annoying) sound.

Iā€™ll test it again when I get home, thank you for all the work!

Some errors after upgrade:

Error doing job: Task exception was never retrieved
6:26:05 PM ā€“ (ERROR) core.py
00 šŸ”Š Google Home Resume - Restore non playing: Error executing script. Invalid data for call_service at pos 5: invalid slug resume_active__32a633f7fb2fb7e001a09d0a7b772914 (try resume_active_32a633f7fb2fb7e001a09d0a7b772914) for dictionary value @ data['object_id']
6:26:05 PM ā€“ (ERROR) Script
Template variable warning: 'group_id' is undefined when rendering 'resume_active_{{ group_id }}_{{ context.id }}'
6:26:05 PM ā€“ (WARNING) helpers/template.py
Error in 'choose[0]' evaluation: In 'template' condition: TypeError: first argument must be string or compiled pattern
6:26:05 PM ā€“ (WARNING) helpers/script.py

Double underscore

I am testing this, but then the (spotify) song is resumed, it doesnt resume from when it was interrupted, but from a much earlier point (sometimes 30 seconds, sometimes more). Volume is restored correctly.
Any ideas?
Thanks!

Still now with 1.5.1?

I actually let Spotcast handle that.
Never noticed it myself, but Iā€™ll do a check.

Itā€™s OK now. Thank you

Iā€™ve been tring to get this script integrated in my setup but I cannot get it to work properly.
From what Iā€™m guessing Iā€™ve got 2 different problems:

  1. A google speaker pair doesnā€™t seem to display what it is playing anymore (the google app will say itā€™s playing but doesnā€™t display the contents (eg name of the radiostation) anymore) so when trying the resume script it doesnā€™t resume playback. with single speakers or in a group that doesnā€™t include a speaker pair it is working as expected.

  2. there are 2 camerastreams being started, one of them is on the google hub in the kitchen so looks like to get that working I need to incorporate the same solution you suggested for ā€œthe cobra 666ā€?

this is the automation Iā€™m using at the moment:

alias: Melding Deurbel
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.deurbel
    from: 'off'
    to: 'on'
condition: []
action:
  - service: camera.snapshot
    data:
      filename: /config/www/images/deurbel.jpg
    target:
      device_id: 10eb922exxxxxxxxxxxxxxx318c3eafc38
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
  - service: notify.mobile_app_yvette_gsm
    data:
      message: Er staat iemand aan de deur! šŸšŖ
      title: Hoi Yvette,
      data:
        image: /local/images/deurbel.jpg
  - service: notify.mobile_app_mvt_gsm
    data:
      message: Er staat iemand aan de deur! šŸšŖ
      title: Hey Maurice,
      data:
        image: /local/images/deurbel.jpg
  - service: notify.lg_webos_tv_ym
    data:
      message: ER STAAT IEMAND AAN DE DEUR. šŸšŖ
  - service: script.google_home_resume
    data:
      action:
        - service: media_player.volume_set
          data:
            volume_level: 0.25
          target:
            entity_id:
              - media_player.kantoor
              - media_player.badkamer
              - media_player.woonkamer_speakers_2
        - service: media_player.play_media
          data:
            media_content_id: http://192.168.2.3:8123/local/audio/duckbell.mp3
            media_content_type: music
          target:
            entity_id:
              - media_player.kantoor
              - media_player.badkamer
              - media_player.woonkamer_speakers_2
  - service: browser_mod.popup
    data:
      command: popup
      title: De deurbel gaat
      large: 'true'
      card:
        entity: camera.voordeurcamera_profile_000
        type: picture-entity
        camera_view: live
        deviceID:
          - 06xxxx-xxxx0ec
  - service: camera.play_stream
    target:
      device_id: 10eb922e29xxxxxxxxx18c3eafc38
    data:
      media_player: media_player.keuken
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: media_player.media_stop
    data: {}
    target:
      device_id: 18f56952xxxxxxxxxxx682d6a05d38d
  - service: browser_mod.close_popup
    data: {}
mode: single

Speaker pairs indeed seem to have some issues with displaying the data. There is nothing I can do about that, best would be to log an issue for Home Assistent Core related to the cast integration.

For you second the approach I suggested earlier to create two scripts seems best for your automation as well.

1 Like

I donā€™t think itā€™s an issue with home assistant. the google home app doesnā€™t display the data either. As far as I can find it has something to do with the patent case from google/sonos.

I will try out the suggested scripts for the automation, thanks!

Sorryā€¦ Iā€™m new in HA and Node red. How do i ā€œinstallā€ this script so the music repeats when the TTS has been played from Node red? The announcement work from Node red to speakers, but it stops ex. playing TuneIn Radio

I have no experience with node red, but I assume you can call the script like shown above from Node-Red as well.

When I just copy past the script in a new script create window (in yaml mode) it always says: Message malformed: extra keys not allowed @ data[ā€˜google_home_resumeā€™]

What am I doing wrong?