How to use a Sonoff Switch with Home Assistant

sonoff s22 switch mqtt firmwareThis tutorial covers the ways to integrate a Sonoff S22 switch into Home Assistant using Sonoff MQTT and flashing Tasmota to replace the supplied Sonoff firmware.

I’m going to cover two different methods to get the switch working in Home Assistant. The first just uses the S22 as it comes out-of-the-box. The second requires flashing Tasmota onto the switch and setting up MQTT in Home Assistant.

Honestly sometimes it seems MQTT is build up as the greatest thing and it is useful however depending on what you want to achievt, it’s not necessarily the best option.

For instance: I have my Raspberry Pi that runs Home Assistant on a smart plug. I suppose this is pretty much a legacy approach because when I had an SD-Card that was not ‘reliable’ I found that Home Assistant would just lock up and become non responsive every day or so. (I no longer have this problem as I was using a better quality SD-Card and lately switched to running off a USB stick as a learning exercise and never switched back). So if Home Assistant is non-responsive and you need to reboot the Raspberry Pi, it’s not much use if the only way to reboot the Pi is to turn it off at the switch and the switch is controlled by MQTT (say if I was away from home and didn’t have physical access to the switch). So in that case it would make more sense to have a switch that worked with Home Assistant but was also independent and could be controlled with an app on my phone if Home Assistant was not responding.

Using Sonoff S22 with Home Assistant without MQTT

The Sonoff S22 as supplied uses an app called Ewelink available on the Apple iOS and Android Play store. So download and install the app.

When you power the switch up, there are two modes you can use to configure the switch to work with the app. If you press and hold the button for 6 seconds it enters a pairing mode which is three flashes followed by a pause and then three flashes etc. When it is in this mode the app will prompt for the network SSID and Password and will then pair to the switch. If this fails, there is also a legacy pairing mode which you use by again pressing the switch for 6 seconds. In this legacy mode, the light will flash constantly. Then you connect to the SSID of the switch and back to the app to configure it with your normal network SSID and password. I’m sorry I don’t have better instructions and screenshots for this but all my switches are now using MQTT. It’s pretty straightforward and the app has good instructions and online help.

Unfortunately, Home Assistant does not have a component built in that supports the Ewelink App. This makes it more difficult to control than if you were using a Wemo or an Xiaomi switch for instance however it can be done fairly easily using IFTTT. I’ve previously documented using IFTTT here (see the bottom of that post) so take a look if you need help getting IFTTT to work.

The S22 is going to be controlled using IFTTT and a Binary Switch in Home Assistant with some Automations.

First step, in your configuration.yaml file you need to add the following information:

  key: enter-your-ifttt-api-key-here

Get your IFTTT API key from your profile on IFTTT web site. This key enables IFTTT to send commands to the switch using Home Assistant.

We also need to add the Binary Switch in configuration.yaml. Add these lines:

# Sonoff S22 Switches
  name: Home Assistant Switch
  initial: on

What this does is it creates a slider in the Home Assistant front end that you can use to turn the switch on and off. At the moment we have created the slider and you can switch it on and off but it won’t actually DO anything in the real world!

So we also use some Automations to ‘Do’ something when the slider is switched on or off. Add the following to your automations.yaml file:

- action:
  - data:
      event: sonoff-2-on
    service: ifttt.trigger
  alias: Home Assistant Switch On
  condition: []
  id: 'some-unique-number'
  - entity_id: input_boolean.sonoff_switch_2
    from: 'off'
    platform: state
    to: 'on'
- action:
  - data:
      event: sonoff-2-off
    service: ifttt.trigger
  alias: Home Assistant Switch Off
  condition: []
  id: 'some-unique-number'
  - entity_id: input_boolean.sonoff_switch_2
    from: 'on'
    platform: state
    to: 'off'

So if you look at the trigger for turning the switch on, the trigger is that switch slider being clicked. When that is clicked, Home Assistant sends IFTTT a command with the event ‘sonoff-2-on’. It uses the IFTTT API key you configured so IFTTT knows it’s coming from ‘you’ Similarly, for turning the switch off it will send ‘sonoff-2-off’.

