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

UPDATE

9 February 2022

:sparkles: New feature

  • I added a new field resume_this_action. This is a boolean (true or false). The default is true. If set to false the actions which you use in the script will not be resumed if the script is called again. I can best explain this using a real life example.
    I’ve set up a tag scanner on which my kids can scan a card, and then some song will play. If there was already something playing (a TuneIn stream for example) I want that stream to resume after the song finished. However, the kids tend to scan the card a second time when they don’t like the song. If that happens the first kids song which was already playing, would be resumed afterwards. With resume_this_action: false this will not be the case.
  • To let the resume_this_action: false setting work, I’ve added two dynamically created groups. Because this can get messy when you do a script reload while the scripts are running, the script will remove these groups if it is started for the first time (attribute current is 1).

:bug: Bug fixes

  • Resuming of the ytube_mucis_player entities actually works like I intended it now.
1 Like

Hi!

I have no idea how to implement this script. If I copy the lines from your script to script section in HA it tells me that Message malformed: extra keys not allowed @ data[‘google_home_resume’] , I have no idea how to solve this so I have copied the script in the scripts.yaml, also with my

speaker_groups:
    media_player.group_speakers:
      - media_player.living_room
      - media_player.kitchen_speaker

and
primary_spotcast: lastname_name

I have restarted HA, and in developer tools i wanted to test with

service: script.google_home_resume
data:
  target:
    area_id: living_room
    entity_id: media_player.living_room
  action:
    - service: tts.google_say
      data:
        entity_id: media_player.living_room
        message: test test test

And i get the error Failed to call service script.google_home_resume. Unable to find service group.remove

Here is where I don’t know what to do anymore. Maybe I am doing this wrong and I didn’t understand how to implement this.

I have spotify and spotcast integrations installed on HA (in docker).
spot cast works tested with

service: spotcast.start
data:
  entity_id: media_player.living_room
  random_song: true
  force_playback: true

Any help, please?

Hi,

Could you please format your code as code next time (using backticks), or the code button in the GUI (the one with </> as icon)

It seems you don’t have the group integration set up.
Could you try if it works if you just add group: to your configuration.yaml

Many thanks! The last 2 days was a adventure for me to setup this to work and I have stumbeld and asked for help for the simplest issue. It WORKS! Thank you again also for the script!

Update

Version 1.1.1 - 10 February 2022

:star2: Improvements

  • Changed some templates to a more efficient version

Hi,
I have this warning in the log:

Logger: homeassistant.components.group
Source: components/group/__init__.py:292
Integration: Group (documentation, issues)
First occurred: 3:17:50 PM (3 occurrences)
Last logged: 3:17:50 PM

remove:Group 'resume_script_active' doesn't exist!
remove:Group 'resume_active' doesn't exist!
remove:Group 'resume_action_false' doesn't exist!

group: enabled in config

Thanks, i already know what causes this. I will fix this asap.

Update

Version 1.1.2 - 10 February 2022

:star2: Improvements

  • Temporary fix to avoid log warnings when the script tries to remove non existing groups (will replace these 3 choose blocks with a repeat in the next version. (Thanks for reporting this @Manazer )
  • Groups will also be removed when the last instance of the script finishes.

HI

Thanks for this !

have you considered a blueprint ?

Yes, but it wasn’t working. See also this comment I made in the comments on the topic of the TTS-only version of this script: Script to resume radio (TuneIn) and Spotify after TTS on Google Home speakers - #68 by TheFes

1 Like

Sorry, but after update 1.1.2 another error occures:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1834
First occurred: 8:20:07 AM (3 occurrences)
Last logged: 8:20:07 AM

Template variable error: 'integration_enities' is undefined when rendering '{{ 'group.resume_script_active' in integration_enities('group') }}'
Template variable error: 'integration_enities' is undefined when rendering '{{ 'group.resume_active' in integration_enities('group') }}'
Template variable error: 'integration_enities' is undefined when rendering '{{ 'group.resume_action_false' in integration_enities('group') }}'

and warning:

Logger: homeassistant.helpers.script
Source: helpers/script.py:659
First occurred: 8:20:07 AM (3 occurrences)
Last logged: 8:20:07 AM

Error in 'choose[0]' evaluation: In 'template' condition: UndefinedError: 'integration_enities' is undefined

Sorry, that’s what you got when you quickly want to change something on mobile.
There was a typo in the template function. Should be fixed now in 1.1.3.

Update

Version 1.1.3 - 11 February 2022

:bug: Bug fixes

  • Fixed typo in templates. Mental note: do not edit on mobile :wink:

Fully agree :+1:

#Update

Version 1.1.4 - 11 February 2022

:star2: Improvements

  • Better way of removing groups at start and end of script

Hey @TheFes, great work on the comprehensive script.

Any light at the end of the tunnel for no-screen devices playing YouTube Music ? i.e. Google Home Mini.
Why doesn’t it work when there is no screen ? I can see my mini speaker has media_content_id when playing YouTube music, app_name shows “Youtube Music” and I can control the player through Home Assistant when it’s already “loaded” (started by voice and playing something). I’ve been trying different ways to get it to play something using the media_player.play_media service but no luck. When using app_name: Youtube Music then nothing happens but when using app_name: Youtube then it loads the youtube player in Home Assistant (but nothing starts playing because youtube and youtube music content id’s probably don’t match). Anyways, if you have ideas how to get this to work, that would be super cool, probably many other people looking for this solution as well.

Hi. What you describe here is exactly what I do in the script as well, and what does work for devices with a screen. So the problem is not that the media_content_id doesn’t match, the problem is that the app_name: youtube is not supported on devices without a screen.

I’ve tried everything I could think of as app_name for YouTube Music, but nothing worked.
The only way I could get it to work is using the ytube_music_player custom integration, but only if the stream was started using that integration.

Do note that resuming the stream using cast is limited as well, as it will only resume the video/song which was playing, and not the complete playlist/album/channel

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?