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

Update

Version 2022.11.3 - 26 November 2022

:bug: Bug fixes

  • Fix template copy/paste error causing variable store to fail

Update

2022.12 Happy Holidays; letā€™s bring the family together

:red_circle: BREAKING

  • Integrated into the Google Home Resume package, update according to the instructions

:star2: Improvements

  • Template improvements

  • More use of YAML anchors

Hi, just found out this script, gonna read about it later, but just quick question, does it only work on google home devices? Or does it also work on Google Assistant devices, like a watch ?

No, it only works on Google Home devices.
It uses Google Home routines which start a ambient sound on the device. That will work on phones and watches as well, but those are not exposed as media_player in HA, so HA canā€™t see that the ambient sound is playing.

ok, thnx for feedback!

Got hopeful when I saw this. But then got a whole lot of 404 errors. Guess it was removed.

Itā€™s moved, not removed.
Itā€™s part of my Google Home Resume package now, which you can find here

Awesome. Thatā€™s the one time I love being wrong! :slight_smile:

I know this is an old thread. I hope this still works. Iā€™m just trying to use the google home voice script to announce a message with the value from sensor of mine. Iā€™m still very confused about this ambient sound stuff and how it all fits in. In the google home app, I setup a routine that runs a script I created and then plays the ā€œWhite noiseā€ sleep sound. Google home wonā€™t let me put the sound before running of my script. It says the sound has to be the last action in the routine, but in looking at your script, I donā€™t think thatā€™s the problem.

The errors Iā€™m getting now is the media_player_entity_id is not set when itā€™s trying to call the tts_speak service. I thought I had that part setup correctly, but I guess not.

This is my script:

alias: Say Glucose Reading
sequence:
  - variables:
      message: >
        Your current blood glucose reading is {{
        states('sensor.dexcom_xxxxxxxx_custom_data') }} and the trend is {{
        states('sensor.dexcom_xxxxxxxx_glucose_trend') }}
  - service: script.google_home_voice
    metadata: {}
    data:
      check_for_title: White noise
      use_resume: true
      target_variable: true
      action:
        - alias: Announce Glucose Reading
          service: tts.speak
          data:
            message: "{{ message }}"
            media_player_entity_id: "{{ voice_target }}"
mode: single
icon: mdi:gauge

And this is the error I see in the logs:

Say Glucose Reading: Error executing script. Invalid data for call_service at pos 2: not a valid value for dictionary value @ data['media_player_entity_id']

But if I donā€™t have that media_player_entity_id key, then the service call fails as itā€™s required.

So, what happens when I try to call my routine, is I hear the ambient noise start playing and thatā€™s it. I have to manually tell it to stop playing the noise and I never hear my announcement.

Itā€™s really hard for me to understand the documentation without more examples specifically for the voice script, so any help you can give me would be appreciated. I seem very close.

Does this maybe help?

I definitely was looking at that page. Thatā€™s where I got the starting point for my script. But I just noticed that in the description of the variables at the bottom, you list "action" as "The title of the ambient sound as shown in developer tools > states". I didnā€™t notice that before, but is that correct? The action is a section with the other params isnā€™t it?

I think it might be related to my using the tts.speak service call. In that service call, ā€˜targetā€™ is not the entity of the google device, but is the entity tts.google_en_com and the media_player_entity_id is the actual voice target. Itā€™s possible your script is not prepared for that. Iā€™m using HA 2024.5.4 BTW.

All Iā€™m really trying to do is grab the value of a sensor and formulate a message with that value and output it to the google home device where I asked for it.

Iā€™m thinking maybe I need to write another script that handles the tts.speak service call correctly and just have the google_home_voice script call that instead.

This is what I had to do. As I suspected, the Google Home Voice script wasnā€™t formatting the service call correctly for the tts.speak call. So, now I call a script to do that part and it works great now. Thanks for this. Itā€™s really a powerful feature to be able to have the proper Google device respond to a request rather than broadcasting to all of them.

1 Like

Great, nice that you managed to solve it!

Hello ! Can you add more details on what you have done please ? I am stuck with the same problem than you but I didnā€™t figure out what you have done exaclty to solve it. Maybe posting the corrected script version of the script you have poster few days ago ?

Sure. There are two scripts now. The first is the script called by Google Home through my routine. The second is the script that I tell the Google Home Voice script to call to actually speak the result. I did these mostly in the UI editor, but Iā€™ll post the yaml version of them to make it easier to see here.

alias: Say Glucose Reading
sequence:
  - variables:
      message: >
        Your current blood glucose reading is {{
        states('sensor.dexcom_xxxxxxxx_custom_data') }} and the trend is {{
        states('sensor.dexcom_xxxxxxxx_glucose_trend') }}
  - service: script.google_home_voice
    metadata: {}
    data:
      check_for_title: White noise
      use_resume: false
      target_variable: true
      action:
        - alias: Announce Glucose Reading
          service: script.google_home_tts_speak
          data:
            voice_message: "{{ message }}"
mode: single
icon: mdi:gauge

google_home_tts_speak

alias: Google Home TTS Speak
sequence:
  - service: tts.speak
    target:
      entity_id: tts.google_en_com
    data:
      cache: false
      media_player_entity_id: "{{ voice_target }}"
      message: "{{ voice_message }}"
mode: single
icon: mdi:speaker-message
fields:
  voice_target:
    selector:
      text: null
    name: voice_target
    description: The target google home device to send to (supplied by google_home_voice script)
    required: true
    default: media_player.all_google_homes
  voice_message:
    selector:
      text: null
    name: voice_message
    description: The message to send to the device
    required: true
1 Like

Thank you so much that works ! I am new with scripting and I learn a lot with your answer.

Also thanks and congratulations to @TheFes for the brilliant idea of using the ambient sounds for detection !

The start of ambient sound and detecting it increases the delay of the answer of about 2 seconds. Is it the same for you too ?

Iā€™m thinking about idea to improve it on one which is the main we use. For now my best idea is for each script to have two variants one with the question and an other with the question always prefixed with ā€œanswer hereā€. On anwsher here call I use TheFes google home voice script technique and on the other one I always target our main google home to be faster on it.

Yes, there is some delay. I havenā€™t really looked into that yet as Iā€™m happy I got it working. There might be a setting we can change to reduce that.

There will always be a delay.
GA has to start the routine, which will first start the script, and I think it waits to perform the next step unitl it got feedback from HA
Then it needs to start the ambient sound
After that HA has to pickup the the state change, and perform the TTS action
Generating the TTS message also takes some time, and after that HA has to send it to the speaker.

Not sure if there is any way to limit this delay, besides using 2 different scripts as described above.

Hi @TheFes
First of all, thanks for your work!
All the links on the first post are not working. May I ask you where I can find the docs?

Thanks :slight_smile:

EDIT: sorry, I just discovered the dedicated repo: GitHub - TheFes/Google-Home-Resume: Google Home Resume package for Home Assistant