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

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] " <contact> (GContact, GEntranceHallway, GZigbee) {channel="mqtt:topic:broker:contactsensorlivingroomdoor:contact"}

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

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


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

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

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

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

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



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

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

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

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



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

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

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

Kind regards, Bas

15 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" }
3 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!

2 Likes

With Mqtt 2.5 the state change only to OPEN but not to CLOSED with this js.

Do you have a idea?

I still have to upgrade to 2.5, once I have done this I’ll get back to you. In the meantime I hope someone else can help you out.

I have done it with a .map transform in the mqtt.things and it works perfect.

Thanks for the help.

I have the same issue.
Could you please show me your mqtt.things and the .map transform?

Here my Thing:

Thing topic xiaomi_kontakt_badfenster “Kontakt Badfenster” @ “MQTT” {
Channels:
Type contact : contact “Kontakt” [ stateTopic = “zigbee2mqtt/xiaomi_kontakt_badfenster/contact”, transformationPattern=“MAP:TuerFensterKontakt.map” ]
Type number : voltage “Spannung” [ stateTopic = “zigbee2mqtt/xiaomi_kontakt_badfenster/voltage” ]
Type number : battery “Batterie” [ stateTopic = “zigbee2mqtt/xiaomi_kontakt_badfenster/battery” ]
Type number : linkquality “Verbindung” [ stateTopic = “zigbee2mqtt/xiaomi_kontakt_badfenster/linkquality” ]

And the Map Transform (TuerFensterKontakt.map):

true=CLOSED
false=OPEN

I hope it works for you.

@R-Rosenow: I think you should mention that your things are only working if you enable attribute: topic in zigbee2mqtt.

Reference: https://www.zigbee2mqtt.io/information/configuration.html

# Optional: experimental options
experimental:
  # Optional: MQTT output type: json, attribute or attribute_and_json (default: shown below)
  # Examples when 'state' of a device is published
  # json: topic: 'zigbee2mqtt/my_bulb' payload '{"state": "ON"}'
  # attribute: topic 'zigbee2mqtt/my_bulb/state' payload 'ON"
  # attribute_and_json: both json and attribute (see above)
  output: 'json'

@R-Rosenow : i need help for the items in openhab.

May ZigBee works and i see the values of my Aqara Temperature Sensor in MQTT.fx.
But i get no informations with my items.
I use openhab 3

I have create a MQTT broker in the settings in openhab not over the code. the other MQTT works, so i dont want change it.

MQTT broker identifier : “mqtt:broker:brokermqtt”

My Zigbee device have the identifier : “zigbee2mqtt/TEMP_OG_LINKS” (i see it in MQTT.fx )

I think i need only the items.
I try:
Number TempOgLinks “Temperatur OG Links [%.1f °C]” {mqtt="<[brokermqtt:zigbee2mqtt/TEMP_OG_LINKS:state:JSONPATH:$.temperature]"}

But i get no Value in openhab.
My broker have an User and a password. But i think its not the problem. I want use this connection how the other parts, too.

Could anyone help me?

You need Items and Things, not just Items. openHAB 3 does not have the V1 MQTT Binding, which your configuration is for - you will need to reconfigure for the V2/V3 binding.

This may help:

OK Thanks die the example.
But I found no solution.

I have configurate a broker in Openhab3 with this setup:

UID: mqtt:broker:brokermqtt
label: MQTT Broker
thingTypeUID: mqtt:broker
configuration:
lwtQos: 0
publickeypin: true
keepAlive: 60
clientid: ce8047bb-1b32-453f-a9d1-d87f9932b061
secure: false
certificatepin: true
password: openhabian
qos: 0
reconnectTime: 60000
host: localhost
lwtRetain: true
enableDiscovery: true
username: openhabian

And now i wrote this things:

Bridge mqtt:broker:brokermqtt2 [ host=“localhost”, port=1883, secure=false, username=“openhabian”, password=“openhabian”]
{
// Contact sensors
Thing topic weathersensorentrancehallway “WeatherSensorEntranceHallway” {
Channels:
Type number : temperature “Temperature” [ stateTopic=“zigbee2mqtt/TEMP_OG_LINKS”, transformationPattern=“JSONPATH:$.temperature”]
Type number : humidity “Humidity” [ stateTopic=“zigbee2mqtt/TEMP_OG_LINKS”, transformationPattern=“JSONPATH:$.humidity”]
Type number : pressure “Pressure” [ stateTopic=“zigbee2mqtt/TEMP_OG_LINKS”, transformationPattern=“JSONPATH:$.pressure”]
Type number : voltage “Voltage” [ stateTopic=“zigbee2mqtt/TEMP_OG_LINKS”, transformationPattern=“JSONPATH:$.voltage”]
Type number : battery “Battery” [ stateTopic=“zigbee2mqtt/TEMP_OG_LINKS”, transformationPattern=“JSONPATH:$.battery”]}

items for example:

Number TemperatureEntranceHallway “Hal Temperatuur [%.1f °C]” {channel=“mqtt:topic:brokermqtt2:weathersensorentrancehallway:temperature”}

Which broker name is write in the frist line? mqtt:broker:brokermqtt that i have create in openhab3 or i need a new name (new broker)?

I get nothing informations with this item. I dont know thats wrong.
I think its the first line of the Thing?

How did you configure this Bridge? What is this a copy/paste from?

This has created a second Bridge to your broker. Not a problem, but just so you’re aware!

When you look in your OH3 UI, in the Things section, do you see either of these with a green ONLINE badge?

Is there a reason for not using the UI? Here’s a guide for setting up MQTT in openHAB 3, and here’s a guide for a simple bulb via zigbee2mqtt..

I can’t see anything immediately wrong with your configurations, but I’m on mobile so it’s not easy to see. Hopefully the links above might help…

I create the broker with the UI of openhab3 and create also items to use sonoff mqtt plugs. This works.

Ok than its a second Bridge … main thing it works :slight_smile:

Under the Things i see only my broker that i have creat over the UI.
I dont see a thing with the name brokermqtt2.

I tried to use the UI to create the things but it dont work.
The manuel for openhab 3 is very good but my thing does not go online.

i have change the configuration of zigbee. Now i see separate values for battery , temperature etc. in MQTT.fx so its works.

But if i write “zigbee2mqtt/TEMPOGLINKS/availability” in the “Availability Topic” the Thing goes offline. If i delete this line the Thing goes online. ( i have change the friendly name to TEMPOGLINKS, so the name is write )

maybe my zigbee2mgtt configuration is not correctly?

homeassistant: false
permit_join: true
mqtt:
base_topic: zigbee2mqtt
server: ‘mqtt://localhost:1883’
user: openhabian
password: openhabian
serial:
port: /dev/ttyACM0
disable_led: false
devices:
‘0x00158d0005866a98’:
friendly_name: ‘TEMPOGLINKS’
experimental:
output: attribute

It would be great if i can create the things over the UI.

I dont know how i can delete the user and passwort.
I have this in this configuration, in the mqtt broker (that i have create over the UI) and in the sonoff konfiguration. But it should work because i see all values in MQTT.fx

Let’s forget your configuration file, and start here. Can you show the configuration of your Thing which you tried to create in the UI? You could share the YAML from the Code tab.