Using Sonoff Power Switches with Tasmota firmware and openHAB2 MQTT2 binding

Does your setup correctly detect changes when you switch the Sonoffs via the physical button, or via a HTTP call from TasmoAdmin ? That is one thing that fails in my setup…

The sonoff firmware uses a different mqtt topic to notify about those events. You need to define another switch channel and bind it to that topic and link the channel to the same item you already have for the other switch channel.

@hakan
My setup does detect the changes triggered by the physical button or via HTTP.

As David stated, there are different firmwares for sonoff. A tools like MQTT.fx will help to see the topics and messages.

Sorry, I wrote misunderstanable. My little zoo of Sonoff devices (Sonoff Basic, Sonoff 20, …) are all flashed with the Tasmota firmware, version 6.3.0.

Also, is there really anyone (at least in this forum) using the Sonoffs with the factory provided firmware? :thinking:

Thank you for the confirmation, I’ll try to recreate your setup on with my devices then.

Thanks for posting. This is very timely for me.

I have been playing with the S20 sonoff plugs and did not have much luck as of yet… I soldered on the pin header (this was a pain) and flashed (successfully) the file. but when i plug the device into the wall, I am unable to see a wifi hotspot that it crated. Do you have any tips? I was going to look into the OTA (over the air) flashing but have not found the time to try.

if you could share some of your thoughts that would be very helpful.

My firmware from Theo is based on version 6.3.0.

Hi, thanks for the info shared!! I’m working to migrate the Sonoff switches but I didn’t have success so far.

I have a doubt: if you write the .things and .items file, do I need to install the MQTT 2.4 binding??

I flash them without soldering any pin heads. I just make sure the four male wires are connected under an angle and it flashes perfectly. Did it now with five devices, 100% first time right. Just a thought.

@paulo9999:
You do need to install the binding first. The .things and .items are using the binding, not replacing it.
Open PaperUI and add the binding as shown below

1 Like

Tried again yesterday without soldering on a new S20 socket and had zero trouble flashing Tasmota.
Though I am using a new USB to TTL adapter, so I wonder if it was the problem all along and not the connection…

Since this success, I’ve since got it working over MQTT with the 1.x binding. ( Could not get the 2.4 MQTT binding to work and so wen’t back to 1.x binding and it worked with the existing config settings. Just changed my item code around and was up and running)

1 Like

HI All,
I’m trying to use MQTT with a sonoff but I’m not able to make it work.
I’m running OH2.4 on RBPI and have set up the

image

then I have set up the

image

my things file is the following:

Bridge mqtt:broker:myMQTTBroker [ host="192.168.2.105", secure=false, username="XXXXXXX", password="XXXXXXXX" ]
{
    Thing mqtt:topic:sonoff_sw01 {
    Channels:
        Type switch : switch [ stateTopic="stat/sonoff_sw01/POWER", commandTopic="stat/sonoff_sw01/cmd/POWER", transformationPattern="JSONPATH:$.POWER", on="ON", off="OFF" ]
    }
}

my items file is the following:

Switch swScaldasalvietteP1_Switch "Scaldasalviette P1"  { channel="mqtt:topic:sonoff_sw01:switch" }

I get the following line in the log which tells me that the broker is online

mqtt:topic:sonoff_sw01' changed from OFFLINE (BRIDGE_OFFLINE) to ONLINE

I get the following line in the console of the sonoff which tells me that it connected to the broker:

19:00:49 MQT: Attempting connection...
19:00:49 MQT: Connected

but when I switch ON/OFF the sonoff I get nothing in openHAB. Also, switching the item ON/OFF via HABPanel nothing happens to the sonoff.

Any help? Did I write well the things and items files?

thanks.

I wasn’t able to figure this out after a few hours, so I removed the 2.4 Mqtt binding and installed the 1.x legacy binding. it was very simple to get working. This was effortless and is what I had used in the past. I prefer to configure things by text and perhaps was missing something in PaperUI of the 2.4 binding.

Assumes you have installed Mosquitto already on your server. ( i used openhabian-config tool)

services/mqtt.cfg

mosquitto.url=tcp://localhost:1883
mosquitto.clientId=openHAB
mosquitto.user=openhabian
mosquitto.pwd=openhabian
mosquitto.qos=1
mosquitto.retain=true

mqtt-persistence.cfg - Not sure if this file is necessary…

# Name of the broker as defined in mqtt.cfg
broker=mosquitto

Sitemap

        Switch item=sonoff1
//Sonoff Basic / Sonoff S20 Smart Socket
Switch sonoff1 "Sonoff #1" <light> (LR,gLight)
    { mqtt=">[mosquitto:sonoff/cmnd/sonoff1/POWER:command:*:default], <[mosquitto:sonoff/stat/sonoff1/POWER:state:default]" }

i set the following in the Tasmota Sonoff MQTT section in the switch :

host - 192.168.1.137
Port - 1883
Client - sonoff1ALT
User - openhabian
Password - openhabian
Topic - sonoff1
Full Topic - sonoff/%prefix%/%topic%/

Using a tool to check what MQTT messages are really send! (mqtt-spy).

I can only guess that it is your commandTopic which I would set to

cmnd/sonoff_sw01/POWER", on=“ON”, off=“OFF”

Great! this solved, thanks Guy!!

Thank you for the help! I’ve finally been able to make it work.
(I spend days trying to make it work…)

Please note that I was forced to restart openhab2.

Hi all, need your help
I use openHab2 and MQTT binding 2.4.0

When I publish topic from terminal -
mosquitto_pub -u mqtt_user -P password -d -t hello/world -m "Hello From Terminal"

I see in openhab log -
mqtt:broker:myUnsecureBroker:lamp triggered Hello From Terminal

But on openhab UI my string always empty (.

default.things file
Thing mqtt:broker:myUnsecureBroker [ host=“192.168.88.110”, secure=false, username=“user”, password=“pass”]
{
Channels:
Type string : lamp “My mqtt test” [ stateTopic=“hello/world”]
}

My default.items file -
String MyMqttTst "My mqtt test" { channel="mqtt:broker:myUnsecureBroker:lamp" }

Got some errors with the sonoff.rules which I cannot figure out:

2019-01-06 22:15:20.699 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'sonoff.rules' has errors, therefore ignoring it: [7,36]: no viable alternative at input '“'

[7,37]: no viable alternative at input 'mqtt'

[7,48]: mismatched input ':' expecting 'end'

[14,6]: no viable alternative at input '“'

sonoff.rules

val sonoff_device_ids = newArrayList("sonoff*")

rule "Update Sonoff Status"
when
    Time cron "0 0 4 1/1 * ? *" // Update twice a day at 4am
then
    val actionsBroker = getActions(“mqtt”,“mqtt:broker:mosB”)
    for (String device_id : sonoff_device_ids) {
        actionsBroker.publishMQTT( “cmnd/” + device_id + “/status”, “2”)
        logInfo("sonoff.rules", "Updating FW Item for Device: " + device_id )
    }
end

rule “Sonoff Maintenance”
when
    Item Sonoff_Action received command
then
        logInfo(“sonoff.rules”, “Sonoff Maintenance on all devices: " + receivedCommand)
        val actionsBroker = getActions(“mqtt”,“mqtt:broker:mosB”) // change to your broker name!
        for (String device_id : sonoff_device_ids) {
            switch (receivedCommand) {
                case “restart” :
                    actionsBroker.publishMQTT( “cmnd/” + device_id + “/restart”, “1”)
                case “queryFW” :
                    actionsBroker.publishMQTT( “cmnd/” + device_id + “/status”, “2”)
                case “upgrade” : {
                    actionsBroker.publishMQTT( “cmnd/” + device_id +”/otaurl", “http://thehackbox.org/tasmota/release/sonoff.bin”)
                    actionsBroker.publishMQTT( “cmnd/” + device_id + “/upgrade”, “1”)
                }
        }
}
Sonoff_Action.postUpdate(NULL)
end

What would be the appropriate MQTT 2.4 version of this MQTT 1.13 item?

String Sonoff_Current_FW_Available "Current Release [%s]" <sonoff_basic> (Sonoff_Maintenance) { http="<[tasmotaRelease:10000:JSONPATH($[0].name)]"}

I guess all the errors obserrved are caused by the false “ instead of ". If you look closely at the code posted, the first string “sonoff*” is surrounded by the correct characters, the “mqtt” in line 7 NOT (more false characters to follow)!

and your item “Sonoff_Current_FW_Available” isn’t using mqtt at all! That item is using the http binding, which didn’t change at all!

Oh I see. So what changes should I make to correct this item file?