[SOLVED] MQTT for SONOFF/TASMOTA this is difficult

PI3b on Openhabian2.2

I am trying to understand how to use Sonoff/Tasmota switches with OH2.

What do I want to do:
I do have a running OH2 with Z-Wave+ and FS20 CUL. Just did now successfully my first steps with great support here in the forum.
Now I would like to extend the OH with some Sonoff/Tasmota switches.

What I need:
I am using a classic PIR with a closer relay to switch the sonoff switch for a second.
(Therefore the PIR relay will close PIN 4 and 5 on my Sonoff)
This Sonoff switch signal I want to read with OH2 in order to switch a Z-Wave switch via OH2.

What I don´t understand:
Is it enough to just install the MQTT Binding or do I need to install JSON or something else?
I am not a developer and therefore I don´really understand this MQTT and
even less what is JSON and how to read such a signal from the sonoff via MQTT.

Who can help? Yes, I was reading in the forum, but to be honest this is a real confusing
topic for me.

Thanks a lot

1 Like

MQTT is how the message gets sent. JSON is the format of the message. If you are using Tasmota you don’t have to worry about JSON.


Look at the log on the Tasmota web page on the switch. It will tell you what it is publishing to which topic.

The “log” on the Tasmota webpage of the switch is called “Console”. Just in case @boris03 is searching at the wrong spot.

1 Like

OK I was reading and now need to come back with some questions.
As I wrote this isn´t easy!

  1. For my example I need to get an information from a Sonoff Tasmota switch that it was switched on. So I assume that this command below is pointing in the right direction, right?

Switch mySwitch {mqtt=">[mybroker:myhouse/office/light:command:*:Switch ${itemName} was turned ${command}]"}

  1. I would like to read only the command “ON” because it is an PIR signal, so I need to change to:

Switch mySwitch {mqtt=">[mybroker:myhouse/office/light:command:ON:Switch ${itemName} was turned ${command}]"} right?

  1. But what the hell is this - this is only the item definition, usually I only do define the device with it´s address details in the items. So why the hell I need to define a outbound item for a command ON?

  2. When I want to send an switch command from OH2 to the switch is this an outbound or an inbound required in this switch item definition?

  3. How do I get this MQTT switch to send the status “ON” to my OH2, is this somewhere a variable in a log?
    And how do I use the received status ON in my OH2 rules, to e.g.
    when status is “ON”
    then activate a ZWAVE switch

The Tasmota ESP will publish a MQTT message with a topic and a payload
OH can receive these messages, it’s called subscribing. An item can subscribe to a particular topic, or two or three…
The Binding syntax tells openHAB what to do with that payload when it receives a payload on that topic.

So your item needs an INBOUND binding

Switch mySwitch {mqtt="<[mybroker:myhouse/office/light:command:state:default]"}

But because the payload is in JSON format this will not work, we need to “TRANSFORM” the JSON into the state that a switch can take ON or OFF

I don’t know what your json from TASMOTA looks like so

Do you have a way to monitor MQTT messages? MQTT.fx or MQTT-spy installed?
Is your MQTT broker up and running?
Is openHAB connected to your broker?

We will do this step by step and get you up and running

Hi Vincent,

Thanks a lot for your support, but I need to postpone a bit. I didn´t install the binding yet.
The sonoffs aren´t defined yet … So still in theoretical mode :slight_smile:

May I come back when up and running?

Set up your sonoff
Install mosquitto (MQTT broker)
Install and set-up the binding
Install an MQTT monitor
When you’re done, we’ll play

Keep it simple to start with. Get a Sonoff device installed, and connected, and simply turning the output on/off, both from within OH and from the device itself.

Switch OF_Heating_Actuator "Office Heating [%s]" { mqtt=">[mosquitto:cmnd/sonoff-1/POWER:command:*:default], <[mosquitto:stat/sonoff-1/POWER:state:default]", autoupdate="false"} 	

This is what I use on my Sonoff Basic/TH16 devices. The state in OH exactly matches the state of the output of the device itself.

Then, once you have this working, you can begin to extend the usage.

OK this sounds promising, I will prepare and then come back maybe a bit less confused

Just for my understanding
This command line is for my sonoff.item file?
What is this [%s] ? For what does it stand for?


[%s] will show the status of ON or OFF in your Sitemap:

(I’ve got mine set up as a Text item, as opposed to a Switch, as I will not be manually changing this specific item from the UI of OH)

Hi Garry, Hi Guys,

Maybe someone can help me to get my first sonoff up and running, I can read a lot and it brings me closerr but still just to difficult.

What I would like to get is a coonection to that sonoff, what allows me to switch it and more important what reflects the status of the device “ON” or “OFF”.

OK this is my sonoff basic up and running

and this is shown in openhab.log, please ignore the messages for Cul and ZWAVE.
Something happens für MQTT but I don´t understand a word

and this is shown in the sonoff console:

Your sonoff is not connecting to your MQTT broker. Check your MQTT settings

Do you have a computer on your network called “domus1”, and is it running an MQTT broker software, such as mosquitto?

Both OH and your Sonoff are attempting to connect, but can’t find a computer of this name running an MQTT broker, so are failing.

There is not much setting right?
The sonoff tasmota device is on
The IP in mqtt.cfg is the IP from the sonoff tasmota
The user and password are also these setup in the sonoff tasmota
No Typo but not working, what´s wrong here?
I already tried all combinations means user/pwd from Openhab and used
domus1 instead of the IP but no luck.

This is the MQTT settings in sonoff tasmota device (the broker)

and this is my mqtt.cfg


Have you install an MQTT broker and is it running?

And don´t know exactly what you mean but I am using a sonoff tasmota basic device and these settings I copied above are the once from that device, it is up and running I can access it via this IP address and I can see the console showing me wrong connections. Copy of this console output is also three messages above.
So Yes I assume the Broker (this sonoff tasmota device) is up and running

To exchange messages tasmota and oh use mqtt. It’s a light weight messaging system.
It needs to be installed and running on your network before the information can pass between tasmota and OH.

Are you using openhabian?

Yes the latest OH2 and installed the MQTT Binding and restarted the device

You need to install mosquitto
the option in is openhabian-config

Hm Sure?
The only thing I can see is under add-ons/Bindings - here the MQTT Binding (installed)
and there is something under add-ons/Actions - MQTT-Action (not installed)