Using Sonoff Power Switches with Tasmota firmware and openHAB2 MQTT2 binding

tsvsj, thanks for the great info. But now i literally don’t get what I’m doing wrong.
So i recently did a clean install of my openhabianpi (moved to 2.4)
started working with the new mqttv2 but I cant mange to use the transformationPattern.
No matter what I use get following issue “Transformation service JSONPATH for pattern $.AM2301.Temperature not found!”

Do you maybe have an idea what i’m doing wrong? Thanks for any advice.

Did you install the JSONPATH transformation?

No, since it was automatically installed. I also checked it in the openhab-console and it’s saying its installed.

Have you tried to use the JSONPATH transformation somewhere else (in a rule, on an item, etc)? Would be interesting to know if it works at all on your installation. A reinstall may work. You may also log in to the openhab console and check all installed bundles (“list:bundles”) for the aforementioned transformation.

David,

I’m using OH since 2017 and it works perfectly. Now, I’m trying to migrate to the new MQTT Biding (2.4) but I cannot configure the sonoffs (with Tasmota firmware) using the UI. As you highlighted, does not make sense to move to this new MQTT binding and continue using configuration files. Do you have a step by step guide to configure sonoffs using the new biding ? Sorry to pester with that but I spent almost two days without success.
thx

Good one! I tried it in a rule and same issue. I reinstalled the JSONPath feature and now its working. Many thanks for the advise!

1 Like

Based on the tutorial from Sven, I was able to convert the MQTT setup for the Sonoff devices using the firmware from Theo Arends: https://github.com/arendst/Sonoff-Tasmota
When combining the definitions of the bridge and things in one file, the system did behave quite strange. Things were not updated or I had to rename the broker. Accordingly I moved the definition of the bridge in a separate file.

bridge.things:

// bridge.things
 
// https://community.openhab.org/t/using-sonoff-power-switches-with-tasmota-firmware-and-openhab2-mqtt2-binding/59969
 
Bridge mqtt:broker:MqttPandora20 "MQTT Broker Pandora" @ "MQTT Pandora" [ 
  host="192.168.1.123",
  port=1883,
  secure="AUTO",
  qos=0,
  retain=false,
  clientid="Oh2Mqtt2Thing",
  //certificate="",
  //certificatepin=false,
  //publickey="",
  //publickeypin=false,
  keep_alive_time=30000,
  reconnect_time=60000,
  //lastwill_message="",
  //lastwill_qos=1,
  //lastwill_topic="",
  username="mqttuser",
  password="mqttpw"
]

sonoff.things
EDIT 30-Dec-2018: the commandTopic for the PowerSwitch was wrong

// sonoff.things
// 
 
Thing mqtt:topic:SonoffPs02_thg "Sonoff POW Switch 02" (mqtt:broker:MqttPandora20) @ "MQTT"  {
  Channels:
    Type switch : PowerSwitch    "Power Switch 02" [ stateTopic="house/trockner/stat/sonoff/POWER", commandTopic="house/trockner/cmnd/sonoff/Power", on="1", off="0" ]
    Type switch : PowerSwitchRes "Switch State 02" [ stateTopic="house/trockner/stat/sonoff/RESULT", transformationPattern="JSONPATH:$.POWER",on="ON",off="OFF"]
    Type string : Version        "Version 02"      [ stateTopic="house/trockner/tele/sonoff/INFO1",  transformationPattern="JSONPATH:$.Version"]
    Type string : fallback       "fallback topic"  [ stateTopic="house/trockner/tele/sonoff/INFO1",  transformationPattern="JSONPATH:$.FallbackTopic"]
    Type string : hostname       "hostname      "  [ stateTopic="house/trockner/tele/sonoff/INFO2",  transformationPattern="JSONPATH:$.Hostname"]
    Type string : IP             "IP            "  [ stateTopic="house/trockner/tele/sonoff/INFO2",  transformationPattern="JSONPATH:$.IPAddress"]
    Type string : time          "Time"             [ stateTopic="house/trockner/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Time" ]
    Type string : uptime        "Uptime"           [ stateTopic="house/trockner/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Uptime" ]
    Type number : vcc           "VCC"              [ stateTopic="house/trockner/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Vcc"  ]
    Type string : wifi-ap       "Wifi AP"          [ stateTopic="house/trockner/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.AP" ]
    Type string : wifi-ssid     "Wifi SSID"        [ stateTopic="house/trockner/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.SSId" ]
    Type string : wifi-channel  "Wifi Channel"     [ stateTopic="house/trockner/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.Channel" ]
    Type string : wifi-rssi     "Wifi RSSI"        [ stateTopic="house/trockner/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI" ]
    Type string : SensorTime    "Sensor Time"      [ stateTopic="house/trockner/tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.Time" ]
    Type string : POW-startTime  "POW Start Time"  [ stateTopic="house/trockner/tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.ENERGY.TotalStartTime" ]
    Type string : POW-Total      "POW Total"       [ stateTopic="house/trockner/tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Total" ]
    Type string : POW-Voltage    "POW Voltage"     [ stateTopic="house/trockner/tele/sonoff/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Voltage" ]
    Type string : devicestate   "Device State"     [ stateTopic="house/trockner/tele/sonoff/LWT" ]
} // end of thing
 