Configuring IFTTT

IFTTT Sonoff Setup Now all we need to do is configure IFTTT so it knows what those events are and what to do when it receives them.

This is showing the ‘recipe’ I made to turn the switch on. Again check my link for information on setting up IFTTT.

What we will do in this case for the new recipe is set it so that If maker Event “sonoff-2-on” then turn on Sonoff-2

This is done by making the IF condition a Web Request. So Home Assistant makes a web request with that event as specified in the above automation. The THEN part is for it to link to the eWeLink app. It will ask for your eWeLink username and password and then it will display the list of your configured switches. Here for me it is showing Options Unavailable because I don’t have any switches in eWeLink anymore (they all use MQTT). You also set the action there – either on or off.

That is pretty much all you need do here.

In Google Home you can also link the eWeLink app so you have voice control.

The only disadvantages of this approach are using IFTTT will introduce a delay – maybe up to a few seconds – for the switch to respond and you are also dependent on someone else’s cloud server if that is a concern for you.

It really is a personal preference.

So lets move onto the fun part! Setting up to work with MQTT.

Using Sonoff S22 with MQTT

So after having everyone tell me about the wonders of MQTT I thought I would set up the switches using that instead of the procedure above. The only ‘catch’ is that to use MQTT, the Sonoff needs to be flashed with Tasmota firmware which meant I needed to get a few pieces of hardware and also needed to open up the S22 so I could access the board to do it. Always fun with a mains powered device! (although safe as it was never connected to the mains while it was open)

First there is a shopping list and some software you will need to download (free)

  1. USB UART TTL 3.3V Converter/Programmer (e.g. CP2102, CH340G, FT232, PL2303)
  2. Some header pins
  3. Some wires to connect the pins to each other
  4. Either Arduino IDE or Visual Studio Code with the PlatformIO IDE extension
  5. Sonoff Tasmota firmware

I searched for CP2102 on eBay and purchased one for around $5AUD inc postage. It came with the wires and I purchased some pins as well. The CP2102 I got didn’t have 4 wires though but I have enough computer ‘rubbish’ lying around that I had an extra wire. Just be aware you will need 4 wires.

The 6 pins are labeled from top to bottom 3.3, RST, 5v, TX0, RX0, GND

The Sonoff Tasmota Firmware is here and you want to download the source code (zip) as of date of writing this file is called

I also needed to download the CP2102 Serial Driver as my up-to-date Windows 10 system didn’t have one. I don’t remember where I found it (Google is your friend) but the file I downloaded was called

When you have downloaded the Sonoff-Tasmota firmware, you need to extract it somewhere on your computer. I put it in my documents folder in it’s own sub folder. It doesn’t matter where but you need to be able to find it.

I did try to use the Arduino IDE as it is referenced in all the tutorials and videos but it was not easy to setup, very convoluted and at the end of the day I got compiler errors that I didn’t understand and couldn’t fix and eventually abandoned. By contrast, Visual Studio Code was easy to setup and use.

To install Visual Studio Code, you need to go to the Store in Windows 10 and search for and install Visual Studio Code. When it is installed you need to run it. In the icon bar running vertically down the page the bottom Icon is Extensions. (Short Code CTRL + SHIFT +X) Click on that and then search for and install the PlatformIO IDE Extension.

Next click on File Open Folder and Navigate to where you extracted the Sonoff Tasmota firmware.

Double Click on the file platformio.ini Down near line 14 you will see a lint that says ;env_default = sonoff. You need to delete the semi colon at the front of this line so that section will then read:

src_dir = sonoff

; *** Uncomment one of the lines below to build/upload only one environment
env_default = sonoff
;env_default = sonoff-CN
;env_default = sonoff-DE
;env_default = sonoff-ES
;env_default = sonoff-FR
;env_default = sonoff-IT
;env_default = sonoff-NL
;env_default = sonoff-PL
;env_default = sonoff-minimal
;env_default = sonoff-ds18x20

What this does is makes sure that only the English version is built and uploaded, thus saving requires memory space.

Next, open up the file (Double-click) user_config.h

Look at Line 42 – This is the Project Name. It has to be different for each switch you want to control.

