Skip to content

Instantly share code, notes, and snippets.

@xbmcnut
Created February 5, 2019 00:40
Show Gist options
  • Save xbmcnut/e238c00ec929e357e46462675dce3988 to your computer and use it in GitHub Desktop.
Save xbmcnut/e238c00ec929e357e46462675dce3988 to your computer and use it in GitHub Desktop.
Package to allow Google TTS to advise of HA states for key devices. Scripts added directly to Cloud config.
script:
speak_garage_status:
alias: 'Ask Google for Garage Status'
sequence:
- delay:
seconds: 2
- service: tts.google_say
entity_id:
- media_player.kitchen_home
- media_player.insignia_speaker
data_template:
message: >-
The garage door is currently {{states('sensor.garage_status')}}
speak_pool_status:
alias: 'Ask Google for Pool Temperature'
sequence:
- delay:
seconds: 2
- service: tts.google_say
entity_id:
- media_player.kitchen_home
- media_player.insignia_speaker
data_template:
message: >-
The pool temperature is currently {{states('sensor.pool_temperature')}} degrees
## Template takes only the time value in 24hr time ##
speak_next_high_tide:
alias: 'Ask Google for next High Tide time'
sequence:
- delay:
seconds: 2
- service: tts.google_say
entity_id:
- media_player.kitchen_home
- media_player.insignia_speaker
data_template:
message: >-
The next high tide is at {{ as_timestamp(states.sensor.onetaunga_bay_tides.attributes.high_tide_time_utc) | timestamp_custom("%H:%M") }}
@Tuttle13
Copy link

Hi @xbmcnut , I’m looking to try and ask my Google Mini “hey google, what is the current power price?”. I then want it to check a sensor I have in HA, which is giving the live price in c/kWh (ie currently the price is 18.74c/kWh).
I then want google to simply reply “the current price is 18.74 cents” or something like that.

I think this is fairly similar to your pool temp request, but I don’t know the very next step to achieve this. Do I need to link GA with HA?
I’ve found a walkthrough on how to do this, but would prefer not to pay $5 a month for cloud access. Do I need to do the long manual setup before I can proceed?

Thanks for any help in achieving this!

@xbmcnut
Copy link
Author

xbmcnut commented Aug 16, 2020

Hi @xbmcnut , Do I need to link GA with HA?

Thanks for any help in achieving this!

Yes, you need to link HA to GA as a first step. Until you can control any entity in HA with GA, you won't be able to achieve any of my code above. I pay the $5 month as I certainly get the value of of that and the long-winded approach looks pretty detailed and is subject to Google API changes in the future.

@Tuttle13
Copy link

Tuttle13 commented Aug 20, 2020

Hi @xbmcnut , Do I need to link GA with HA?

Thanks for any help in achieving this!

Yes, you need to link HA to GA as a first step. Until you can control any entity in HA with GA, you won't be able to achieve any of my code above. I pay the $5 month as I certainly get the value of of that and the long-winded approach looks pretty detailed and is subject to Google API changes in the future.

So I gave the fun manual method a go, and through a combination of perseverance and trial and error, I have successfully linked the two. I don’t have much in my google part of the config.yaml yet, but they are definitely linked. (I haven’t added room entities yet as I’m unsure how to go about this - The plan would be for it to only respond on the device I ask the question on, which is currently 4 google home minis and 2 android phones).
Would you be kind enough to help me work my way through adding the ability for google to report the current price, in response to “Hey google, what’s the power price?”
Thanks again!

@xbmcnut
Copy link
Author

xbmcnut commented Aug 20, 2020

@ptrg13 Pretty easy.

Add your script to your config. Based on mine, it would be something like this.

script:
  speak_power_rates:
    alias: 'Ask Google for the current power rates'
    sequence:
    - delay:
        seconds: 2
    - service: tts.google_say
      entity_id:
        - media_player.kitchen.blahblah
        - media_player.lounge.blahblah2
      data_template:
        message: >-
          The power currently costs {{states('sensor.power_rates')}} cents

Tweak your config so that you exposed 'scripts' and then add your script.speak_power_rates entity ID there and give it any nicknames.

After you've restarted HA, ask Google to 'sync my devices'
Test by asking Google to "turn on speak power rates"
Lastly, make it more useful by adding a routine in the Google Home App that is something like 'when I say "Power Rates", turn on speak power rates. You can add multiple things to say into the routine to cover all that variants but try to think of unique phrases. Don't use 'what' at the beginning as Google will think you're asking a normal search type question. I use the word 'execute' a lot to start my routines to avoid confusion.

