Openhab and zigbee2mqtt

Hi There,

I’m using openhab for home automation for more than a year with a lot of devices and this time I’d like to use it to get data from Xiaomi Aqara sensors using CC2531 USB stick.
To be honest I feel a bit lost here. I’ve managed to install and run zigbee2mqtt service as described here https://www.zigbee2mqtt.io/getting_started/running_zigbee2mqtt.html
and I also managed to pair one of sensors. below you can find my current configuration.yaml of zigbee2mqtt

homeassistant: false
permit_join: true
mqtt:
  base_topic: zigbee2mqtt
  user: openhab
  password: openhab
  server: 'mqtt://localhost'
advanced:
  log_level: debug
serial:
  port: /dev/ttyACM0
devices:
  '0x00158d00048325e0':
    friendly_name: 'xiaomi_aqara_1'
    retain: true
experimental:
    output: attribute

And this is where I stuck.

I wasn’t able to proceed nor with ZigBee Binding neither with MQTT Binding. I’ve went through topics when it was described that bridges are necessary to run it, and on others that bridges are no longer required for openhab to talk to mqtt.

My questions:

  • which binding should I use?
  • is there anything else I should install/configure?
  • does anyone has working configuration to share here with short description?

I am using the setup descriped here: Zigbee2mqtt revisited: No more ugly transformations

It is working fine for me with abprox. 40 things

Thanks @chrode

So I assume that binding you have is MQTT binding.
Where to define username and password from configuration.yaml then?
When I put thing like below

Thing topic xiaomi_aqara_1 "Temperature" @ "Garage"
    {
        Channels:
            Type number : temperature "temperature" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/temperature" ]
            Type number : humidity "humidity" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/humidity" ]
            Type number : pressure "pressure" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/pressure" ]
            Type number : voltage "voltage" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/voltage" ]     
            Type number : battery "battery" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/battery" ]     
            Type number : linkquality "linkquality" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/linkquality" ]               
    }

I receive error like below

2020-07-07 17:43:41.170 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'zigbee.things'
2020-07-07 17:43:41.203 [WARN ] [.thing.internal.GenericThingProvider] - Thing topic does not have a bridge so it needs to be defined in full notation like <bindingId>:topic:xiaomi_aqara_1

Do I need the bridge or not? How to add/define the bridge?

And when I try to use items as below

Number Garage_TEMP "Garage Temp [%.1f °C]"  <temperature> (Zigbee) {channel="mqtt:topic:broker:xiaomi_aqara_1:temperature"}
Number Garage_HUMIDITY "Garage HUMID [%d %%]"    {channel="mqtt:topic:broker:xiaomi_aqara_1:humidity"}
Number Garage_PRESSURE "Garage PRESS [%d mbar]"     {channel="mqtt:topic:broker:xiaomi_aqara_1:pressure"}
Number Garage_VOLTAGE "Garage Volt [%d mV]"   {channel="mqtt:topic:broker:xiaomi_aqara_1:voltage"}
Number Garage_BATTERY "Garage BAT [%.1f %%]" <battery>   {channel="mqtt:topic:broker:xiaomi_aqara_1:battery"}
Number Garage_LINKQUALITY "Garage [%d]"    {channel="mqtt:topic:broker:xiaomi_aqara_1:linkquality"}

I have no values returned. How can I check within mqtt service that my devideis providing values?

It should look like this in the .things file

Bridge mqtt:broker:openhabBroker [ host="192.Z.Y.X", secure=false, username="USERNAME", password="PASSWORD" ]

{        Thing topic x_temp_01 "X Temperature" @ "Xiaomi"
{
    Channels:
        Type number : temperature "temperature" [ stateTopic = "zigbee2mqtt/x_temp_vorrat/temperature" ]
        Type number : humidity "humidity" [ stateTopic = "zigbee2mqtt/x_temp_vorrat/humidity" ]
        Type number : pressure "pressure" [ stateTopic = "zigbee2mqtt/x_temp_vorrat/pressure" ]   
        Type number : battery "battery" [ stateTopic = "zigbee2mqtt/x_temp_vorrat/battery" ]     
        Type number : linkquality "linkquality" [ stateTopic = "zigbee2mqtt/x_temp_vorrat/linkquality" ]
        Type datetime : last_seen "last seen" [ stateTopic="zigbee2mqtt/x_temp_vorrat/last_seen" ]               
}}
1 Like

