Sonoff / Tasmota mit OpenHAB 2.4 MQTT

27. Dezember 2018 at 11:10
Print Friendly, PDF & Email

In OpenHAB 2.4 wurde die Anbindung an MQTT umgestellt. Es sieht jetzt alle wesentlich strukturierter aus – man muss aber alles in OpenHAB umstellen.

Für die SONOFF Geräte verwende ich die firmware von Theo Arends. Es werden viele Geräte unterstützt und das ganze ist sehr gut dokumentiert. https://github.com/arendst/Sonoff-Tasmota

Hier gehe ichbewusst den Weg über die Text-Dateien und nicht über Paper-UI. Bei jedem Upgrade, setze ich das System neu auf. Mir ist noch nicht klar, wie man die Konfiguration aus PaperUI in ein neues System übertragen kann. Bei den Text-Dateien geht dies aber problemlos.

 

MQTT Bridge definieren

Hier legt man die Anbindung an den MQTT Broker fest. In einigen Beispielen sieht man hier auch die Definition der MQTT Things. Davon rate ich ab, da es zu einem seltsamen Effekt beim Aufbau des Systems kommt: Wenn man eine kleine Änderung an den Things vornimmt, aktualisiert sich das System entweder nicht, oder mit einer langen Verzögerung, da jedes mal die Bridge neu definiert wird. Wenn das separat erfolgt (in zwei Dateien) werden Änderungen an den Things sofort übernommen und sichtbar.

bridge.things

[codesyntax lang=”javascript”]

// 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.178.20",
  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"
]

[/codesyntax]

MQTT Things definieren

Zuerst kann man sich die Nachrichten mit MQTT.fx ansehen. Daraus lassen sich die Channels ableiten:

MQTT Nachrichten

[codesyntax lang=”javascript”]

house/light1/cmnd/sonoff/POWER
house/light1/stat/sonoff/RESULT {"POWER":"OFF"}
house/light1/tele/sonoff/LWT    offline
house/light1/tele/sonoff/INFO1  {"Module":"Sonoff Basic","Version":"6.3.0","FallbackTopic":"DVES_420674","GroupTopic":"sonoffs"}
house/light1/tele/sonoff/INFO2  {"WebServerMode":"Admin","Hostname":"sonoff-1652","IPAddress":"192.168.178.140"}
house/light1/tele/sonoff/INFO3  {"RestartReason":"Power on"}
house/trockner/tele/sonoff/STATE
  {"Time":"2018-12-27T08:27:01","Uptime":"6T12:16:27","Vcc":3.514,"POWER":"OFF","Wifi":{"AP":1,"SSId":"A-Team","BSSId":"C0:25:06:F4:D2:C9","Channel":1,"RSSI":82}}
house/trockner/tele/sonoff/SENSOR
  {"Time":"2018-12-27T08:27:01",
          "ENERGY":{"TotalStartTime":"2018-12-20T19:52:03","Total":0.000,"Yesterday":0.000,"Today":0.000,"Period":0,
                    "Power":0,"ApparentPower":0,"ReactivePower":0,"Factor":0.00,"Voltage":0,"Current":0.000}}

[/codesyntax]

sonoff.things

[codesyntax lang=”javascript”]

// sonoff.things
// https://community.openhab.org/t/using-sonoff-power-switches-with-tasmota-firmware-and-openhab2-mqtt2-binding/59969
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/stat/sonoff/cmnd/POWER", on="ON", off="OFF" ]
    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/stat/sonoff/cmnd/POWER", on="ON", off="OFF" ]
    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

[/codesyntax]

MQTT Items

Hier werden die Channels an ein item gebunden. Die Gruppen könne für die Speicherung (persitance) und die Anzheige (sitempas) verwendet werden.

sonoff.items

[codesyntax lang=”javascript”]

// 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" }

[/codesyntax]

MQTT Sitemap

sonoff.sitemap

[codesyntax lang="javascript"]
// 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
[/codesyntax]