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

Remove the speaker_groups variable from the settings, or leave it empty if you think you might need it in the future.

Call the script like this.

- service: script.turn_on
  target:
    entity_id: script.google_home_resume
  data:
    variables:
      target:
        entity_id:
          - media_player.google_mini_1
          - media_player.google_mini_2
      action:
        - service: script.turn_on
          target:
            entity_id: script.your_ding_dong_script

As you are calling a script as action, the resume script will not know which speakers are the actual target. That’s why you have to provide that using the target variable

1 Like

So I have to create another script that will call your script and i will put my media players in target and radio script to resume.
I will test it. thank you for quick feedback

No, I thought you already had a script, I mixed the dingdong and the radio part. Have a look at the examples I provided in the docs

Update

Version 2.1.0 - 10 April 2022

:star2: Improvements

  • primary_spotcast no longer required when only one Spotify account is used
  • Script will check if the primary_spotcast which is entered (if needed) is correct, and if not issue a warning in the log. In that case the primary spotcast account will be used to avoid errors in the script
  • Target entities, areas and devices can be entered anywhere in the service call where that would normally be allowed, so directly in the service call, or under data or under target

well… I looked and overlooked, but I don’t get the point.
I should put google resume script to run in same automation I have for doorbell ?
To have:

  1. youtube music playing something on google mini
  2. someone to press doorbell
  3. automation that runs doorbell theme and at the end of doorbell theme to wait 2 seconds and run google resume script ?
    and i let your script untouched?

If you post the code of your current automation I can show how to add the script in it

Update

Version 2.2.0 / 2.2.1 - 12 April 2022

:star2: Improvements

  • The extra options for the script (wait, volume and screen_tts) can now also be defined under data in the service call, using the key script_extra. This way the options can also be added using the GUI. Example on how to use this.

:bug: Bug fixes

  • Fix for resuming Spotify when only one Spotify account is used, and primary_spotcast is not provided.
  • (2.2.1) Fixed a bug in setting the volume in the extra or script_extra settingpotify account is used, and primary_spotcast is not provided.

- id: '1616028594810'
  alias: 'Sonerie '
  description: Sonerie
  trigger:
  - platform: state
    entity_id: sensor.sonerie_action
    to: 'on'
  condition: []
  action:
  - service: notify.mobile_app_sm_g991ad
    data:
      message: A sunat la usa
  - service: notify.doorbell_calls
    data:
      message: '{{now().strftime("%d.%m.%Y")}};{{now().strftime("%H:%M:%S")}} ; Acasa
        este: {{states("sensor.whoishomedoorbell")}}'
  - service: media_player.play_media
    data:
      media_content_id: http://192.168.0.102:8123/local/sounds/sonerie.mp3
      media_content_type: music
    target:
      device_id:
      - xxx
      - xxx
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: media_player.media_stop
    target:
      device_id:
      - xxx
      - xxx
  mode: single

Okay, some questions:

  • What kind of entities are targeted by this notify service? Are these also Google Home devices, or phones?
  • How long is this sonerie.mp3? Is it 10 seconds or longer?
  • Why did you remove the device_ids, they are just randomly generated ids, nobody can do anything with those :wink:
  1. targets are google mini x 2 pcs
  2. sonerie.mp3 is longer because it is a whole song, but i don’t want to listen all song when somebody rings the bell. If needed I can crop it to exact match 10 seconds to not run media stop service
  3. well…habits I guess

delay is not supported by the script yet, so with the current mp3 file, and the current options provided by the script this will not work. So if you would crop it to 10 seconds, this should work for the script:

- id: '1616028594810'
  alias: 'Sonerie '
  description: Sonerie
  trigger:
  - platform: state
    entity_id: sensor.sonerie_action
    to: 'on'
  condition: []
  action:
  - service: notify.mobile_app_sm_g991ad
    data:
      message: A sunat la usa
  - service: notify.doorbell_calls
    data:
      message: '{{now().strftime("%d.%m.%Y")}};{{now().strftime("%H:%M:%S")}} ; Acasa
        este: {{states("sensor.whoishomedoorbell")}}'
  - service: script.google_home_resume
    data:
      action:
        - service: media_player.play_media
          data:
            media_content_id: http://192.168.0.102:8123/local/sounds/sonerie.mp3
            media_content_type: music
          target:
            device_id:
              - xxx
              - xxx
  mode: single
1 Like

Looks like as of version 2.1.0 (also in 2.2.0) the volume isn’t set correct anymore. I don’t know if you can see that in the trace, but in case you can; here they are:
CodePile | Easily Share Piles of Code (2.2), CodePile | Easily Share Piles of Code (2.1.0)

I would need a trace of the resume script, not of your automation.

@samuel81 Did you already update to 2.2.1? There is a fix for setting the volume in that version.

1 Like

I did, just now. Similar issue.

CodePile | Easily Share Piles of Code – the proper one, sorry.

So, the scenario is this:

  • On some players there is music playing - the volume for the TTS isn’t adjusted
  • On no players there is music playing - the volume for the TTS isn’t adjusted.

