Sonoff Wi-Fi smart switch

 

 

 

About

This device allows you to operate a mains appliance via Alexa or Google Assistant.

However, its firmware can be reflashed with ESPHome allowing Home Assistant to take full control of it on your local network, without need for the internet.

 
 

Safety Notice

⚠️ WARNING: This device switches AC mains power. Only attempt this if you're confident working with mains devices. Do not connect this to the mains supply whilst dismantled.

 

 

 

 


Parts needed

Sonoff Wi-Fi smart switch (as above)

Home assistant - with ESPHome installed

A USB to TTL module (with 3V option selected) to flash the new firmware.

This is the inside of the module. The unit does not come with the 4 way connector. I have soldered this in.

If you prefer, you could solder wires just for programming.

The connections marked up are what should be connected to the TTL port. Swap RX and TX at the USB device.

 
   

 

ESPHome device setup

Before you can flash the firmware, you need to setup a new device in ESPHome.

These instructions were wrote in May 2025. Home Assistant and ESPHome may have change since this.

Navigate to ESPHome Builder in Home Assistant.

Press + NEW DEVICE at the bottom right.

Click Continue.

Enter a name for the new device (this can be changed in the code later if needed)

Choose ESP8266. Its not this, but this is the nearest match. We will change it in the code later.

Click INSTALL

How do you want to install? We will be plugging into the computer running ESPHome Device Builder.

You can cancel now. We can't go any further yet.

 

In ESPHome Builder, find your new device, click edit.

 

ESPHome code



# You should have something similar to below:
###########################################################

esphome:
  name: my-sonoff
  friendly_name: my-sonoff

esp8266:
  board: esp01_1m      # make sure this matches

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "4hugG7G7GGUG6F7fyfiygf78fIFYf6fj="

ota:
  - platform: esphome
    password: "adc6757bd4c9fe87876d787ea97777ca"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "mysonoff Fallback Hotspot"
    password: "ac76e68fd76e"

captive_portal:

##############################################################
# Add the section below which makes the button, LED and relay
# usable in Home Assistant

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0  # Sonoff plug - buton is on GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: button_3
    on_press:
      then:
        - switch.toggle: relay

switch:
  - platform: gpio
    pin: 12
    name: "sonoff power button"
    id: relay
    # icon: "mdi:fridge"
    # unhash the below if you want the device to power on after a mains failure
    # restore_mode: ALWAYS_ON

status_led:
  pin:
    number: GPIO13
    inverted: yes   

   
 

Hold down the sonoff button whilst connecting the USB TTL to a USB port on the Home Assistant PC.

In Home Assistant - whist viewing the yaml code, press INSTALL at the top right.

 

 

Choose the one highlighted.

Choose your USB device.

After some waiting, you should eventually end up with sometihng like this:

 

 

At this point, as a quick test, unplug from the USB port, and plug it back in.

The LED on the Sonoff should start to flash.

Go back to the ESPHome Builder page, locate your device and click LOGS:

Choose "Plug into computer running ESPHome Dashboard"

Choose the USB device again.

You might see various information about the status of the device. You can press the button on the unit, this should show in the log.

 

 
   

By now, you should have an item waiting in Notifications.

Click this, New devices discovered > Check it out

Find the new item, click ADD

Thats it. The Sonoff device is now a tighly integrated part of Home Assistant. It does not use the internet to connect, just your own WiFi directly to Home Assistant.

 
   
   

 

← Back to Projects