Howto use zigbee2mqtt with openHAB, removing proprietary bridges / gateways

Is there no way to use the new one also? Do i have to replace any code part by the topic of the device? Zigbee/MD or so?

I did not look at the new binding yet, because it does not support everything necessary to fulfill all my usecases. But you can run both bindings side by side. I will look into v2 as soon as it supports everything I need. I don’t want half the devices run v1 and the other half v2.

Ok I will try that. Do I have to exchange part of the code to the device specific topic path/name?

Am I able to use with that code one item for motion sensor and one to get the illumination values?

And how do I get the battery values out of it?

Yes.

For the battery, just use one of the battery definition in o. p.

Exchange the topic as well there.

I got the motion one working now by the mqtt1 Binding. Still I get this in my logs, eventhough it is working:\

2019-01-28 14:08:07.832 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state ‘OFF’ on item ‘MD_MOTION_WC’ with pattern ‘MAP(HM-Sen-MDIR-O-2.map):%s’: An error occurred while opening file.

What does it mean?

The lumen values I get, even though they are always just showing 6 lumen (I don’t know why), by the item definition, but not the battery values. I used this item definition for the battery. Is there a mistake?

Number MD_BATTERY_WC "MD BAT WC [%.1f %%]" {mqtt="<[broker:zigbee2mqtt/mqtt_aqara_motion_sensor:state:JSONPATH($.battery)]"}

I added all transformations like posted in the first post in my transform folder btw.

EDIT: The battery values all the sudden appeared, but the lumen value never changed, but it looks like it actually never changed in the mqttlens view either…

I am working on the Aqara Cube items now, three of the four values I got:

Number CUBE_BATTERY "CUBE BAT [%.1f %%]" {mqtt="<[broker:zigbee2mqtt/mqtt_aqara_cube:state:JSONPATH($.battery)]"}
Number CUBE_VOLTAGE "CUBE Volt [%d mV]" {mqtt="<[broker:zigbee2mqtt/mqtt_aqara_cube:state:JSONPATH($.voltage)]"}
Number CUBE_ANGLE "CUBE Volt [%d deg]" {mqtt="<[broker:zigbee2mqtt/mqtt_aqara_cube:state:JSONPATH($.angle)]"}

This here I am getting on mqttlens:
{“battery”:55,“voltage”:2995,“linkquality”:73,“action”:“rotate_right”,“angle”:77.59}

Linkquality is not so interesting for me I think. My issue is the last one “action”. That one changes between these states here:
“action”:“rotate_right”
“action”:“wakeup”
“action”:“shake”
“action”:“flip90”,“from_side”:1,“to_side”:2
“action”:“flip180”,“side”:2

Probably even more states… How can I use these??

Today I got my first zigbee devices and setup everything. OpenHab with recent MQTT 2 is easier to use.
Here is my sample. Hope it will be helpful for someone else.
zigbee2mqtt is configured with default settings.

My zigbee.things with the mqtt specific setup.

