Howto use zigbee2mqtt with openHAB, removing proprietary bridges / gateways

If you want to use zigbee2mqtt with openHAB, please advance to the next level. The following is a far nicer implementation of the same stuff, just with the mqtt2 binding and a new option of zigbee2mqtt

The old stuff, if you prefer the mqtt1 binding and too many tranformations, please go ahead.

Recently io discovered zigbee2mqtt, which made me think of getting rid of my hue bridge and being able to use a large number of cheap sensors. This works pretty well. I already joined 15 devices (including my Osram Smart Plugs which did not behave nicely on the hue bridge.)

I know, openHAB also enjoys a zigbee binding, but i did not try it yet for several reasons. Most important one is my number of devices, this will easily exceed the 35 device limit of the cc2531. With one or more RaspberryPi running Kodi in every floor, i could easily extend to several floor centric zigbee networks.

Back on topic, it works fine. I already ordered some new sensors (about 10€ each) and attached them easily, although the mqtt binding configuration is a bit tricky. (I am no mqtt expert).

As always, hints are welcome. :wink:

Here are my results:

  • OSRAM Smart+/Lightify Plug:
Switch SmartPlug_TOGGLE "SmartPlug [%s]"        <poweroutlet> {mqtt=">[broker:zigbee/SmartPlug/set:command:*:JS(setZigbeeState.js)],<[broker:zigbee/SmartPlug:state:JSONPATH($.state)]"}
  • Xiaomi Aqara wireless switch WXKG11LM
String BUTTON_CLICK "BUTTON [%s]"  <button>  {mqtt="<[broker:zigbee/BUTTON:command:JS(getZigbeeClick.js)]"}
Number BUTTON_VOLTAGE "BUTTON Volt [%d mV]" {mqtt="<[broker:zigbee/BUTTON:state:JSONPATH($.voltage)]"}
Number BUTTON_BATTERY "BUTTON BAT [%.1f %%]" {mqtt="<[broker:zigbee/BUTTON:state:JSONPATH($.battery)]"}
  • Xiaomi Aqara water leak sensor SJCGQ11LM
Contact LEAK_STATE "Leak [%s]"  <water> {mqtt="<[broker:zigbee/LEAK:state:JS(getZigbeeLeak2Contact.js)]" }
Number LEAK_VOLTAGE "Leak Volt [%d mV]" {mqtt="<[broker:zigbee/LEAK:state:JSONPATH($.voltage)]"}
Number LEAK_BATTERY "Leak BAT [%.1f %%]" {mqtt="<[broker:zigbee/LEAK:state:JSONPATH($.battery)]"}
  • Xiaomi Aqara human body movement and illuminance sensor RTCGQ11LM
Switch MD_MOTION "MD Motion [MAP(HM-Sen-MDIR-O-2.map):%s]"  <motion> {mqtt="<[broker:zigbee/MD:state:JS(getZigbeeOccupancy2Switch.js)]" }
Number MD_BRIGHTNESS "MD Brightness [%d lumen]"  {mqtt="<[broker:zigbee/MD:state:JSONPATH($.illuminance)]"}
  • Xiaomi Aqara temperature, humidity and pressure sensor WSDCGQ11LM
Number THPS_TEMP "THPS Temperature [%.1f °C]"  <temperature>  {mqtt="<[broker:zigbee/THPS:state:JSONPATH($.temperature)]"}
Number THPS_HUMIDITY "THPS Humidity [%d %%]" {mqtt="<[broker:zigbee/THPS:state:JSONPATH($.humidity)]"}
Number THPS_PRESSURE "THPS Pressure [%d mbar]" {mqtt="<[broker:zigbee/THPS:state:JSONPATH($.pressure)]"}
Number THPS_VOLTAGE "THPS Volt [%d mV]" {mqtt="<[broker:zigbee/THPS:state:JSONPATH($.voltage)]"}
Number THPS_BATTERY "THPS Battery [%.1f %%]" {mqtt="<[broker:zigbee/THPS:state:JSONPATH($.battery)]"}
  • IKEA TRADFRI LED bulb E27 1000 lumen, dimmable, opal white LED1623G12
Switch IKEAE27_TOGGLE "IKEAE27 [%s]"         <light> {mqtt=">[broker:zigbee/IKEAE27/set:command:*:JS(setZigbeeState.js)],<[broker:zigbee/IKEAE27:state:JSONPATH($.state)]", expire="120m,command=OFF"}
Dimmer IKEAE27_DIMM "IKEAE27 Dimm"         <light>  {mqtt=">[broker:zigbee/IKEAE27/set:command:*:JS(setZigbeeBrightness.js)],<[broker:zigbee/IKEAE27:state:JS(getZigbeeBrightness.js)]"}

I have not converted my other bulbs/lamps (innr, OSRAM, Philips) yet, i need the alert functionality implemented in zigbee2mqtt first.

I will add this one soon:

  • Xiaomi Aqara door & window contact sensor MCCGQ11LM

