Setting up Xiaomi Mi SmartPlug (Zigbee) with OpenHAB 3

There is a already a similar topic (Setting up Xiaomi Mi SmartPlug (Zigbee) with Conbee II and OpenHab 2), but that one is about setting up the device with OpenHAB 2 and with the use of another binding.

What I’ve done:
At first I tried to set up this device with the ZigBee-Binding in OpenHAB (at first 2, then 3). But in both versions I was only able to bind the “switch” channel to switch the plug on and off.
On www.zigbee2mqtt.io I read, that ZigBee2MQTT supports all channels of this device. Therefore I set up a server with ZigBee2MQTT and a Mosquitto service as MQTT broker and installed the add on “MQTT Binding”. For that I followed the instructions in [OH3] MQTT Setup and Configuration and read also [OH3] Tasmota relay via MQTT (Sonoff Basic with optional DHT22).

According to this documentation I created in OH 3 a MQTT Broker and connected it to my Mosquitto service. After that the “MQTT Broker” thing was online. At next I created a Generic MQTT thing and connected it to the “MQTT Broker” thing.
At next I tried to connect it to the related topic of the MQTT broker. But this failed. I wasn’t able to find out the right (Availability) topic. I used the logfile of the broker and also the nice MQTT Explorer. There were topics for the bridge and also for the socket. So I think, that on that side everything was up and running.

I’ve the following two questions:

  1. How can I detect the so called “Availability Topics”?
  2. Are these topics related to the bridge or to the device?
  3. Isn’t it possible to create a channel to the Xiaomi Mi SmartPlug with the help of a Generic MQTT thing as described in [OH3] Tasmota relay via MQTT (Sonoff Basic with optional DHT22)?

Note: At least I was able to successfully connect the connector to OH 3 using the text configuration. I’ll post this later.

What version of zigbee2mqtt are you running? It’s possible that older versions, or some devices, don’t support this topic. You don’t need it for the Generic MQTT Thing to work.

Device.

Yes.

Show us what you tried, and what MQTT Explorer sees from your device. Though if you’ve got it working via the config files I’m not sure what the issue is?

1 Like

I found a way to couple the socket to OpenHAB 3 via a ZigBee stick and the ZigBee2MQTT binding and to use all channels.
To achieve this, I did the following
(Note: These steps are working, but for steps 5 to 10 there is an easier way - look at the post from hafniumzinc or for an even more easier solution at the post from sihui):

  1. Installation of the MQTT broker Mosquitto. This is described in Install Mosquitto Broker Raspberry Pi | Random Nerd Tutorials.
  2. Installation of the ZigBee2MQTT service. This is very well described in Running Zigbee2MQTT | zigbee2mqtt.io.
  3. Installation of the add-on “MQTT Binding”. To do this, click on the “Bindings” link in OH3 in the “Administration” area under “Settings”, “Add-ons”. Then click on the blue “+” button at the bottom right and install the “MQTT Binding” add-on.
4. Pairing of the socket and assigning of a 'friendly name'.
  • To pair the socket, you may have first to change the entry “permit_join:” in the file “/opt/zigbee2mqtt/data/configuration.yaml”. You must set its value from false to true. You must restart the service to reload the configuration!
  • Then plug in the socket and min. press the button for 5 seconds (until the LED flashes blue).
  • If the pairing was successful, an entry with the substring “Successfully interviewed” can be found in the log. In addition, the device is attached to the file /opt/zigbee2mqtt/data/configuration.yaml. For me it looked like this:

permit_join: true
:
devices:
‘0x04cf8cdf3c777242’:
friendly_name: ‘0x04cf8cdf3c777242’

  • Change the friendly name, add a retain statement and change the permit_join value to false. The file should now looks like the following:

permit_join: false
:
devices:
‘0x04cf8cdf3c777242’:
friendly_name: ‘MobileSocketMi1’
retain: false // I don’t see why we should save messages, so disable this

  • With that the topic for the device in the MQTT broker has now the name MobileSocketMi1. This will be needed for the configuration of the thing in OH.
5. Translation scripts are needed in OH3. For that two add-ons must be installed
  1. In the “Administration” area click on “Settings”.
  • Click on “Transformations” on the page under “Add-ons”.
  • Click on the blue “+” button at the bottom right.
  • Install the add-ons “Javascript Transformation” and "JSONPath Transformation.
  1. The translation scripts must be in the directory /etc/openhab/transform/js
    created (possibly create the subdirectory js beforehand).
  2. The first script is used for translation of the values true and false of the channel
    “consumer_connected” to the string values “ON” and “OFF”, used by OH. This
    happens with the help of the file “consumer_connected_Bool2Switch.js”:
8. Content of consumer_connected_Bool2Switch.js
(function(x){

    var result = "";
 
    var json = JSON.parse(x);  
    if (json.consumer_connected) 
    {
        result="ON";
    } 
    else 
    {
        result="OFF";
    }
    return result;
    
})(input)
  1. The second script is required to be able to switch the socket. It
    transforms the OpenHAB values 0 and 1 into the strings “ON” and “OFF”. This
    happens with the help of the file “set_Number2Switch.js”.