Bridge mqtt:broker:myMQTTBroker [ host="mqtt", secure=false, username="$user", password="$pass" , clientID="$client" ]
{
    Thing topic zigbeeMQTT "Zigbee2mqtt" {
    Channels:
        Type switch : permitJoin         [ commandTopic="zigbee2mqtt/bridge/config/permit_join", on="true", off="false" ]
        Type string : state              [ stateTopic="zigbee2mqtt/bridge/state" ]
        Type string : logType            [ stateTopic="zigbee2mqtt/bridge/log",  transformationPattern="JSONPATH:$.type" ]
        Type string : logMessage         [ stateTopic="zigbee2mqtt/bridge/log",  transformationPattern="JSONPATH:$.message" ]
    }

    Thing topic zigbeeButtonOne "Zigbee button one" {
    Channels:
        Type string : click              [ stateTopic="zigbee2mqtt/0x00158d000276e6ed",  transformationPattern="JSONPATH:$.click" ]
        Type number : voltage            [ stateTopic="zigbee2mqtt/0x00158d000276e6ed",  transformationPattern="JSONPATH:$.voltage" ]
        Type number : battery            [ stateTopic="zigbee2mqtt/0x00158d000276e6ed",  transformationPattern="JSONPATH:$.battery" ]
        Type number : linkquality        [ stateTopic="zigbee2mqtt/0x00158d000276e6ed",  transformationPattern="JSONPATH:$.linkquality" ]
    }

    Thing topic zigbeeSensorOne "Zigbee sensor one" {
    Channels:
        Type number : temperature        [ stateTopic="zigbee2mqtt/0x00158d0002c8cca6",  transformationPattern="JSONPATH:$.temperature" ]
        Type number : humidity           [ stateTopic="zigbee2mqtt/0x00158d0002c8cca6",  transformationPattern="JSONPATH:$.humidity" ]
        Type number : pressure           [ stateTopic="zigbee2mqtt/0x00158d0002c8cca6",  transformationPattern="JSONPATH:$.pressure" ]
        Type number : voltage            [ stateTopic="zigbee2mqtt/0x00158d0002c8cca6",  transformationPattern="JSONPATH:$.voltage" ]
        Type number : battery            [ stateTopic="zigbee2mqtt/0x00158d0002c8cca6",  transformationPattern="JSONPATH:$.battery" ]
        Type number : linkquality        [ stateTopic="zigbee2mqtt/0x00158d0002c8cca6",  transformationPattern="JSONPATH:$.linkquality" ]
    }
}

zigbee.items

// Control zigbee2mqtt
Group  gZigbeeMQTT    "Zigbee2mqtt"
String ZigbeeState               "Zigbee2mqtt state"                  <switch>    (gZigbeeMQTT) { channel="mqtt:topic:myMQTTBroker:zigbeeMQTT:state" }
Switch ZigbeePermitJoin          "Permit join new devices"            <switch>    (gZigbeeMQTT) { channel="mqtt:topic:myMQTTBroker:zigbeeMQTT:permitJoin" }
String ZigbeeLogType             "Zigbee2mqtt log type"                           (gZigbeeMQTT) { channel="mqtt:topic:myMQTTBroker:zigbeeMQTT:logType" }
String ZigbeeLogMessage          "Zigbee2mqtt log message"                        (gZigbeeMQTT) { channel="mqtt:topic:myMQTTBroker:zigbeeMQTT:logMessage" }

// Xiaomi Aqara wireless buttons WXKG01LM
Group  gZigbeeButton    "Zigbee Buttons"

String ZigbeeButtonOneClick        "Button One Click [%s]"            <button>    (gZigbeeButton) { channel="mqtt:topic:myMQTTBroker:zigbeeButtonOne:click" }
Number ZigbeeButtonOneLinkQuality  "Button One Link Quality [%d]"     <network>   (gZigbeeButton) { channel="mqtt:topic:myMQTTBroker:zigbeeButtonOne:linkquality" }
Number ZigbeeButtonOneVoltage      "Button One Voltage [%d mV]"       <energy>    (gZigbeeButton) { channel="mqtt:topic:myMQTTBroker:zigbeeButtonOne:voltage" }
Number ZigbeeButtonOneBattery      "Button One Battery [%.1f %%]"     <battery>   (gZigbeeButton) { channel="mqtt:topic:myMQTTBroker:zigbeeButtonOne:battery" }

// Xiaomi Aqara temperature, humidity and pressure sensor WSDCGQ11LM
Group  gZigbeeSensor    "Zigbee Sensors"

Number ZigbeeSensorOneTemperature  "Sensor One Temperature [%.2f °C]"  <temperature>  (gZigbeeSensor) { channel="mqtt:topic:myMQTTBroker:zigbeeSensorOne:temperature" }
Number ZigbeeSensorOneHumidity     "Sensor One Humidity [%.2f %%]"     <humidity>     (gZigbeeSensor) { channel="mqtt:topic:myMQTTBroker:zigbeeSensorOne:humidity" }
Number ZigbeeSensorOnePressure     "Sensor One Pressure  [%d hPa]"     <pressure>     (gZigbeeSensor) { channel="mqtt:topic:myMQTTBroker:zigbeeSensorOne:pressure" }
Number ZigbeeSensorOneLinkQuality  "Sensor One Link Quality [%d]"      <network>      (gZigbeeSensor) { channel="mqtt:topic:myMQTTBroker:zigbeeSensorOne:linkquality" }
Number ZigbeeSensorOneVoltage      "Sensor One Voltage [%d mV]"        <energy>       (gZigbeeSensor) { channel="mqtt:topic:myMQTTBroker:zigbeeSensorOne:voltage" }
Number ZigbeeSensorOneBattery      "Sensor One Battery [%.1f %%]"      <battery>      (gZigbeeSensor) { channel="mqtt:topic:myMQTTBroker:zigbeeSensorOne:battery" }