Here are the Javascript transformations (in order of appearance):

  • setZigbeeState.js
(function(x){

    var result = new Object();
    result.state = x;
 
    return JSON.stringify(result);
    
})(input)
  • getZigbeeClick.js
(function(x){
 
    var result = "none";
 
    var json = JSON.parse(x);  
    try
    {
        result = json.click;
    }
    catch(e)
    {
        result = "none";
    }

    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)
  • getZigbeeOccupancy2Switch.js
(function(x){

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

    var brightness = x*255/100;

    var result = new Object();
    result.brightness = brightness;
 
    return JSON.stringify(result);
    
})(input)
  • getZigbeeBrightness.js
(function(x){

    var result;
 
    var json = JSON.parse(x);  
    result = json.brightness * 100 / 255;

    return result;
    
})(input)
16 Likes

Is this a limit of just the cc2531 coordinators or is it a limit on some of the others as well? I did some searching but my Google skills were not up to the task. I run an HUSBZ-1 which has an Ember based chip but I’ve not added any Zigbee devices yet. Knowing there is a limit may help guide my future growth.

As far as i understood, it is a limit of the cc2531 in combination with the firmware used. ->network size

The hue bridge for example is limited to 50 devices.

I also have a Raspbee lying around, i will test this with the zigbee binding as soon as it is supported.

Great work :slight_smile: I am about to migrate my xiaomi devices over to zigbee2mqtt myself, and this will save me from lots of work. I am also considering migrating my trĂĄdfri bulbs over from the zigbee-binding as i find it quite unstable at the moment.

1 Like

That’s the reason for my post. :wink:

My reason were the problems with the OSRAM plugs & the hue bridge. Some plugs made the bridge crash, some refused to work after some days.

On the other hand, my experience with zigbee2mqtt is rock stable. The cheap Xiaomi sensors/buttons were a bonus i discovered later on. :smiley:

For my bulbs, i wait until the alert function is implemented.

Hi @job,

Really really nice! This would not only lower the costs of the power bill, when you can skip all those Bridges ;).

I will def. look into this, but unfortunately I’m a newbie, so may I ask what Zigbee device you use in order to be able to communicate with the IKEA/Xiaomi etc. devices?

The CC2531. Bought the debugger, the downloader cable and the sniffer, flashed the firmware and off i went.

Have a look here, it’s pretty straight forward.

BTW, i doubt the power bill of the bridges is anything worth to think about. The big plus is vendor indenpendence.

1 Like

Hey Joachim,

Indeed - was just joking :slight_smile:

Yeah looks pretty straight forward. Better get the tools bought then :slight_smile:.
The flashed CC2531, you just plug that into a separate pi running either docker or stretch explained on the guide or are you using it on your pi with openHAB?

Are the bulbs running stable over a longer period of time?Only asking as i experience periodic loss of control over the bulbs using the zigbee binding.

I’m using my openHABPi currently. I may add the OSMC Kodis later on, when i switched all devices due to the 35 devices limit.

But be aware, it just works with stretch. I had to reinstall my openhabian, becuase it was still on jessi.

I can’t comment on that, i just added one bulb for testing a few days ago. And my test lamp is getting switched off quite often.

Ok, i will migrate one tonight for testing.

So I guess I’m good to go, when I see this on my openHAB installation:
Release = Raspbian GNU/Linux 9 (stretch)

What do you mean:

Hey Lars,

Are you running the same strech with the C2531 unit?

I am running Raspberry Pis with Kodi (OSMC) in every floor. These are based on stretch as well. Whenever i reach the limit, i will add a cc2531 to one of those pi, install zigbee2mqtt and move the devices from that floor to the new instance.

Hi, I am using a CC2531 on a separate raspberry. Planing on moving my Mosquitto over to that RPI also to keep my Openhab RPI “clean”.

As you ad routers(eg trĂĄdfri-bulb\xiaomi powerplugs) the limit will increase? Or am i mistaking?

I don’t think so. I will see. I have plenty of routers already. I understod it, that there is not enough space on the cc2531 to store the device information. And you could add another cc2531 as a router with another bunch of devices to overcome the limit.

I doubt the data is outsourced to any router in the network.

I found som discussion here: https://github.com/Koenkk/zigbee2mqtt/issues/26

This should not be a practical limit. ZigBee routers (and the coordinator is a router) each allow a certain number of children to join. Once they are full, then they no longer publish that they can accept new children - but other routers will still allow joins. This means that the network can be a lot bigger than the number of children that the coordinator can accept - assuming that you have more than 1 router in your network (every mains device is a router).

The coordinator is just a router with some additional functionality (such as the Trust Centre). Unlike some protocols, it doesn’t maintain a list of all connected devices in the network, so the coordinator should not limit the network size.

This limitation will also be a limitation of the MQTT bridge as well as the OH ZigBee binding, but as above, this really isn’t likely to be a practical issue.

I suspect that this is an implementation issue with Hue, and it is not related to ZigBee.

3 Likes