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

Could you also send the trace for the main script?

Here you go! Main script trace.

Thank you!

Thanks. Spotcast tries to use an account named ekkesa. Did you add that to your configuration?

Based on the information in and trace, your spotcast configuration should look similar like this:

spotcast:
  sp_dc: !secret sp_dc
  sp_key: !secret sp_key
  accounts:
    <redacted>:
      sp_dc: !secret sp_dc_i
      sp_key: !secret sp_key_i
    <redacted>:
      sp_dc: !secret sp_dc_e
      sp_key: !secret sp_key_e

Note that is important that these account names match with the Spotify entity ids

I’m off to bed now, so if this doesn’t help, I’ll look further into it tomorrow.

Correct the order is just different, but otherwise its the same.

spotcast:
  sp_dc: !secret SpotCastMain_sp_dc
  sp_key: !secret SpotCastMain_sp_key
  accounts:
    <redacted>:
      sp_dc: !secret SpotCast1_sp_dc
      sp_key: !secret SpotCast1_sp_key
    <redacted>:
      sp_dc: !secret SpotCast2_sp_dc
      sp_key: !secret SpotCast2_sp_key

The ids also match the media_player.spotify_* format.

Thank you for your effort - Much Appreciated!

Does it work when you run this from Developer Tools > Services?

service: spotcast.start
data:
  entity_id: media_player.living_room_speaker_cast
  force_playback: true
  account: <redacted>

The problem seems to be in Spotcast, so there doesn’t seem much I can do on my end. If the configuration is like you posted above, it should work.

The service call in this post is exactly what was performed in the script. Please check the log for errors concerning spotcast, it could be that your keys are expired, and therefor the account can not be set up. You can test with the other accounts as well (remove the line with account to use the main account).

If it doesn’t work in Developer Tools > Services, you can log an issue on the spotcast Github.

Hi,

It works from services - And without changing anything I tested it again and it worked… Guess it just needed some time. I’m a bit dumbstruck as to what resolved it, but it is working a charm now!

Thank you so much for your effort…

Could it be that you added the additional accounts, but did not reboot afterwards, and in the meantime you did reboot?

Nope - I did work on some other scripts/automations and reloaded them a few times. Perhaps that jogged something… I’m just happy it is working - It always annoyed me that the TTS stops the playback and now that is a thing of the past :slight_smile:

image

Update

Version 2.5.1 - 12 July 2022

:bug: Bug fixes

  • Template fix to prevent errors on missing attributes when creating the data variables
2 Likes

With the first versions of HA 2022.7, I seemed to be getting errors. They stopped popping up in 2022.7.3, but appear to be there again since 2022.7.4 (so I don’t think it necessarily has to do with your script), which causes Google Cast devices not to resume (in my case, Spotify). Still posting them here for your or future reference (others).

Logger: pychromecast.controllers
Source: /usr/local/lib/python3.10/site-packages/pychromecast/controllers/media.py:599
First occurred: 18:29:21 (3 occurrences)
Last logged: 18:29:21

STOP command requested but no session is active.

Traces are here:
CodePile | Easily Share Piles of Code | Pile
CodePile | Easily Share Piles of Code | Pile

Seems to be an issue in the cast integration, which uses pychromecast.
Maybe you can provide more information on this issue:

1 Like

Thank you for this wonderful script. A few questions:

  • Is there a way to specify the interrupting TTS volume within the script? What is the default TTS volume? The current TTS volume seems to be a near perfect volume, however I used a input boolean to specify the interrupting TTS notification volume in my former script, and so I wonder if this would be helpful as I continue to use the script.
  • Is there a way to easily make this script work with Google Podcasts? Thankfully I can just use my voice to ask google to resume the interrupted podcast, however it would be wonderful if your script could automatically resume the podcast.

