Zigbee2mqtt command

Hello,I searched in vain how to write a post, that’s why I write to you directly inbox
i have problem with zigbee2mqtt.
i use xiaomi aqara device. For aqara switch, (wall switch, socket plug, wireless switch)… i don’t understand how to send command. i can’t control device via openhab. please help

.thing file

Bridge mqtt:broker:mybroker [ host="localhost", port=1883, secure=false ]
{
// Door sensors
Thing topic doorsensor1 "doorsensor1" {
Channels:
    Type contact : contact "Contact"        [ stateTopic="zigbee2mqtt/doorsensor1", transformationPattern="JS:getZigbeeContact2Contact.js"]
    Type number : voltage "Voltage"         [ stateTopic="zigbee2mqtt/doorsensor1", transformationPattern="JSONPATH:$.voltage"]
    Type number : battery "Battery"         [ stateTopic="zigbee2mqtt/doorsensor1", transformationPattern="JSONPATH:$.battery"]
    Type number : linkquality "Linkquality" [ stateTopic="zigbee2mqtt/doorsensor1", transformationPattern="JSONPATH:$.linkquality" ]

}

Thing topic wirelessdoubleswitch1 "wirelessdoubleswitch1" {
Channels:
    Type string : click "Click"             [ stateTopic="zigbee2mqtt/wirelessdoubleswitch1", commandTopic="zigbee2mqtt/wirelessdoubleswitch1/system/set", transformationPattern="JS:getZigbeeClick.js" ]
    Type number : voltage  "Voltage"        [ stateTopic="zigbee2mqtt/wirelessdoubleswitch1",  transformationPattern="JSONPATH:$.voltage" ]
    Type number : battery  "Battery"        [ stateTopic="zigbee2mqtt/wirelessdoubleswitch1",  transformationPattern="JSONPATH:$.battery" ]
    Type number : linkquality "Linkquality" [ stateTopic="zigbee2mqtt/wirelessdoubleswitch1",  transformationPattern="JSONPATH:$.linkquality" ]
}

Thing topic wallsocketplug1 "wallsocketplug1" {
Channels:
    Type switch : command "State"           [ stateTopic="zigbee2mqtt/wallsocketplug1", commandTopic="zigbee2mqtt/wallsocketplug1/system/set", transformationPattern="JSONPATH:$.power" ]
    Type number : power "Power"             [ stateTopic="zigbee2mqtt/wallsocketplug1", transformationPattern="JSONPATH:$.power"] 
    Type number : voltage  "Voltage"        [ stateTopic="zigbee2mqtt/wallsocketplug1", transformationPattern="JSONPATH:$.consumption" ]
    Type number : temperature "Temperature" [ stateTopic="zigbee2mqtt/wallsocketplug1", transformationPattern="JSONPATH:$.temperature"]
    Type number : battery  "Battery"        [ stateTopic="zigbee2mqtt/wallsocketplug1", transformationPattern="JSONPATH:$.battery" ]
    Type number : linkquality "Linkquality" [ stateTopic="zigbee2mqtt/wallsocketplug1", transformationPattern="JSONPATH:$.linkquality" ]
}

Thing topic wallsingleswitch1 "wallsingleswitch1" {
Channels:
    Type switch : command "State"           [ stateTopic="zigbee2mqtt/wallsingleswitch1", commandTopic="zigbee2mqtt/wallsingleswitch1/state" ]
    Type number : power "Power"             [ stateTopic="zigbee2mqtt/wallsingleswitch1", transformationPattern="JSONPATH:$.power"] 
    Type number : voltage  "Voltage"        [ stateTopic="zigbee2mqtt/wallsingleswitch1", transformationPattern="JSONPATH:$.consumption" ]
    Type number : temperature "Temperature" [ stateTopic="zigbee2mqtt/wallsingleswitch1", transformationPattern="JSONPATH:$.temperature"]
    Type number : battery  "Battery"        [ stateTopic="zigbee2mqtt/wallsingleswitch1", transformationPattern="JSONPATH:$.battery" ]
    Type number : linkquality "Linkquality" [ stateTopic="zigbee2mqtt/wallsingleswitch1", transformationPattern="JSONPATH:$.linkquality" ]
}

/*
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" ]


    } 
*/

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

}

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