My observation is that there seems to be some volume adjustment, but that the volume adjustment appears relative to the volume of the speaker before the TTS is send out. Does that make sense?

Ah, I think I see what’s happening.
The volume set action is after the actual service call is performed. I don’t know what is in your script, but possible there is a delay or wait action in it. As you use service: script.your_script and not service: script.turn_on, the volume action will only be performed after your tts script is fully completed. With script.turn_on it would be performed immediately after the service call.

What is actually done in that script? Why is an additional script required here?

I created a test 2.2.2 where the volume is set first, could you test that?

Thanks! Tested that! Trace is here: CodePile | Easily Share Piles of Code
Similar outcome as 2.2.1.

My automation(s) run the following:

service: script.turn_on
target:
  entity_id: script.google_home_resume
data:
  variables:
    action:
      - service: script.test_for_tts
        target:
          entity_id: media_player.all_speakers
        data:
          entity: media_player.all_speakers
        extra:
          volume: 0.5

They’ll call my the script.test_for_tts (which isn’t perfect yet, but still) that supplies for the TTS messages based on TOD and some other factors (I guess you now know how we try to run the household :wink: ). Since I have other automations (with similar script calls) for other TTS announcements (on different speakers, so it doesn’t wake me up when the wife gets to work on my day off), they will use different TTS scripts that are called in a similar manner as above.

