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

I think this can already be a workaround for time being:

  1. Download a long silent mp3
  2. Change the way you call the script to
          - service: script.google_home_resume
            data:
              action:
                - service: tts.cloud_say
                  data:
                    entity_id: media_player.nest_schlafzimmer
                    message: Dein Handy klingelt.
                  extra:
                    wait: true
                - service: media_player.play_media
                  data:
                    entity_id: media_player.nest_schlafzimmer
                    media_content_id: location of your silent mp3
                    media_content_type: music
                - service: script.warten_auf_anruf_ende
                  data: {}
              target:
                entity_id: media_player.nest_schlafzimmer
          - service: scene.turn_on
            target:
              entity_id: scene.schlafzimmer_bunt
            metadata: {}
  1. Create a new script with a short delay (2 or 3 seconds) and an action to stop the media player
  2. Call that script as the last action of you existing script, but make sure to use script.turn_on so it wonā€™t wait on it to finish

You can listen to the tts service call event, and store the message service data in your helper

Update

Version 1.7.0 - 04 August 2022

:sparkles: New feature

  • Support for Google Home Resume automation for easy resume without the need to call the script manually. More info here.

:star2: Improvements

  • Music Assistant resume now uses the snapshot function

Hi,

Just another thought I had.
Only the TTS message actually interrupts the current audio, the phone call is rather irrelevant here.
Why not let it resume the audio after the TTS, but set the volume to 0 as long as the call is going on. Then you only have to set the volume to the original state after the call finished.

Hi!

I just wanted to try your first idea. Thanks for your second Idea, it is perfect! Works great and I think I will keep it that way. The only ā€œproblemā€ is that it has a little delay (Music ā†’ TTS ā†’ Music ā†’ Mute), but it works good enough. Thanks again for your idea and brilliant service! :slight_smile:

Very cool projectā€¦ I had been imagining something like this for a while and finally got around to searching. This is exactly what I needed! Iā€™ve got everything working but have a question about the new automation. It doesnā€™t yet support resuming Youtube for the screen-devices, right? I only see reference to the audio sources in the automation itself and it doesnā€™t seem to resume youtube for me out of the box. Let me know if Iā€™m missing somethingā€¦ Thanks!

Could you send a trace of the resume script and the helper script when this happens?

The automation checks for the media_player.play_media service call, but the actual resume is handled by the resume script.
It should resume YouTube as well, for now I donā€™t see a reason why it shouldnā€™t

I seem to be receiving some errors in the log, since upgrading to this version early this afternoon:

Logger: homeassistant.helpers.script
Source: helpers/script.py:405
First occurred: 18:33:36 (3 occurrences)
Last logged: 18:33:36

Error in 'if[0]' evaluation: In 'template' condition: UndefinedError: 'homeassistant.helpers.template.Wrapper object' has no attribute 'volume_level'

and

Logger: homeassistant.helpers.template
Source: helpers/template.py:141
First occurred: 18:33:06 (2 occurrences)
Last logged: 18:33:36

Template variable warning: 'action' is undefined when rendering '{{ iif(action is mapping,[ action ], action) | selectattr('service', 'defined') | list }}'
Template variable warning: 'homeassistant.helpers.template.Wrapper object' has no attribute 'mass_playing' when rendering '{{ player.mass_playing }}'

Paste ofCode
and
Paste ofCode

(Sorry, CodePile is now paid service). Did I do something wrong?

No, nothing wrong on your end, just some loose ends I did not tie well :slight_smile:

1 Like

Update

Version 2.7.1 - 9 August 2022

:bug: Bug fixes

  • Template fixes to avoid log warnings about missing variables

Thanks, many thanks, Iā€™ll try again tomorrow morning :slight_smile:

Thanks for the replyā€¦ on closer inspection, it appears the resume automation isnā€™t firing at all. This is the output of a node red service_call event listener when Iā€™m interrupting Youtube on the display. Do you spot anything that could account for the automation not triggering?

Thanks!

{"event_type":"call_service","event":{"domain":"media_player","service":"play_media","service_data":{"media_content_id":"media-source://camera/camera.doorbell","media_content_type":"image/jpeg","announce":"true","entity_id":"media_player.kitchen_hub"}},"origin":"LOCAL","time_fired":"2022-08-10T02:32:30.102140+00:00","context":{"id":"01GA2SH9MNB3YGPXPW0XC6KFJ3","parent_id":null,"user_id":"553b5af1974343deb7e35dfa1bb13d44"}}

Update

Version 2.8.0 - 18 August 2022

:sparkles: New features

  • Resume after sending an image (eg a camera snapshot after somebody rang the doorbell) using media_player.play_media is now supported (also in the automation added in v2.7.0). While sending the image, the speaker is not in state playing so there is a new variable added to define a delay after which the resume will be started. The default is 20 seconds (if not provided).
2 Likes

I had this script up and running on version 2.6.0 - 21 July 2022, It was working beautifully. Now Iā€™ve attempted to migrate to version 2.8.0 - 18 August 2022 and I canā€™t seem to get it to work. It seems to play Spotify, play for the tts interruption, then I can tell Spotify is reloaded on the speaker, but it doesnā€™t seem to have loaded the media to play. Iā€™m at a loss.

Logger: homeassistant.core
Source: custom_components/spotcast/spotify_controller.py:104
Integration: Spotcast (documentation, issues)
First occurred: X
Last logged: X

Error executing service: <ServiceCall spotcast.start (c:01GBT68CW0TT2TNMH0DW3ACARZ): entity_id=media_player.main_living_area, force_playback=True, limit=20, repeat=off, random_song=False, ignore_fully_played=False, offset=0, shuffle=False, start_volume=101>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 1731, in catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1756, in _execute_service
    await self._hass.async_add_executor_job(
  File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/spotcast/__init__.py", line 190, in start_casting
    spotify_device_id = spotcast_controller.get_spotify_device_id(
  File "/config/custom_components/spotcast/spotcast_controller.py", line 209, in get_spotify_device_id
    spotify_cast_device.startSpotifyController(access_token, expires)
  File "/config/custom_components/spotcast/spotcast_controller.py", line 88, in startSpotifyController
    sp.launch_app()
  File "/config/custom_components/spotcast/spotify_controller.py", line 104, in launch_app
    raise LaunchError(
pychromecast.error.LaunchError: Timeout when waiting for status response from Spotify app

There are issues with Spotcast currently, this is not related to the script itself.

4 Likes

Thank you. I was banging my head against the wall wondering how I misconfigured your script. Good to know its an outside issue. Hopefully itā€™s up and running soon. Issues Ā· fondberg/spotcast Ā· GitHub

I assume this issue is only related when trying to resume Spotify? This script works fine for me when listening to the radio and receiving tts

Yes, Spotcat is only used to resume Spotify

For those in a hurry about Spotcast: hereā€™s an working branch
Fixed in original branch.

Is there an working example (script) to test this? Iā€™m trying to resume after an image and canā€™t manage to get it working.

Ok, it seems to work with local media, but not with images from URL.

Here's the working example
alias: Resume after image cast
sequence:
  - service: script.google_home_resume
    data:
      action:
        - service: media_player.play_media
          data:
            media_content_type: image/jpg
            media_content_id: /media/media/1200px-Oranges.jpg
          target:
            entity_id: media_player.nesthub1a61
      target:
        entity_id: media_player.nesthub1a61
mode: single