I got the Xiaomi Aqara cube a couple of weeks ago. It’s a very creative controller which can be used to control lights or as a security system. To see what else the cube could be capable it was time to use Xiaomi Aqara cube in NodeRED.
Xiaomi Aqara cube in NodeRED
To use the Xiaomi Aqara cube in NodeRED you have to have the Xiaomi Mi Home Gateway. Read this article to learn the basic concept of sending the information back and forth between the gateway and NodeRED. You will need this to use the events from the cube.
How does Aqara cube work?
To save power, cube goes to sleep after 1 min of inactivity. When touched, the cube sends an alert message over the Zigbee protocol to the hub and it will start reporting the gestures. Thanks to this reporting I can intercept extra information than the Mi Home app.
When a debug node is connected to the gateway, Xiaomi Aqara cube in NodeRED will report with messages “report”. These will contain the cube information and the type of event that causes the cube to send the update.
{ "cmd": "report", "model": "sensor_cube.aqgl01", "sid": "158d0xxxxxx", "short_id": 49508, "data": { "status": "alert" } }
The standard gestures are (status data available as msg.payload.data.status):
- shake (“shake_air”)
- push (“move”)
- rotate (“rotate”:”XX,YYY”)
- flip 90° (“flip90”)
- flip 180° (“flip180”)
- double tap (“tap_twice”)
Thanks to “report” entries we can also see:
- “alert” – the moment where the cube is woken up
- “plane rotation” – access to direction and degrees
Looking at the Cube node available in the NodeRED – it acts just like a filter. It is designed to act only when a specific cube has passed the information and output the information in two available formats:
Just Values
{ "status": "alert", "duration": null, "lux": null, "previous_status": "tap_twice", "voltage": 2.985, "voltage_level": "high", "time": 1546716697006, "device": "Cube" }
As you can see, in this setting I have additional information about the battery voltage. This is great, as ideally, I’d like to be notified if the battery needs changing.
Full Data
{ "cmd": "report", "model": "sensor_cube.aqgl01", "sid": "158d00xxxxx", "short_id": 49508, "data": { "status": "tap_twice" } }
In this mode, we get the raw output from the gateway. It’s interesting to see that battery information is not available even if I change the debug node to the “object” mode.
At first, I thought I could skip the Aqara cube node entirely, having the battery information is useful, and for now, I’m not aware of getting the battery information in any other way. Having the node deployed you can use the msg.payload.status to filter your gestures. Pay attention to rotation action as in this case the rotation info is stored in a different object.
The flow above shows you how to intercept all the gestures. What’s great about NodeRED is that we can actually create contexts for the gestures itself. That way I can chain the gestures into 2-3 gesture commands multiplying the numbers of controls from 6 to over 12.
Nothing stops you from changing the way the controls work based on the room that you are in using ie Alexa’s or Google Assistant’s “The cube is in the kitchen”, “The cube is in the office” commands enabling different devices for the motion control.
To couple up the controls you can use batcher node:
sudo npm install node-red-contrib-batcher
The node will await Xms for another input and couple the inputs together if another message arrives on time. If you shake the cube and tap it twice you will receive msg.payload:
["shake_air","tap_twice"]
Which can be used to control yet another device. I’d advise you to use this with less time-sensitive actions. You don’t want to perform 10 gesture routine just to put the lights on! I applied a filter to remove the “alert” message from the commands. Now that you know how to filter the gestures you can like the motion control to a smart socket, light bulb or other devices.
Conclusion
The Aqara cube becomes even more useful in NodeRED. Chaining commands and creating additional conditions enable extra functionality and fun ways to interact with everyday objects. Just imagine incredibly intricate alarm system where you have to shake the cube once, tap it twice, shake it again and push it to disable the sound! With that said… it’s time to shoot that video for Instagram 🙂 Did I mention, you can control computers with the cube too? Or generate online insults with a shake of the fist? Check this out.
Project Download
Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.