I know @chris , our developer for the Zigbee binding is very dedicated. No need to flash firmware ot add another protocol into the mix.

Ok, so after @chrode suggestions I’ve changed the configuration to the one below and it looks that OH finally got it as thing status is online

Things

Bridge mqtt:broker:openhabBroker [ host="192.168.1.203", secure=false, username="openhab", password="openhab" ]
{ 
Thing topic xiaomi_aqara_1 "Temperature" @ "Garage" 
    {
        Channels:
            Type number : temperature "temperature" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/temperature" ]
            Type number : humidity "humidity" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/humidity" ]
            Type number : pressure "pressure" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/pressure" ]
            Type number : voltage "voltage" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/voltage" ]     
            Type number : battery "battery" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/battery" ]     
            Type number : linkquality "linkquality" [ stateTopic = "zigbee2mqtt/xiaomi_aqara_1/linkquality" ]               
    }
}

Items

Number Garage_TEMP "Garage Temp [%.1f °C]"  <temperature> (Zigbee) {channel="mqtt:topic:openhabBroker:xiaomi_aqara_1:temperature"}
Number Garage_HUMIDITY "Garage HUMID [%d %%]"   (Zigbee) {channel="mqtt:topic:openhabBroker:xiaomi_aqara_1:humidity"}
Number Garage_PRESSURE "Garage PRESS [%d mbar]"    (Zigbee) {channel="mqtt:topic:openhabBroker:xiaomi_aqara_1:pressure"}
Number Garage_VOLTAGE "Garage Volt [%d mV]"  (Zigbee) {channel="mqtt:topic:openhabBroker:xiaomi_aqara_1:voltage"}
Number Garage_BATTERY "Garage BAT [%.1f %%]" <battery>  (Zigbee) {channel="mqtt:topic:openhabBroker:xiaomi_aqara_1:battery"}
Number Garage_LINKQUALITY "Garage [%d]"   (Zigbee) {channel="mqtt:topic:openhabBroker:xiaomi_aqara_1:linkquality"}

However as I still don’t have any data I’m about to check with MQTT explorer if there is any communication betweeen mqtt and devices

Hmm … once I executed
sudo service zigbee2mqtt restart && sudo journalctl -u zigbee2mqtt.service -f
it started to work, I need to check why service was not working since system started, however no it looks as below
image
Thank you a lot @chrode

Not an issue, but if secure=false do you need the username and password defined? Never tried myself…

Edit: they’re not linked, see post #11.

Yep, valid point. This was rather dirty config just for make it running and I didn’t put much attention to this flag, however I will change this for sure. Thanks!

No problem - it’s obviously working for you either way!

If you’re interested in monitoring zigbee2mqtt itself from within openHAB, have a look at this post, and the original post linked from within it too!

1 Like

yes, because secure defines the connection type not login with credentials

  • secure : Uses TLS/SSL to establish a secure connection to the broker. Can be true or false. Defaults to false.
1 Like

One more question to not to create new topic.

I’ve just received my Aqara Magic Cube, and I’m able to see for example action and angle in openhab using below configuration

Things file (inside previously creted bridge)

Thing topic cube “Xiaomi” @ “Cube”
{
Channels:
Type string : action “action” [ stateTopic = “zigbee2mqtt/cube/action” ]
Type number : angle “angle” [ stateTopic = “zigbee2mqtt/cube/angle” ]
}

Items file

String AT_Office_ACTION “Akcja” (AT_Office) {channel=“mqtt:topic:openhabBroker:cube:action”}
Number AT_Office_ANGLE “Angle” (AT_Office) {channel=“mqtt:topic:openhabBroker:cube:angle”}

However from some reason rule on channel is not working at all. Rule is not triggered, however during rule file refresh there are no errors logged.