Good luck.

@Tuttle13
Copy link

Tuttle13 commented Aug 20, 2020

Thanks so much for the help!

So to expose the script, in the google section I’ve added in config.yaml, do I need to add this?
exposed_domains:
- script
Bit confused by this part..

And then I attach your kindly provided script at the bottom of the same config file?

I’ve also got no mention of my devices in the config yet. Do I just go to google home and note down the names? (Such as “gym speaker”, “bedroom speaker” and “kitchen speaker”? Which would end up being media_player.kitchen.speaker and media_player.gym.speaker ?

So based on the above, will my script addition to my config look like this?

script:
   speak_power_rates:
     alias: 'Ask Google for the current power rates'
     sequence:
     - delay:
         seconds: 2
     - service: tts.google_say
       entity_id:
         - media_player.kitchen.speaker
         - media_player.bedroom.speaker
         - media_player.bathroom.speaker
         - media_player.gym.speaker  
       data_template:
         message: >-
           The power currently costs {{states('sensor.amber_general_usage_price')}} cents

Fingers crossed I’m on the right path here!

Thanks again

@xbmcnut
Copy link
Author

xbmcnut commented Aug 20, 2020

Your config for Google should probably look like this.

# Example configuration.yaml entry
google_assistant:
  project_id: YOUR_PROJECT_ID
  service_account: !include SERVICE_ACCOUNT.JSON
  report_state: true
  exposed_domains:
    - switch
    - light
    - script
  entity_config:
    switch.speak_power_rates:
      name: CUSTOM_NAME_FOR_GOOGLE_ASSISTANT
      aliases:
        - Power Rates
        - Power Price

You need to add the Google Cast integration to Home Assistant and that will find all your Google's and given them entity ID's inside HA that you can use in your script. One you have a media player in HA, you can click on an entity and test the speech by entering some text.

image

@Tuttle13
Copy link

Thanks again for that - i had missed that step. So i have now tested all the speakers, and google is reading out whatever I type.
I have also typed in the correct speaker names into the config.
Do I need the speakers turned "on" on the main dashboard? They were off by default.

So I have added the sections I think needed adding to my config, did the sync thing on google, and restarted HA.

When I ask google "hey google, turn on speak power rates", she replies "sorry, I didn't understand".

I have created a route in Google Home also (when I say "power price", action is to "turn on speak power rates") but she again replies, "sorry, I didn't understand".

I've tried tweaking what I can, but nothing seems to be changing the outcome. Perhaps it's easiest if I post my config/yaml here, as most likely I have made a basic error..
(You might also see my Amber details added as a sensor directly below my Bhyve watering system (completely separate). But every time I tried moving this, it would lose its data in HA, so I took the easy way out and just left it where it is :(
Thanks once again :)

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

bhyve:
 username: MY_EMAIL
 password: MY_PASSWORD
sensor:
 - platform: bhyve

 - platform: amberelectric
   postcode: '3031'

switch:
 - platform: bhyve

google_assistant:
  project_id: MY_PROJECT_ID
  service_account: !include /config/MY_SERVICE_ACCOUNT.json
  report_state: true
  exposed_domains:
    - switch
    - light
    - script
  entity_config:
    switch.speak_power_rates:
      name: speak_power_rates
      aliases:
        - Power Rates
        - Power Price

script:
   speak_power_rates:
     alias: 'Ask Google for the current power rates'
     sequence:
     - delay:
         seconds: 2
     - service: tts.google_say
       entity_id:
         - media_player.kitchen_speaker
         - media_player.bedroom_speaker
         - media_player.bathroom_speaker  
       data_template:
         message: >-
           The power currently costs {{states('sensor.amber_general_usage_price')}} cents

# Text to speech
tts:
  - platform: google_translate

@xbmcnut
Copy link
Author

xbmcnut commented Aug 21, 2020

@ptrg13 Your Google config has switch.speak_power_rates (my fault) and it should be script.speak_power_rates

Change that, restart HA, add the script.speak_power_rates to Lovelace and activate it manually to confirm the script is working. Sync your devices again and then ask Google. Scripts do not appear in the Google Home app so you can't see it or test it there.

@xbmcnut
Copy link
Author

xbmcnut commented Aug 21, 2020

@ptrg13 Actually, you can add the script.speak_power_rates to Lovelace now and test it before doing anything else.

@xbmcnut
Copy link
Author

xbmcnut commented Aug 21, 2020

@ptrg13 You can also copy The power currently costs {{states('sensor.amber_general_usage_price')}} cents` into Developer Tools > Template too and see if it spits out the right information.

@Tuttle13
Copy link

Ok, so i first changed the switch wording to script, then restarted, and I also confirmed with the template tool that it is spitting out the right info ("22.26c")
But I can't seem to add script.speak_power_rates to Lovelace? Do I do that by clicking the plus on the main dash, adding a toggle? I cant work out how to actually add it.

And I also again synced all the google devices and tried asking "turn on speak power rates", but again she "couldn't understand"..?

@Tuttle13
Copy link

Update: I added the line service_name: google_say to my TTS, and now when I go to Configurator>Scripts, and press play on this, it does broadcast the correct info to all my google speakers!

But still, when I ask google "hey google, turn on speak power price" she doesn't understand. Same goes for my "hey google, power price" routine..

@xbmcnut
Copy link
Author

xbmcnut commented Aug 21, 2020

Just add an entity to LL and start typing script...... and it should find your script. Then you can fire it from the dash. Let me know what happens.

@xbmcnut
Copy link
Author

xbmcnut commented Aug 21, 2020

@ptrg13 I'm unsure how Google integrates with Home Assistant on the do it yourself version as I use Nabu Casa. Best to test with a switch or light first (add the dummy component if you don't have any switches or lights yet) as those will show in the Google Home app once synched.

@Tuttle13
Copy link

Yep, so I added it as a single entity, it by default said OFF. So I clicked on it, then clicked execute. When I did this, it ran, and all my devices responded correctly with the price.
So I think my google minis aren't triggering this when I ask them directly

@Tuttle13
Copy link

Solved it! I needed to add the line: expose_by_default: true above my exposed entities, and voila! its working... ITS WORKING (in 8yo Anakin voice).

Thank you again for all your help, absolute ANZAC spirit! haha. I wouldn't have stood a chance without your ongoing help. :)

@xbmcnut
Copy link
Author

xbmcnut commented Aug 21, 2020

Well that's great to hear. Well done. It's awesome when you finally get something working!

@Tuttle13
Copy link

There's one remaining (small) issue. My wife cannot run this script from her Google home account. When I make the identical routine on her phone, and run it, Google replies "not sure"..

Her GH can see all the room assigned switches etc, but c nt access the script.

I read on another forum that assigning a room to the script in the config can make these scripts "appear" as an item in GH, do you know if this is correct? (I think remember seeing an example that replicated the "- script" for each room. Reckon this can make them "appear" as they seem to do for some people?

@Tuttle13
Copy link

Tuttle13 commented Aug 21, 2020

@xbmcnut
Copy link
Author

xbmcnut commented Aug 21, 2020

You all need to be part of one home with added people, not everyone with their own account. https://support.google.com/assistant/answer/9155535?hl=en

@Tuttle13
Copy link

Yeah I thought that was it, but I checked and my wife is definitely already a home member ( already listed under "people in this home")
She can see a test added HA switch for a bhyve timer I added in her GH, but like mentioned in that thread, she can't "access" the script.
It doesn't show as a "device" (not does it for me), and when she replicates my routine, she gets a "sorry..."
If she doesn't have the routine and tries the same routine trigger I have "hey Google power price" Google says sorry (but I can ask the same question straight after and get the correct response)..

@xbmcnut
Copy link
Author

xbmcnut commented Aug 22, 2020

I've got my wife to test a script tonight and it does not work for here though other routines that don't use scripts do. I'll look into that room theory when I get some time.

@Tuttle13
Copy link

Tuttle13 commented Aug 22, 2020

Well, I’m not sure what I did, but I got it working? Like I mentioned, my wife could see the HA switches, so I went to Routines, created a new one, “add actions” then “browse popular actions”.
There it was, speak_power_rates, on her google home.
So I added it, made a voice trigger, and voila!

@Tuttle13
Copy link

One final, some what unrelated question for you, absolutely fine if you can’t answer it or it’s outside your knowledge.
I am trying to setup a IFTTT trigger, from HA, to change a wifi light colour.
In other words, I’m hoping, when the power price is say below 24c, so send a trigger to IFTTT. A second trigger would be when price is between 24.01 and 35c, and a third trigger would be anything greater than 35c.

I’m not sure if a multi-option trigger can be sent beyond on/off, but even if it’s 3 individual triggers, that would be fine.

I’m looking at runing this in HA service tab (obviously with the ifttt and it’s API key added to my config)
Domain: ifttt
Service: tigger
Service domain: {"event": "EventName", "value1": "Hello World", "target": "YOUR_KEY_NAME1"}

I think the first 2 stay the same, but how would I translate my data required (say a first range trigger from -999c to 24.00c) ?

That way I could have a lamp set up which, being always on, will change colour based on the power price.
Cheers for any assistance (once again!)

@Tuttle13
Copy link

Tuttle13 commented Aug 23, 2020

Or would an automation and script be a better idea?

# Example configuration.yaml Automation entry
automation:
  alias: Startup Notification
  trigger:
    platform: homeassistant
    event: start
  action:
    service: script.ifttt_notify
    data_template:
      value1: 'HA Status:'
      value2: "{{ trigger.event.data.entity_id.split('_')[1] }} is "
      value3: "{{ trigger.event.data.to_state.state }}"

and

#Example Script to send TestHA_Trigger to IFTTT but with some other data (homeassistant UP).
ifttt_notify:
  sequence:
    - service: ifttt.trigger
      data_template: {"event":"TestHA_Trigger", "value1":"{{ value1 }}", "value2":"{{ value2 }}", "value3":"{{ value3 }}"}

Edit, I have been experimenting (trial and many errors) in template with the following
value_template: "{% if states.sensor.amber_general_usage_price.state | float > 35.005 %} red {% endif %}"
value_template: "{% if states.sensor.amber_general_usage_price.state | float < 23.00 %} green {% endif %}"
value_template: "{% if states.sensor.amber_general_usage_price.state | 23.005 < float > 35.00 %} yellow {% endif %}"

The current price is dictating the red and green text to be output (currently only outputting green due to the price being below 23c right now). But I can’t get yellow working. Is there a particular format needed to do a greater than AND less than?

Is any of this event helpful in creating a trigger for IFTTT lol?

@xbmcnut
Copy link
Author

xbmcnut commented Aug 23, 2020

Probably best to post on HA forum as everyone gets to benefit from the solution. Personally, I wouldn't involved IFTTT unless you have no other choice as it will add unnecessary delays.

@kmanan
Copy link

kmanan commented Mar 31, 2023

What do you mean by cloud config? Is this added to the config.yaml? Which files am I editing here?

@xbmcnut
Copy link
Author

xbmcnut commented Mar 31, 2023

What do you mean by cloud config? Is this added to the config.yaml? Which files am I editing here?

In configuration.yaml

## Includes ##
automation: !include automations.yaml
binary_sensor: !include_dir_merge_list binary_sensor
cloud: !include cloud.yaml

That points us to a file in your config folder called cloud.yaml and all code above is in there.

@kmanan
Copy link

kmanan commented Apr 2, 2023

What do you mean by cloud config? Is this added to the config.yaml? Which files am I editing here?

In configuration.yaml

## Includes ##
automation: !include automations.yaml
binary_sensor: !include_dir_merge_list binary_sensor
cloud: !include cloud.yaml

That points us to a file in your config folder called cloud.yaml and all code above is in there.

That's helpful, thank you. Any ideas why I am getting this error:

image

Here's my config.yaml:
image

@kmanan
Copy link

kmanan commented Apr 2, 2023

Since I couldn't add cloud.yaml, pasted the script code in script.yaml.

This what my script.yaml looks like:
script:
speak_last_feed:
alias: 'Ask Google for Last Feed'
sequence:
- delay:
seconds: 2
- service: tts.google_say
entity_id:
- media_player.bedroom_speaker
- media_player.kitchen_speaker
- media_player.living_room_speaker
data_template:
message: >-
The last feed was {{ as_timestamp('sensor.last_feeding')}}

This is what my configuration.yaml looks like

Example configuration.yaml entry

google_assistant:
project_id:
service_account: !include SERVICE_ACCOUNT.JSON
report_state: true
exposed_domains:
- switch
- input_boolean
- input_select
- script
- media_player
- sensor
entity_config:
switch.speak_last_feed:
name: speak_last_feed
aliases:
- Last Feed
-

This is the error I am getting:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment