Using Sonoff Power Switches with Tasmota firmware and openHAB2 MQTT2 binding

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?

? None. The http binding, which is a openhab1 binding, should be setup using the hhtp.cfg file.
You have most probably copied that item from somewhere, look there.

got it!.
I got these errors with my sonoff.rules

2019-01-07 22:09:34.581 [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 '“'

This is my sonoff.rules

val sonoff_device_ids = newArrayList("sonoffs")

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”) 
        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

Where did I go wrong?

As said before:

2 Likes

Thank you so much for your posts. It helped me a lot. Finally I got my system working.

Do you know how can I get the results on state1/state2/state3 for a 4CH SONOFF at .things?

Any help would be appreciated!!

Got this error with my rule for updating sonoff

2019-01-18 20:14:56.111 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Update Sonoff Status': Instance is not an MQTTActions class.

What is the MQTTActions class ?

Which version of MQTT are you using? Version 1 (With MQTT binding and action) or version 2 ?

Please post the rule.

I am using MQTT 2.4
I shifted the “val…” line to be before the rule

val sonoff_device_ids = newArrayList("sonoffs")
val actionsBroker=getActions("mqtt","mqtt:broker:mosB")

rule "Update Sonoff Status"
when
    Time cron "0 0 20 * * ?" or
	System started
then
    for (String device_id : sonoff_device_ids) {
        logDebug("sonoff.rules", "Updating FW Item for Device: " + device_id )
        actionsBroker.publishMQTT("mosB", "cmnd/" + device_id + "/STATUS", "2")
    }
end

Now I am getting this error.

2019-01-18 22:34:54.504 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'sonoff.rules', using it anyway:

The value of the local variable actionsBroker is not used

2019-01-18 22:34:54.557 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sonoff.rules'

2019-01-18 22:34:59.803 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Update Sonoff Status': Instance is not an MQTTActions class.

I guess you tired it with the “actionsBroker” declared within the rule as well.
Honestly,at the moment I have no clue.
How are your things and items defined? (if by files, please post)
Do you have mqtt binding (Ver 1) and/or mqtt Action (Ver 1) installed in your System?

As far as I understand this, you would Need to send a “…cmnd/Power1” (empty message) in oder to get a state1 message.

1 Like

Hi Jurgen,
Thank you for the quick reply.
I only have mqtt 2.4 binding installed, no mqtt action or mqtt v1 binding installed.

This is my items file

String Sonoff_Action "Sonoff Action" <sonoff_basic> (Sonoff_Maintenance)
String Sonoff_FW_Available "Latest Firmware Release [%s]" <sonoff_basic> (Sonoff_Maintenance) { http="<[tasmotaRelease:10000:JSONPATH($[0].name)]" }

My broker in the Thing file

Bridge mqtt:broker:mosquitto "mosB" [ host="xxx.xxx.x.xxx", port=1883, secure=false, username="xxx", password="xxx", clientID="xxx" ]

Further questions:
The error about “is not an MQTTActions class” is reported very shortly after the error “in configuration model …”, which is probably raised while saving an actually incomplete file (for example during an edit).
Do you see another log entries with " Refreshing model ‘sonoff.rules’" after the last error? In this case the error would have been raised again while saving an actually incomplete file and I think the rule would be working.

Hi Jurgen,
I made sure the rule is not an incompleted save file.
I see this error again this time:

2019-01-18 22:34:59.803 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Error during the execution of startup rule 'Update Sonoff Status': Instance is not an MQTTActions class.

really scratching my head here to where the error is