MQTT 2.4, ZigBee2MQTT, Xiaomi Aqara Sensors (sample config)

Tags: #<Tag:0x00007f2fbb60f918> #<Tag:0x00007f2fbb60f788> #<Tag:0x00007f2fbb60f648>

Migrated to MQTT 2.4 binding and had to add things and change item definitions. It took me some time to test it all out and convert the files. Below the new file content as a reference and help to others.

I’m using Zigbee2MQTT (https://koenkk.github.io/zigbee2mqtt/) to bridge Aqara ZigBee sensors to MQTT and bind them to items in OpenHAB.

Currently I’m using:

  • motion sensors (RTCGQ11LM)
  • contact sensors (MCCGQ11LM)
  • water sensors (SJCGQ11LM)
  • weather sensors (WSDCGQ11LM)

Please note I’ve changes the names of the MQTT topics under which the ZigBee messages are published using NodeRed with the ZigBee2MQTT Admin panel (or you could do it manually on the filesystem in the config file) see: https://github.com/ben423423n32j14e/zigbee2mqttadminpanel .

MQTT.things

Bridge mqtt:broker:broker “MQTT Broker” [ host=“localhost”, port=1883, secure=false, clientID=“openHAB2” ]
{
// Contact sensors
Thing topic contactsensorlivingroomdoor “ContactSensorLivingRoomDoor” {
Channels:
Type contact : contact “Contact” [ stateTopic=“zigbee2mqtt/ContactLivingRoomDoor”, transformationPattern=“JS:getZigbeeContact2Contact.js”]
Type number : voltage “Voltage” [ stateTopic=“zigbee2mqtt/ContactLivingRoomDoor”, transformationPattern=“JSONPATH:$.voltage”]
Type number : battery “Battery” [ stateTopic=“zigbee2mqtt/ContactLivingRoomDoor”, transformationPattern=“JSONPATH:$.battery”]
}

// Weather sensors
Thing topic weathersensorentrancehallway "WeatherSensorEntranceHallway" {
Channels:
    Type number : temperature "Temperature" [ stateTopic="zigbee2mqtt/WeatherEntranceHallway", transformationPattern="JSONPATH:$.temperature"]
    Type number : humidity "Humidity" [ stateTopic="zigbee2mqtt/WeatherEntranceHallway", transformationPattern="JSONPATH:$.humidity"]
    Type number : pressure "Pressure" [ stateTopic="zigbee2mqtt/WeatherEntranceHallway", transformationPattern="JSONPATH:$.pressure"]
    Type number : voltage "Voltage" [ stateTopic="zigbee2mqtt/WeatherEntranceHallway", transformationPattern="JSONPATH:$.voltage"]
    Type number : battery "Battery" [ stateTopic="zigbee2mqtt/WeatherEntranceHallway", transformationPattern="JSONPATH:$.battery"]
}

//Motion sensors
Thing topic motionsensorentrancehallway "MotionSensorEntranceHallway" {
Channels:
    Type switch : motion "Motion" [ stateTopic="zigbee2mqtt/MotionEntranceHallway", transformationPattern="JS:getZigbeeOccupancy2Switch.js"]
    Type number : brightness "Brightness" [ stateTopic="zigbee2mqtt/MotionEntranceHallway", transformationPattern="JSONPATH:$.illuminance"]
    Type number : voltage "Voltage" [ stateTopic="zigbee2mqtt/MotionEntranceHallway", transformationPattern="JSONPATH:$.voltage"]
    Type number : battery "Battery" [ stateTopic="zigbee2mqtt/MotionEntranceHallway", transformationPattern="JSONPATH:$.battery"]
}

//Water Sensors
Thing topic watersensormeteringcloset "WaterSensorMeteringCloset" {
Channels:
    Type contact : contact "Contact" [ stateTopic="zigbee2mqtt/LeakageMeteringCloset", transformationPattern="JS:getZigbeeLeak2Contact.js"]
    Type number : voltage "Voltage" [ stateTopic="zigbee2mqtt/LeakageMeteringCloset", transformationPattern="JSONPATH:$.voltage"]
    Type number : battery "Battery" [ stateTopic="zigbee2mqtt/LeakageMeteringCloset", transformationPattern="JSONPATH:$.battery"]
}

}

getZigbeeContact2Contact.js

(function(x){
    var result = "";
    var json = JSON.parse(x);
    if (json.contact)
    {
        result="CLOSED";
    }
    else
    {
        result="OPEN";
    }
    return result;
})(input)

getZigbeeOccupancy2Switch.js

(function(x){
    var result = "";
    var json = JSON.parse(x);
    if (json.occupancy)
    {
        result="ON";
    }
    else
    {
        result="OFF";
    }
    return result;
})(input)

getZigbeeLeak2Contact.js

(function(x){
    var result = "";
    var json = JSON.parse(x);
    if (json.leak)
    {
        result="OPEN";
    }
    else
    {
        result="CLOSED";
    }
    return result;
})(input)

MQTT.items

Contact ContactLivingRoomDoor "Woonkamerdeur Contact [%s] " &lt;contact&gt; (GContact, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:contactsensorlivingroomdoor:contact"}

Number VoltContactSensorLivingRoomDoor "Woonkamerdeur Contact Voltage [%d mV] " &lt;energy&gt; (GVoltage, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:contactsensorlivingroomdoor:voltage"}

Number BatteryContactSensorLivingRoomDoor "Woonkamerdeur Contact Batterij [%.1f %%] " &lt;batterylevel&gt; (GBattery, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:contactsensorlivingroomdoor:battery"}


Number TemperatureEntranceHallway "Hal Temperatuur [%.1f °C]" &lt;temperature&gt; (GTemperature, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:weathersensorentrancehallway:temperature"}

Number HumidityEntranceHallway "Hal Vochtigheid [%d %%] " &lt;humidity&gt; (GHumidity, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:weathersensorentrancehallway:humidity"}

Number PressureEntranceHallway "Hal Luchtdruk [%d mbar] " &lt;pressure&gt; (GPressure, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:weathersensorentrancehallway:pressure"}

Number VoltWeatherSensorEntranceHallway "Hal Weersensor Voltage [%d mV] " &lt;energy&gt; (GVoltage, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:weathersensorentrancehallway:voltage"}

Number BatteryWeatherSensorEntranceHallway "Hal Weersensor Batterij [%.1f %%] " &lt;batterylevel&gt; (GBattery, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:weathersensorentrancehallway:battery"}



Switch MotionEntranceHallway "Hal Beweging [%s]" &lt;motion&gt; (GMotion, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:motionsensorentrancehallway:motion" }

Number BrightnessEntranceHallway "Hal Lichtsterkte [%d lumen]" &lt;sun&gt; (GBrightness, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:motionsensorentrancehallway:brightness"}

Number VoltMotionSensorEntranceHallway "Hal Bewegingsensor Voltage [%d mV] " &lt;energy&gt; (GVoltage, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:motionsensorentrancehallway:voltage"}

Number BatteryMotionSensorEntranceHallway "Hal Bewegingsensor Batterij [%.1f %%] " &lt;batterylevel&gt; (GBattery, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:motionsensorentrancehallway:battery"}



Contact LeakageMeteringCloset "Meterkast Lekkage [%s]" &lt;water&gt; (GWater, GMeteringCloset, GZigbee) {channel="mqtt:topic:broker:watersensormeteringcloset:contact"}

Number VoltWaterSensorMeteringCloset "Meterkast Lekkagesensor Voltage [%d mV] " &lt;energy&gt; (GVoltage, GMeteringCloset, GZigbee) {channel="mqtt:topic:broker:watersensormeteringcloset:voltage"}

Number BatteryWaterSensorMeteringCloset "Meterkast Lekkagesensor Batterij [%.1f %%] " &lt;batterylevel&gt; (GBattery, GMeteringCloset, GZigbee) {channel="mqtt:topic:broker:watersensormeteringcloset:battery"}

Kind regards, Bas

11 Likes

Thank you for your contribution.
Nice post.
I have edited a bit (The <code> bits didn’t work)

2 Likes

Hi there,

I have adapted the very same as in your post to my test Openhab, but I dont get any Value from mqtt.

Where is my fault?

things file:

Bridge mqtt:broker:mosquitto "PinkyMqtt" [ host="192.168.178.55", secure=false ]
{

    Thing topic Temperatur_WZ "Wohnzimmer Temperatur" {
    Channels:
        Type number : temperature "Temperature" 
            [ stateTopic="zigbee2mqtt/Temperatur_WZ", transformationPattern="JSONPATH:$.temperature"]
        Type number : humidity "Humidity" 
            [ stateTopic="zigbee2mqtt/Temperatur_WZ", transformationPattern="JSONPATH:$.humidity"]
        Type number : pressure "Pressure" 
            [ stateTopic="zigbee2mqtt/Temperatur_WZ", transformationPattern="JSONPATH:$.pressure"]
        Type number : voltage "Voltage" 
            [ stateTopic="zigbee2mqtt/Temperatur_WZ", transformationPattern="JSONPATH:$.voltage"]
        Type number : battery "Battery" 
            [ stateTopic="zigbee2mqtt/Temperatur_WZ", transformationPattern="JSONPATH:$.battery"]
    }
}

Items File:

Number Wohnzimmer_Temperatur "Wohnzimmer Temperatur [%.1f °C]" <temperature>  
 {channel="mqtt:topic:broker:Temperatur_WZ:temperature"}

sitemap :

sitemap MW29 label="Rolladen" {
        Frame label="Wohnzimmer" {
                Text item=Wohnzimmer_Temperatur label="Temperatur WZ"
        }
}

Can’t see it right away. But here are a few hints.

a) your item should be defined:

Number Wohnzimmer_Temperatur "Wohnzimmer Temperatur [%.1f °C]" &lt;temperature&gt; {channel="mqtt:topic:mosquitto:Temperatur_WZ:temperature"}

I replaced “broker” with “mosquitto” since that is what id you gave to the bridge.

a) you’re defining the Bridge in lowercase (don’t know if that matters)

b) Are you receiving MQTT messages on the source topic?

mosquitto_sub -t zigbee2mqtt/Temperatur_WZ

I think fixing a would probably work.

Alse please post any errors from your Openhab logs.

Kind regards,

Bas

Hi, you can add the Aqara Window Sensors.
You need:
MQTT.things:

Thing topic aqaradoorsensor2 "Door Sensor 2"  @ "Upper Floor" {
        Channels:
            Type contact : status "Status" [ stateTopic="mqtt/zigbee2mqtt/Aqara_DoorSensor2", transformationPattern="JS:doorsensor.js" ]
            Type number : voltage "Voltage"[ stateTopic="mqtt/zigbee2mqtt/Aqara_DoorSensor2", transformationPattern="JSONPATH:$.voltage" ]
            Type number : battery "Battery"[ stateTopic="mqtt/zigbee2mqtt/Aqara_DoorSensor2", transformationPattern="JSONPATH:$.battery" ]
            Type number : linkquality "RSSI"[ stateTopic="mqtt/zigbee2mqtt/Aqara_DoorSensor2", transformationPattern="JSONPATH:$.linkquality" ]


    } 

The transformation from true/false state of the contact to OPEN/CLOSED:

(function(x){
    var result = "";
    var json = JSON.parse(x);
    if (json.contact)
    {
        result="CLOSED";
    }
    else
    {
        result="OPEN";
    }
    return result;
})(input)

And the .items file:

//Door Sensor 2
Contact DoorSensor2_Contact      "Sensor 2 [%s]"                  (fg_gAqara)   { channel="mqtt:topic:mosquitto:aqaradoorsensor2:status" }
Number DoorSensor2_LinkQuality  "RSSI Sensor 2 [%d dBm]"                (fg_gAqara)   { channel="mqtt:topic:mosquitto:aqaradoorsensor2:linkquality" }
Number DoorSensor2_Volt         "Volt Sensor 2 [%d mV]"                 (fg_gAqara)   { channel="mqtt:topic:mosquitto:aqaradoorsensor2:voltage" }
Number DoorSensor2_Battery          "Batterie Sensor 2 [%.1f %%]"       (fg_gAqara,fg_gStatus_gBattery,fg_gStatus_gBattery_glowest)   { channel="mqtt:topic:mosquitto:aqaradoorsensor2:battery" }
2 Likes

Hi,can you tell me how to use this ZigBee2MQTT in openhab? I cannot find the binging in PaperUI.

And , the ZigBee2MQTT supports Xiaomi sensor, can working without Xiaomi Smart Gateway?

Yes, the idea is that you don’t need the gateway.

As for the binding, I understand that ZigBee2MQTT converts ZigBee signals from Xiaomi to MQTT messages, so you should work with Openhab in the same way you’d do with MQTT.

Thanks! I got it!

1 Like