/*
//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"]
}
*/
}

.items

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

String WirelessDoubleSwitch1Button       "Click [%s]"            <button>          (gZigbeeButton)  ["Switchable"] { channel="mqtt:topic:mybroker:wirelessdoubleswitch1:click" }
Number WirelessDoubleSwitch1LinkQuality  "Réseau [%d]"           <network>         (gZigbeeButton) { channel="mqtt:topic:mybroker:wirelessdoubleswitch1:linkquality" }
Number WirelessDoubleSwitch1Voltage      "Voltage [%d mV]"       <energy>          (gZigbeeButton) { channel="mqtt:topic:mybroker:wirelessdoubleswitch1:voltage" }
Number WirelessDoubleSwitch1Battery      "Batterie [%.1f %%]"    <battery>         (gZigbeeButton) { channel="mqtt:topic:mybroker:wirelessdoubleswitch1:battery" }


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

Number  HumiditySensor1Temperature  "Temperature [%.2f °C]"   <temperature>         (gZigbeeSensor)  ["CurrentTemperature"] { channel="mqtt:topic:mybroker:humiditysensor1:temperature" }
Number  HumiditySensor1Humidity     "Humidité [%.2f %%]"      <humidity>            (gZigbeeSensor)  ["CurrentHumidity"] { channel="mqtt:topic:mybroker:humiditysensor1:humidity" }
Number  HumiditySensor1Pressure     "Pression [%d hPa]"       <pressure>            (gZigbeeSensor) { channel="mqtt:topic:mybroker:humiditysensor1:pressure" }
Number  HumiditySensor1Voltage      "Voltage [%d mV]"         <energy>              (gZigbeeSensor) { channel="mqtt:topic:mybroker:humiditysensor1:voltage" }
Number  HumiditySensor1Battery      "Batterie [%.1f %%]"      <battery>             (gZigbeeSensor) { channel="mqtt:topic:mybroker:humiditysensor1:battery" }
Number  HumiditySensor1LinkQuality  "Réseau [%d]"             <qualityofservice>    (gZigbeeSensor) { channel="mqtt:topic:mybroker:humiditysensor1:linkquality" }


Contact DoorSensor1                 "Porte [%s]"              <door>                (gZigbeeSensor)  ["Switchable"] { channel="mqtt:topic:mybroker:doorsensor1:contact" }
Number  DoorSensor1Voltage          "Voltage [%d mV]"         <energy>              (gZigbeeSensor) { channel="mqtt:topic:mybroker:doorsensor1:voltage" }
Number  DoorSensor1Battery          "Batterie [%.1f %%]"      <battery>             (gZigbeeSensor) { channel="mqtt:topic:mybroker:doorsensor1:battery" }
Number  Doorsensor1LinkQuality      "Réseau [%d]"             <qualityofservice>    (gZigbeeSensor) { channel="mqtt:topic:mybroker:doorsensor1:linkquality" }


String  MotionSensor1               "Mouvement [%s]"          <motion>              (gZigbeeSensor)  ["Switchable"] { channel="mqtt:topic:mybroker:motionsensor1:motion" }
Number  MotionSensor1Brightness     "Luminescance [%d lumen]" <sun>                 (gZigbeeSensor) { channel="mqtt:topic:mybroker:motionsensor1:brightness" }
Number  MotionSensor1Voltage        "Voltage [%d mV]"         <energy>              (gZigbeeSensor) { channel="mqtt:topic:mybroker:motionsensor1:voltage" }
Number  MotionSensor1Battery        "Batterie [%.1f %%]"      <battery>             (gZigbeeSensor) { channel="mqtt:topic:mybroker:motionsensor1:battery" }
Number  MotionSensor1LinkQuality    "Réseau [%d]"             <qualityofservice>    (gZigbeeSensor) { channel="mqtt:topic:mybroker:motionsensor1:linkquality" }

