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

No, nothing changed with 2022.10.
I will look at your traces later today/tomorrow

1 Like

Do you have the latest script and automation buddy?

Yes, I’m using the latest script and automation. As far as I’m aware, no changes have been made, it just stopped working but considering it seems to be only me with the issue I guess it is something to do with my config.

I kinda forgot this, sorry about that.
Had a quick look, and there seems to be something wrong with the data which is sent to the helper script.
I have to dig into this a bit further

1 Like

It generated a string with the player data instead of a dictionary. What I can see is that the artist which was playing was James O’Bryan and I think the quote in the artist name caused the issue.
Was this a one time issue, or do you still have it now? If so, could you also send me a trace of that?

hmm that’s odd, I always listen to that station and haven’t had this issue before. I still have the issue now but I will do some tests tomorrow with different radio stations/artists to make sure the issue isn’t only with that station (Pretty sure it is happening on multiple radio stations though). I will send the trace tomorrow with a different artist/station if the issue occurs. Thank you.

I tried with a few different stations/artists but it still doesn’t work.

Google Home Resume Trace - Oct 13 9:27 AM - Codeshare
Helper Script Trace -Oct 13 9:28 AM - Codeshare

Looking at it on a computer now, instead of a mobile phone screen, and it looks like this is causing the issue:

media_content_type: <MediaType.MUSIC: 'music'>

Could you show a screenshot from Developer Tools > States for the entity playing?

For me it looks like this:


media_content_type is just simply music

For some reason for you this seems to be something else.
How are you initiating these TuneIn streams?

Screenshot 2022-10-13 at 11.52.13

I initiate the stream by asking google “play :radio station name:” this activates a routine to ensure the radio station is played via TuneIn radio. Routine is “Play :radio station name: on TuneIn”.

device_class: speaker
icon: mdi:google-home
friendly_name: Main bedroom speaker
supported_features: 152461
volume_level: 0.550000011920929
is_volume_muted: false
media_position_updated_at: '2022-10-13T10:51:10.569136+00:00'
app_id: 12F05308
app_name: TuneIn Free
media_content_id: http://media-ice.musicradio.com/RadioXLondonMP3
media_position: 2.19739
media_content_type: music
media_title: Toby Tarrant
media_artist: Radio X London
entity_picture_local: >-
  /api/media_player_proxy/media_player.main_bedroom_room_speaker?token=9a0f51201bf154c365381ffb4ca0a22c6d4f8b44059f166b62ac2c9b46ec10b7&cache=bef7577b9469bef5
entity_picture: https://cdn-radiotime-logos.tunein.com/p0q.png

This looks fine, but this is the data in your trace:
"player_data": "[{'data_source': 'resume_script', 'entity_id': 'media_player.dining_room_tv_cast', 'state': 'off', 'members': [], 'type': 'no screen'}, {'data_source': 'resume_script', 'entity_id': 'media_player.grace_bedroom_speaker', 'state': 'off', 'members': [], 'type': 'no screen'}, {'data_source': 'resume_script', 'entity_id': 'media_player.kitchen_display', 'state': 'off', 'members': [], 'type': 'screen'}, {'data_source': 'resume_script', 'entity_id': 'media_player.living_room_speaker', 'state': 'off', 'members': [], 'type': 'no screen'}, {'data_source': 'resume_script', 'entity_id': 'media_player.main_bedroom_room_speaker', 'state': 'playing', 'app_name': 'TuneIn Free', 'volume_level': 0.44999998807907104, 'media_content_id': 'http://media-ice.musicradio.com/RadioXLondonMP3', 'media_title': 'Toby Tarrant', 'media_artist': 'Radio X London', 'media_content_type': <MediaType.MUSIC: 'music'>, 'entity_picture': 'https://cdn-radiotime-logos.tunein.com/p0q.png', 'media_position': 170.96515899046327, 'members': [], 'type': 'no screen'}]",

There you see that the media_content_type for media_player.main_bedroom_room_speaker is <MediaType.MUSIC: 'music'>

That’s not shown in the screenshot and the data you just shared.