Thing mqtt:topic:SonoffPs01_thg "Sonoff Power Switch 01" (mqtt:broker:MqttPandora20) @ "MQTT"  {
  Channels:
    Type switch : PowerSwitch    "Power Switch 01" [ stateTopic="house/light1/stat/sonoff/POWER", commandTopic="house/light1/cmnd/sonoff/Power", on="1", off="0" ]
    Type switch : PowerSwitchRes "Switch State 01" [ stateTopic="house/light1/stat/sonoff/RESULT", transformationPattern="JSONPATH:$.POWER",on="ON",off="OFF"]
    Type string : Version        "Version 01"      [ stateTopic="house/light1/tele/sonoff/INFO1",  transformationPattern="JSONPATH:$.Version"]
    Type string : fallback       "fallback topic"  [ stateTopic="house/light1/tele/sonoff/INFO1",  transformationPattern="JSONPATH:$.FallbackTopic"]
    Type string : hostname       "hostname      "  [ stateTopic="house/light1/tele/sonoff/INFO2",  transformationPattern="JSONPATH:$.Hostname"]
    Type string : IP             "IP            "  [ stateTopic="house/light1/tele/sonoff/INFO2",  transformationPattern="JSONPATH:$.IPAddress"]
    Type string : time          "Time"             [ stateTopic="house/light1/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Time" ]
    Type string : uptime        "Uptime"           [ stateTopic="house/light1/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Uptime" ]
    Type number : vcc           "VCC"              [ stateTopic="house/light1/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Vcc"  ]
    Type string : wifi-ap       "Wifi AP"          [ stateTopic="house/light1/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.AP" ]
    Type string : wifi-ssid     "Wifi SSID"        [ stateTopic="house/light1/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.SSId" ]
    Type string : wifi-channel  "Wifi Channel"     [ stateTopic="house/light1/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.Channel" ]
    Type string : wifi-rssi     "Wifi RSSI"        [ stateTopic="house/light1/tele/sonoff/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI" ]
    Type string : devicestate   "Device State"     [ stateTopic="house/light1/tele/sonoff/LWT" ]
} // end of thing

sonoff.items

// sonoff.items
//  itemname ["labeltext"] [<iconname>] [(group1, group2, ...)] [{bindingconfig}]
// Itead SONOFF - Tasmoto
// https://github.com/arendst/Sonoff-Tasmota/wiki
/************************************************** Gruppen ********************************************/
Group      gSonoffSwitches      "SONOFF Sensors" // used for chart
Group      gSonoffSw1           "sonoff switch 1"
Group      gSonoffSw1Info       "sonoff Info 1"
Group      gSonoffSw2           "sonoff switch 2"
Group      gSonoffSw2Info       "sonoff Info 2"
 
/************************************************** Items ********************************************/
String Switch_TH_Temperatur         "Temperatur [%s °C]"    <temperature>   { channel="mqtt:topic:myMQTTBroker:sonoff_TH_Thing:Temperature"}
 
