Howto use zigbee2mqtt with openHAB, removing proprietary bridges / gateways

Please format your post nicely, the log is mostly unreadable like that.

Please check your JS transformation install.

JS(setZigbeeState.js)

should never reach zigbee2mqtt, this is openhab logic which should result in on or off.

Hi, this is my setZigbeeState.js unter transformation:
(function(x){

var result = new Object();
result.state = x;

return JSON.stringify(result);

})(input)

Did you install JS transformation?

hey there! i am a newby @ohab
I’ve read almost the whole thread and am still stucked. I also tried that linked mqtt tutorial but it just dont work :frowning:

Can you please help me?

I have sucessfully installed mosquitto on my raspi and prepared zigbee2mqtt. I can log in with mqtt.fx (mqtt-client) and can controll a phillips hue with that. Standard-topic is: zigbee2mqtt/bulbID/set for commands and zigbee2mqtt/bulbid for state.

But it just doesnt work with openhab. At first i ve installed the binding “MQTT Binding” and read the documentation over and over and
 i just dont get it. I’ve tried out several configurations.

  1. Question: Where can i configure the prefix. It seems u can change it since some of u are using “broker”, others “pi” 

    I mean that: {mqtt=">[broker:zigbee


  2. What do i really need to send and receive messages to my broker? I’ve tried out to install a thing with paper ui and also the exemple from the documentation of the mqtt binding

mqttConnections.things:

mqtt:broker:myUnsecureBroker [ host="192.168.0.42", secure=false]

When i just use the paper ui and choose the “MQTT Broker” thing and enter my mosquitto-ip, it seems that ohab successfully connects to the broker.

LOG:

2019-01-14 22:02:57.938 [INFO ] [.transport.mqtt.MqttBrokerConnection] - Starting MQTT broker connection to ‘192.168.178.101’ with clientid paho15871661283957 and file store ‘/var/lib/openhab2/mqtt/192.168.178.101’
2019-01-14 22:02:57.956 [hingStatusInfoChangedEvent] - ‘mqtt:broker:zigbee2mqtt’ changed from OFFLINE to ONLINE

2019-01-14 22:02:57.966 [me.event.ThingUpdatedEvent] - Thing ‘mqtt:broker:zigbee2mqtt’ has been updated.

I also tried the examples from the linked tutorial with the
test.items:

Switch MQTT_Test "Testing..." { mqtt="<[broker:testing/mqtt/topic:state:default], >[192.168.178.101:testing/mqtt/back-topic:command:*:default]" }  
// ^-- Item Type              ^-- Item linked Channel
//         ^-- Item Name              ^-- Inbound MQTT configuration              ^-- Outbound MQTT configuration
//                   ^-- Item Label

and then publishing (with MQTT.fx) “ON” at TOPIC: testing/mqtt/topic 
 but nothing happens in the log viewer.

You need to enable the 1.x mqtt binding to make this work.

I use your code for my Osram bulb but I modified it a little bit, so the hsv value is only convert to rgb and brightness:

/**
 * hsv2xyz.js Convert OpenHab HSV to xy for Hue/Lightify
 * 8/2018 C.Schmidhuber 
 * @param str string "h,s,v"
 * @return json-string 
 */
(function(str){

    // hsv/hsb to rgb
    // h,s,v = [0..1]
    var hsvToRgb = function(h, s, v) 
    {
        // @see https://gist.github.com/mjackson/5311256
        var r, g, b;
      
        var i = Math.floor(h * 6);
        var f = h * 6 - i;
        var p = v * (1 - s);
        var q = v * (1 - f * s);
        var t = v * (1 - (1 - f) * s);
      
        switch (i % 6) {
          case 0: r = v, g = t, b = p; break;
          case 1: r = q, g = v, b = p; break;
          case 2: r = p, g = v, b = t; break;
          case 3: r = p, g = q, b = v; break;
          case 4: r = t, g = p, b = v; break;
          case 5: r = v, g = p, b = q; break;
        }
      
        return [ r , g , b  ];
      };

    x = str.split(",");
    var brightness=x[2];

    x = hsvToRgb(x[0]/360, x[1]/100, x[2]/100);
    red = x[0];
    green =x[1];
    blue = x[2];

    return JSON.stringify({
        'brightness':Math.round(brightness*2.55), 'color': {'r':Math.round(red * 255),'g':Math.round(green * 255),'b':Math.round(blue * 255)} 
    });
    
})(input)
1 Like

I am too stupid to get it up and running. I want to get a Xiaomi Aqara motion sensor to run.

My Zigbee2MQTT is correctly running, MQTTBroker is running and working with OH2 (have a Tasmota Switch that works in OH already), I installed the js, the jason and the map transformation, The Xiaomi Sensor is sending values (tested with MQTTlens), I created like described above an item(MQTT_Motion_Sensor_Guest_Bathroom.items) in the item folder with the following content:

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)]"}

and the following file (getZigbeeOccupancy2Switch.js) in the transformations folder:

(function(x){

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

Now I have to add a channel in the MQQT2 binding in PaperUI? Which one is it? On Off Switch? How to work with the luminance sensor vs the motion sensor here? Thanks in advance!!

EDIT: And how to get the Battery values??
EDIT2: Error is on the logs:Failed transforming the state ‘NULL’ on item ‘MD_MOTION’ with pattern ‘MAP(HM-Sen-MDIR-O-2.map):%s’: An error occurred while opening file.

You have to use the old1.x mqtt binding.

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?