Google Assistant Relay

I was looking for an easy way to setup broadcast to our google homes from OH. I found this project on github and it was a breeze to setup.

This tutorial assumes you have a project setup on the Google Developers console, and you’ve enabled the Google Assistant API, created credentials, and downloaded your client secret.

  • Make sure you have Node installed from https://nodejs.org/en/
  • Install PM2 as a pre-requisite if not already installed sudo npm i pm2 -g
  • Download a copy of the latest release from https://github.com/greghesp/assistant-relay/releases and then use npm i to get started. Once setup, run npm run start . You do NOT want the source files
  • Navigate to the webpage shown and follow the configuration instructions. It should be http://server-ip:3000
  • Run some tests in the sand box to confirm communication with your GH’s
  • Create the rules below. Be sure to update your sever ip.
  • Add ‘String broadcast’ and ‘String ghCommand’ to an item file
  • You can test by adding the follwing to a sitemap.
    Switch item=broadcast mappings=[“lunch”=“Lunch”,“bedtime”=“Bed Time”,“movietime”=“TV Time”]

rule “broadcast”
when
Item broadcast received update
then
var message = broadcast.state.toString
var command
switch (message) {
case “wakeup”: command = ‘{“preset”:"’+ message +‘"}’
case “breakfast”: command = ‘{“preset”:"’+ message +‘"}’
case “lunch”: command = ‘{“preset”:"’+ message +‘"}’
case “dinner”: command = ‘{“preset”:"’+ message +‘"}’
case “timetoleave”: command = ‘{“preset”:"’+ message +‘"}’
case “arrivedhome”: command = ‘{“preset”:"’+ message +‘"}’
case “ontheway”: command = ‘{“preset”:"’+ message +‘"}’
case “movietime”: command = ‘{“preset”:"’+ message +‘"}’
case “tvtime”: command = ‘{“preset”:"’+ message +‘"}’
case “bedtime”: command = ‘{“preset”:"’+ message +‘"}’
default : command = ‘{“command”:"’+ message +‘",“broadcast”:“true”}’
}
sendHttpPostRequest(“http://server-ip:3000/assistant”, “application/json”, command )
logInfo(“Broadcast”, “Message Sent:”+message)
end

rule “Google Command”
when
Item ghCommand received update
then
var message = broadcast.state.toString
var command
command = ‘{“command”:"’+ message +‘"}’
sendHttpPostRequest(“http://sever-ip:3000/assistant”, “application/json”, command )
logInfo(“Broadcast”, “Command Sent:”+message)
end

Update the Broadcast item to broadcast to your GH network, and update the ghCommand item with any command you could speak to your GH.

One note, which is a bit annoying, is it adds a “User says…” to any custom broadcast and some presets, where User is the first name of the google user account the project is setup under. I created a separate account and changed the first name to Openhab so everyone knows when its a system message.

5 Likes

I am using chromecast binding for this.

/Mike

The benefit of this over the Chromecast api is that what ever was playing will resume after the broadcast.

2 Likes

Mmmm - I got a bunch of warnings and errors on a few (not all) of the modules and the final package would not run as it failed to compile. I am running on Windows 10 64-Bit however.