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

Sorry wrong typed. It’s Plex Server
With plex media the service call and state of google home is as below. Can you show me how to get it or where I need to update for my own without effect to all script

- service: media_player.play_media
        data:
          media_content_id: 'plex://{"library_name": "tin_tuc", "shuffle": "1", "allow_multiple":"1"}'
          media_content_type: music
        target:
          entity_id: media_player.loa

image

That screenshot doesn’t look promisng, as there is not a lot data which refers to the service call you also placed.
For example, the library name is not shown, and also the shuffle and repeat settings are not shown.

I’m not sure where this /library/metadata/12950 refers to, but from what I can see in the media_player data there is not much information to base the resume on.

Thanks for give me suggestions
It’s seem I have to try hard to resume it :joy::joy:. I’ll try base on your config and hope it’s work for me.

Hi TheFes,
after upgrade 2.4.0 more errors emerged.
The trace: CodePile | Easily Share Piles of Code

2022-05-06 21:35:24 ERROR (MainThread) [homeassistant.components.script.google_home_resume] 00 🔊 Google Home Resume: Error executing script. Error for wait_template at pos 21: In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current'
2022-05-06 21:35:24 ERROR (MainThread) [homeassistant.components.automation.google_home_notification_branicka_open] Google_Home_Notification_Branicka_open: Error executing script. Error for call_service at pos 1: In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current'
2022-05-06 21:35:24 ERROR (MainThread) [homeassistant.components.automation.google_home_notification_branicka_open] Error while executing automation automation.google_home_notification_branicka_open: In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current'
2022-05-06 21:56:53 ERROR (MainThread) [homeassistant.components.script.google_home_resume_perform_resume] 00 🔊 Google Home Resume - Perform resume: Error executing script. Error for wait_template at pos 1: In 'template' condition: TemplateError: str: Invalid domain name ''
2022-05-06 21:56:53 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1859, 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 2, in top-level template code
  File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 303, in getitem
    return obj[argument]
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 643, in __getattr__
    raise TemplateError(f"Invalid domain name '{name}'")
homeassistant.exceptions.TemplateError: str: Invalid domain name ''
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/condition.py", line 678, in async_template
    value = info.result()
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 265, in result
    raise self.exception
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 525, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 411, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TemplateError: str: Invalid domain name ''
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 428, in _async_run
    return await self.script.async_run(script_vars, context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1513, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 405, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 449, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 472, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 447, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 577, in _async_wait_template_step
    if condition.async_template(self._hass, wait_template, self._variables, False):
  File "/usr/src/homeassistant/homeassistant/helpers/condition.py", line 680, in async_template
    raise ConditionErrorMessage("template", str(ex)) from ex
homeassistant.exceptions.ConditionErrorMessage: In 'template' condition: TemplateError: str: Invalid domain name ''
2022-05-06 21:56:53 ERROR (MainThread) [homeassistant.components.script.google_home_resume] 00 🔊 Google Home Resume: Error executing script. Error for wait_template at pos 21: In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current'
2022-05-06 21:56:53 ERROR (MainThread) [homeassistant.components.automation.google_home_notification_branicka_open] Google_Home_Notification_Branicka_open: Error executing script. Error for call_service at pos 1: In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current'
2022-05-06 21:56:53 ERROR (MainThread) [homeassistant.components.automation.google_home_notification_branicka_open] Error while executing automation automation.google_home_notification_branicka_open: In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current'

Thanks, I’ll look into this.

1 Like

Yeah I have the same error too. and the speakers are not set back to the original volume.

00 🔊 Google Home Resume: Error executing script. Error for wait_template at pos 21: In 'template' condition: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current'

@Manazer @bomba23
Should be working again now

Update

Version 2.4.1 - 7 May 2022

:bug: Bug fixes

  • Fix for restore in case there were non playing entities

It was an easy fix, because I actually already fixed it earlier, but I guess did not properly save it. Anyway, should be working again now

It appears to be activating all speakers (Google sound), but gives an error…

CodePile | Easily Share Piles of Code | Pile

@samuel81 Ah, that error is caused by the former helper script which is now left in state unavailable and now has no attribute current anymore.
However, that check is only performed at the end of the script, when the dynamic groups are removed. There is no link with the service call not working, and there are no errors or issues mentioned in the trace regarding that.

I see you are calling an other script, maybe there is something related to that? It could be that the restore is started too soon, and the entities are restored before the TTS is started. Could you maybe also send the TTS script code, and a trace of the perform resume script?

Update

Version 2.4.2 - 8 May 2022

:star2: Improvements

  • Changes some more service calls to the new 2022.5 versions

:bug: Bug fixes

  • Fix error caused by orphan helper script which restored the state of non playing entites before 2.4.0

Sure!

CodePile | Easily Share Piles of Code and CodePile | Easily Share Piles of Code | Pile

This is the automation calling the script:

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

Actual script has changed since this post: Script to resume Google Cast devices after they have been interrupted by any action - #258 by samuel81

It looks like the resume is indeed started to soon.
To see what is happening I need to know what is exactly in script.test_for_tts

1 Like

Update

Version 2.4.3 - 9 May 2022

:star2: Improvements

  • Add continue_on_error: true to service calls provided in action

:bug: Bug fixes

  • Volume restore and turning off players with screen which were not playing was performed too soon
1 Like

I’ll give it a try, the script is here: CodePile | Easily Share Piles of Code | Pile

Okay, and what does the trace of that script show, was it performed correctly? I don’t see anything in it which would cause issues, besides if the template you are using is not rendered correctly.

BTW I I were you I would create a template sensor with the message in the attribute, then you don’t need the script, and you could just add a tts.cloud_say service call with a reference to the sensor as message.

And I also see that you are using a way to get the states of other entities which is adviced against in the docs. See: Templating - Home Assistant and especially:

Warning

Avoid using states.sensor.temperature.state, instead use states('sensor.temperature'). It is strongly advised to use the states(), is_state(), state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

And to get the current hour you can simply use now().hour. It will already be an integer, so no need to use | int then

1 Like

I hope you don’t mind, but I went through your template and made some changes, and put some comments in it. Hope you didn’t translate it to English for this though, as Dutch would have been perfectly fine :slight_smile:

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

1 Like

It works fine now, resumes fine and also plays the script when there isn’t any media playing! Excellent, all back to how things were before 2022.5 :wink:

Wow! You’ve really put quite the work in going through the script! I’ll check the template later tonight, everything is in English for the wife but she went on a presumably long leave of absence - unfortunately. I’ll get back to you on the template/script later - again thx a million. The random act of kindness…

I’ve been trying to get this to run for a couple weeks now and keep running into issues. I think I’ve resolved my issue with the Spotify Integration and I walked through a Spotcast error but for the life of me I cannot get this script to work. Here’s the log entry for running the script, any support is greatly appreciated.

Logger: homeassistant.components.script.google_home_resume_perform_resume
Source: custom_components/spotcast/spotcast_controller.py:174
Integration: Script (documentation, issues)
First occurred: 10:32:02 PM (8 occurrences)
Last logged: 10:35:11 PM

00 :loud_sound: Google Home Resume - Perform resume: Resume needed?: choice 1: Resume playing: Spotify?: Primary spotcast account?: default: Error executing script. Unexpected error for call_service at pos 1: ‘NoneType’ object has no attribute ‘get’
00 :loud_sound: Google Home Resume - Perform resume: Resume needed?: choice 1: Resume playing: Spotify?: Error executing script. Unexpected error for choose at pos 1: ‘NoneType’ object has no attribute ‘get’
00 :loud_sound: Google Home Resume - Perform resume: Resume needed?: choice 1: Error executing script. Unexpected error for choose at pos 4: ‘NoneType’ object has no attribute ‘get’
00 :loud_sound: Google Home Resume - Perform resume: Error executing script. Unexpected error for choose at pos 3: ‘NoneType’ object has no attribute ‘get’
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 447, in _async_step
await getattr(self, handler)()
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 680, in _async_call_service_step
await service_task
File “/usr/src/homeassistant/homeassistant/core.py”, line 1627, in async_call
task.result()
File “/usr/src/homeassistant/homeassistant/core.py”, line 1670, in _execute_service
await self._hass.async_add_executor_job(
File “/usr/local/lib/python3.9/concurrent/futures/thread.py”, line 58, in run
result = self.fn(*self.args, **self.kwargs)
File “/config/custom_components/spotcast/init.py”, line 170, in start_casting
client = spotcast_controller.get_spotify_client(account)
File “/config/custom_components/spotcast/spotcast_controller.py”, line 183, in get_spotify_client
return spotipy.Spotify(auth=self.get_token_instance(account).access_token)
File “/config/custom_components/spotcast/spotcast_controller.py”, line 174, in get_token_instance
dc = self.accounts.get(account).get(CONF_SP_DC)
AttributeError: ‘NoneType’ object has no attribute ‘get’

@tjuel Could you send me a trace, see this link for more info.

I will need the trace of the both the Google Home Resume main script, and the Google Home Resume - Perform Resume script, for a script call in which you got this error.

But it looks like you’ve set your primary_spotcast setting incorrect. I’ve seen this error before when the spotcast.start service is used with an account, which is not configured as account.

Please have a look at this and check if everything is configured correctly.
If you are using multiple Spotify integrations, you also need to configure spotcast for all Spotify accounts.