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

@samuel81 (I replied to myself apparently)
Please try test v2.2.2.2 or even test v2.2.2.3

1 Like

edit: hold on

Ah right, missed something there
This one should do the trick. Version number is 2.2.2 again as I thought it was ready to merge.

I’m sorry, I need to revise what I wrote before:

Tested both, test v2.2.2.2 works, test v2.2.2.3 doesn’t turn up the volume. Both traces are here:

V2.2.2.2:

V2.2.2.3:

V2.2.2:

Scenario’s:

Scenario 1:

alias: 'Test: Spotify Resume with TTS Script'
description: Test automation for Spotify Resume, using script by TheFes
trigger:
  - platform: state
    entity_id: input_select.matthijs_status_dropdown
    to: Extended Away
condition: []
action:
  - service: script.turn_on
    target:
      entity_id: script.google_home_resume
    data:
      variables:
        action:
          - service: script.test_for_tts
            target:
              entity_id: media_player.all_speakers
            data:
              entity: media_player.all_speakers
            extra:
              volume: 0.5
mode: single

Scenario 2 ( script.turn_on with variables under data .):

alias: 'Test2: Spotify Resume with TTS Script (Duplicate)'
description: Test automation for Spotify Resume, using script by TheFes
trigger:
  - platform: state
    entity_id: input_select.matthijs_status_dropdown
    to: Extended Away
condition: []
action:
  - service: script.turn_on
    target:
      entity_id: script.google_home_resume
    data:
      variables:
        target:
          entity_id: media_player.all_speakers
        action:
          - service: script.turn_on
            target:
              entity_id: script.test_for_tts
            data:
              variables:
                entity: media_player.all_speakers
            extra:
              volume: 0.4
mode: single

Tested with:

  • On some players there is music playing.
  • On no players there is music playing.
  • Music was played with Spotify.

You are right, I got confused myself.
Setting the volume with scenario 2 will not work, it will only work with scenario 1.
The separate target is only used for resume, not for the volume_set action.

I will have to clear that up in the docs.

So, please use scenario 1. I will look into why the volume isn’t working though.

1 Like

micro version numbers are back test 2.2.2.5 is ready for testing.
Volume setting and resume should work with scenario 1, thinking of adding an option to add targets to extra to make scenario 2 work

1 Like

Works! Great!

https://www.codepile.net/pile/vQv9kjYq

Just uploaded v2.2.2.6

That should also allow scenario 2 if you use it it like this:

alias: 'Test2: Spotify Resume with TTS Script (Duplicate)'
description: Test automation for Spotify Resume, using script by TheFes
trigger:
  - platform: state
    entity_id: input_select.matthijs_status_dropdown
    to: Extended Away
condition: []
action:
  - service: script.turn_on
    target:
      entity_id: script.google_home_resume
    data:
      variables:
        target:
          entity_id: media_player.all_speakers
        action:
          - service: script.turn_on
            target:
              entity_id: script.test_for_tts
            data:
              variables:
                entity: media_player.all_speakers
            extra:
              volume: 0.4
              entity_id: media_player.all_speakers
mode: single

Unfortunately not with Scenario 2, but still works with Scenario 1…
Trace for Scenario 2: CodePile | Easily Share Piles of Code

Okay, I will dig into that, seems to be another issue.
Thanks for testing

Found it :slight_smile: With the new v2.2.2 v2.3.0 you should be able to use the script like I suggested.

#Update

Version 2.3.0 / 2.3.1 - 14 April 2022

:sparkles: New features

  • Targets (entity_id, device_id and/or area_id) can be added to extra or script_extra so volume can be applied to those entities for a specific service call in which they are not mentioned as targets themselves (eg when calling a script)

:star2: Improvements

  • Moved the volume change before the service call
  • As volume changes to a Google Home speaker group are no longer synced to their members (due to patent reasons) the volume set in the extra or script_extra setting will be sent to the members of the speaker group.
  • Targets entered in extra or script_extra will also be taken into account for resuming afterwards

:bug: Bug fixes

  • Removed a bug where group members of a speaker group were added to the players to be resumed, while they were not playing
  • (2.3.1) Fix for targets provided under extra
1 Like

Hmm… some errors after upgrade:

Google Home Resume script track: CodePile | Easily Share Piles of Code

Error doing job: Task exception was never retrieved
5:18:24 PM – (ERROR) helpers/condition.py
00 🔊 Google Home Resume - Restore non playing: Error executing script. Error for wait_template at pos 1: In 'template' condition: TemplateError: str: Invalid domain name ''
5:18:24 PM – (ERROR) Script
[([::ffff:c0a8:116]):8009] Failed to connect to service ServiceInfo(type='host', data=('[::ffff:c0a8:116]', 8009)), retrying in 5.0s
5:16:15 PM – (ERROR) /usr/local/lib/python3.9/site-packages/pychromecast/socket_client.py - message first occurred at 5:16:15 PM and shows up 2 times

The error seems to be in the script to restore the non playing entities, could you send the trace of that script?

Oh wait, skip that, it’s not in that script. Let me investigate this a bit further

Could you give 2.3.2 a try?
For what I can see one of your speakers was unavailable. This caused that an incorrect value was stored in the player data, which then caused that the whole player data list was stored as a string.
And this then caused that incorrect data was sent to the script to restore the non playing entity.

Logger: homeassistant.components.script.google_home_resume
Source: helpers/script.py:1397
Integration: Script ([documentation](https://www.home-assistant.io/integrations/script), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+script%22))
First occurred: 8:40:59 AM (1 occurrences)
Last logged: 8:40:59 AM

00 🔊 Google Home Resume: Error executing script. Error rendering template for variables at pos 9: TemplateRuntimeError: No test named 'off'.

Sorry about that, should be fixed in 2.3.3

Different error:

Logger: pychromecast.socket_client
Source: /usr/local/lib/python3.9/site-packages/pychromecast/socket_client.py:396
First occurred: 10:16:28 AM (2 occurrences)
Last logged: 10:16:28 AM

[([::ffff:c0a8:116]):8009] Failed to connect to service ServiceInfo(type='host', data=('[::ffff:c0a8:116]', 8009)), retrying in 5.0s
[([::ffff:c0a8:116]):32001] Failed to connect to service ServiceInfo(type='host', data=('[::ffff:c0a8:116]', 32001)), retrying in 5.0s

And if tried youtube music interrupted by TTS (not working):
Logger: homeassistant
Source: helpers/template.py:409
First occurred: 10:20:32 AM (2 occurrences)
Last logged: 10:22:03 AM

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 407, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1820, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 926, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 8, in top-level template code
jinja2.exceptions.UndefinedError: 'time' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 412, in _async_run
    return await self.script.async_run(script_vars, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1275, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 359, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 379, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 832, in _async_variables_step
    self._variables = self._action[CONF_VARIABLES].async_render(
  File "/usr/src/homeassistant/homeassistant/helpers/script_variables.py", line 62, in async_render
    rendered_variables[key] = template.render_complex(
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 132, in render_complex
    return value.async_render(variables, limited=limited, parse_result=parse_result)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'time' is undefined

I don’t think the PyChromecast error is something I can fix.
The other error I’m not sure, could you send me the trace?