MQTT Tasmota Smart Plug binding to Openhab - newbie question

Dear all,
I am new to openhab and I did try sorting this bug for about 5 hours now…

I have this question: I run openhab on raspberry pi and I want to do a first smart device: Tasmota Plug Nous A1T. I followed this tutorial on integrating this: Control Tasmota flashed devices via MQTT

The problem is that the first time I did this tutorial, it all worked. Happy, I unplugged tasmota and plugged it to the socket I planned to control, and then it stopped working. After I reset the plug, and reconnect to the wifi, then it stops responding (the new IP of the plug where you configure the MQTT). Even though, I can see it connected via IP on wifi.

In MQTT explorer I see this discovery message constantly:
{“ip”:“192.168.0.127”,“dn”:“Tasmota”,“fn”:[“Tasmota”,null,null,null,null,null,null,null],“hn”:“tasmota-1-0474”,“mac”:“48551917C1DA”,“md”:“NOUS A1T”,“ty”:0,“if”:0,“ofln”:“Offline”,“onln”:“Online”,“state”:[“OFF”,“ON”,“TOGGLE”,“HOLD”],“sw”:“12.1.1”,“t”:“tasmota_1”,“ft”:“%prefix%/%topic%/”,“tp”:[“cmnd”,“stat”,“tele”],“rl”:[1,0,0,0,0,0,0,0],“swc”:[-1,-1,-1,-1,-1,-1,-1,-1],“swn”:[null,null,null,null,null,null,null,null],“btn”:[0,0,0,0,0,0,0,0],“so”:{“4”:0,“11”:0,“13”:0,“17”:0,“20”:0,“30”:0,“68”:0,“73”:0,“82”:0,“114”:0,“117”:0},“lk”:0,“lt_st”:0,“sho”:[0,0,0,0],“sht”:[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],“ver”:1}

If I change LWT from Offline to Online, I get this in Openhab logs:
2023-10-24 20:43:55.737 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command ‘Offline’ from channel ‘mqtt:topic:tasmota_1:power’ not supported by type ‘OnOffValue’: No enum constant org.openhab.core.library.types.OnOffType.Offline

2023-10-24 20:44:02.256 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command ‘Online’ from channel ‘mqtt:topic:tasmota_1:power’ not supported by type ‘OnOffValue’: No enum constant org.openhab.core.library.types.OnOffType.Online

when I change the command to turn off, I have this in the log:
2023-10-24 20:41:56.258 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘tasmota_1’ received command OFF
2023-10-24 20:41:56.261 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘tasmota_1’ predicted to become OFF
2023-10-24 20:41:56.265 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘tasmota_1’ changed from ON to OFF

BUT, it doesn’t change the stat/tasmota_1_POWER from Online to Offline.

If I reset tasmota (long hold on power button), then I can try reconnect it to wifi (it never connects on the first try to it, but always on the second), and write again the config of MQTT broker (after which it restarts and is not reachable via assigned IP, nor it responds to my commands from Openhab switch).

It actually once started working after this state, because I was clicking on something, and somehow something worked, but I don’t know what it is, because the next time I unplugged it, it stopped working again.

Thank you for the help!

I can help with some of this.

First, it helps us if you post code, logs, and other formatted text using code fences.

```
code goes here
```

That preserves the white spaces.

Elaborate on this. It’s constantly being published over and over again or the message is just staying there.

If the message is retained, it will always be there. That isn’t unexpected for a discovery message.

Are you sure you are changing the LWT message? That error is complaining that “Offline” and “Online” are not appropriate for the channel mqtt:topic:tasmota_1:power which looks more like a Switch to control the device instead of the LWT config.

Note that handling the LWT is not part of the tutorial you link to.

It wouldn’t. The way MQTT online/offline is handled is through the LWT topic. The Tasmota device, when it registers as a client with the MQTT Broker includes a topic and message. Then a heart beat is set up behind the scenes between the MQTT Broker and the Tasmota. If that heart beat fails for some reason, the MQTT Broker will publish the registered message to the registered topic.

Ideally, in addition, the Tasmota device will publish an online message to that LWT topic when it successfully connects to the broker. Often these messages are retained meaning they will remain on the topic until a new retained message is published to that topic.

I didn’t follow the Tasmota side of the config but suspect it does not include LWT.

On the openHAB side, you can configure the MQTT Thing to watch that LWT topic and set the Thing as ONLINE or OFFLINE based on the messages on the LWT topic. However, that’s not part of the config in the tutorial you linked to.

The LWT stuff is not required to get this to work.

Take it in stages. One of the advantages of MQTT is that it’s somewhat independent and everything is adjudicated through the MQTT broker.

