Using Sonoff Power Switches with Tasmota firmware and openHAB2 MQTT2 binding

mqtt
mqtt-binding
mqttsonoff
Tags: #<Tag:0x00007fe0592ddb88> #<Tag:0x00007fe0592dda20> #<Tag:0x00007fe0592dd6d8>

(Kenny Zsombik) #23

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


(David Graeff) #24

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.


(Luiz Machado) #25

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


(SiHui) #26

(Kenny Zsombik) #27

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!


(Thomas H W) #28

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



(Hakan Tandogan) #29

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…


(David Graeff) #30

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.


(Thomas H W) #31

@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.


(Hakan Tandogan) #32

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.


(Gadget Guy) #33

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.


(SiHui) #34

(Thomas H W) #35

My firmware from Theo is based on version 6.3.0.


(Paulo Wang) #36

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??


(Maurits) #37

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.


(Thomas H W) #38

@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


(Gadget Guy) #39

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)


(Luciano) #40

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.


(Gadget Guy) #41

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%/


(Jürgen Baginski) #42

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”