Could you place the following code in Developer Tools > Templates, and replace the entity_id in the 3rd line with an entity which is playing on TuneIn? After that, please share the result.

          {%- if player_data is undefined %}
            {%- set ns = namespace(info=[]) %}
            {%- for entity in expand('media_player.replace_here')  %}
                {%- set general = dict(
                                        data_source = 'resume_script',
                                        entity_id = entity.entity_id,
                                        state = entity.state
                                        )
                %}
                {%- if entity.state == 'playing' %}
                  {%- set additional = dict(
                                            app_name = entity.attributes.app_name,
                                            volume_level = entity.attributes.volume_level,
                                            media_content_id = entity.attributes.get('media_content_id', 'no media_content'),
                                            media_title = entity.attributes.get('media_title', 'no title'),
                                            media_artist = entity.attributes.get('media_artist', 'no artist'),
                                            media_content_type = entity.attributes.get('media_content_type', 'no type'),
                                            entity_picture = entity.attributes.get('entity_picture', 'no pic'),
                                            media_position =  as_timestamp(now()) | round(2)
                                                              - as_timestamp(entity.attributes.get('media_position_updated_at', now())) | round(2) 
                                                              + entity.attributes.get('media_position', 0)
                                            )
                  %}
                {%- endif %}
              {%- set ns.info = ns.info + [ dict(general, **additional) if additional is defined else general ] %}
            {%- endfor %}
            {%- set player_data = ns.info %}
          {%- endif %}
          {{ ns.info }}

I see what you mean…

[{'data_source': 'resume_script', 'entity_id': 'media_player.main_bedroom_room_speaker', 'state': 'playing', 'app_name': 'TuneIn Free', 'volume_level': 0.550000011920929, 'media_content_id': 'http://media-ice.musicradio.com/RadioXLondonMP3', 'media_title': 'Toby Tarrant', 'media_artist': 'Radio X London', 'media_content_type': <MediaType.MUSIC: 'music'>, 'entity_picture': 'https://cdn-radiotime-logos.tunein.com/p0q.png', 'media_position': 2.1412319904632566}]

[{‘data_source’: ‘resume_script’, ‘entity_id’: ‘media_player.main_bedroom_room_speaker’, ‘state’: ‘playing’, ‘app_name’: ‘TuneIn Free’, ‘volume_level’: 0.550000011920929, ‘media_content_id’: ‘http://media-ice.musicradio.com/RadioXLondonMP3’, ‘media_title’: ‘Toby Tarrant’, ‘media_artist’: ‘Radio X London’, ‘media_content_type’: <MediaType.MUSIC: ‘music’>, ‘entity_picture’: ‘https://cdn-radiotime-logos.tunein.com/p0q.png’, ‘media_position’: 2.1412319904632566}]

Yes, and for me, also started via voice command, it is:

[
  {
    "data_source": "resume_script",
    "entity_id": "media_player.zolder_mini_martijn",
    "state": "playing",
    "app_name": "TuneIn Free",
    "volume_level": 0.25,
    "media_content_id": "https://playerservices.streamtheworld.com/api/livestream-redirect/VERONICAAAC.aac?dist=tunein&DIST=TuneIn&TGT=TuneIn&maxServers=2&partnertok=eyJhbGciOiJIUzI1NiIsImtpZCI6InR1bmVpbiIsInR5cCI6IkpXVCJ9.eyJ0cnVzdGVkX3BhcnRuZXIiOnRydWUsImlhdCI6MTY2NTY2MzU2NiwiaXNzIjoidGlzcnYifQ.KmLqblCVVrgKhXHMk-TnIrpJWn_FUdPM-DeEdmYX4aM",
    "media_title": "Montell Jordan - This Is How We Do It",
    "media_artist": "Radio Veronica",
    "media_content_type": "music",
    "entity_picture": "https://cdn-albums.tunein.com/gn/758Q45RMG7g.jpg",
    "media_position": 1.520998
  }
]

Ah crap.
I was running a custom version of the cast integration, keeping my players idle when not playing anything.
I now removed that integration, and now I see the same as you do. So apparantly there has been a change in the cast integration which is causing this. Let me dig into this further.

1 Like

Really weird, I have tried multiple stations and it all shows <MediaType.MUSIC: 'music'>

I even tried Spotify and that also shows <MediaType.MUSIC: 'music'> (not sure if Spotify should or not)

Update

Version 2.8.5 - 13 October 2022

:star2: Improvements

  • Improved template to gather speaker data by removing dedundant templates

:bug: Bug fixes

  • Fix for media_content_type being stored incorreclty causing the data to be saved as string
  • Additional check in automation if groups are already created before starting the script
1 Like

Should be fixed now!

@TheFes You’re the best!! It is working now! My family will no longer hate my automations (well not as much). Thank you!!

I follow this thread, because I heavily use your script. I just wanted to say that the kindness and time you invest in whatever people experience with it - its real nice to see that.

7 Likes

I’ve been trying to get it up and running again. But I keep getting the same error when I try to run the script and automation.

Logger: homeassistant.components.script.google_home_resume
Source: helpers/script.py:409
Integration: Script (documentation, issues)
First occurred: 13:15:05 (1 occurrences)
Last logged: 13:15:05

00 - Google Home Resume: Error executing script. Error rendering template for variables at pos 7: ValueError: dictionary update sequence element #0 has length 1; 2 is required

Could you please send me the trace of the script. Thanks