Switch  WallSocketPlug1             "Etat [%s]"               <poweroutlet_eu>      (gZigbeeSensor)  ["Switchable"] { channel="mqtt:topic:mybroker:wallsocketplug1:command" }
Number  WallSocketPlug1Power        "Puissance [%d watt]"     <line>                (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsocketplug1:power" }
Number  WallSocketPlug1Voltage      "Voltage [%d mV]"         <energy>              (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsocketplug1:voltage" }
Number  WallSocketPlug1Temperature  "Temperature [%.2f °C]"   <temperature>         (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsocketplug1:temperature" }
Number  WallSocketPlug1Consumption  "Batterie [%.1f %%]"      <line>                (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsocketplug1:consumption" }
Number  WallSocketPlug1LinkQuality  "Réseau [%d]"             <qualityofservice>    (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsocketplug1:linkquality" }

Switch  WallSingleSwitch1             "Etat [%s]"               <light>              (gZigbeeSensor)  ["Switchable"] { channel="mqtt:topic:mybroker:wallsingleswitch1:command:ON" }
Number  WallSingleSwitch1Power        "Puissance [%d watt]"     <line>               (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsingleswitch1:power" }
Number  WallSingleSwitch1Voltage      "Voltage [%d mV]"         <energy>             (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsingleswitch1:voltage" }
Number  WallSingleSwitch1Temperature  "Temperature [%.2f °C]"   <temperature>        (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsingleswitch1:temperature" }
Number  WallSingleSwitch1Consumption  "Batterie [%.1f %%]"      <line>               (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsingleswitch1:consumption" }
Number  WallSingleSwitch1LinkQuality  "Réseau [%d]"             <qualityofservice>   (gZigbeeSensor) { channel="mqtt:topic:mybroker:wallsingleswitch1:linkquality" }


1 Like

I suggest starting with a single device such as the wallsocketplug1. After successfully switching the device on and off then work to add the other values.

Once you have a single device and all it’s values working move to the next device.

Hi,

I had the same problem trying to get the Xiaomi Aqara socket (ZNCZ02LM) going on openhab using zigbee2mqtt.

I don’t know if you have figured it out yet but the type in your things file:

Type switch : command "State"           [ stateTopic="zigbee2mqtt/wallsocketplug1", commandTopic="zigbee2mqtt/wallsocketplug1/system/set", transformationPattern="JSONPATH:$.power" ]

You need to:

1.) Remove “system” from your commandTopic (see zigbee2mqtt documentation under “zigbee2mqtt/[DEVICE_ID]/set” section: https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html)
2.) Add , on=“ON”, off=“OFF” parameters

Type switch : command "State"           [stateTopic="zigbee2mqtt/wallsocketplug1",  commandTopic="zigbee2mqtt/wallsocketplug1/set", on="ON", off="OFF" ]

My Setup is as follows:

Zigbee2mqtt Configuration: (/opt/zigbee2mqtt/data/configuration.yaml)

devices:
  '0x00158d0002165834':
    friendly_name: aqara_socket_1
    retain: false

Things: (aqara_sockets.thing)

Bridge mqtt:broker:mosquitto [ host="192.X.X.X", secure=false, username="user", password="pass"] {
    // Aqara Socket 1
    Thing topic zigbeeAqaraSocket1 "Living Room Socket" {
    Channels:
        Type switch : state              [ stateTopic="zigbee2mqtt/aqara_socket_1", commandTopic="zigbee2mqtt/aqara_socket_1/set", on="ON", off="OFF" ]
        Type number : temperature        [ stateTopic="zigbee2mqtt/aqara_socket_1",  transformationPattern="JSONPATH:$.temperature" ]
        Type number : consumption        [ stateTopic="zigbee2mqtt/aqara_socket_1",  transformationPattern="JSONPATH:$.consumption" ]
        Type number : voltage            [ stateTopic="zigbee2mqtt/aqara_socket_1",  transformationPattern="JSONPATH:$.voltage" ]
        Type number : power              [ stateTopic="zigbee2mqtt/aqara_socket_1",  transformationPattern="JSONPATH:$.power" ]
        Type number : linkquality        [ stateTopic="zigbee2mqtt/aqara_socket_1",  transformationPattern="JSONPATH:$.linkquality" ]
    }
}

Items: (aqara_sockets.item)

// Xiaomi Aqara socket ZNCZ02LM
Group gZigbeeAqaraSocket1    "Zigbee Aqara Socket 1"

Switch ZigbeeAqaraSocket1State        "Living Room Socket State [%s]"             <poweroutlet>  (gZigbeeAqaraSocket1) ["Switchable"] { channel="mqtt:topic:mosquitto:zigbeeAqaraSocket1:state" }
Number ZigbeeAqaraSocket1Temperature  "Living Room Socket Temperature [%.1f °C]"  <temperature>  (gZigbeeAqaraSocket1) [ "CurrentTemperature" ] { channel="mqtt:topic:mosquitto:zigbeeAqaraSocket1:temperature" }
Number ZigbeeAqaraSocket1LinkQuality  "Living Room Socket Link Quality [%d]"      <network>      (gZigbeeAqaraSocket1) { channel="mqtt:topic:mosquitto:zigbeeAqaraSocket1:linkquality" }
Number ZigbeeAqaraSocket1Voltage      "Living Room Socket Voltage [%d mV]"        <energy>       (gZigbeeAqaraSocket1) { channel="mqtt:topic:mosquitto:zigbeeAqaraSocket1:voltage" }
Number ZigbeeAqaraSocket1Power        "Living Room Socket Power [%d mV]"          <energy>       (gZigbeeAqaraSocket1) { channel="mqtt:topic:mosquitto:zigbeeAqaraSocket1:power" }
Number ZigbeeAqaraSocket1Consumption  "Living Room Socket Consumption [%d mV]"    <energy>       (gZigbeeAqaraSocket1) { channel="mqtt:topic:mosquitto:zigbeeAqaraSocket1:consumption" }

Sitemap: (aqara_sockets.sitemap)

sitemap aqara_sockets label="Smart Sockets" {
    Frame label="Zigbee devices" {
        Group  item=gZigbeeAqaraSocket1
    }
}

Logs:

In Basic UI, when you toggle the switch, you should see something similar in the openhab event log (http://openhabianpi:9001/)

2019-04-10 02:15:00.699 [ome.event.ItemCommandEvent] - Item 'ZigbeeAqaraSocket1State' received command ON

2019-04-10 02:15:00.717 [nt.ItemStatePredictedEvent] - ZigbeeAqaraSocket1State predicted to become ON

2019-04-10 02:15:00.751 [vent.ItemStateChangedEvent] - ZigbeeAqaraSocket1State changed from NULL to ON

==> /var/log/openhab2/openhab.log <==

2019-04-10 02:15:00.953 [WARN ] [eneric.internal.generic.ChannelState] - Command '{"state":"ON","linkquality":78,"power":0,"voltage":227.8,"consumption":0,"temperature":25}' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.{"state":"ON","linkquality":78,"power":0,"voltage":227.8,"consumption":0,"temperature":25}

And in your zigbee2mqtt log (/opt/zigbee2mqtt/data/log/2019-04-10.02-06-10/log.txt) you should see:

4/10/2019, 2:15:00 AM - info: Zigbee publish to device '0x00158d0002165834', genOnOff - on - {} - {"manufSpec":0,"disDefaultRsp":0} - null
4/10/2019, 2:15:00 AM - info: MQTT publish: topic 'zigbee2mqtt/aqara_socket_1', payload '{"state":"ON","linkquality":78,"power":0,"voltage":227.8,"consumption":0,"temperature":25}'
2 Likes

Any tips on how to do this all in the items file?
It’s just MQTT so should be possible…

If you use the mqtt1 binding, all the information has to be stored in the items file.

With mqtt2 this is not possible because the topic structure is defined on thing level. The item level just references it.

Here are plenty of examples to use zigbee2mqtt with the mqtt1 binding. You can use JSONPATH transformation directly, but I decided against because of the dynamic message structure and constant exception due to that.

1 Like

Thanks @Job I had not been keeping up so wasn’t aware that the new MQTT binding required things…
I am still using the old V1 MQTT binding so will look in your mentioned topic for reference.

Thanks!