rule “Turn on the light”
when
Channel “mqtt:topic:openhabBroker:cube:action” triggered
then
logInfo(“Debug”, “Rule: Turn on the lamp test”)
var actionName = receivedEvent.getEvent()
logInfo(“Debug”, “Rule: Turn on the lamp action:” + actionName)
switch(actionName) {
case “slide”: {
AT_Office_Light.sendCommand(ON)
}
case “rotate_left”: {
AT_Office_Light.sendCommand(ON)
}
case “rotate_right”: {
AT_Office_Light.sendCommand(ON)
}
}
end

And this for example works

rule “Turn on the light”
when
Item AT_Office_ACTION received update
then
logInfo(“Debug”, “Rule: Turn on the lamp test”)
var state = AT_Office_ACTION.state
logInfo(“Debug”, “Rule: Turn on the lamp action:” + state)
end

Is there a reason to not to use second approach and what causes an issue with first one

Your second approach is, in my mind, the most normal approach: triggering off Items is how most rules are written.

Another question … I have currently several Aquara temperature sensors and Aqara magic cube working well. Recently I decided to buy also Aqara smart plug. I want to use it as repeater (and it doing it weel) but I also want to control it. I have it already visible in both OpenHab and MQTT explorer, however I can’t find a way to control it. Is there any way to do so?

Thing topic smart_plug_1 "Xiaomi" @ "Plug1"
{
    Channels:
        Type string : state "state" [ stateTopic = "zigbee2mqtt/smart_plug_1/state" ]
        Type number : power "power" [ stateTopic = "zigbee2mqtt/smart_plug_1/power" ]
        Type number : linkquality "linkquality" [ stateTopic = "zigbee2mqtt/smart_plug_1/linkquality" ]
}

String AT_Office_Plug_STATE “State” (Home, AT) {channel=“mqtt:topic:openhabBroker:smart_plug_1:state”}
Number AT_Office_Plug_POWER “Power [%d]” (Home, AT) {channel=“mqtt:topic:openhabBroker:smart_plug_1:power”}
Number AT_Office_Cube_LINKQUALITY “Link quality [%d]” (Home) {channel=“mqtt:topic:openhabBroker:smart_plug_1:linkquality”}

image

image

What is topic
zigbee2mqtt/smart_plug_1/power

?

You could try publishing a command via MQTT Explorer to:

zigbee2mqtt/smart_plug_1/power/set

with the message

1

and see what happens? Perhaps also try that with the state topic. Send 1 to

zigbee2mqtt/smart_plug_1/state/set

and see what happens


EDIT

Looking at a couple of supported devices, it looks like all you need to do is send ON or OFF to zigbee2mqtt/smart_plug_1/set

So try adding a switch type channel with the following:

Type switch : switch "Switch" [ stateTopic = "zigbee2mqtt/smart_plug_1/state", commandTopic="zigbee2mqtt/smart_plug_1/set", on="ON", off="OFF" ]

The topic is as below

Also when I try to publish ON/OFF/0/1 as on attached image nothing happens

Also gives nothing

Did you see my edit? You need to send ON or OFF to zigbee2mqtt/smart_plug_1/set

Failing that, you’ll have to provide a link to the device on the zigbee2mqtt website so we can see what we’re dealing with.

Yep, however focused on MQTT explorer first.

So … your solution works and thank you a lot for that.

Below is complete code to make it work

Thing topic smart_plug_1 “Xiaomi” @ “Plug1”
{
Channels:
Type switch : state “state” [ stateTopic = “zigbee2mqtt/smart_plug_1/state”, commandTopic=“zigbee2mqtt/smart_plug_1/set”, on=“ON”, off=“OFF” ]
Type number : power “power” [ stateTopic = “zigbee2mqtt/smart_plug_1/power” ]
Type number : linkquality “linkquality” [ stateTopic = “zigbee2mqtt/smart_plug_1/linkquality” ]
}
}

Switch AT_Office_Plug_STATE “State” (Home, AT, AT_Office) [“Switchable”] {channel=“mqtt:topic:openhabBroker:smart_plug_1:state”}

I’ve also managed to trigger change via MQTT explorer. The key was to set new topic SET and then value to ON/OFF