In this tutorial, I thought I’d show you how simple it is to utilise the Aqara sensors and link them directly to Google Home and Alexa. I will be building on top of some tutorials I’ve already published, so I’ll skip through some of the set up basics. For those looking to create their own custom entities in Google Home using a third party bridge check here. For those of you looking to work with Alexa instead (basically the same principle, with a slightly different tweak for the output) you can check here. This primer will assume that you’ve got the appropriate node and account set up at https://alexa-node-red.bm.hardill.me.uk/ or https://googlehome.hardill.me.uk/ respectively. Both node installations are covered in the previous tutorials.
My Set Up
My set up consists of an Intel Nuc (6th gen celeron) with cheap Zigbee USB stick (CC2531). My NUC is running Hassio (that name will never die :)) / Home-Assistant Supervised, and I have Node-red running as an Add-on. We’ll be focusing on Node-red here. You could just as easily get by with node-red running on a Raspberry Pi zero and a zigbee stick (total cost would be about $15-20). You wouldn’t even need Home-Assistant for this step, you can use MQTT instead (for Google anyway).
In our household, we have both Google Home and Alexa so I’m going to set up a temperature sensor in both to show you how to do it. Let’s get started.
Google Home
Let’s start with Google. Click here and create the device we want to read values from:
Next we need to import these into Google Home, so go into the app and add/set up a device. Be sure to move it into the correct room.
Next, let’s go into node-red. I want to offer a huge thanks to Pete Guy for helping with the function node here.
See below for the full flow. Note in the latest Version of Home-Assistant (2021.5.5 at the time of writing) the State change node has been expanded to give you more information. So if your node looks slightly different, make sure you go into manage palette and update the nodes to the latest version.
Be sure to check “Output On Connect” otherwise your sensor won’t fire a reading into the flow until that sensor changes, which could take a couple of minutes or longer. This way, as soon as you deploy the flow, the reading is triggered. Also for State Type, be sure to select Number. By default this was “String” on mine, and whilst it worked for Humidity, it didn’t work for Temperature.
This is where the magic happens (thanks to Pete). This function node effectively strips out the temperature payload and changes it into a function that Google Home can work with. To make it easier to copy/paste, here’s the code below (remember to fix your spacing).
msg.payload = {
params: {
temperatureAmbientCelsius: msg.payload
}
}
return msg;
Next pass the flow through a rate limiter (rbe node). This effectively makes sure that your sensors aren’t coninually sending updates to the broadcast node unless there’s an actual change in value. This helps to cut overhead on your internal network and will help not to continually hammer the bridge.
And finally the Google Broadcast node. Be sure to hit the refresh wheel in devices to import the latest changes you’ve made in the bridge. If all goes well you should be able to select the device you created in step 1.
Next deploy the node, and then ask Google what the temperature is in the room you placed the device. If all goes well, she should return you the correct reading. Now do the same with Humidity.
Note the function is slightly different here as we’re not dealing with a temperature scale, but a percentage. As before, in your state change node, be sure to select “Number”, and “Output On Connect.”
Troubleshooting
- If you’re getting a reading of either temperature 22 or humidity 50% this means Google Home isn’t getting the measurement, and just returning you a default reading, so go back over your steps.
- If Google Home tells you they’re not able to connect to the sensor in question, then double check you have the “Output On Connect” checked. Make sure the State Change Node is actually receiving the correct measurements (usually seen underneath the node, with a green box).
- If you’re having issues importing the sensor into Google Home, make sure you have the bridge set up correctly and linked. More on how to do this here.
What if you don’t use Home-Assistant?
Wait, what? What is this blasphemy! If you don’t use Home-Assistant, you can still get these sensors to work using Nodered and Zigbee2mqtt. All we do is substitute the State Change mode from the Home-Assistant palette for an incoming MQTT node.
Amazon Alexa
Ok, we can’t take care of one without looking after the other. I mean what kind of threesome is this 🙂 Working with Alexa is signifcantly easier (IMO).
In the interests of keeping this post short, I’ll show a slightly different way of achieving the same thing, but this time I’ll focus just on the temperature
Set up a device on the Alexa bridge as discussed at length here. You should have something that looks like this:
Now go to the Amazon Alexa app on your phone and tablet, and choose add devices. Give it a moment to search and then select the newly created entity and set it up accordingly.
In this flow, we select the input (voice) node and the Response Node at the end. For the input, select the sensor you’ve just created. We use a simple check state node
Then on to where the magic happens, the function node!
Note this is completely different to the coding needed to expose the sensor to Google Home. Now connect this node up to the Alexa Response Node. Hit deploy, and marvel at your efforts. Job Done!
Unfortunately, i’ve been unable to find a way to expose the Aqara sensor directly to Alexa via MQTT. It seems that once you sever the payload path (by using mqtt out and then in), you’ve essentially broken the flow. Am sure someone smarter than me will show me a different method, but as I have HA, for me, my job is done 🙂
These posts might be of interest:
- Xiaomi Aqara Sensors: My 2 year review
- Sensory Overload – what does it cost to kit out a three bedroom home with sensors and switches on a budget.
- Adding Xiaomi Aqara Sensors to Home-Assistant/Hassio with a USB CC2531 and no gateway
If your interested in sharing your own solutions, tips and tricks with like minded people perhaps you’d consider joining our facebook group. The aim of this group will hopefully be more show and tell rather than support, but that’s not to say we can’t lend a helping hand!
https://www.facebook.com/groups/386238285944105