Script to send actions to the right Google Home (based on voice commands)

UPDATE

Version 1.3.0 - 25 February 2022

:red_circle: BREAKING

  • Changes in the way data is shared with the Google Home Resume script. So if you update this script, make sure to also update the other one. However, as of this version, it is also possible to use this script without the Google Home Resume script

:star2: Improvements

  • The ytube music player integration will now resume at the right position in the track
1 Like

Update

Version 1.3.1 - 14 March 2022

:star2: Improvements

  • Do not store unnecessary data of non playing devices

:receipt: Documentation

  • Created seperate file for older changes

Older changes can be found here

Update

Version 1.3.2 - 21 March 2022

:star2: Improvements

  • No need to for the variable primary_spotcast anymore, this will be handled by the Google Home Resume script
  • Only data of playing Spotify entities will be stored (other non playing entities were already excluded in 1.3.1)

Update

Version 1.3.3/1.3.4 - 22 March 2022

:star2: Improvements

  • Added a update of Spotify entities before data is stored, so the most recent data is used
  • Only relevant data of entities is stored (depending on state and playing app)

:bug: Bug fixes

  • (1.3.4) Template fix

Update

Version 1.3.5 - 2 April 2022

:star2: Improvements

  • Ambient sound will now be stopped after the data from the media_player is stored

Update

Version 1.3.6 - 8 April 2022

:sparkles: New features

  • Support for extra variables in service calls added. This is a new feature of the Google Home Resume script. Version 2.0.0 or higher of the Google Home Resume script is required for this. For more information see here

Hello!
I’m really thankfull for your work, but I haven’t been able to modify all the settings of both script to make them work; seems that there are a lot of scripts inside, with radios and all, lots of diferrent entities that I don’t have, and I really don’t want that. All that I really wanted was to know the entity_id of the media_player that was used to call a script, is there any way to simplify this, without thinking in youtube / spotify or any other integration for that matter? I thisnk that would be very usefull, for a lot of people, on it’s own.

For instance, I have this list of media_players, and is possible to know the “last_changed”, but that doesn’t quite work as expected:

{% set colunas = ['media_player.coluna_da_sala', 'media_player.coluna_da_cozinha', 'media_player.coluna_da_cave'] %}

{{ expand(colunas) | sort(attribute='last_changed', reverse=true) | map(attribute='entity_id') | first }}

Is there something that I could do before just to get the correct media_player ?
Thanks in advance!

The script is intended to actually send an action to the right Google Home.
The Google Home Resume script is not required, you can use this script without the Google Home Resume script if you use: use_resume: false

I don’t know what you actually mean with your template example, because it doesn’t seem related to starting an action by voice on your Google Home, and knowing which one you asked it to in Home Assistant.

What your template does is determine which media_player was changed last, which of course could be because of the question asked, but also because someone changed the volume or whatever. So that might be the reason why it doesn’t work as expected.

1 Like

I know you are right, and I’m thankfull for your approach. I will try to make both scripts work, but mainly understand the sequence to send the white noise and get the media_player from there. That’s mainly what I want.
I managed to replicate the white noise and possibly get the player, but as it goes last in the sequence in Google Home app it has to wait for it to be identified, as I understand. Don’t know any better aproach, so I will try to addapt your scrits to my entities. Thanks again.

You could use a trigger based template sensor with an event trigger on state change
Then use the media_title attribute with the title for the ambient sound, and use the entity_id which triggered is as the state of the sensor

Something like this:

template:
  - trigger:
      - platform: event
        event_type: state_changed
        event_data:
          new_state:
            attributes:
              media_title: "White Noise"
    sensor:
      name: Google Home entity
      state: "{{ trigger.event.data.entity_id }}"

Every time an entity starts playing White Noise (please check if this is the correct title), the state of this sensor (sensor.google_home_entity) will reflect the entity_id playing the sound.

1 Like

THANKS!!! That’s defenitly the path to follow! :slight_smile: But I’m still gonna try to use your scripts :wink:

Version 1.4.0 - 5 May 2022

:red_circle: BREAKING

  • Due to implemented changes introduced in 2022.5 this version is now required for the script

:star2: Improvements

  • Code review and implementation of new script actions introduced in HA 2022.5

:bug: Bug fixes

  • Bug fix in case target replacement was used (Ambient sound would not be stopped)

Update

Version 1.5.0 - 20 May 2022

:sparkles: New feature

  • Added the target_variable option. When set to true it will add the variable voice_target to the service call containing the entity_id of the Google Home which was used to trigger the script. This can be used to use the entity in a script. It will only be added on script service calls. In case the Google Home Resume script is used as well, it will also add the entity_id under extra, so the resume script will recognize it as a target to be resumed.

Hi,

