DIY Projects

Test Zigbee2MQTT gateway. Part 2, include Xiaomi Aqara or Mijia accessories to Domoticz with Node-RED

We will see how to integrate Xiaomi Aqara or Mijia accessories without needing the official gateway to a Domoticz server. In the previous post, we were able to test the project zigbee2mqtt which allows to intercept measurements from different accessories Xiaomi Aqara. the Zigbee2mqtt project uses a Zigbee sniffer that will be connected to a Raspberry Pi. The intercepted messages are then published to an MQTT server. If you missed the previous episode, you can read the replay here.


I propose to test several accessories Aqara. The room sensor (temperature, humidity, barometer), the door or window opening detector and the infrared presence detector. However, I encountered difficulties in creating a Dummy device that combines measurements of temperature, humidity and atmospheric pressure (Aqara WSDCQ11LM sensor). I do not despair of finding the solution but in the meantime, the measurements will be presented in separate Devices. To integrate the temperature and humidity probes (or temperature, humidity and atmospheric pressure), it is necessary to fill with a zero the unknown data.

Prepare the Zigbee2mqtt gateway for Aqara and Mijia accessories

You will need to build the Zigbee2mqtt gateway and install the project on the Domoticz server. If the Zigbee coverage is insufficient, you can make several gateways. However, I advise you not to include the same Aqara accessory on multiple gateways so as not to needlessly multiply MQTT messages.

The zigbee2mqtt bridge uses a sniffer based on the Texas Instrument CC2531 circuit. You will need to buy a Texas Instrument CC Debugger (about $18) to install and update the firmware on the sniffer as well as a specific cable (GBAN) that connects the CC Debugger to the CC2531 Sniffer (about $3) every time you want to update the firmware.

Follow the previous tutorial for making the Zigbee gateway. I also published a 3D print box on Thingiverse here.

Install an MQTT Server (Mosquitto) on the Raspberry

You will also need an MQTT server. The simplest is to install it on the same Raspberry Pi on which Domoticz is installed. Run the following command to install the MQTT Mosquitto server and client on Raspbian. Client installation is optional but strongly recommended for testing and tracking MQTT messages published by the Zigbee Gateway.

sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

To learn more about MQTT and how to secure server access, you can read this tutorial.

Include Aqara or MIjia accessories at Zigbee2mqtt Bridge

As we saw in the first part, just press the inclusion microswitch available on each accessory. The trick is to press several times (each second) a dozen times to prevent it from “falling asleep” (the life of the battery is really excellent on Aqara accessories). Here are two illustrations of the include button for the door switch and the temperature / humidity / barometer sensor.

Start the gateway script

cd /opt/zigbee2mqtt
npm start


Wait for the Zigbee message: allowing new devices to join.

As soon as the first message arrives (motion detection, temperature measurement …), it’s over, your Zigbee accessory is now associated.

2018-6-4 12:37:41 INFO Zigbee: allowing new devices to join.
2018-6-4 12:37:48 WARN Message without device!
spinlock: false []
2018-6-4 12:38:04 INFO New device with address 0x00158d000155d2b7 connected!
2018-6-4 12:38:04 INFO MQTT publish, topic: 'zigbee2mqtt/bridge/log', payload: '{"type":"device_connected","message":"0x00158d000155d2b7"}'
2018-6-4 12:38:08 INFO Connecting with device...
2018-6-4 12:38:08 INFO MQTT publish, topic: 'zigbee2mqtt/bridge/log', payload: '{"type":"pairing","message":"connecting with device"}'
2018-6-4 12:38:13 INFO Connecting with device...

I then advise you to rename each accessory (by default, this is the serial number that is assigned as the device name).

nano /opt/zigbee2mqtt/data/configuration.yaml


At the bottom of the configuration file, you will find a new section named devices. Indicate the name of each accessory with the key friendly_name. Avoid putting spaces in the name of the accessories to avoid Topic problems with MQTT.

    friendly_name: 'MiSWitch'
    retain: false


Restart the script of the gateway when done.

Install Node-RED on Raspbian