/*
  Lighting Room1
*/
Switch SonoffPs01Switch_Switch 		  "SPS01 Switch 1" 	        (gSonoffSw1)     { channel="mqtt:topic:SonoffPs01_thg:PowerSwitch" }
Switch SonoffPs01Switch_State 		  "SPS01 State 1"           (gSonoffSw1)     { channel="mqtt:topic:SonoffPs01_thg:PowerSwitchRes"}
Number SonoffPs01Switch_Vcc 		    "SPS01 VCC  [%s]" 		    (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:vcc" }
String SonoffPs01Switch_WifiAp 		  "SPS01 Wifi AP  [%s]" 	  (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:wifi-ap" }
String SonoffPs01Switch_WifiSsid 	  "SPS01 Wifi SSID  [%s]" 	(gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:wifi-ssid" }
String SonoffPs01Switch_WifiChannel "SPS01 Wifi Channel [%s]" (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:wifi-channel" }
String SonoffPs01Switch_WifiRssi 	  "SPS01 Wifi RSSI  [%s]" 	(gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:wifi-rssi" }
String SonoffPs01Switch_Uptime 		  "SPS01 Uptime" 		        (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:uptime" }
String SonoffPs01Switch_Time 		    "SPS01 Time" 		          (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:time" }
String SonoffPs01Switch_Version 		"SPS01 Version [%s]"      (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:Version" }
String SonoffPs01Switch_Hostname 	  "SPS01 Hostname  [%s]" 	  (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:hostname" }
String SonoffPs01Switch_IP     		  "SPS01 IP  [%s]" 	        (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:IP" }
String SonoffPs01Switch_DeviceState "SPS01 Device State"      (gSonoffSw1Info) { channel="mqtt:topic:SonoffPs01_thg:devicestate" }
String SonoffPs01Switch_SensorTime 	"SPS01 Sensor Time"       (gSonoffSw1)     { channel="mqtt:topic:SonoffPs01_thg:SensorTime" }
 
/*
  Trockner
*/
Switch SonoffPs02Switch_Switch 		  "SPs02 Switch 1" 	        (gSonoffSw2)     { channel="mqtt:topic:SonoffPs02_thg:PowerSwitch" }
Switch SonoffPs02Switch_State 		  "SPs02 State 1"           (gSonoffSw2)     { channel="mqtt:topic:SonoffPs02_thg:PowerSwitchRes"}
Number SonoffPs02Switch_Vcc 		    "SPs02 VCC  [%s]" 		    (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:vcc" }
String SonoffPs02Switch_WifiAp 		  "SPs02 Wifi AP  [%s]" 	  (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:wifi-ap" }
String SonoffPs02Switch_WifiSsid 	  "SPs02 Wifi SSID  [%s]" 	(gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:wifi-ssid" }
String SonoffPs02Switch_WifiChannel "SPs02 Wifi Channel [%s]" (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:wifi-channel" }
String SonoffPs02Switch_WifiRssi 	  "SPs02 Wifi RSSI  [%s]" 	(gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:wifi-rssi" }
String SonoffPs02Switch_Uptime 		  "SPs02 Uptime" 		        (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:uptime" }
String SonoffPs02Switch_Time 		    "SPs02 Time" 		          (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:time" }
String SonoffPs02Switch_Version 		"SPs02 Version [%s]"      (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:Version" }
String SonoffPs02Switch_Hostname 	  "SPs02 Hostname  [%s]" 	  (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:hostname" }
String SonoffPs02Switch_IP     		  "SPs02 IP  [%s]" 	        (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:IP" }
String SonoffPs02Switch_DeviceState "SPs02 Device State"      (gSonoffSw2Info) { channel="mqtt:topic:SonoffPs02_thg:devicestate" }
String SonoffPs02Switch_SensorTime 	"SPs02 Sensor Time"       (gSonoffSw2)     { channel="mqtt:topic:SonoffPs02_thg:SensorTime" }
String SonoffPs02Switch_POW_startTime "SPS02 POW Start Time"  (gSonoffSw2)     { channel="mqtt:topic:SonoffPs02_thg:POW-startTime" }
String SonoffPs02Switch_POW_Today 	  "SPS02 POW Today"       (gSonoffSw2)     { channel="mqtt:topic:SonoffPs02_thg:POW-Total" }
String SonoffPs02Switch_POW_Voltage   "SPS02 POW Voltage"     (gSonoffSw2)     { channel="mqtt:topic:SonoffPs02_thg:POW-Voltage" }

sonoff.sitemap

// meinHeim.sitemap
// list of icons: https://github.com/eclipse/smarthome/tree/master/extensions/ui/iconset/org.eclipse.smarthome.ui.iconset.classic/icons
 
  sitemap sonoff label="Sonoff Devices" { 
 
	Frame label="sonoff Licht 01" {
		Switch	item=SonoffPs01Switch_Switch
		Switch 	item=SonoffPs01Switch_State
		Group   item=gSonoffSw1Info
		Text 	item=SonoffPs01Switch_SensorTime
	} // end of Frame
 
	Frame label="sonoff Trockner" {
		Switch	item=SonoffPs02Switch_Switch
		Switch 	item=SonoffPs02Switch_State
		Group   item=gSonoffSw2Info
		Text 	item=SonoffPs02Switch_SensorTime
		Text 	item=SonoffPs02Switch_POW_Today
		Text 	item=SonoffPs02Switch_POW_Voltage
		Text 	item=SonoffPs02Switch_POW_startTime
	} // end of Frame
 
 
} // end of sitemap


2 Likes

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.