In this article...
Introduction
In a recent Smart RV blog post, I shared how and why we run Home Assistant as the smart hub in our RV. It's the brains of the operation, but out of the box it can't do much on its own - we need to connect some things to it.
Within the RV, there are broadly speaking two large categories of "things":
- Sensors - things that provide information to the brain
- Doers - things that the brain controls to "do" something
Today we're going to be talking about one of the simplest "doers" - a switch.
We're starting with simple switches that control the binary flow of current to a device - in other words, they can turn it on or off. No fancy dimming just yet - just power on or power off.
To keep things really simple, instead of starting with wall-mounted switches, we're going to start with plugs that can switch on or off - kind of like those timer plugs you can use to turn lights on or off at certain times, except these smart plugs will get their instructions from Home Assistant.

Smart Plugs
We need something that can talk to Home Assistant and turn its commands into physical actions. That means some type of communication and logic is necessary, and that means the plug also needs to have a brain.
Compared to Home Assistant, the brain on a smart plug can be pretty small. But it still needs powering. And smart plugs get their power from the same power supply they use to power the device.
Since most homes in the US have 120V outlets all over the place but no trace of 12V, most commercially-available smart plugs are designed to run on and control 120V appliances. That's why today, we're focusing on 120V not 12V.
That's not to say you can't control 12V appliances with Home Assistant - you absolutely can, and we do (I'll cover that in a later blog post)! But I wanted to start with the simplest possible approach, and this way is literally plug and play.
What to look for
When you're shopping for smart plugs, there are a few things I always look for:
- Current rating - can it handle the amount of current I'm planning to switch with it?
- Voltage rating - is it rated to run 120V appliances (as we use in the US)? There are often 220-240V versions, etc that will work elsewhere.
- Home Assistant integration - can it actually integrate with Home Assistant?
The last point depends first on whether Home Assistant has an integration built-on already. If it does, make sure it's a local integration and not a cloud integration - in other words, will it work even when you don't have internet access?
You also want to check if you have the right wireless hardware to talk to it. There are lots of different wireless communication systems for smart devices - WiFi, RF, Zigbee, Z-Wave, BLE and many, many more.
While each wireless protocol has its own strengths (power consumption, range, security, mesh networking, license-free, etc), we're going to start with WiFi to keep things simple. For reference, we've added more hardware to our Home Assistant machine so it now also uses Zigbee, Z-Wave and RF - but we'll save that for future blog posts!
So the bottom line is we want a smart switch that runs on 120V, can handle at least 15A (that's the rating of our AC breaker), communicates over WiFi and can integrate with Home Assistant.
Sonoff S31 & POW R2
Sonoff is a range of devices made by a company called Itead. They're relatively inexpensive (although not the absolute cheapest), and I've been impressed with the quality. Out of the box they work with Nest, Alexa, Google Home and IFTTT, but with a little work they can be hacked to work very effectively with Home Assistant.
The two devices I'd recommend looking at are the Sonoff S31 (I like to buy the 2-packs) and the Sonoff POW R2 (also available in 2-packs).

The Sonoff S31 is the simplest to use - you plug it into your outlet, and then plug your device into it. It can switch up to 15A.
By comparison, the Sonoff POW R2 can handle up to 16A at 120V, however rather than a plug, this needs to be wired in by splicing the power cord for your device and inserting this inline with it.
As well as switching the power on or off, both of these devices can provide energy monitoring - they have built-in sensors to measure the power, current and voltage, and all this information can be sent back to Home Assistant.
Out of the box, neither of these devices integrate directly with Home Assistant. That sounds like a big problem, but it isn't!
One of the reasons I (and many others) really like Sonoff devices is that they're designed to be hacked (in a good way) - internally they're based on an ESP8266! The key is to replace the original Itead firmware on the Sonoff device with something that can speak to Home Assistant - something, in fact, that we have full control over!
ESPHome
And that is where ESPHome comes in. ESPHome is an open source project that replaces the firmware on the Sonoff devices and allows them to communicate with Home Assistant. There are other projects that do similar things (including Tasmota that I used to use) but I've found ESPHome works best for us.
Flashing the device
The process of loading new firmware onto the Sonoff is called flashing. ESPHome supports over-the-air (OTA) flashing but to get ESPHome onto it in the first place we have to use a wired connection. Only basic tools are required - a small screwdriver, soldering iron and USB to TTL adapter.

A little while ago I wrote a separate blog post about how to wire up a Sonoff S31 for flashing, and how to flash Tasmota firmware onto it. We've since switched to ESPHome but the wiring is exactly the same. The ESPHome documentation explains how to install ESPHome and upload your firmware to the device.
Configuration
A big difference between Tasmota and ESPHome is how they manage configuration. With Tasmota, you flash the exact same firmware onto every device and then configure the settings on the device - e.g. through the web interface.
With ESPHome, the configuration is done using YAML files which are then used to create a specific firmware binary to upload to your device. I much prefer this approach as I can then store all my firmware in version control. And that's exactly what I have done - all our ESPHome configuration is in our agileoffgrid/esphome repository on GitHub. I've licensed it under MIT so you're free to do pretty much anything you want with it.
The easiest way to get started is to use the ESPHome wizard, or alternatively you can create a very simple firmware like the following:
esphome:
platform: ESP8266
board: esp01_1m
name: my-sonoff-s31
logger:
level: DEBUG
api:
reboot_timeout: 15min
ota:
wifi:
ssid: my_wifi_ssid
password: my_wifi_password
fast_connect: true
reboot_timeout: 300s
The config above is YAML, and whitespace is important so make sure it's indented correctly. Replace my_wifi_ssid
and my_wifi_password
with your own details and once you upload this firmware, your Sonoff S31 (or Sonoff POW R2) will connect to your WiFi and advertise itself to Home Assistant.
If you have auto discovery enabled on Home Assistant then you should get a notification to tell you that a new device has been discovered.
The empty ota:
section in the config is particularly clever. This tells ESPHome to configure your device for over-the-air (OTA) updates. This means you can disconnect the wires and as long as your WiFi information is correct, you can flash new firmware to the device over your WiFi.
Once you've disconnected the flashing wires and reassembled the Sonoff S31, you can plug it into a 120V outlet.
Home Assistant Integration
The configuration above is the bare minimum to load ESPHome onto your Sonoff S31. In fact, it's so simple that the switch doesn't do anything now! Let's get that working.
Switch
ESPHome works by embedding and configuring platforms which add new capabilities - we've already added platforms for WiFi, OTA updates, and logging.
To make the switch work, we're going to add two more platforms - a binary sensor to detect when the button on the S31 has been pressed, and a switch that will be controllable by Home Assistant to turn the relay on and off.
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: true
name: My Sonoff S31 Button
internal: true
id: button
on_press:
- switch.toggle: device_switch
switch:
- platform: gpio
name: My Sonoff S31
pin: GPIO12
id: device_switch
You'll probably want to change the names above to something more specific. We keep the Sonoff S31 connected to the appliance so I name them things like "Dehumidifier" or "Electric Heater", but you could also keep them plugged into the wall and name them "Bathroom Outlet", etc.
The binary sensor GPIO platform tells ESPHome to listen on GPIO (General Purpose Input Output) pin 0 which is the pin the button on the Sonoff S31 is connected to. When it detects the button has been pressed, it should toggle the switch.
The switch GPIO platform tells ESPHome to control a switch on GPIO pin 12 - this is the pin that the Sonoff S31 relay is connected to. This tiny bit of configuration not only lets the button on the Sonoff S31 control the relay again, but also exposes a switch entity to Home Assistant as well.
Energy Monitoring
As well as simple switching, the Sonoff S31 and Sonoff POW R2 have energy monitoring capabilities using a CSE7766 power monitoring chip. It's easy to enable this in ESPHome using its built-in cse7766 support - note you have to configure UART and disable logging.
logger:
baud_rate: 0
uart:
rx_pin: RX
baud_rate: 4800
sensor:
- platform: cse7766
update_interval: 1s
current:
name: My Sonoff S31 Current
id: current
voltage:
name: My Sonoff S31 Voltage
id: voltage
power:
name: My Sonoff S31 Power
id: power
Once you've recompiled and uploaded the firmware, you'll see some new sensors appears in Home Assistant for the current, voltage and power.
Status LED
The last thing to configure is the status LED. ESPHome has a built-in component for a Status LED - it's normally off but will blink slowly if there's something not quite right, and quickly if there's an error. In particular, it blinks slowly when WiFi isn't connected, so it's a good way to check everything is OK.
status_led:
pin: GPIO13
Configuring it is as simple as adding these lines to your configuration, recompiling and uploading.
Advanced Features
So far, our simple configuration for the Sonoff S31 has restored the basic functionality of the button and relay, exposed control (and state, including power, voltage and current) to Home Assistant and configured the status LED. This gives you full Home Assistant control over whatever device you plug into the Sonoff S31.
But can we do more? Of course! There are almost limitless possibilities, but here are a few ideas to get you started.
Power Mode
Sometimes the device you plug into the Sonoff S31 has different modes, and each one uses a different amount of power. For example, our portable electric heater has fan-only, low and high - each easily identifiable based on how much power it's consuming. While you could do this calculation in Home Assistant, I like to keep device logic as close to the device as possible, so I added this configuration to our electric heater's ESPHome configuration.
text_sensor:
- platform: template
name: ${friendly_name} Mode
lambda: |-
if (id(power).state < 2) {
return {"Off"};
} else if (id(power).state < 100) {
return {"Fan"};
} else if (id(power).state < 1000) {
return {"Low"};
} else {
return {"High"};
}
update_interval: 1s
This uses the lambda feature in ESPHome - a simple function that runs once per update_interval
and in this case returns a textual description of the current mode based on the power being consumed.
You may also notice the reference to ${friendly_name}
in the configuration above. This is an advanced feature called substitutions. We're running the bleeding edge version of ESPHome to get access to substitutions, packages and more - features that make our ESPHome config easier to manage.
Cumulative Energy Consumption
As well as looking at instantaneous power consumption, it's sometimes also useful to see cumulative energy consumption - how much energy did a device use in a given time period.
Again, this is logic that can be computed in Home Assistant, but ESPHome also offers an easy way to sum up the total daily energy consumption.
sensor:
- platform: total_daily_energy
name: ${friendly_name} Total Daily Energy
power_id: power
time:
- platform: sntp
id: sntp_time
To use this, we need to add another sensor and tell it where to find the power information - in our case, we set the id
to power
in our CSE7766 configuration. It also needs to know the current time (so it can reset at midnight), and in this case we can pull in the sntp
time component.
Automations
With your devices now exposed to Home Assistant, you can start to build all kinds of clever logic systems, often in the form of automations. Automations are a way of executing a particular flow based on triggers and conditions.
Schedulers
One of the simplest forms of automation is a schedule - for example, turning a device on or off at different times of day. Automations are configured in Home Assistant, and we use a custom component from the Home Assistant Community Store called Scheduler Card.

It works really well, and we use it to run all sorts of schedules. For example, it automatically turns the electric water heater on for 30 minutes each morning, turns the awning lights on from sunset until 9pm, and adjusts the thermostat during the day.
Thermostat
Last year we installed a smart thermostat in our RV that is connected to Home Assistant. It's been working really well!
But, the thermostat in the RV controls the furnace which is loud and burns propane - that's one of the reasons we often prefer our portable electric heater. It's much quieter and often cheaper to run.
Using Home Assistant's generic thermostat integration, I created a custom thermostat that uses the temperature sensor from the RV thermostat to control our portable electric heater.
- platform: generic_thermostat
name: Electric Heater
heater: switch.electric_heater
target_sensor: sensor.thermostat_temperature
This appears as two separate thermostats (our smart RV thermostat plus the electric heater's custom thermostat) in Home Assistant that we can control independently.

More Ideas
As we've integrated more and more devices and sensors into Home Assistant, the opportunities have multiplied. While switches may be one of the most basic components of the system, they can be incredibly powerful.
By using temperature and humidity data from both inside and outside our RV, Home Assistant will now automatically turn on the dehumidifier when there is a risk of condensation.
Home Assistant has knowledge about all aspects of our electrical system and can, for example, automatically switch the fridge from propane to AC when we're running off solar and our batteries are almost full. We used this to full effect earlier on this year, and it was able to save us money on propane on sunny days.
Similarly, since Home Assistant knows how much power we're using, it can automatically turn off devices to prevent us tripping the main breaker. I call this our "untrippable breaker".
Summary
My hope is that this blog post has covered the basics of getting a simple Sonoff S31 or Sonoff POW R2 up and running to control 120V devices. We'll build on this in future blog posts as we explore some of the more complex automations and controls that we've implemented.