The Sonoff Basic is an inexpensive wi-fi switch. This post shows how to control the Sonoff with the ESPHome component. The ESPHome firmware also allows me to control a host of other ESP8266/ESP32-based devices, sensors and switches directly from the Home Assistant dashboard.
Flashing ESPHome on to the Sonoff requires taking the device out of its plastic casing and connecting it to the PC USB port using an USB-to-serial interface such as the FTDI. Since I don’t have an FTDI interface, I use an Arduino UNO instead. Here’s how to connect it up as a USB-to-serial interface:

- Remove ATMega chip from UNO
- Sonoff GDN –> UNO GND (black)
- Sonoff TX –> UNO TX (orange)
- Sonoff RX –> UNO RX (white)
- Sonoff 3V3 –> UNO 3V3 (red)
- Install ESPHome component in Home Assistant:
- click Hass.io on Home Assistant main panel
- click Add-on Store tab and enter https://github.com/esphome/hassio to Add new repository by URL
- scroll to the bottom of the page, select ESPHome then Install
- start ESPHome add-on then click on Open Web-UI, when prompted enter your Home Assistant credentials to log on
- add a new device with the following settings:
Device name: sonoff_1
Device type: Generic 8266
Wifi SSID: "your wifi SSID"
Wifi password: "your wifi password"
- I configured the Sonoff button to toggle the relay on or off, with the built-in LED indicating the relay status. Edit the configuration yaml file as follow:
# Configure Sonoff button (GPIO0) to toggle the relay and LED
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
name: "sonoff_1 Button"
on_press:
- switch.toggle: relayandled
# Define Sonoff LED (GPIO13) as output
output:
- platform: esp8266_pwm
id: sonoff_1_led
pin:
number: GPIO13
inverted: True
# Define light and tie it to Sonoff LED
light:
- platform: monochromatic
name: "sonoff_1 LED"
output: sonoff_1_led
id: led
switch:
- platform: gpio
pin: GPIO12
id: relay
# Template to turn relay and LED on/off together
- platform: template
name: "sonoff_1 Switch"
optimistic: true
id: relayandled
turn_on_action:
- switch.turn_on: relay
- light.turn_on: led
turn_off_action:
- switch.turn_off: relay
- light.turn_off: led
- Save the yaml file and back on the main page, click Validate. If everything checks out, click the three dots and Compile. After the compilation, save the bin file on the local drive.
- Flash ESPHome firmware:
- download and install ESPHome Flasher for Windows or Mac from the project release page
- press and hold the Sonoff button while connecting the Arduino UNO to the computer USB port. Continue to hold the button for 5 more seconds, then release launch ESPHome Flasher. The correct serial port should automatically be selected
- select the saved firmware file and click “Flash ESP”
- when flashing completes, the online color dot turns green indicating that the device is online
- To add the device to Home Assistant, go to Configuration -> Integration and click Configure ESPHome: sonoff_1. The Sonoff switch will show up in Home Assistant with entity ID switch.sonoff_1_switch.