10. Content of set_Number2Switch.js
// Transforms an input value of 0 or 1 to "OFF" or "ON" respectively and formats as JSON
  (function(x) {
    var result = new Object();
    if (x == 1) {
        result.state = "ON"
    }
    else if (x == 0) {
        result.state = "OFF"
    }
    else {
        result.state = x
    }
    return JSON.stringify(result);
  })(input)
  1. Definition of the plug thing with all channels. For this a text file (eg zigbee2mqtt.things) must be created. In my installation this file must be created in /etc/openhab/things.
12. Content of zigbee2mqtt.things

Bridge mqtt:broker:mosquitto [host="zigbee2mqtt", port=1883, secure=false, clientID="openHAB3"] { Thing topic MobileSocketMi1 "MobileSocket, Mi, 1: ZigBee2MQTT" @ "Mobil at Home" { Channels: Type switch : consumer_connected "Connected" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JS:js/consumer_connected_Bool2Switch.js"] Type number : consumption "Energy" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JSONPATH:$.consumption"] Type number : current "Current" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JSONPATH:$.current"] Type number : link_quality "Link Quality" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JSONPATH:$.linkquality"] Type number : power "Power" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JSONPATH:$.power"] Type switch : state "Status" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JSONPATH:$.state", commandTopic="zigbee2mqtt/MobileSocketMi1/set", transformationPatternOut="JS:js/set_Number2Switch.js"] Type number : temperature "Temperature" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JSONPATH:$.temperature"] Type number : voltage "Voltage" [ stateTopic="zigbee2mqtt/MobileSocketMi1", transformationPattern="JSONPATH:$.voltage"] } }

  1. After restarting all services, the socket with all channels should be visible in OH3 and can be bound to items in the normal way.
2 Likes

You can simplify this a bit. You can get rid of your two JS transformations in your Switch Channels by simply adding

on="1",
off="0"

to your Switch Channel configuration where relevant, and

on="true",
off="false"

where relevant. Just remember to remove the JS transformation, but add in a JSONPATH transformation if required.

You can double check the available options for each Channel Type in the documentation.

1 Like

It is the version 1.17.0. I cloned it with
git clone https://github.com/Koenkk/zigbee2mqtt.git
Therefore I think it should be one of the latest - isn’t that true?

I think the UI variant is probably easier. So I can imagine going this way with the next ZigBee device. In any case, it should be interesting for other beginners. So I posted this here.
But you’re right, since I have a solution, we don’t have to go into this any further here. I don’t wanna waste your or others time.

But if you feel like it and have the time, here is what the MQTT Explorer delivers for the plug:
MobileSocketMi1 =
{
“consumer_connected”: true,
“consumption”: 0.18,
“current”: 0,
“linkquality”: 68,
“power”: 0,
“state”: “OFF”,
“temperature”: 20,
“voltage”: 229
}

The hierarchy is “zigbee2mqtt/MobileSocketMi1”.
I’ve tried to set the Availability Topic to the following values:

  • zigbee2mqtt/MobileSocketMi1
  • zigbee2mqtt/MobileSocketMi1/LWT
  • zigbee2mqtt/bridge
  • zigbee2mqtt/bridge/LWT

I suggest using the attribute parameter, makes everything a lot easier and omits the need of any transformation:

1 Like

So with that (on=“1”, off=“0”) the configuration line:

Type switch : consumer_connected “Connected” [ stateTopic=“zigbee2mqtt/MobileSocketMi1”, transformationPattern=“JS:js/consumer_connected_Bool2Switch.js”]

will be:

Type switch : consumer_connected “Connected” [ stateTopic=“zigbee2mqtt/MobileSocketMi1”, on=“1”, off=“0”, transformationPattern=“JSONPATH:$.consumer_connected”]

Right?

Well, for consumer_connected, according to this:

{
“consumer_connected”: true,
“consumption”: 0.18,
“current”: 0,
“linkquality”: 68,
“power”: 0,
“state”: “OFF”,
“temperature”: 20,
“voltage”: 229
}

you get back true or false, so not 0 and 1. But otherwise, yes!

Type switch : consumer_connected "Connected" [ stateTopic="zigbee2mqtt/MobileSocketMi1", on="true", off="false", transformationPattern="JSONPATH:$.consumer_connected"]

Just beware of small edits to Things files not being loaded correctly by openHAB. You may need to restart openHAB, or do this.

1 Like

Thanks, I run in the “not being loaded correctly” problem last night. :slight_smile:

Just an off topic question: I always have difficulties quoting long lines in the postings so that they are scrolled - like you do. How did you achieve that?

Code fences! Sandwich your code in between three backticks ```:

```

Like
This

```

1 Like

Thanks, I started reading this document last night, but stopped after reading these two lines:

experimental:
  output: attribute

I didn’t want to start with an experimental feature that might become problems later. Do you think, that it is a stable feature now?

I have used it without issue for the past year.

1 Like

As he said.

1 Like