Howto add MQTT Thing

I read a lot in the last days abaout zigbee and zigbee2mqtt and how to add devices to openhab. But i cant get my sonof door / window sensor to work. i install MQTT Binding and it works as Bridge with my mosquitto Broker. I do not know where and how i make the logical connection to the device and openHAB. Actually i think its in the Channel and the mqtt state field, but it do not work. I tried mqtt:IEEE Address, or mqtt:device_name, or zigbee2mqtt/ieee address and so on.
What is correct, or where to link the device with openHAB?

You create a new generic mqtt thing and point it to the mqtt broker.

Then you add your channels and items for example below:

UID: mqtt:topic:mqttbroker:0bc8c6461a
label: Zigbee door back sensor
thingTypeUID: mqtt:topic
  payloadNotAvailable: offline
  availabilityTopic: zigbee2mqtt/door-back/availability
  payloadAvailable: online
bridgeUID: mqtt:broker:mqttbroker
location: Kitchen small deck
  - id: zigbeedoorback
    channelTypeUID: mqtt:contact
    label: Zigbee door back
    description: ""
      stateTopic: zigbee2mqtt/door-back
      transformationPattern: JSONPATH:$.contact
      off: "true"
      on: "false"
  - id: zigbeedoorbackbattery
    channelTypeUID: mqtt:number
    label: Zigbee door back battery
    description: ""
      stateTopic: zigbee2mqtt/door-back
      transformationPattern: JSONPATH:$.battery
      unit: "%"

take note of the jsonpath as you need that so you can extract the data coming in from the device.

Also you need to install the JSONPATH addon.

Either auto-detection (doesn’t work well for many devices), UI (as @ubeaut described above) or text file.
While the UI is great for almost anything I still like to define my zigbee2mqtt things in a.thing file. This is because you have to define the channels anyway which makes it quicker (for me).

A good start with many examples is:

This way you don’t need JSON transformations anymore as all attributes are sent separately and can be directly attached to a channel.

Here is an example for a smart plug:

Thing mqtt:topic:PlugWaschmaschine "PlugWaschmaschine" (mqtt:broker:MosquittoMqttBroker) { Channels: 
   Type switch   : state     "state"     [ stateTopic = "zigbee2mqtt/PlugWaschmaschine/state",      commandTopic = "zigbee2mqtt/PlugWaschmaschine/set/state", on="ON", off="OFF" ] 
   Type datetime : last_seen "last_seen" [ stateTopic = "zigbee2mqtt/PlugWaschmaschine/last_seen" ]
   Type number   : power     "power"     [ stateTopic = "zigbee2mqtt/PlugWaschmaschine/power"     ]
   Type number   : energy    "energy"    [ stateTopic = "zigbee2mqtt/PlugWaschmaschine/energy"    ]
   Type number   : current   "current"   [ stateTopic = "zigbee2mqtt/PlugWaschmaschine/current"   ]

You save this as anyName.things under …openHAB-conf/things.
Then I usually create all items with “Add Equipment to Model” that you find in the channel tab of your new thing.

Let’s start with the basics, what is this device you have, brand, model and what firmware is it running on?

the broker is ok, thats what i understand, no problem. But i thought i can configure all in tue ui, but it seems not so, right? Since now i use Homematic Devices and Bridge and thats very simple, so i am a little bit confused to include mqtt Things :).

Sorry, you right, it was late last night and after search the net since houres for a solution, i want to ask quick. So its Sonoff SNZB-04 Door/Windows Sensor with Firmwaredate: 20211103. Its out of the box.

It is all done in the UI.
I don’t use files at all.
I just included the code of what I have set up for an example for you.

Watch this from time 4:45 …It is in German but you will get the idea.

I tried it in many ways, but it doesnt work. So i download MQTT Explorer and connect to the Broker, but the Device isnt shown. Any Idea to solve this?

Sorry but without more knowledge about your setup it is very hard to help you. What zigbee controller do you have??

Ok, step by step…

  1. Did you setup zigbee2mqtt successfully ? (Should show connection to your broker in the log)
  2. Did you pair your contact to zigbee2mqtt and is it shown in the z2m dashboard ?

When 1 and 2 are answered with yes, we can move forward to the openHAB part.

Does any of this look like your setup?

yes to both. Mosquitto is installed correct and zigbee2mqtt shows paired device in ui. Logs seems to be ok. I installed Mosquitto and Zigbee2 MQTT on a different host than openHAB. In openHAB the MQTT Bridge is successfull installed and shows Online, also in the Logs.

What do you have in the zigbee2mqtt configuration.yaml for the server ip address?
Mine looks like this:

  base_topic: zigbee2mqtt
  server: mqtt://
  keepalive: 60

If you have IP address of localhost or you won’t see it on another computer. You will need the actual IP of the server you are running it on and that IP address would be what you use in the mqtt broker in openhab.

If it was all on the one computer you could use the

Yes Mosquitto Broker and Zigbee2MQTT is on the same Host. Anywhere tried it with IP-Address, but no change. Host listen on 1883: TCP *:1883 (LISTEN) and OH client can see it: [IP] 1883 (?) open

Published Topic is: MQTT publish: topic 'zigbee2mqtt/DoorWindow, payload ‘{“battery”:100,“battery_low”:false,“contact”:true,“linkquality”:51,“tamper”:false,“voltage”:3100}’
So there seems to be no MQTT State, right?
I created now a new Thing, with no further special settings.
The Channel has:

  • State Topic = zigbee2mqtt/0x00
  • on Value = false
  • off Value = true
  • Value Transformation = REGEX:(.contact.)∩JSONPATH:$.contact

If the topic is zigbee2mqtt/DoorWindow then why do you have this as the state topic: zigbee2mqtt/0x00

There is no way that is going to work.

Run this command on a computer that has the mosquitto client change the ip address to your address:

mosquitto_sub -h -v -t "zigbee2mqtt/#"

Then copy and paste the output of the device you are looking at.

Here is what mine looks like:

zigbee2mqtt/door-back {"battery":100,"battery_low":false,"contact":true,"last_seen":"2023-09-11T07:52:26+10:00","linkquality":98,"tamper":false,"voltage":3100}

With all the examples I have provided you should be able to work it out.

Also the state will be the contact of the device once you create an item for that device.

I didnt understand it too. Yes it should be clear after all your help, but it isnt. I run the command and the only thing i see is the friendly name, no data. Topic was a test with uid, friendly name doesnt work too.

What did you run?
What did you see?
Can’t help if I don’t know what you are doing.

What did you run?
→ [mosquitto/zigbee2mqtt host] # mosquitto_sub -v -t “zigbee2mqtt/#” -u -P ‘’ -h -p 1883
What did you see?
→ a lot and “devices”:{“0x00124bxx60c”:{“friendly_name”:“DoorWindow”}}
→ if i search the output for contact, i didnt find expected information
Can’t help if I don’t know what you are doing.
→ ofcourse

You have a username and a password on the mqtt broker (zigbee2mqtt).
Why don’t you take that off and get it to work and then put it back if needed later?
I don’t bother with the username and password. (less hassle)
Up to you but the example I have given you are for NO username or password.

Or have you entered the username and password?