// -- Project -------------------------------------
#define PROJECT                "sonoff2"         // PROJECT is used as the default topic delimiter and OTA file name

In this case I used sonoff2 as my project name.

Next we also set the WiFi information. Navigate down to line 55. Enter your SSID and Password here and also an alternate SSID 2 and change line 55 to WIFI_MANAGER as shown. That setting enables the Sonoff to establish it’s own adhoc network you can connect to if either of the configured networks are unavailable.

#define STA_SSID1              "YOUR_SSID_1"     // [Ssid1] Wifi SSID
#define STA_PASS1              "password"        // [Password1] Wifi password
#define STA_SSID2              "YOUR_SSID_2"     // [Ssid2] Optional alternate AP Wifi SSID
#define STA_PASS2              "password"        // [Password2] Optional alternate AP Wifi password
#define WIFI_CONFIG_TOOL       WIFI_MANAGER      // [WifiConfig] Default tool if wifi fails to connect

That’s all the preparation you need. Now comes the tricky part.

Flashing the Sonoff with Tasmota

You will need to connect the CP2102 to the S22 with the wires and header pins you purchased.

The S22 opens up easily by removing the 2 screws on the back – remove those and it just falls apart. No glue or any other things to worry about. The S22 looks like this when you open it up. I have labeled the pins for easy identification.

sonoff s22 inside

On this photo the header pins have been soldered to the board however the S22 will come without the pins. If you really want to you can solder in pins but that is not necessary or needed. I just attached header pins to the cables and then pushed them into the holes and applied sideways pressure. I attached all 4 pins to the S22 side first. I also attached the RX, TX and GND pins to the CP2102. Here’s the CP2102 again:

The 6 pins are labeled from top to bottom 3.3, RST, 5v, TX0, RX0, GND

In the photo the RX and TX are jumpered together – just remove that and connect all pins except the 3.3v pin on the CP2102

NOTE: TX on the Board goes to RX on the CP2102 and the RX on the Board goes to the TX on the CP2102. If you have them around the wrong way, no harm but you won’t be able to flash it.

When you have the pins pressed into the board and have the TX, RX and GND pins connected to the CP2102, you need to press and hold the button on the switch and connect 3.3. When you do this the computer will beep and the switch will be in programming mode. Once it’s in programming mode you can let go of the button. IF the light on the switch is flashing it means it’s not in programming mode!

flashing tasmota to sonoff for mqtt

When the switch is in programming mode click the side arrow highlighted above and hopefully within 30 seconds you will get a green success message and you’re done!

Setting up MQTT and Sonoff in Home Assistant

As I said, if using MQTT you will need Home Assistant to control the switches. The eWeLink app will no longer work.

First, you need to add the MQTT broker information in configuration.yaml as per below:

  broker: core-mosquitto
  username: your-mqtt-user-name
  password: your-mqyy-password

In which I use, you also need to install the MQTT Broker from the Add-in Store. For other versions of Home Assistant, check the documentation for the MQQT Broker installation.

In, I changed the anonymous flag to false and entered the username and password as shown below. Leave the port as the default port 1883

  "plain": true,
  "ssl": false,
  "anonymous": false,
  "logins": [
      "username": "your-mqtt-user-name",
      "password": "your-mqtt-password"
  "customize": {
    "active": false,
    "folder": "mosquitto"
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"

Additionally in configuration.yaml, you need to configure the switches. Enter that information like this:

# Switches
  - platform: mqtt
    name: "Toothbrush"
    command_topic: "cmnd/sonoff1/power"
    state_topic: "stat/sonoff1/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
  - platform: mqtt
    name: "Coffee Maker"
    command_topic: "cmnd/sonoff2/power"
    state_topic: "stat/sonoff2/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

I have 2 Sonoff switches as you can see above flashed with Tasmota and using MQTT.

The only other thing I changed was setting a flag so that Home Assistant got the current state of the switch when it restarted. To do that there are a number of ways but perhaps the easiest is to load the =following page for each switch in a web browser:



You should also be able to browse to http://ip-address-of-switch in a web browser and get a Sonoff Tasmota menu.

Leave a Reply