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

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

@samuel81 (I replied to myself apparently)
Please try test v2.2.2.2 or even test v2.2.2.3

1 Like

edit: hold on

Ah right, missed something there
This one should do the trick. Version number is 2.2.2 again as I thought it was ready to merge.

I’m sorry, I need to revise what I wrote before:

Tested both, test v2.2.2.2 works, test v2.2.2.3 doesn’t turn up the volume. Both traces are here:

V2.2.2.2:

V2.2.2.3:

V2.2.2:

Scenario’s:

Scenario 1:

alias: 'Test: Spotify Resume with TTS Script'
description: Test automation for Spotify Resume, using script by TheFes
trigger:
  - platform: state
    entity_id: input_select.matthijs_status_dropdown
    to: Extended Away
condition: []
action:
  - 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
mode: single

Scenario 2 ( script.turn_on with variables under data .):

alias: 'Test2: Spotify Resume with TTS Script (Duplicate)'
description: Test automation for Spotify Resume, using script by TheFes
trigger:
  - platform: state
    entity_id: input_select.matthijs_status_dropdown
    to: Extended Away
condition: []
action:
  - 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.4
mode: single

Tested with:

  • On some players there is music playing.
  • On no players there is music playing.
  • Music was played with Spotify.

You are right, I got confused myself.
Setting the volume with scenario 2 will not work, it will only work with scenario 1.
The separate target is only used for resume, not for the volume_set action.

I will have to clear that up in the docs.

So, please use scenario 1. I will look into why the volume isn’t working though.

1 Like

micro version numbers are back test 2.2.2.5 is ready for testing.
Volume setting and resume should work with scenario 1, thinking of adding an option to add targets to extra to make scenario 2 work

1 Like

Works! Great!

https://www.codepile.net/pile/vQv9kjYq

Just uploaded v2.2.2.6

That should also allow scenario 2 if you use it it like this:

alias: 'Test2: Spotify Resume with TTS Script (Duplicate)'
description: Test automation for Spotify Resume, using script by TheFes
trigger:
  - platform: state
    entity_id: input_select.matthijs_status_dropdown
    to: Extended Away
condition: []
action:
  - 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.4
              entity_id: media_player.all_speakers
mode: single

Unfortunately not with Scenario 2, but still works with Scenario 1…
Trace for Scenario 2: CodePile | Easily Share Piles of Code

Okay, I will dig into that, seems to be another issue.
Thanks for testing