Thank you for your effort! This script is amazing.

  1. You can set the volume for the TTS message in the script call. See this example: https://github.com/TheFes/HA-configuration/blob/8c75ef28bed4dd6bb393015f16ec640b2e5937a3/include/script/00_general/google_cast/docs/examples_google_home_resume.md#send-tts-and-apply-volume-for-the-tts-message.
    The default_volume in the general variables for the script is used as the volume during the restore afterwards in case it could not be retrieved (as a failsafe)
  2. I will look into this, never used it myself :slight_smile:
1 Like

Update

Version 2.6.0 - 21 July 2022

:sparkles: New features

  • Music playing via Music Assistant will be resumed by sending the TTS or media which interrupts the stream to the MA entity with announce: true. I will change this to creating a snapshot and resuming the snapshot in the future, so I can also support resume of Music Assistant with the Google Home Voice and Google Home Event script.

  • Google Podcasts will be resumed by taking the url of the stream and sending that via media_player.play_media. Do note that Google Podcasts won’t know this happened, which might cause it to resume at a previous podcast when you issue a voice command.

:bug: Bug fixes

  • Template fix: the template to gather the player data was referring to app_name even if it was not defined.
2 Likes

Resuming Google Podcasts works! Thank you!

Hi and thank you so much for this amazing tool! It made my automations so much better!

Today I wanted to add it to another automation, which tells me when my phone is ringing. Unfortunately it does not resume the music after my call ends (waiting for call to end via script).

This is my automation:

alias: 'P10 Anruf '
description: Bei Anruf Notification & Musik stumm / TV Pause
trigger:
  - platform: state
    entity_id:
      - sensor.huaweip10_phone_state
    to: ringing
    id: empfang
condition:
  - condition: zone
    entity_id: sensor.huaweip10_geocoded_location
    zone: zone.home
action:
  - choose:
      - conditions:
          - condition: trigger
            id: empfang
        sequence:
          - service: notify.hass_agent_surfacepro8_lan
            data:
              message: Dein Handy klingelt!
              title: Handy klingelt
          - service: notify.android_tv_fire_tv
            data:
              title: Dein Handy klingelt
              data:
                fontsize: large
                position: right
                duration: 5
                transparency: 25%
                color: black
              message: Es wird ein Anruf empfangen.
          - service: script.google_home_resume
            data:
              action:
                - service: tts.cloud_say
                  data:
                    entity_id: media_player.nest_schlafzimmer
                    message: Dein Handy klingelt.
                - service: script.warten_auf_anruf_ende
                  data: {}
              target:
                entity_id: media_player.nest_schlafzimmer
          - service: scene.turn_on
            target:
              entity_id: scene.schlafzimmer_bunt
            metadata: {}
      - conditions: []
        sequence: []
    default: []
mode: restart

This is my script:

alias: Warten auf Anruf Ende
sequence:
  - wait_for_trigger:
      - platform: state
        entity_id:
          - sensor.huaweip10_phone_state
        to: idle
    continue_on_timeout: false
  - service: notify.hass_agent_surfacepro8_lan
    data:
      message: Anruf beendet
mode: restart

I’ve added a message at the end of the script and the light change at the end of the automation to see, if the automation arrives at those actions.

Everything works fine, the TTS works, but the music doesn’t start. The lights at the end of the automation come with a delay. I don’t know if it has something to do with the scipt, or what.

Am I missing something? I don’t exactly know what the problem is. According to the Trace everything works fine.
Thanks a lot in advance! :slight_smile:

Could you send the trace (json) of both the main script and the helper script? You can upload them to dpaste.org

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

Of course, here it is.

Main Script
Helper Scipt

I thought it might be a problem due to other automations at the same time (I tried calling via another phone that’s also included in a similar automation), but even with them off it does have this issue.

Thank you very much for your support! :slight_smile:

I kinda already know what is going wrong in your case. The helper script checks if the state of the speaker has changed since the main script has started, and if the speaker is playing something (the TTS for example).
However, in your case the player will be idle already, because it was waiting for the script to finish. That is why it didn’t resume.

I have to think a bit on how to solve this particular use case

2 Likes

Thank you very much @TheFes
I hope this doesn’t take too much of your time. Very much appreciated!