Hi guys,
after hours of working on my openHAB2 configuration for my Sonoff Smart Switches, using the Tasmota firmware, I got that running, using the new MQTT2 binding. As for now I have 7 Sonoff Power Switches and didn’t want to configure them through PaperUI. After a hint from @Dim I got my configuration completely in files, providing me with the ability to do some slight changes on all channels at the same time.
This is just meant as an example and I would be more than happy, get more hints and ideas of how to use them in a better way - especially to avoid duplicate configurations. Currently there is no documentation for that thing, that I’m aware of. That’s why I choosed to share my findings and hope, that they may be useful for anyone of you.
In the configuration I have put all of the available options. If I don’t make use of them for now, they are commented out, but I’m already aware of their existence.
/etc/openhab2/things/mqtt.things
Bridge mqtt:broker:MosquittoMqttBroker "Mosquitto MQTT Broker" @ "MQTT" [
host="x.x.x.x",
secure=false,
port=1883,
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="mymqttuser",
password="mymqttpass"
]
{
// Sonoff Power Switch 01
Thing mqtt:topic:SonoffPs01 "Sonoff Power Switch 01" @ "MQTT" {
Channels:
Type switch : switch "Power Switch" [
stateTopic="tasmota/sonoff-ps01/POWER",
commandTopic="tasmota/sonoff-ps01/cmnd/POWER",
//retained=false,
//postCommand="",
//transformationPattern="JSONPATH:$.POWER",
//formatBeforePublish="%s",
on="ON",
off="OFF"
]
Type string : state01 "Switch State 01" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.POWER",
on="ON",
off="OFF"
]
Type string : state02 "Switch State 02" [
stateTopic="stat/sonoff-ps01/POWER",
on="ON",
off="OFF"
]
Type string : state03 "Switch State 03" [
stateTopic="stat/sonoff-ps01/RESULT",
transformationPattern="JSONPATH:$.POWER",
on="ON",
off="OFF"
]
Type number : vcc "VCC" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.Vcc"
]
Type string : wifi-ap "Wifi AP" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.Wifi.AP"
]
Type string : wifi-ssid "Wifi SSID" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.Wifi.SSId"
]
Type string : wifi-channel "Wifi Channel" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.Wifi.Channel"
]
Type string : wifi-rssi "Wifi RSSI" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.Wifi.RSSI"
]
Type string : uptime "Uptime" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.Uptime"
]
Type string : time "Time" [
stateTopic="tasmota/sonoff-ps01/STATE",
transformationPattern="JSONPATH:$.Time"
]
Type string : devicestate "Device State" [
stateTopic="tasmota/sonoff-ps01/LWT"
]
}
// Sonoff Power Switch 02
Thing mqtt:topic:SonoffPs02 "Sonoff Power Switch 02" @ "MQTT" {
Channels:
Type switch : switch "Power Switch" [
stateTopic="tasmota/sonoff-ps02/POWER",
commandTopic="tasmota/sonoff-ps02/cmnd/POWER",
//retained=false,
//postCommand="",
//transformationPattern="JSONPATH:$.POWER",
//formatBeforePublish="%s",
on="ON",
off="OFF"
]
Type string : state02 "Switch State 02" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.POWER",
on="ON",
off="OFF"
]
Type string : state02 "Switch State 02" [
stateTopic="stat/sonoff-ps02/POWER",
on="ON",
off="OFF"
]
Type string : state03 "Switch State 03" [
stateTopic="stat/sonoff-ps02/RESULT",
transformationPattern="JSONPATH:$.POWER",
on="ON",
off="OFF"
]
Type number : vcc "VCC" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.Vcc"
]
Type string : wifi-ap "Wifi AP" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.Wifi.AP"
]
Type string : wifi-ssid "Wifi SSID" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.Wifi.SSId"
]
Type string : wifi-channel "Wifi Channel" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.Wifi.Channel"
]
Type string : wifi-rssi "Wifi RSSI" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.Wifi.RSSI"
]
Type string : uptime "Uptime" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.Uptime"
]
Type string : time "Time" [
stateTopic="tasmota/sonoff-ps02/STATE",
transformationPattern="JSONPATH:$.Time"
]
Type string : devicestate "Device State" [
stateTopic="tasmota/sonoff-ps02/LWT"
]
}
}
This will create 3 things for you:
- The Broker Connection to (in my case) Mosquitto Broker
- Thing Sonoff Power Switch 01
- Thing Sonoff Power Switch 02
My items are configured in /etc/openhab2/items/sonoff-power-switches.items
/*
Lighting Room1
*/
Switch SonoffPs01Switch_Switch "SPS01 Switch" (SonoffPowerSwitches_Basic,ChristmasLivingrooms) { channel="mqtt:topic:SonoffPs01:switch" }
String SonoffPs01Switch_State "SPS01 State" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:state01", channel="mqtt:topic:SonoffPs01:state02", channel="mqtt:topic:SonoffPs01:state03", channel="mqtt:topic:SonoffPs01:switch" }
Number SonoffPs01Switch_Vcc "SPS01 VCC" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:vcc" }
String SonoffPs01Switch_WifiAp "SPS01 Wifi AP" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:wifi-ap" }
String SonoffPs01Switch_WifiSsid "SPS01 Wifi SSID" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:wifi-ssid" }
String SonoffPs01Switch_WifiChannel "SPS01 Wifi Channel" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:wifi-channel" }
String SonoffPs01Switch_WifiRssi "SPS01 Wifi RSSI" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:wifi-rssi" }
String SonoffPs01Switch_Uptime "SPS01 Uptime" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:uptime" }
String SonoffPs01Switch_Time "SPS01 Time" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:time" }
String SonoffPs01Switch_DeviceState "SPS01 Device State" (SonoffPowerSwitches_Basic) { channel="mqtt:topic:SonoffPs01:devicestate" }
/*
Lighting Room2
*/
Switch SonoffPs02Switch_Switch "SPS02 Switch" (SonoffPowerSwitches_S20,ChristmasNightLights) { channel="mqtt:topic:SonoffPs02:switch" }
String SonoffPs02Switch_State "SPS02 State" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:state01", channel="mqtt:topic:SonoffPs02:state02", channel="mqtt:topic:SonoffPs02:state03", channel="mqtt:topic:SonoffPs02:switch" }
Number SonoffPs02Switch_Vcc "SPS02 VCC" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:vcc" }
String SonoffPs02Switch_WifiAp "SPS02 Wifi AP" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:wifi-ap" }
String SonoffPs02Switch_WifiSsid "SPS02 Wifi SSID" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:wifi-ssid" }
String SonoffPs02Switch_WifiChannel "SPS02 Wifi Channel" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:wifi-channel" }
String SonoffPs02Switch_WifiRssi "SPS02 Wifi RSSI" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:wifi-rssi" }
String SonoffPs02Switch_Uptime "SPS02 Uptime" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:uptime" }
String SonoffPs02Switch_Time "SPS02 Time" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:time" }
String SonoffPs02Switch_DeviceState "SPS02 Device State" (SonoffPowerSwitches_S20) { channel="mqtt:topic:SonoffPs02:devicestate" }
I’m currently using Sonoff Basis and S20 switches. There is not difference in channels or items that I’m aware of.
They are configured using the HTTP API and Postman. If of interest, I could share my collection as well.
Happy automation!
Cheers,
Sven
Edit 1
During the creation of this topic @David_Graeff has created a great blog post, giving more information on how to use the MQTTv2 binding in several ways: https://www.openhab.org/blog/2018-12-16-mqtt-arrives-in-the-modern-openhab-2-x-architecture.html