zigbee.rules with additional rule to automatically disable “permit join” with 2 minutes delay


rule "Auto switch off permit join"
when
  Item ZigbeePermitJoin changed to ON
then
    createTimer(now.plusMinutes(2)) [|
                sendCommand(ZigbeePermitJoin, OFF)
            ]
end

rule "Button WXKG01LM One"
when
  Item ZigbeeButtonOneClick received update
then
    if (ZigbeeButtonOneClick.state.toString == "single") {
        if(KidsWardrobeLamp.state == ON) {
            sendCommand(KidsWardrobeLamp, OFF)
        } else {
            sendCommand(KidsWardrobeLamp, ON)
        }
    } else if (ZigbeeButtonOneClick.state.toString == 'double') {
        if(MirrorLamp.state == ON) {
            sendCommand(MirrorLamp, OFF)
        } else {
            sendCommand(MirrorLamp, ON)
        }
    } else if (ZigbeeButtonOneClick.state.toString == 'long') {
        if(KidsWardrobeLamp.state == ON || BedroomWardrobeLamp.state == ON) {
            sendCommand(KidsWardrobeLamp, OFF)
            sendCommand(BedroomWardrobeLamp, OFF)
        } else {
            sendCommand(KidsWardrobeLamp, ON)
            sendCommand(BedroomWardrobeLamp, ON)
        }
    }
end

finally sitemap

	Frame label="Zigbee devices" {
		Group  item=gZigbeeMQTT
		Group  item=gZigbeeButton
		Group  item=gZigbeeSensor
	}
5 Likes

The main problem with the new binding is that there is no way (or limited in some cases) to use the commandTopic with zigbee2mqtt.

See also: Need help to migrate to the mqtt binding v2 in openhab 2.4

and another tut: MQTT 2.4, ZigBee2MQTT, Xiaomi Aqara Sensors (sample config)

@job Is there already some progress regarding the OSRAM lamps?
I would like to add my Lightify RGBW Flex, but the only thing I can do right now, is to switch it off…

Hello!

I set up zigbee2mqtt with a aquara door contact. Zigbee2mqtt is publishing via mqtt, so its working. But i cant get it to work with OH. I am using @tebore sample and have installed the javascript transformation.

zigbee2mqtt.items:

Group gXiaoMiZigbee	"XiaoMi sensors"	<zigbee>

String Zigbee2MQTT_Pair		"Pairing Mode [$s]"	<qualityofservice>	{mqtt="<[mosquitto:zigbee2mqtt/config/permit_join:state:default], >[mosquitto:zigbee2mqtt/config/permit_join:command:*:default]"}

Group gXiaoMiZ2BContact	"Xiaomi Contact"	<zigbee>	(gXiaoMiZigbee)
Contact XAIOMI_CONTACT 	"Contact"  						(gXiaoMiZ2BContact)	{mqtt="<[mosquitto:zigbee2mqtt/Fenstersensor1:state:JS(getZigbeeContact.js)]"}
Number 	XAIOMI_VOLTAGE 	"Voltage [%d mV]" 	<battery>	(gXiaoMiZ2BContact)	{mqtt="<[mosquitto:zigbee2mqtt/Fenstersensor1:state:JSONPATH($.voltage)]"}
Number 	XAIOMI_BATTERY 	"Battery [%.1f %%]" <battery>	(gXiaoMiZ2BContact)	{mqtt="<[mosquitto:zigbee2mqtt/Fenstersensor1:state:JSONPATH($.battery)]"}