in the example at the first post you used the service highlighted to send the tts message.
in the latest script this part is missing.
Sorry for the noob question but “where” I have to call the chosen tts service?

You mean in the example in this section?

You can provide any service call there, so just provide the tts service call as you would normally do under action in the script call. But don’t provide the target, as that is determined by the script.

    - service: script.google_home_voice
      data:
        action:
          - service: tts.your_tts_service
            data:
              message: "Your message"
        volume: 35 # optional volume setting for the tts message
1 Like

ok so I have to copy the script.google_home_voice somewhere and then in my automation call the script and the tts service I would use. Right?

What are you trying to achieve with this script? Could you explain your goal here?

I’m wondering how you want to use an automation for this.

Sure, thank you :slight_smile:

I had this script based on a previous version:

alias: Buondì Marco
sequence:
  - variables:
      check_for_title: Rumore bianco
  - alias: Wait until white noise started
    wait_template: |
      {{ 
        expand(states.media_player)
          | selectattr('attributes.media_title', 'eq', check_for_title)
          | map(attribute='entity_id') 
          | list 
          | count > 0 
      }}
  - variables:
      tts_target: |
        {{ 
          expand(states.media_player)
            | selectattr('attributes.media_title', 'eq', "Rumore bianco")
            | map(attribute='entity_id') 
            | join
        }} 
  - service: script.my_notify
    data:
      notify: 0
      priority: 1
      google:
        media_player: '{{ tts_target }}'
        mode: 'on'
        volume: 0.5
      message: >-
        Sono le {{states('sensor.time')}} di {{states('sensor.weekday')}}
        {{states('sensor.data')}} ed in casa ci sono mediamente {{
        state_attr('climate.riscaldamento_casa',
        'current_temperature')|int|string  }} gradi. Fuori il clima è
        {{states('sensor.dark_sky_summary')}}, con una temperatura esterna di
        {{states('sensor.dark_sky_apparent_temperature')|int|string }} gradi. La
        qualità dell aria è {{states('sensor.aqicn')}}.  Durante la giornata il
        clima sarà {{ states.sensor.dark_sky_summary_0d.state }}. La massima
        sarà di
        {{states.sensor.dark_sky_daytime_high_temperature_0d.state|round}} gradi
        e la minima
        {{states.sensor.dark_sky_overnight_low_temperature_0d.state|round}}
        gradi.  Oggi c è il {{states('sensor.dark_sky_precip_probability_0d') |
        round (0)}} percento di probabilità di pioggia. Citazione di oggi.
        {{state_attr('sensor.wikiquote', 'entries')[-1].summary|striptags}}.
  - alias: Wait until white noise started
    wait_template: |
      {{ 
        expand(states.media_player)
          | selectattr('attributes.media_title', 'eq', check_for_title)
          | map(attribute='entity_id') 
          | list 
          | count > 0 
      }}
  - service: media_player.media_stop
    target:
      entity_id: '{{ tts_target }}'
mode: single
icon: mdi:tie

it doesn’t work anymore.

That’s weird because other script with similal structure

alias: Notifica Posizione Renata Hub
sequence:
  - variables:
      check_for_title: Rumore bianco
  - alias: Wait until white noise started
    wait_template: |
      {{ 
        expand(states.media_player)
          | selectattr('attributes.media_title', 'eq', check_for_title)
          | map(attribute='entity_id') 
          | list 
          | count > 0 
      }}
  - variables:
      tts_target: |
        {{ 
          expand(states.media_player)
            | selectattr('attributes.media_title', 'eq', "Rumore bianco")
            | map(attribute='entity_id') 
            | join
        }} 
  - service: script.my_notify
    data:
      notify: 0
      priority: 1
      google:
        media_player: '{{ tts_target }}'
        mode: 'on'
        volume: 0.5
      message: >-
        Renata dovrebbe essere vicino al civico {{ state_attr('sensor.renata',
        'street_number') }} in {{ state_attr('sensor.renata', 'street') }}, {{
        state_attr('sensor.renata', 'city') }}. 
  - alias: Wait until white noise started
    wait_template: |
      {{ 
        expand(states.media_player)
          | selectattr('attributes.media_title', 'eq', check_for_title)
          | map(attribute='entity_id') 
          | list 
          | count > 0 
      }}
  - service: media_player.media_stop
    target:
      entity_id: '{{ tts_target }}'
mode: single
icon: mdi:tie

still works.

Ah, seems you created your own version.
I tried to put all the logic to determine the target in one script, where you’ve put it directly in the script called by your Google Home.

I don’t see a reason why the second script would work, and the first one not. Is there any information in the trace?

Anyway, as you seem to be calling another script, and send the target as a variable, you could use the latest version of my script, and use the target_variable option. However, the variable containing the entity_id of the Google Home will be voice_target and not tts_target