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

You are absolutely right, I do not know why it happened… anyway I will repeat the same tests and I will place here the right traces.

I noticed something strange for me, looking at the trace these trace files.
‘’’ YAML
“timestamp”: {
“start”: “2022-11-29T21:21:30.625858+00:00”,
“finish”: “2022-11-29T21:21:36.381221+00:00”
‘’’
“”" YAML
“timestamp”: {
“start”: “2022-11-29T21:21:32.706740+00:00”,
“finish”: “2022-11-29T21:21:35.758982+00:00”
},
“”"
Firstly, the two scripts do not start at the same time, the second thing is that they last several minutes…is it normal? If I make a second announce while the script is not finished, what will happen?

Anyway, please explain me again how to put the code in the same way you do, because…I cannot understand where I am wrong…

The helper script is started by the main script. So first then main script is triggered, it does some stuff like store the current state of the devices, perform the actions etc. Then the helper script is started per device to actually perform the resume.

And the difference between these start and end timestamps is not several minutes, it’s several seconds.
The helper script has to wait until the TTS message has finished to perform the resume, and then has to wait until it is playing again to perform some final actions.
The main script waits until the helper scripts are finished.

“timestamp”: {
“start”: “2022-11-29T21:21:30.625858+00:00”,
“finish”: “2022-11-29T21:21:36.381221+00:00”
}

The difference between these two timestamps is less than 6 seconds

To format text as code you need to use backticks, `

“timestamp”: {
“start”: “2022-11-29T21:21:30.625858+00:00”,
“finish”: “2022-11-29T21:21:36.381221+00:00”
}

Here I see that the script has started at 21:30 and finished at 21:36…while you are telling me that are 6 seconds and not 6 minutes?

@ronyn77 Yes, because it is 21:21:30 and 21:21:36

It is very strange, I make a tts call in my office, where I have the old version yaml which before worked with only one device Ufficio_speaker…today it does not work anymore…it didn’t resume.
I do not know what it is happening.
I made a remote test on my devices at home where there is the new yaml code and I test with the second case, it does the same as yesterday. The camerastudio device goes in idle after. If I call the group tss, all the 3 devices turn off after…I cannot find the logic…

Anyway, later when I will be at home, I will do again the same test and I will send you the traces. If you have any suggestions to some additional test that I could do, to help you to understand more, please let me know.

Do you have two HA servers?
Otherwise i don’t understand why you would have 2 versions of the script

yes, I have two servers. One is in the office and the other one is at home. In the office I have only a google nest mini device, at least for now. While at home I have several devices.

Well, we start again with some tests.
I put the radio on camerastudio and spotify on salottomini. I called the tts service on camerastudio. It does not resume, while the spotify on salottomini continued.
These are the traces :
salottomini&camerastudio - Pastebin.com
salottomini&camerastudio helper - Pastebin.com
I take also the values from the two devices before and after executing the script :

30/11/2022, 21:39:35node: result
msg : Object
object
_msgid: "ca791dc3cfc998c8"
camera: object
entity_id: "media_player.camerastudio"
state: "playing"
attributes: object
volume_level: 0.44999998807907104
is_volume_muted: false
media_content_id: "https://kisskiss.fluidstream.eu/KKNapoli.aac?FLID=3"
media_content_type: "music"
media_position: 1.527398
media_position_updated_at: "2022-11-30T19:39:28.384581+00:00"
media_title: "Night Revolution"
media_artist: "Radio Kiss Kiss Napoli"
app_id: "12F05308"
app_name: "TuneIn Free"
entity_picture_local: "/api/media_player_proxy/media_player.camerastudio?token=695a4f8cb9c6aa79b6eec4a936c4afc52589776075ffe579009e037998ca0027&cache=7c185707e863c710"
device_class: "speaker"
entity_picture: "https://cdn-radiotime-logos.tunein.com/p306849q.png"
friendly_name: "CameraStudio"
supported_features: 152461
last_changed: "2022-11-30T19:39:27.095865+00:00"
last_updated: "2022-11-30T19:39:40.149Z"
context: object
id: "01GK50TK7NBBC2SMJ1BSMYPYVZ"
parent_id: null
user_id: null
timeSinceChangedMs: 21281
30/11/2022, 21:39:35node: result
msg : Object
object
_msgid: "ca791dc3cfc998c8"
salotto: object
entity_id: "media_player.salottomini"
state: "playing"
attributes: object
volume_level: 0.30000001192092896
is_volume_muted: false
media_content_id: "spotify:track:5KTBaWu8IOczQ0sPWzZ7MY"
media_content_type: "music"
media_duration: 218
media_position: 1.94
media_position_updated_at: "2022-11-30T19:39:13.189112+00:00"
media_title: "Dicke Titten"
media_artist: "Rammstein"
media_album_name: "Zeit"
app_id: "CC32E753"
app_name: "Spotify"
entity_picture_local: "/api/media_player_proxy/media_player.salottomini?token=58aab18f6806874297b55e2c78f1ef540389d12b368b98f618aa6a7cd4a10d0b&cache=ba5362b1bf728369"
device_class: "speaker"
entity_picture: "https://i.scdn.co/image/ab67616d0000b2731108fef87966d83948b6a037"
friendly_name: "SalottoMini"
supported_features: 152511
last_changed: "2022-11-30T19:39:12.193511+00:00"
last_updated: "2022-11-30T19:39:13.190Z"
context: object
id: "01GK50SRX6EMY87ZKHXB9M8SZS"
parent_id: null
user_id: null
timeSinceChangedMs: 36196
30/11/2022, 21:40:22node: result
msg : Object
object
_msgid: "9dfbdfb33aa6ad13"
camera: object
entity_id: "media_player.camerastudio"
state: "idle"
attributes: object
volume_level: 0.800000011920929
is_volume_muted: false
media_content_id: "http://10.10.10.181:8123/api/tts_proxy/879afdd3fdf2ef269a14069e1cc4b235363a3022_it_-_google_translate.mp3"
media_duration: 2.544
media_position: 0
media_position_updated_at: "2022-11-30T19:40:18.184188+00:00"
app_id: "CC1AD845"
app_name: "Default Media Receiver"
entity_picture_local: null
device_class: "speaker"
friendly_name: "CameraStudio"
supported_features: 152463
last_changed: "2022-11-30T19:40:18.185Z"
last_updated: "2022-11-30T19:40:18.185Z"
context: object
id: "01GK50VKW8B97KG3KE52AKYWWQ"
parent_id: null
user_id: "075aa9724ad240129f341371eb98aa18"
timeSinceChangedMs: 17209
30/11/2022, 21:40:22node: result
msg : Object
object
_msgid: "9dfbdfb33aa6ad13"
salotto: object
entity_id: "media_player.salottomini"
state: "playing"
attributes: object
volume_level: 0.30000001192092896
is_volume_muted: false
media_content_id: "spotify:track:5KTBaWu8IOczQ0sPWzZ7MY"
media_content_type: "music"
media_duration: 218
media_position: 1.94
media_position_updated_at: "2022-11-30T19:39:13.189112+00:00"
media_title: "Dicke Titten"
media_artist: "Rammstein"
media_album_name: "Zeit"
app_id: "CC32E753"
app_name: "Spotify"
entity_picture_local: "/api/media_player_proxy/media_player.salottomini?token=58aab18f6806874297b55e2c78f1ef540389d12b368b98f618aa6a7cd4a10d0b&cache=ba5362b1bf728369"
device_class: "speaker"
entity_picture: "https://i.scdn.co/image/ab67616d0000b2731108fef87966d83948b6a037"
friendly_name: "SalottoMini"
supported_features: 152511
last_changed: "2022-11-30T19:39:12.193511+00:00"
last_updated: "2022-11-30T19:39:13.190Z"
context: object
id: "01GK50SRX6EMY87ZKHXB9M8SZS"
parent_id: null
user_id: null
timeSinceChangedMs: 83211

As you can see, the value of the media_id of camerastudio after executing the script, remains the tts message.

Executing only the radio on camerastudio (so spotify is not active on salottomini), the script worked.
camerastudio - Pastebin.com
camerastudio helper - Pastebin.com

So definitively in this case it seems that there is something going in conflict with spotify…if spotify is active on a device, the resume it does not work on the other device.

Executing two different radios, one on camerastudio and the other one on salottomini, calling the tts message as group, it resumes on both devices. I noticed one thing…on salottomini I choosed the radio from the HA integration while for camerastudio I put the radio with my voice directly on the google device. On the HA dashboard there is a picture of the radio per camerastudio, while there is not any picture for salottomini. You can see also here below :

30/11/2022, 22:06:52node: result
msg : Object
object
_msgid: "2043e2130ec0f9d3"
camera: object
entity_id: "media_player.camerastudio"
state: "playing"
attributes: object
volume_level: 0.44999998807907104
is_volume_muted: false
media_content_id: "https://kisskiss.fluidstream.eu/KissKiss.aac?FLID=3"
media_content_type: "music"
media_position: 1.231867
media_position_updated_at: "2022-11-30T19:56:30.753760+00:00"
media_title: "Io Tu e KissKiss"
media_artist: "Radio Kiss Kiss"
app_id: "CC1AD845"
app_name: "Default Media Receiver"
entity_picture_local: "/api/media_player_proxy/media_player.camerastudio?token=695a4f8cb9c6aa79b6eec4a936c4afc52589776075ffe579009e037998ca0027&cache=a6d4dc2b8eed182d"
device_class: "speaker"
entity_picture: "https://cdn-profiles.tunein.com/s61955/images/logoq.jpg?t=1"
friendly_name: "CameraStudio"
supported_features: 152463
last_changed: "2022-11-30T19:56:29.697378+00:00"
last_updated: "2022-11-30T19:56:30.754Z"
context: object
timeSinceChangedMs: 635999
30/11/2022, 22:06:53node: result
msg : Object
object
_msgid: "2043e2130ec0f9d3"
salotto: object
entity_id: "media_player.salottomini"
state: "playing"
attributes: object
volume_level: 0.30000001192092896
is_volume_muted: false
media_content_id: "https://azulpop.ozelip.com/8006/stream"
media_position: 0.967816
media_position_updated_at: "2022-11-30T20:06:40.664648+00:00"
app_id: "CC1AD845"
app_name: "Default Media Receiver"
entity_picture_local: null
device_class: "speaker"
friendly_name: "SalottoMini"
supported_features: 152463
last_changed: "2022-11-30T20:06:39.023544+00:00"
last_updated: "2022-11-30T20:06:40.666Z"
context: object
timeSinceChangedMs: 26683
30/11/2022, 22:09:01node: result
msg : Object
object
_msgid: "71ad759e3904effb"
camera: object
entity_id: "media_player.camerastudio"
state: "playing"
attributes: object
volume_level: 0.44999998807907104
is_volume_muted: false
media_content_id: "https://kisskiss.fluidstream.eu/KissKiss.aac?FLID=3"
media_content_type: "music"
media_position: 1.461962
media_position_updated_at: "2022-11-30T20:08:42.878322+00:00"
media_title: "Io Tu e KissKiss"
media_artist: "Radio Kiss Kiss"
app_id: "CC1AD845"
app_name: "Default Media Receiver"
entity_picture_local: "/api/media_player_proxy/media_player.camerastudio?token=695a4f8cb9c6aa79b6eec4a936c4afc52589776075ffe579009e037998ca0027&cache=a6d4dc2b8eed182d"
device_class: "speaker"
entity_picture: "https://cdn-profiles.tunein.com/s61955/images/logoq.jpg?t=1"
friendly_name: "CameraStudio"
supported_features: 152463
last_changed: "2022-11-30T20:08:41.589552+00:00"
last_updated: "2022-11-30T20:08:42.879Z"
context: object
timeSinceChangedMs: 32346
30/11/2022, 22:09:01node: result
msg : Object
object
_msgid: "71ad759e3904effb"
salotto: object
entity_id: "media_player.salottomini"
state: "playing"
attributes: object
volume_level: 0.30000001192092896
is_volume_muted: false
media_content_id: "https://azulpop.ozelip.com/8006/stream"
media_content_type: "music"
media_position: 1.373739
media_position_updated_at: "2022-11-30T20:08:44.929271+00:00"
media_title: "no title"
media_artist: "no artist"
app_id: "CC1AD845"
app_name: "Default Media Receiver"
entity_picture_local: null
device_class: "speaker"
friendly_name: "SalottoMini"
supported_features: 152463
last_changed: "2022-11-30T20:08:43.521449+00:00"
last_updated: "2022-11-30T20:08:44.931Z"
context: object
timeSinceChangedMs: 30427
'''
I do not know why it is in this way.

Test 1:
The Helper script was not completed yet, It was still waiting for a state change on the player.

When you only target the camerastudio entity, the salottomini player will not be affected by the script. It will not not be stopped, so there is no need to take any action on it.
Please allow the script to complete before sending the traces.
As the salottomini player was not involved, it has nothing to do with Spotify. The data in the helper script looked fine, it has all the information that it was playing on TuneIn and that is should be resumed.
It was still waiting on a state change of the player.

Test 2:
The TuneIn cast integration on the speaker itself provides a picture, which is also used after the resume (proably a picture of the song which was playing, which will now remain static).
The stream started by HA doesn’t have a picture before the resume, so it won’t have a picture after the resume as well.

General:
I don’t see anything related to Spotify here. Can you retest the scenario which didn’t work and send traces after the script is completed.

I will repeat the test a second time and I will wait more…anyway I have pasted you also the values of the objects before and after starting the script, just to show you the time within. We are speaking for around 30 seconds. How much time I have to wait to finish the script?

Until it’s finished. Which you can check by pressing under “(i) Information” when you are in Settings > Scripts

I made the same test 1, but I waited more…at least 5 minutes.
These are the results from the object values (you can see how much time has been passed between the two values check):

1/12/2022, 19:11:59node: result
msg : Object
object
_msgid: "86b5a4cc2e987554"
camera: object
entity_id: "media_player.camerastudio"
state: "playing"
attributes: object
volume_level: 0.44999998807907104
is_volume_muted: false
media_content_id: "https://4c4b867c89244861ac216426883d1ad0.msvdn.net/radiodeejay/radiodeejay/master_ma.m3u8"
media_duration: -1
media_position: 11.216203
media_position_updated_at: "2022-12-01T17:06:26.964858+00:00"
app_id: "CC1AD845"
app_name: "Default Media Receiver"
entity_picture_local: null
device_class: "speaker"
friendly_name: "CameraStudio"
supported_features: 152463
last_changed: "2022-12-01T17:06:16.000200+00:00"
last_updated: "2022-12-01T17:06:26.967Z"
context: object
timeSinceChangedMs: 357399
1/12/2022, 19:11:59node: result
msg : Object
{ _msgid: "86b5a4cc2e987554", salotto: object }
1/12/2022, 19:17:13node: result
msg : Object
object
_msgid: "65725649fa587d79"
camera: object
entity_id: "media_player.camerastudio"
state: "off"
attributes: object
device_class: "speaker"
friendly_name: "CameraStudio"
supported_features: 152461
last_changed: "2022-12-01T17:17:19.106Z"
last_updated: "2022-12-01T17:17:19.106Z"
context: object
timeSinceChangedMs: 7916
1/12/2022, 19:17:13node: result
msg : Object

During all the time while the script was trying to resume, it showed:

Afterwards I try to download the 2 traces. The first has been downloaded as always.

camerastudio - Pastebin.com

For the helper i noticed the same as yesterday, but I didn’t pay so much attention why it took so much time. In the explorer It was like seeing a download when it tries to start the download, but it does not start immediately…usually happens if the server for such a reason cannot immediately give the start…what I am thinking is that probably the helper was still running. After a couple of other minutes, it downloaded the file.

camerastudio helper - Pastebin.com

I returned on the dashboard page, and I saw that camerastudio was turned off.

Probably the helper cannot complete its task.

You can clearly see the helper script was still running on line 5 of the trace
It keeps hanging on this step:

          - alias: "Wait until player is idle again, and all other scripts are finished"
            wait_template: >
              {%- set current = expand(states.group
                                  | selectattr('entity_id', 'search', 'group.resume_script_target_')
                                  | rejectattr('entity_id', 'search', context)
                                  | map(attribute='entity_id')
                                  | list) | map(attribute='entity_id') | list
              %}
              {%- set checklist = [player.entity_id] + player.members %}
              {{
                dashboard_cast or
                expand(checklist) | rejectattr('state', 'in', ['idle', 'off', 'paused']) | list | count == 0
                and current | select('eq', player.entity_id) | list | count == 0
              }}

Could you check in Developer Tools > States if there are existing entities which start with group.google_home_ and have this media_player entity as it’s member?

If so, please try to restart HA and try again.

This is what I found if I put in the filter group: Imgur: The magic of the Internet

After restarting, it is not anymore present in the states.

But again, after calling the script, it is present again: Imgur
OfCourse, also this time, it didn’t resume

As the groups are not defined in your configuration, but created by the script, they will never appear after a reboot, so that was as expected.

For some reason the script was still in a waiting state. At least you have a clean slate now, could you do some more tests.

I’ll also think about implementing a maximum script run time.

as stated above, it returned at the moment I call back the script. Afterwards I turned off the google home device on which I started the script and the groups disappeared again.
I would like to point you that this happen, only if on one of the devices is active Spotify and I do not need to call the tts message on the device which is running Spotify. I have just now load on camerastudio a radio, I tried the script and immediately it resumed.

I found the reason why your groups are acting weird. Something changed in the cast integration, and now groups are showing as playing when only one member is playing, or when they are all playing different things.

I think this is the cause of your problems. I’m currently working on it.
I’m testing this new version, if everything works correct I will release it in as version 2022.12.0

Update

2022.12 Happy Holidays; let’s bring the family together

It’s December which is the time to come together with your family for the holidays. That is exactly what this version does, it brings all scripts together into one package!

:globe_with_meridians: GENERAL

:star2: Improvements

  • All scripts related to the Google Home devices are now combined in one package. This brings toghether the Google Home Resume, Google Home Voice and Google Home Event script. For me it makes it easier to update them, I can make more efficient usage of YAML anchors,so I don’t need to update templates and settings in 3 places.
  • Instructions on installation and usage of the package moved to GitHub
  • A lot of bigger and smaller template fixes
  • More usage of YAML anchors, which also reduces the total number of lines for the combined scripts

:recycle: RESUME SCRIPT

:star2: Improvements

  • Template to generate the target_list has been improved
  • Apparantly Google Home Speaker groups now show as playing when a member is playing media. Not only when the group has been targeted. The template to generate the player_data has been amended to avoid showing groups as playing when they are not.

:bug: Bug fixes

  • There was bug when using the automation which would start the script for a group member when that was the target of a service call, but the group was playing. This caused the resume to fail.

Please try the new version, I hope that will solve the issues.