You are already using MQTT Explorer so that’s a good place to start. Cut OH out of the equation. Watch the messages in MQTT Explorer. Send some messages and see what happens. If you get the status when manually toggling the switch do you see a message? What happens if you send the on command message to the command topic? Does it work?

For the most part, you will want to figure out how this thing works independent of OH. What messages do you get from the device? What messages cause the device to do stuff?

Once you know and understand the topics and messages involved you can configure OH to handle/produce those messages. I would recommend configuring the MQTT Thing through the UI, at least to start, as all the options will be shown with some description on what each one does. With the knowledge that you’ve gained from the experiments with MQTT Explorer it should be fairly obvious wh at you need to configure.

One reason why I recommend this approach is that the tutorial you linked to is rather old. I know a bunch of new stuff was added to MQTT on the OH side. I suspect that a lot has changed on the Tasmota side as well.

Finally, beware that case matters.

Thank you for your help!

So, literally, the only thing between it not working and working was:

  1. waiting for 4 hours
  2. At 23:30, disconnecting my latpop’s ethernet. The plug is not working. It is not connected to a router.
  3. Restarting the two of the mesh nodes (like wifi, but not wifi).
  4. At 23:36 I see the IP address for the plug is reachable, MQTT broker starts working
  5. At 23:50 I found that the switch on-off from PaperUI started working.

So, the plug can connect to wifi, but it wont bug out before I config its MQTT broker, after which it goes into this not reachable state by IP and the wifi router shows that it receives very little packets.

I want to violate the engineering principle of “if it works, then leave it be”, and find out why this happens? Can it be the plug simply trying to reconnect, and then falls into some sort of a deadlock?

I saw in MQTT a topic tasmota/discovery/48551917C1DA/config: it published once (the time it was working and I unplugged it), and then after it started working recently. So, I assume, this one just starts as it connects the 1st time.

{"ip":"192.168.0.127","dn":"Tasmota","fn":["Tasmota",null,null,null,null,null,null,null],"hn":"tasmota-1-0474","mac":"48551917C1DA","md":"NOUS A1T","ty":0,"if":0,"ofln":"Offline","onln":"Online","state":["OFF","ON","TOGGLE","HOLD"],"sw":"12.1.1","t":"tasmota_1","ft":"%prefix%/%topic%/","tp":["cmnd","stat","tele"],"rl":[1,0,0,0,0,0,0,0],"swc":[-1,-1,-1,-1,-1,-1,-1,-1],"swn":[null,null,null,null,null,null,null,null],"btn":[0,0,0,0,0,0,0,0],"so":{"4":0,"11":0,"13":0,"17":0,"20":0,"30":0,"68":0,"73":0,"82":0,"114":0,"117":0},"lk":0,"lt_st":0,"sho":[0,0,0,0],"sht":[[0,0,0],[0,0,0],[0,0,0],[0,0,0]],"ver":1}

The code I used:

Bridge mqtt:broker:MQTT_BROKER "Mosquitto MQTT Broker" [
        host="192.168.0.238",
        secure=false,
        port=1883,
        clientID="OpenHAB2",
        username="",
        password=""
]
Thing mqtt:topic:tasmota_1 "tasmota_1_air_cleaner" (mqtt:broker:MQTT_BROKER) {
        Channels:
                Type switch : power "Power Switch" [
                        commandTopic="cmnd/tasmota_1/POWER",
                        stateTopic="stat/tasmota_1/POWER",
                        on="ON",
                        off="OFF"
                ]
}

Switch tasmota_1 "tasmota_1_air_cleaner_switch"  { channel="mqtt:topic:tasmota_1:power" }

It sounds like you have a wider networking issue which we are ill equipped to help with on this forum. MQTT has a lot of moving parts and everything needs to successfully connect for it to work.

That’s going to be on the Tasmota device side of things. There won’t be anything that can be done from the OH side to address that behavior. And unless the Tasmota device can connect to the MQTT broker it’s not going to work.

Ok, thank you.

So I would conclude then that it’s not a very good plug:
it has problems connecting to a wifi, then, when it’s unplugged it tries to reconnect indefinitely, and then if I reset it and give it the broker IP + port, it would again go into this loop, which somehow resolves it sometimes…

A quick question though, is there some OH code to safely turn off tasmota plug? Maybe, something like a maintenance code - while the tasmota plug is connected to MQTT broker and is able to receive commands.

That too is something that the Tasmota plug would have to implement. OH can definitely publish such a message, but the Tasmota device is going to have to dictate the topic, message and implement the reaction to it.

Because the problem is the device can’t keep a connection, the message should be published as retained so that the device gets the message even if it wasn’t connected at the time that the message was sent.

Maybe try to upgrade Tasmota to the current version.
Sometimes flashing the same version also helps when there is a problem with stability.