getZigbeeContact.js

(function(x){

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

    return result;
    
})(input)

Log:

2019-02-17 09:54:35.160 [WARN ] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.

org.openhab.core.transform.TransformationException: An error occurred while loading script.

	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) ~[212:org.openhab.core.compat1x:2.4.0]

	at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:133) [222:org.openhab.binding.mqtt:1.13.0]

	at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:570) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:475) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:379) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:183) [223:org.openhab.io.transport.mqtt:1.13.0]

	at java.lang.Thread.run(Thread.java:748) [?:?]

2019-02-17 09:54:35.165 [WARN ] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.

org.openhab.core.transform.TransformationException: Invalid path '$.voltage' in '{"contact":false,"linkquality":36}'

	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) ~[212:org.openhab.core.compat1x:2.4.0]

	at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:133) [222:org.openhab.binding.mqtt:1.13.0]

	at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:570) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:475) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:379) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:183) [223:org.openhab.io.transport.mqtt:1.13.0]

	at java.lang.Thread.run(Thread.java:748) [?:?]

2019-02-17 09:54:35.169 [WARN ] [.mqtt.internal.MqttMessageSubscriber] - Error processing MQTT message.

org.openhab.core.transform.TransformationException: Invalid path '$.battery' in '{"contact":false,"linkquality":36}'

	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:67) ~[212:org.openhab.core.compat1x:2.4.0]

	at org.openhab.binding.mqtt.internal.MqttMessageSubscriber.processMessage(MqttMessageSubscriber.java:133) [222:org.openhab.binding.mqtt:1.13.0]

	at org.openhab.io.transport.mqtt.internal.MqttBrokerConnection.messageArrived(MqttBrokerConnection.java:570) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.deliverMessage(CommsCallback.java:475) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.handleMessage(CommsCallback.java:379) [223:org.openhab.io.transport.mqtt:1.13.0]

	at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:183) [223:org.openhab.io.transport.mqtt:1.13.0]

	at java.lang.Thread.run(Thread.java:748) [?:?]

his is the output ofmqtt.fx:

{"contact":false,"linkquality":34}

Please help me!

1 Like

Solved it partially. I put the *.js in the wrong directory…

But why isnt the battery status transmitted?

Edit: Also solved. The battery status is now submittet after a short press onthe button on the sensor

Can anybody help me with my request here:
Howto use zigbee2mqtt with openHAB, removing proprietary bridges / gateways - #74 by DarkZark (why do I get that warning/error message?)

here I asked for how to use the other aqara cube values:

My last question is, how can I display on HabPanel the times a sensor was last time triggered? I guess in some way by the openhab log… but how?

I thought I would share this in case anybody else finds it useful.

I have managed to get zigbee2MQTT working with an Innr and Ikea Tradfri bulb using the latest MQTT binding without using a javescript transformation, just a JSON one for the incoming message.

Things file.

