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

Wow, using the ambient sound to check which speaker was talked to, is a great idea! Cumbersome to integrate but very interesting!

1 Like

Hi, just wanted to say this is a brilliant idea - specifically, passing the TTS to the right Google Home

Unfortunately I am have an issue with this. I’ve gone through the debug and the issue seems to be that I am not passing a tts_volume (I would like the TTS volume to be whatever the media_player. is set to).

This is the error I am getting, which is generated on this part of the code:

Stopped because an error was encountered at January 2, 2022, 15:08:20 (runtime: 0.04 seconds)
Error rendering data template: TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’

alias: Apply TTS volume
service: media_player.volume_set
target:
  entity_id: '{{ tts_target_list }}'
data:
  volume_level: '{{ tts_volume if tts_volume < 1 else tts_volume / 100 }}'

According to the debug, the tts_volume is being set as:

tts_volume: ‘’

The media player volume is being picked as

volume_old_not_playing:
  - entity_id: media_player.living_room_speaker
    volume_level: 0.05
screen_not_playing: []

I think the TTS is set correctly as per the example. But looks like it is being taken as a string where it defaults to undefined?

    - alias: "TTS"
      service: script.google_home_say
      data:
        voice_tts_target: "{{ tts_target }}"
        tts_message: "{{ tts_message }}"
        tts_volume: "{{ tts_volume if tts_volume is defined else undefined }}"

Really out of my depth here - not sure whats causing the error nor how to fix it.
Thank you in advance.

Kudos for this idea. I have wanted to do that quite some time.
Does this also work with your new script, or do I need to use the one stated in the first post?

It also works, but then you need to change the google_home_say_voice as well.

Here is the updated version.

I will change the post accordingly somewhere in the near future.

I’ve update the first post so it explains how it works with the new script (and amended the script to work with other actions besides TTS)

1 Like

Update

Version 1.0.0 - 9 February 2022

:sparkles: New feature

  • Now the new script is used, other actions besides TTS are supported

:star2: Improvements

  • Added version number as a variable to the sequence. This will make it more easy to see which changes there are since you last updated the script
  • Several improvements in the templates
  • Update of the first post of this forum topic
1 Like

Update

Version 1.1.0 - 10 February 2022

:star2: Improvements

  • No need to define speaker_groups anymore, as I found a way to define them using a template.

Update

Version 1.2.0 - 13 February 2022

:red_circle: BREAKING

  • Made several changes in how data is sent from this script to 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

:sparkles: New feature

  • It is now possible to use target_conversion. This means you can give your voice command, and perform your actions on another target eg the speaker group the target belongs to.
  • By setting the use_resume variable to false you can send the actions without using the Google Home Resume script. This can be used if you for example want to start a radio stream which is not in TuneIn, and do not want to resume other streams in case that one is stopped.
  • The target determined by the ambient sound will only be used in case you omit the target in your service call. If you provide it, that target will be used. This can be useful if you also want to provide other actions, for example setting the state of an input_boolean.
  • In case you don’t have the need to use the Google Home Script at all, you can set use_resume: false in the variables of the Google Home Voice script, and it will just send the actions.

:star2: Improvements

  • Information is shared with the Google Home Resume script in a better way. This was also needed because now multiple targets or groups as target are possible, where in the old version there was always only one target.
  • Information on for the ytube_music_player integration is now also shared.
1 Like

Sorry for all the updates :slight_smile:

Update

Version 1.2.1 - 14 February 2022 :heart_decoration:

:bug: Bug fixes

  • Fix for resuming ytube_music_player after voice command

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