Tutorial: Toggle wifi switch via MQTT using MQTT Binding + OpenHab 2.5 and SonoffS31 Lite (Flashed with Tasmota)

Notice: This tutorial does not cover the flashing of Tasmota to the Sonoff S31 Lite. ( there are plenty of existing tutorials. However, this S31 device was very easy to open and flash and I like that you could use 2 of them in a standard outlet due to the slim profile.)

I am about to start the process of configuring MQTT and connecting a smartPlug (Sonoff S31) to control a lamp. I figured that I would take some notes and share as I have struggled in the past when setting this up.

This is on a OH2.5 system I put together for a friend this past Christmas for a friend. The system consists of a raspberry pi3b and has worked flawless since then and has been 100% stable and available. I really like OH!

Notice: I prefer text config files, so most of this will be done without using the UI.

First, I establish a ssh session to the system and will perform a backup then update to openhab v.2.5.12 and all installed packages the latest version using the openhabian-config tool.

(I received some update errors as raspbian buster is no longer the latest stable release)
`E: Repository ‘Index of /raspbian buster InRelease’ changed its ‘Suite’ value from ‘stable’ to ‘oldstable’

N: This must be accepted explicitly before updates for this repository can be applied. See apt-secure(8) manpage for details.`

I solved with the following command:
sudo apt update --allow-releaseinfo-change

General Overview / Listing of of steps covered

  1. install mosquitto mqtt service (sudo apt-get install mosquitto)
  2. Install mqtt binding in paperUI
  3. Edit binding config details
  4. input mqtt server details into tasmota device (so it knows where the mqtt host is and how to talk to it)
  5. Configure Item
  6. Update sitemap - For manual toggle of state (on/off)
  7. Create rule (every day on at sunset, off at 10PM)

1 ) Install Mosquitt MQTT server

sudo apt-get install mosquitto

2) Install MQTT binding in paperUI

3) Configure MQTT Server

Once the binding is installed, we will need to configure the settings.

create file services/mqtt.cfg and include the following:

# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.

# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.

# Optional. User id to authenticate with the broker.

# Optional. Password to authenticate with the broker.

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>

4) Configure Thing

This is the part that usually trips me up. I forget to add the thing.
file: things/mqtt.things

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhabian", password="openhabian", clientID="openHAB" ]

{    // Sonoff S31 Output (PLUG02)
    Thing topic plug02 "Sonoff S31 Plu02" {
        Type switch : power     "Power"  [ stateTopic="stat/PLUG02/POWER", commandTopic="cmnd/PLUG02/POWER" ]

5) Configure Item

Inside sonoff.items file

Switch plug02_sw     "S31 Outlet (PLUG02)"   <switch>  { channel="mqtt:topic:mosquitto:plug02:power" }

6) Update Sitemap

Switch item=plug02_sw // Sonoff S31 Lite [PLUG02]

7) Create a rule to turn on and off daily

I am using the astro binding so that the on time is adaptive to the time of year. if you do not have the astro binding, then you can remove the 'Channel astro… ’ line and uncomment the // Time cron…line.

create file rules/indoorLights.rule

rule "Turn On Den Lamp"
    Channel 'astro:sun:home:set#event' triggered START  // when the sun sets, turn on the lights
    // Time cron "0 45 16 1/1 * ? *" // 10:45PM every day - Turn backyards garden lights off
    logInfo("RULE","Turn On Den Lamp.")

rule "Den Lamp - Turn Off nightly at 10:00 PM"

    Time cron "0 0 22 1/1 * ? *"
    if (plug02_sw.state==ON) {
        logInfo("RULE","Turning off Den Lamp, it was on.")


This is now working for me.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.