Connect Sonoff Switches to OpenHAB through MQTT

Hello everyone,

I have recently installed openhabian in RPi3 and I have flashed tasmota in the sonoff smart switches.
I do not know how to connect sonoff smart switches to openhab to meet my goal of turning the switch ON/OFF using openhab.
I have installed Mqtt-binding add-on. Can someone please provide a step-by-step how to connect sonoff switch to openhab using Mqtt (external MQTT broker such as HiveMQ) ?

Thank you

There’s a detailed tutorial using the new mqtt binding Sonoff Tasmota with MQTT Binding 2.4 (using config files)
Have a search through the forums - you should find other similar tutorials. I’d encourage you to do that. Openhab does have bit of a steep learning curve (well it did for me…) but there’s plenty help out there and quite a few pointers for you to make progress.

Here’s a quick pointer to what I have set up, which may be of some help:
Firstly, you’d need to check that the sonoff is connecting to mqtt OK. I’m assuming that you’ve got that working OK and that you can see the sonoff reporting its status ok via mqtt? On my pi I’d use a command like

mosquitto_sub -h -v -t #

which would show all often messages published to mosquito - replace the # with the specific topic if you wish.
I’ve still not updated my system to the mqtt v2 binding - I’m still using v1. So in my case, in the .items file ($CONF/items/.items) I have lines like the following:

Switch SF_S61 “SF S20-61 [%s]” (gSf,gSfSocket) {mqtt=">[mqtt:cmnd/sonoff/S20-61/power:command:*:default],
<[mqtt:stat/sonoff/S20-61/POWER:state:default],
<[mqtt:tele/sonoff/S20-61/STATE:state:JSONPATH($.POWER)],
<[mqtt:stat/sonoff/S20-61/RESULT:state:JSONPATH($.POWER)]"}

this sends the ON/OFF command to the switch via mqtt, and also updates the switch value if the switch is changed elsewhere (e.g. via the intranet page).

Hope that gives you some pointers.

1 Like

Why don’t you try reading the MQTT documentation and/or searching this forum for threads about mqtt2 and sonoff? This topic has been discussed several times.
BTW: The topic Tutorials & Examples is meant for posting such.

@arvvai
take a look on my post here

pretty much everything you need, adjust your setting for hivemq which I have no experience with

here is sonoff wallswitch config for your Thing file (make sure you have correct topic, mine is different from standard tasmota which has got cmnd and stat at the beginning)

Thing mqtt:topic:WallSwitchWorkroom "WallSwitch Light Workroom" (mqtt:broker:home) @ "Workroom" {
Channels:
    Type switch : switch        "Workroom Lights"       [ stateTopic="home/workroom/light/stat/POWER", commandTopic="home/workroom/light/cmnd/POWER", on="ON", off="OFF" ]
}  

more features to obtain like so …

Thing mqtt:topic:WallSwitchWorkroom "WallSwitch Light Workroom" (mqtt:broker:home) @ "Workroom" {
Channels:
    Type switch : switch        "Workroom Lights"   [ stateTopic="home/workroom/light/stat/POWER", commandTopic="home/workroom/light/cmnd/POWER", on="ON", off="OFF" ]
    Type string : wifi-ssid     "Wifi SSID"         [ stateTopic="home/workroom/light/tele/STATE", transformationPattern="JSONPATH:$.Wifi.SSId" ]
    Type string : wifi-rssi     "Wifi RSSI"         [ stateTopic="home/workroom/light/tele/STATE", transformationPattern="JSONPATH:$.Wifi.RSSI" ]
    Type string : uptime        "Uptime"            [ stateTopic="home/workroom/light/tele/STATE", transformationPattern="JSONPATH:$.Uptime" ]
    Type string : time          "Time"              [ stateTopic="home/workroom/light/tele/STATE", transformationPattern="JSONPATH:$.Time" ]
    Type string : devicestate   "Device State"      [ stateTopic="home/workroom/light/tele/LWT" ]
}  
1 Like