Bridge mqtt:broker:myUnsecureBroker [ host="192.168.2.4", secure=false ]
{
    Thing topic zigbeeMQTT "Zigbee2mqtt" {
    Channels:
        Type switch : permitJoin         [ commandTopic="zigbee2mqtt/bridge/config/permit_join", on="true", off="false" ]
        Type dimmer : Test_Lamp "Test Lamp" [ stateTopic="zigbee2mqtt/Her_Lamp", commandTopic="zigbee2mqtt/Her_Lamp/set", min=1, max=254, step=1, transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{   \"brightness\": %s }" ]
        //Main Study Lights
        Type dimmer : Main_Study_1 "Test Main Study 1" [ stateTopic="zigbee2mqtt/Main_Study_1", commandTopic="zigbee2mqtt/Main_Study_1/set", min=1, max=254, step=1, transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{   \"brightness\": %s }" ]
        Type dimmer : Main_Study_2 "Test Main Study 2" [ stateTopic="zigbee2mqtt/Main_Study_2", commandTopic="zigbee2mqtt/Main_Study_2/set", min=1, max=254, step=1, transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{   \"brightness\": %s }" ]
        Type dimmer : Main_Study_3 "Test Main Study 3" [ stateTopic="zigbee2mqtt/Main_Study_3", commandTopic="zigbee2mqtt/Main_Study_3/set", min=1, max=254, step=1, transformationPattern="JSONPATH:$.brightness", formatBeforePublish="{   \"brightness\": %s }" ]
    }
}

The ‘Min’ value is set to 1 as that’s what my bulbs report as off, ‘Max’ is 254 so the value can be between 1-255.

The ‘transformationPattern’ selects just the ‘Brightness’ value from the incoming JSON message. The ‘formatBeforePublish’ works to format the outgoing message into JSON format.

I have tested it with a couple of my lights and I am able to control them fine using Basic UI, HabPanel and through my Alexa.

2 Likes

@DarkZark, I’m no expert but this is how I am doing similar with a Xiaomi Button

Item

String CUBE_ACTION "Cube Action" {mqtt="<[broker:zigbee2mqtt/mqtt_aqara_cube:state:JSONPATH($.action)]"}

Then you can make a rule that will trigger when CUBE_ACTION is updated.

rule "Cube Action"
when
    Item CUBE_ACTION recieved update
then
    switch(CUBE_ACTION) {
        case "rotate_right":{
            //Do Something
        }
        case "shake": {
            //Do Something Else
        }
    }
end

You can add more case entries for each of the different actions the cube reports.

In terms of the error you are seeing about a motion sensor, can you confirm you have a file called “HM-Sen-MDIR-O-2.map” in you transform folder?

I have recently installed zigbee2mqtt with the help of this thread and I’m quite pleased - thanks to all of you.
But now I’m facing a problem where I need help.
I added my Xiaomi Mijja smoke sensors to zigbee2mqtt and that works quite well.
I added these to oh2:

Contact XIAOMI_SMOKE_EG1_STATE “Smoke [%s]” {mqtt="<[broker:zigbee2mqtt/xyz:state:JSONPATH($.smoke)]"}
Contact XIAOMI_SMOKE_EG_STATE “Smoke [%s]” {mqtt="<[broker:zigbee2mqtt/xyz:state:JS(getZigbeeSmoke.js)]" }
Number XIAOMI_SMOKE_EG_VOLTAGE “Smoke Volt [%d mV]” {mqtt="<[broker:zigbee2mqtt/xyz:state:JSONPATH($.voltage)]"}
Number XIAOMI_SMOKE_EG_BATTERY “Smoke BAT [%.1f %%]” {mqtt="<[broker:zigbee2mqtt/xyz:state:JSONPATH($.battery)]"}
Number XIAOMI_SMOKE_EG_LINK “Smoke Link [%d]” {mqtt="<[broker:zigbee2mqtt/xyz:state:JSONPATH($.linkquality)]"}

Both of the smoke state items do not work.
When i press the button on the smoke sensor i receive the following warning in the log:

2019-02-24 11:09:34.890 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn’t post update for ‘XIAOMI_SMOKE_EG1_STATE’
2019-02-24 11:09:35.426 [WARN ] [b.core.events.EventPublisherDelegate] - given new state is NULL, couldn’t post update for ‘XIAOMI_SMOKE_EG_STATE’

I have other json transformations that work quite well - so i guess it is not the transformation setup anyway this is the getZigbeeSmoke.js

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

I’m really stuck here - so any help is highly appreciated
Thanks

@Bond1986
Not that I know of. But I also did not follow the development closely. I am currently at the limit, for each device I add, another one does not work anymore.

@hipohop
Could you show the message sent? Without it is impossible to help. It looks like the message does not contain that smoke attribute.

From the zigbee2mqtt log:

Feb 28 08:19:33 raspberrypi npm[400]: zigbee2mqtt:info 2019-2-28 08:19:33 MQTT publish: topic ‘zigbee2mqtt/0x…’, payload ‘{“battery”:100,“voltage”:3105,“linkquality”:35,“smoke”:false}’

Ahhh… Now I see. If you use a Contact item you need to use OPEN and CLOSED as states. If you like to use ON/OFF the item must be defined as a Switch.

1 Like

Ahhh … perfect! Thank you very much !