The Zigbee2mqtt bridge publishes Xiaomi sensor measurements and states to an MQTT server. These are generic messages that will have to be “re-processed” to be used by a home automation server for example. For the moment, the project is only compatible with Home Assistant natively

To send messages understandable by Domoticz, I propose to use Node-RED. You just need 3 blocks (nodes) to publish the measurements on the Domoticz MQTT connector. I also advise you to install Node-RED on the same server that hosts Domoticz by running the following command.

bash <(curl -sL


This command starts the installation script that takes care of everything!

To go further with Node-RED, go here. Here are some articles that might also interest you

Prepare Domoticz, add an MQTT connector and a Dummy Device

Before sending messages to Domoticz, it is necessary to know the IDX of the device that will receive the measurements (or the On / Off states). Start Domoticz and open the menu Setup then hardware

In the hardware list, select MQTT Client Gateway with LAN Interface. Enter the address on the MQTT server. If it is installed on the same Raspberry Pi as Domoticz, specify locolhost. By default the port is 1883 unless your configuration is different. If you have enabled the user / password protection, enter it in the Username and Password fields. Finally click Add to add the material.

Domoticz now listens to all the posts that are published on the topic domoticz/in.

In the list, select the Dummy material now. This is a hardware that can create virtual devices that will display the measurements and reports returned by Xiaomi Aqara accessories (or any other accessories or object connected DIY for that matter). To learn more, you can read this tutorial.

Now, whenever you want to add a Xiaomi accessory to Domoticz, click the Create Virtual Sensors button on the hardware line.

Attention, the vocabulary and the organization of the elements is not always very obvious. It often takes a little to find the right sensor. We will start by creating a door contact type device. In the list, choose the type Switch.

In the device list (from the Settings menu) you will find the device that has just been created. We take the opportunity to note his IDX. As you can see, it’s a Light / Switch type

See you in the switches. By default, the device takes the form of a lamp. Click on the Edit button at the bottom.

In the list of switch types, choose the Door Lock type and save the change.

That’s it, everything is ready.

Include the Xiaomi Mijia door and window detector (model MCCGQ01LM) in Domoticz

The first accessory that I propose to include is the door and window detector. It is a magnetic contactor (reed switch). Like all Xiaomi accessories, it is attached with a sticker. The detection of opening (or closing) is triggered beyond 15mm. The sensor operates using a 3V CR1632 battery. Small defect related to the principle of the sensor, the detector does not work (or badly) on the metal surfaces. For example, if you want to monitor access to a fridge or freezer, it will be necessary to provide a support to shift the sensor of the surface of the opening.

Here is the payload (message) returned for a door contactor. The JSON contains three keys. Contactor status (true if closed, false if open), battery voltage and equivalent (0 to 100% charge)


Open Node-RED and import this flow. The first Node connects to the MQTT server and then listens for messages posted on the door sensor topic.

[{"id":"7f356e5c.dafc6","type":"mqtt out","z":"8cb500b2.1fda4","name":"","topic":"domoticz/in","qos":"2","retain":"","broker":"72f9bb18.b361b4","x":830,"y":320,"wires":[]},{"id":"a22ad10c.51a01","type":"mqtt in","z":"8cb500b2.1fda4","name":"Door/Windows Aqara Sensor","topic":"zigbee2mqtt/ContacteurPorte","qos":"2","broker":"8687cf12.76eee","x":260,"y":300,"wires":[["b6337ad1.688718"]]},{"id":"b6337ad1.688718","type":"json","z":"8cb500b2.1fda4","name":"","property":"payload","action":"","pretty":false,"x":470,"y":300,"wires":[["eb08f4b7.7da738"]]},{"id":"eb08f4b7.7da738","type":"function","z":"8cb500b2.1fda4","name":"Split","func":"msg1={};\n\nif ( ) {\n msg1.payload = {\n \"command\" : \"switchlight\",\n \"idx\" : 1,\n \"switchcmd\" : 'Off'\n }\n} else {\n msg1.payload = {\n \"command\" : \"switchlight\",\n \"idx\" : 1,\n \"switchcmd\" : 'On'\n }\n}\n\n\nreturn [msg1];\n\n","outputs":1,"noerr":0,"x":650,"y":300,"wires":[["7f356e5c.dafc6","8179c4a2.d53af8"]],"inputLabels":["json"],"outputLabels":["Etat"]},{"id":"8179c4a2.d53af8","type":"debug","z":"8cb500b2.1fda4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":280,"wires":[]},{"id":"72f9bb18.b361b4","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"8687cf12.76eee","type":"mqtt-broker","z":"","name":"MQTT Local","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

The MQTT message must be formatted to conform to the Domoticz JSON API. It is very well detailed on the official documentation. A door contactor is a switchlight type control that takes the state On or Off (be careful, it must respect the capital if it does not work). We also indicate the IDX of the Domoticz device (here it is 5).


if ( ) {
msg1.payload = {
"command" : "switchlight",
"idx" : 5,
"switchcmd" : 'Off'
} else {
msg1.payload = {
"command" : "switchlight",
"idx" : 5,
"switchcmd" : 'On'

return [msg1];


Finally the block MQTT publishes the message on the topic domoticz /in. All you have to do is roll out the flow and play with the sensor to see the state change on Domoticz. Do not worry, it takes a few seconds for the pictogram to change but it is very fast in the facts. It’s simply the discounted web browser that is slower on Domoticz.


Presence or motion detection

The presence detector works exactly like the door switch. The state of the part is contained in the key occupancy which is true or false



We will test the msg.payload.occupancy key in the javascript code of the flow Node-RED instead of the contact key.

[{"id":"b63ec8d.7b71b38","type":"mqtt out","z":"8cb500b2.1fda4","name":"","topic":"domoticz/in","qos":"2","retain":"","broker":"72f9bb18.b361b4","x":830,"y":200,"wires":[]},{"id":"15ee505c.e93de","type":"mqtt in","z":"8cb500b2.1fda4","name":"Xiaomi Aqara Motion Sensor","topic":"zigbee2mqtt/MiMotion","qos":"2","broker":"8687cf12.76eee","x":260,"y":180,"wires":[["ec0dadbb.52174"]]},{"id":"ec0dadbb.52174","type":"json","z":"8cb500b2.1fda4","name":"","property":"payload","action":"","pretty":false,"x":470,"y":180,"wires":[["ce8fb417.0314f8"]]},{"id":"ce8fb417.0314f8","type":"function","z":"8cb500b2.1fda4","name":"Split","func":"msg1={};\n\nif ( msg.payload.occupancy ) {\n msg1.payload = {\n \"command\" : \"switchlight\",\n \"idx\" : 5,\n \"switchcmd\" : 'Off'\n }\n} else {\n msg1.payload = {\n \"command\" : \"switchlight\",\n \"idx\" : 5,\n \"switchcmd\" : 'On'\n }\n}\n\nreturn [msg1];","outputs":1,"noerr":0,"x":650,"y":180,"wires":[["b63ec8d.7b71b38","bac3f455.44d448"]],"inputLabels":["json"],"outputLabels":["Etat"]},{"id":"bac3f455.44d448","type":"debug","z":"8cb500b2.1fda4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":160,"wires":[]},{"id":"72f9bb18.b361b4","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"8687cf12.76eee","type":"mqtt-broker","z":"","name":"MQTT Local","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

Edit the device and choose the Motion Detector type from the device list.

And that’s it, the integration is perfect!

Aqara temperature, humidity and atmospheric pressure sensor

The THP sensor of the Aqara 2018 range is often cheaper than the old model Mijia and embeds a bonus atmospheric pressure sensor.

The MQTT message also returns the battery level (0 to 100%, the voltage delivered by the battery). the temperature is expressed in degrees Celcius. Moisture in%. The atmospheric pressure in Pascal.

"Battery" "100.00"
"Voltage": 3055,
"Temperature": 22.38,
"Humidity": 61.33,
"Pressure": 972

As I said in the introduction, you have to fill in the missing information with a zero for Domoticz to correctly decode the MQTT message. For example Domoticz expects a savlue of the form svalue = TEMP; HUM; HUM_STAT; BAR; BAR_FOR or HUM_STAT and BAR_FOR take a value between 0 and 4. It is absolutely necessary to indicate 0 (which corresponds to No Info) and not to leave a empty space (my mistake!). Domoticz badly decodes the message MQTT (probably a bug of the current version) if we combine the three measurements on the same sensor. A zero is added which completely shifts the measures as you can see. If you have the solution, do not hesitate to share.

If the states are left blank, Domoticz incorrectly decodes the JSON

While completing with a zero, it is perfect

To integrate the room sensor, it will be necessary to create a sensor for each measurement (temperature, humidity, atmospheric pressure). At least until a solution is found or the problem is fixed on Domoticz.

[{“id”:”4fef80ac.ab9e1″,”type”:”mqtt out”,”z”:”8cb500b2.1fda4″,”name”:””,”topic”:”domoticz/in”,”qos”:”2″,”retain”:””,”broker”:”72f9bb18.b361b4″,”x”:830,”y”:580,”wires”:[]},{“id”:”fc3a2d7c.cf34a”,”type”:”mqtt in”,”z”:”8cb500b2.1fda4″,”name”:”Temperature”,”topic”:”zigbee2mqtt/THPsensor”,”qos”:”2″,”broker”:”8687cf12.76eee”,”x”:230,”y”:560,”wires”:[[“8dcc4e45.d50f6”]]},{“id”:”8dcc4e45.d50f6″,”type”:”json”,”z”:”8cb500b2.1fda4″,”name”:””,”property”:”payload”,”action”:””,”pretty”:false,”x”:470,”y”:560,”wires”:[[“980977ea.477a68”]]},{“id”:”980977ea.477a68″,”type”:”function”,”z”:”8cb500b2.1fda4″,”name”:”Split”,”func”:”msg1={};\nmsg1.payload = {\n \”command\” : \”udevice\”,\n \”idx\” : 4,\n \”nvalue\”: 0,\n \”svalue\” : msg.payload.temperature.toString(),\n \”battery\”: msg.payload.battery\n}\n\nreturn [msg1];”,”outputs”:1,”noerr”:0,”x”:650,”y”:560,”wires”:[[“4fef80ac.ab9e1″,”2f3b9fd2.c57b8″]],”inputLabels”:[“json”],”outputLabels”:[“Etat”]},{“id”:”2f3b9fd2.c57b8″,”type”:”debug”,”z”:”8cb500b2.1fda4″,”name”:””,”active”:true,”tosidebar”:true,”console”:false,”tostatus”:false,”complete”:”false”,”x”:830,”y”:540,”wires”:[]},{“id”:”72f9bb18.b361b4″,”type”:”mqtt-broker”,”z”:””,”broker”:”localhost”,”port”:”1883″,”clientid”:””,”usetls”:false,”compatmode”:true,”keepalive”:”60″,”cleansession”:true,”birthTopic”:””,”birthQos”:”0″,”birthPayload”:””,”willTopic”:””,”willQos”:”0″,”willPayload”:””},{“id”:”8687cf12.76eee”,”type”:”mqtt-broker”,”z”:””,”name”:”MQTT Local”,”broker”:”localhost”,”port”:”1883″,”clientid”:””,”usetls”:false,”compatmode”:true,”keepalive”:”60″,”cleansession”:true,”birthTopic”:””,”birthQos”:”0″,”birthPayload”:””,”closeTopic”:””,”closeQos”:”0″,”closePayload”:””,”willTopic”:””,”willQos”:”0″,”willPayload”:””}]

You must convert the measure to a string using Javascript toString () so that Domoticz can correctly interpret the message.

That’s it, the principle is similar in most cases. The integration of other devices should not be too much trouble. It is necessary each time to use which corresponds to the accessory Xiaomi. Here are some of them according to the source code of the Zigbee2mqtt project




That’s it, the principle is similar in most cases. The integration of other devices should not be too much trouble. It is necessary each time to use which corresponds to the accessory Xiaomi. Here are some of them according to the source code of the Zigbee2mqtt project

In short, what to integrate all home automation accessories Xiaomi

In the next article, we’ll see how to integrate the Honeywell smoke detector from Xiaomi.


Click to rate this post!
[Total: 0 Average: 0]
Exit mobile version