Controlling gadgets with Google Home & IFTTT & Webhook Relay

Hi guys, I was looking for a simple solution how to integrate Google Home with my Node-RED and my TV. While in this example I have only got TV enabled, based on IFTTT payloads you will be able to define anything you want.

In this post, I will show you how easy and quick it is to add voice controlled commands to your home/office or any other environment. Once you set up your first flow, any other features on top of that won't take even 1 minute to add.

We will be using:

  • Node-RED our main tool to wire everything together.
  • Google Home - I am using Google Home Mini to launch tasks.
  • IFTTT will be transforming commands coming from Google Home into webhooks.
  • Webhook Relay is going to act as a broker to deliver webhooks to our Raspberry PI running Node-RED without exposing it to the internet.

Webhook Relay node removes a lot of work that is required to securely expose your Node-RED to the internet. It is especially useful when you can't receive webhooks in your local network due to:

  • ISP blocks incoming connections
  • Double NAT due to using 4G
  • No static IP
  • Lack of knowledge how to set up HTTPS and reverse proxy

In short, it provides an encrypted, one-way transport to your Node-RED through a single node. There's a free tier so no need to pay. I run it as my side project so if you need higher limits, feel free to ping me and I will increase them :slight_smile:

1. Preparing to receive webhooks from IFTTT

Webhook Relay will be acting as a message broker between Google Home with IFTTT and Node-RED. Naturally, let's configure it first. Got to buckets page and create a new bucket called 'gactions':

In the bucket details page, you should see 'Default public endpoint' URL that starts with https://my.webhookrelay.com/v1/webhooks/.... Keep this tab open, you will need to copy that URL into IFTTT.

2. Set up Google Home with IFTTT

Head to IFTTT, then to your applets and click on "New Applet". Search for "Google Assistant":

IF THIS (Google Assistant)

When choosing a trigger pick 'Say a simple phrase' for our scenario. You can try other ones later for different automation. Now, in 'What do you want to say?' section type 'turn the TV on' or something similar. Whatever you want basically. Populate other fields and pick your response phrase. Click "Create trigger".

THEN THAT (Webhook)

For the action service, choose webhook:

In the URL part, take the 'Default public endpoint' from step 1 (that starts with https://my.webhookrelay.com/v1/webhooks/...). Choose method to "POST", set Content Type to 'application/json' and set body to:


{

"action": "tv_on"

}

Once done, click "Create Action". You can now repeat the same process for more commands such as turning the TV off, mute, lower the sound and so on. I have configured three applets in total to send webhooks to the same endpoint:

To turn it off:


{

"action": "tv_off"

}

To mute it:


{

"action": "tv_mute"

}

Adding new commands is very fast, takes less than a minute once you have finished with the first one since you don't have to modifying anything except adding more conditions into your switch node.

3. Configure Node-RED

Node-RED flow consists of three steps:

  1. Receive webhooks through node-red-contrib-webhookrelay node.

  2. Extract body from the webhook and parse it using simple function and JSON node.

  3. Launch actions based on switch node.

To control TV I am using node-red-contrib-tv-bravia node. Pretty much any internet connected device will also listen to node-red-node-wol (Wake on LAN).

The flow can be found on GitHub gist here. You can either import it or add nodes one by one. For the learning purposes, I would advise to add them manually so you can better understand how it works.

Let's get started. First, get authentication tokens and set them to the node-red-contrib-webhookrelay node. In the "Buckets" field, add our "gactions" bucket which we created previously:

<br/>


Extracting & parsing body

Now, we need to extract body and parse it. Create a function node and in the function body add this:


return {

payload: msg.payload.body

};

This will extract the webhook body from the whole webhook message (it includes input, bucket metadata, as well as request method and headers). Then, add a json node and configure it with:

  • Action: Convert between JSON String & Object

  • Property: msg.payload

Launching actions based on payload

Finally, time to add the switch and main control nodes. Remember the payloads that we configured in IFTTT? Time to read that action value:

For the control I am using node-red-contrib-tv-bravia node. Follow their instructions to set up the TV. In short - you need to know the IP address of your TV and MAC (for the Wake on LAN node). You can either find it from your router or go to your TV network settings and pick it up from there. Each switch wire goes to a different action, this makes it simple and robust.

Future work

Once you have the flow ready up to the switch node, feel free to add more gadgets behind it. As you can see, IFTTT makes it super easy to issue commands and act based on those payloads in your flow. If you have your Node-RED exposed to the internet, you can even skip Webhook Relay node and receive webhooks directly through an http node.

_I have originally published this as a blog post here: https://webhookrelay.com/blog/2019/01/29/google-home-ifttt-node-red/_

5 Likes

I use Beebotte, it's similar concept with IFTTT -> Webhook, but then uses MQTT which you can use directly or bridge with your own MQTT server. Advantage is that with the free-tier option you can do a lot more requests on Beebotte.

4 Likes

Thanks for sharing. The webhook relay is a great contrib node indeed. I was not aware of the node-red-contrib-tv-bravia though. So, time to modify the flow and play a joke with my wife.

2 Likes

I found the guide I used for Beebotte; https://maker.itnerd.space/from-google-assistant-to-mqtt/

I'm new to Webhook Relay, so bear with me on this one! I've set up a webhook relay node (on a Rasp Pi) and have linked it to a Webhooks Bucket and provided the Key and Secret to the node. When I deploy the flow, the webhook relay node initially shows connected (green dot) but after a few seconds it reverts back to unauthorized (red square). Strangely enough, the webhook relay node still works and receives forwarded webhooks from webhooks relay. Checking the node red console on the Pi, i see this:

10 Oct 16:52:19 - [info] Stopping flows

10 Oct 16:52:19 - [info] Stopped flows

10 Oct 16:52:19 - [info] Starting flows

10 Oct 16:52:19 - [info] Started flows

{"message":"manual disconnect","level":"info"}

{"message":"subscribed to webhook stream successfully","level":"info"}

10 Oct 16:52:20 - [info] [webhookrelay:&&&&&&&&.825b78] webhook relay status event received: 'subscribed', message: 'subscribed to buckets: AutoHat'

{"message":"connection closed, reconnecting..","level":"info"}

{"message":"authorization failed, key &&&&&&&&-fa1d-4161-ba86-","level":"error"}

10 Oct 16:52:29 - [info] [webhookrelay:&&&&&&&&.825b78] webhook relay status event received: 'unauthorized', message: 'key or secret invalid, visit https://my.webhookrelay.com/tokens to generate a new key and secret pair, closing connection...'

The node then keeps re-trying after the first failure.
Any ideas why the authorization is failing shortly after it is successfully subscribed?

1 Like

Hi, I have seen something similar due to some state left hanging in the Node.js itself. Could you please try restart whole node-red process?

Thanks - That seems to have sorted it out, the connection is now stable.:grinning:

Nice tutorial Karolis, thanks for posting.

Taking this a step further... any ideas how to feed a response back to Google Home?

For example, you tell google to switch on a Shelly wifi relay;
Google passes the command to IFTTT > Webhook Relay > node-RED > Shelly
Shelly then lets node-RED know that the device status is now 'ON' (MQTT), how can that then be fed back to Google Home and provide a response such as "The Shelly has been switched on"?

Is it possible?

it's probably just about calling some google text-to-speech API from node-red directly :slight_smile: there shouldn't be problem in this case as node-red would definitely be able to reach google home API. Haven't looked into it though, good idea for improvements as just "okay" from IFTTT is not always helpful :slight_smile:

3 Likes