alias: Informational TTS Announcements
sequence:
  - service: tts.cloud_say
    data:
      entity_id: '{{ entity }}'
      message: >

        {% if now().strftime("%H")|int < 12 %} Good morning, today is
        {{states.sensor.full_date.state}}, and the time is
        {{states.sensor.time.state}}. It's currently
        {{states.sensor.openweathermap_temperature.state|round(1)}} degrees in
        Woonplaats, but feels like
        {{states.sensor.openweathermap_feels_like_temperature.state|round(1)}}
        degrees. The weather is currently
        {{states.sensor.openweathermap_condition.state}}, later it's expected to
        be {{states.sensor.openweathermap_forecast_condition.state}}.The
        temperature today will be between
        {{states.sensor.openweathermap_forecast_temperature_low.state|round(1)}}
        and {{states.sensor.openweathermap_forecast_temperature.state|round(1)}}
        degrees.

        {% elif now().strftime("%H")|int < 18 %} Good afternoon, welcome home.
        Today is {{states.sensor.full_date.state}}, and the time is
        {{states.sensor.time.state}}.

        {% elif now().strftime("%H")|int < 18 %} Good afternoon, welcome home.
        Today is {{states.sensor.full_date.state}}, and the time is
        {{states.sensor.time.state}}.

        {% elif now().strftime("%H")|int < 22 %} Good evening, welcome home.
        Today is {{states.sensor.full_date.state}}, and the time is
        {{states.sensor.time.state}}.

        {% elif now().strftime("%H")|int < 22 %} Good evening, welcome home.
        Today is {{states.sensor.full_date.state}}, and the time is
        {{states.sensor.time.state}}.

        {% else %} Good night, welcome home. {% endif %}

        {% if now().strftime("%H")|int > 12 and now().strftime("%H")|int < 18
        and is_state('input_boolean.house_is_vacuumed_today', 'on') %} George
        has cleaned the house in your absence, so the downstairs floor should be
        clean.

        {% elif now().strftime("%H")|int > 12 and now().strftime("%H")|int < 18
        and is_state('input_boolean.house_is_vacuumed_today', 'off') and
        is_state('sensor.vacuum_status', '6') %} George hasn't been asked to, so
        unfortunately the house has not been cleaned in your absence.    

        {% elif now().strftime("%H")|int > 18 and now().strftime("%H")|int < 22
        and is_state('input_boolean.house_is_vacuumed_today', 'off') and
        is_state('sensor.vacuum_status', '6') %} George hasn't been asked to, so
        unfortunately the house has not been cleaned in your absence.

        {% elif now().strftime("%H")|int > 12 and now().strftime("%H")|int < 18
        and is_state('input_boolean.house_is_vacuumed_today', 'off') and
        is_state('sensor.vacuum_status', '4') %} George has been asked to clean
        the house but ran into an obstacle, so unfortunately the house has not
        been cleaned in your absence.

        {% elif now().strftime("%H")|int > 18 and now().strftime("%H")|int < 22
        and is_state('input_boolean.house_is_vacuumed_today', 'off') and
        is_state('sensor.vacuum_status', '4') %} George has been asked to clean
        the house but ran into an obstacle, so unfortunately the house has not
        been cleaned in your absence.

        {% elif now().strftime("%H")|int > 18 and now().strftime("%H")|int < 22
        and is_state('input_boolean.house_is_vacuumed_today', 'on') %} George
        has cleaned the house in your absence, so the downstairs floor should be
        clean.

        {% else %}{% endif %}

         {% if states('sensor.vacuum_dustbin_days_since_last_emptied')|int >= 4 %} Please empty the Robot Vacuum Cleaner Dustbin, as George will otherwise not be able to clean today.

         {% elif states('sensor.vacuum_dustbin_days_since_last_emptied')|int < 4 %}

         {% elif states('sensor.vacuum_dustbin_days_since_last_emptied')|int <= 4 %} Please empty the Robot Vacuum Cleaner Dustbin, as George will otherwise not be able to clean tomorrow.

        {% else %}{% endif %}

        {% if now().strftime("%H")|int > 12 and now().strftime("%H")|int < 19
        %}             
            {% set bathroomplant = states('sensor.bathroom_plant_days_since_last_watered') | float %}
            {% set toiletplant = states('sensor.toilet_plant_days_since_last_watered') | float %}
            {% set studycactus = states('sensor.study_cactus_days_since_last_watered') | float %}
            {% set bedroomplant = states('sensor.bedroom_plant_days_since_last_watered') | float %}
            {% if bathroomplant < 14 and toiletplant < 14 and studycactus < 21 and bedroomplant < 14 %}
            {% elif bathroomplant >= 14 or toiletplant >= 14 or studycactus >= 21 or bedroomplant >= 14  %} Please pay some attention to your plants, as they need watering and your care to flourish.
            {% else %}
            {% endif %}

        {% elif now().strftime("%H")|int >= 19 and now().strftime("%H")|int <
        22  %} 
            {% set bathroomplant = states('sensor.bathroom_plant_days_since_last_watered') | float %}
            {% set toiletplant = states('sensor.toilet_plant_days_since_last_watered') | float %}
            {% set studycactus = states('sensor.study_cactus_days_since_last_watered') | float %}
            {% set bedroomplant = states('sensor.bedroom_plant_days_since_last_watered') | float %}
            {% if bathroomplant < 14 and toiletplant < 14 and studycactus < 21 and bedroomplant < 14 %}
            {% elif bathroomplant >= 14 or toiletplant >= 14 or studycactus >= 21 or bedroomplant >= 14  %} Please pay some attention to your plants, as they need watering and your care attention to flourish.
            {% else %}
            {% endif %}

        {% else %}{% endif %}

        {% if now().strftime("%H")|int > 12 and now().strftime("%H")|int < 19
        %}             
            {%- if is_state('sensor.mijnafvalwijzer_vandaag', 'papier-pmd') -%}
            The trash was collected today. Please retrieve the blue and orange coloured container.
            {%- elif is_state('sensor.mijnafvalwijzer_vandaag', 'duobak') -%}
            The trash was collected today. Please retrieve the black and green coloured container.
            {%- elif is_state('sensor.mijnafvalwijzer_vandaag', 'kerstbomen') -%}
            Your christamas tree was collected today.       
            {% else %}
            {%- endif %}

        {% elif now().strftime("%H")|int >= 19 and now().strftime("%H")|int <
        22  %} 
            {%- if is_state('sensor.mijnafvalwijzer_vandaag', 'papier-pmd') -%}
            The trash was collected today. Please retrieve the blue and orange coloured container.
            {%- elif is_state('sensor.mijnafvalwijzer_vandaag', 'duobak') -%}
            The trash was collected today. Please retrieve the black and green coloured container.
            {%- elif is_state('sensor.mijnafvalwijzer_vandaag', 'kerstbomen') -%}
            Your christamas tree was collected today.           
            {% else %}
            {% endif %}

        {% elif now().strftime("%H")|int < 12 %} 
            {%- if is_state('sensor.mijnafvalwijzer_vandaag', 'papier-pmd') -%}
            The trash will be collected today. Please put out the blue and orange coloured container.
            {%- elif is_state('sensor.mijnafvalwijzer_vandaag', 'duobak') -%}
            The trash will be collected today. Please put out the black and green coloured container.
            {%- elif is_state('sensor.mijnafvalwijzer_vandaag', 'kerstbomen') -%}
            The trash will be collected today. Please put out the christmas tree if you have any.           
            {% else %}
            {% endif %}

        {% endif %}
mode: single

Are you saying that what I should do is change the service call in here? Because that doesn’t seem to do anything:

service: script.turn_on
target:
  entity_id: script.google_home_resume
data:
  variables:
    action:
      - service: script.turn_on
        target:
          entity_id: script.test_for_tts
        data:
          entity: media_player.all_speakers
        extra:
          volume: 0.5

When you use script.turn_on you need to add variables under data.
Besides that, you will need to let the script know what the actual target is now, as it is not in the service call.

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.5

Tried that just now, CodePile | Easily Share Piles of Code, it doesn’t change the speaker volume for the TTS.

But…
Your script doesn’t have any delays or waits, and 2.2.2 didn’t solve it, so something else is going on here, and I allready know why.
Volume sync for speaker groups has been removed from Google Home, as a result of a patent lawsuit with Sonos. So I will have to address the members seperataly in the service call to set the volume.
Let me change that. one moment :slight_smile:

1 Like

@samuel81 just in case you missed it, see my post above yours.

1 Like