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

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

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

Based on the results I failed.

I will try to follow you suggestion and let you know :slight_smile:

Thank you

Your approach should also work fine, and as it works in one version I guess you should be able to get it working in the other one as well.
But are there no messages in the log or trace you can get more information from?

I found the issue. It was a template sensor in the message that stopped working (the sensor.wikiquote). Now the message should work!

Good to hear you got it resolved!