This article will show you how to use the €15 power switch Sonoff Pow to connect an ordinary electric heater to your WiFi network. You’ll be able to control it with your phone, set timers and measure power consumption. Sonoff also supports Amazon Alexa, Google Assistant and Google Nest. No more getting up when you feel cosy and want to turn on the heating!

Optionally, you can integrate your smart heating to the home automation hub Home Assistant as described later.

### Parts and Materials

• electric heater
• Sonoff POW
• power cord with a free end (for testing the Sonoff)
• 0.5 m of 1.5 mm2 3 core wire (they’ll cut it for you at the hardware store so you don’t have to buy the whole spool)
• 2 pcs 3.5 mm self tapping screws (with wall plugs if needed)

### Tools

• wire stripper
• utility knife
• pencil
• electric drill
• screwdriver
• multimeter
• needle nose pliers for manipulating cables (optional)

#### Tools for Flashing Sonoff (optional)

• soldering iron
• USB to serial converter
• jumpers

### Check the Power Rating

Before we start, we need to check the power rating of our heater. It must be lower than the one of our Sonoff. Otherwise, we would be risking fire and electrocution.

Take a look at the Sonoff. Depending on which version you have, it should say something like Maxload: 250V 16A on the box. Use a multimeter to check the voltage of your wall outlet. There are plenty of tutorials on YouTube. In my country, we should get 230 V and I measured 232 V. That’s within the 250 V limit.

Now let’s check the current. It must be under 16 A. My heater has a sticker on the side which says 3000 W. But how do we know how many amps? We’ll use the power law P = IV (or power = current * voltage).

I = P/V = 3000 (W) / 230 (V) = 13,04 A < 16 A

We’re within the 16 A limit. The last thing we need to calculate to avoid serious danger is wire size. In the table of wire sizes we can see that for our 13 A current, a 16 AWG wire (1.31mm2) is sufficient. However, it will be safer to use the thicker 15 AWG (or 1.5mm2 for us Europeans).

Connect the power chord to the Sonoff. Follow the diagram on the box (highlighted in the picture below). L stands for line, N is neutral and E is earth ground. In my case, the wires are brown, blue and yellow-green respectively. The colours might vary in different areas.

Make sure that the Sonoff works by pairing it to the app. While you’re at it, have some fun. Explore the app, connect a lamp (with the power cord disconnected!), measure the power consumption, etc.

### Flashing Sonoff (optional)

This step is only required if you want integration with Home Assistant. Homekit and Google Home users can skip this as Sonoff supports both out of the box.

Home Assistant doesn’t support the original Sonoff firmware. Luckily, Sonoff devices are relatively easy to flash. I’ve flashed mine with an alternative firmware called Sonoff Tasmota. It is open-source and easy to integrate with Home Assistant. Some soldering and a USB-to-serial converter are required. There are tutorials on flashing the Sonoff available.

Never try to flash the device while connected to mains power.

If you decide to flash the firmware, make sure you test the device with the original firmware first.

### Wiring

We are going to connect the Sonoff according to the diagram on the box. For that, we’ll need a hole in the skirting board to run the cables through. First, place the Sonoff against the skirting board at the right angle. Mark the hole for the cables and cut it as in the picture below.

Drill holes in the wall and attach the Sonoff. Measure how much cable you’re going to need to connect the Sonoff to the socket. Leave some extra length and cut your 3 core wire. Remove a bit of the outer insulation and use pliers to pull out the three inner wires.

Before we start fiddling with your electrical wiring, put on your headlamp. Make sure your girlfriend doesn’t need the heating badly in case anything goes wrong. Take a picture of the original wiring. You might want to refer to it later.

Before you continue, turn off the circuit breaker to avoid injuries. Use a multimeter or a tester screwdriver to check that the power is off.

Connect the cables according to the diagram on the Sonoff. Remember you’ll need to screw the cover back to the Sonoff. It’s a tight fit so make sure your cables aren’t crossing at the point of contact.

Test your connections for continuity with a multimeter. A loose connection could cause a fire.

Put the skirting board and the socket cover back on and voilà! You have a smart heater. If you’d like to connect it to Home Assistant, keep reading.

## Integration with Home Assistant (optional)

Unlike Google Home and Apple Homekit, Home Assistant is highly customizable, local and open-source. I run my instance on a Raspberry Pi. Pick your hardware and install it according to these instructions.

I assume you’ve flashed your Sonoff with the alternative firmware Sonoff Tasmota. If not, go back to the section Flashing the Sonoff.

### MQTT

To connect Home Assistant to Tasmota, we’ll need an MQTT message broker. We’ll use Mosquitto as the embedded broker is deprecated. To set it up on Hassbian, follow Bruh’s video tutorial. Hass.io users, use the Mosquitto Addon.

You can publish MQTT messages from Home Assistant, from the command line (mosquitto_pub) or using a graphical client (MQTT.fx works for me on Mac). I’ll leave the choice to you.

### Configuring Tasmota

Configure WiFi and MQTT according to Tasmota’s documentation.

Set the topic setting to sonoff1. Your MQTT configuration should look like this:

Now you can send commands to Tasmota in the form cmnd/sonoff1/<command> <parameter>. Available commands are listed here. Let’s try one now:

cmnd/sonoff1/POWER ON

Subscribe to stat/sonoff1/RESULT for command results.

{ "POWER": "ON" }

### Adding switches to Home Assistant

To be able to control our heating from Home Assistant, we’ll create a software switch as shown in the picture above. Add the following to your configuration.yaml and restart Home Assistant.

switch:
- platform: mqtt
name: "Bedroom Heating"
state_topic: "stat/sonoff1/POWER"
command_topic: "cmnd/sonoff1/POWER"
optimistic: true
retain: true

### Dealing with Power Outages

My heaters (and boiler) have a special load control switch which turns off the power at certain times. In turn, I get a cheaper tariff but it also means my Sonoff might be offline when I want to control it. In cases like this, when we flip the switch, we don’t want to wait for confirmation from the Sonoff. In the config above, we use the optimistic option which makes the switch change states immediately.

After the Sonoff goes online again, we want it to pull the state instantly (not the next time we flip the switch). By setting retain to true, we make Home Assistant publish MQTT messages with the retain flag. It tells newly-subscribed clients this is the “last good state”. To make Tasmota pull the state after startup, publish the following MQTT commands:

cmnd/sonoff1/ButtonTopic 1
cmnd/sonoff1/ButtonRetain 1

Now you should be able to use the switch in Home Assistant even when your Sonoff is offline.

### Securing Sonoff Tasmota

Tasmota is incredibly easy to set up but the default settings are rather insecure. We’ll tweak them using Tasmota’s MQTT commands.

#### Secure the Web Server

Anyone in your local network could access the web interface and take complete control of the device. Set a password using the following command:

Disable the web server and enable it only for administrative purposes.

cmnd/sonoff1/WebServer 0

#### Disable AP mode

By default, Tasmota goes to the access point mode every time connection to WiFi is lost. So, when your router is offline, Tasmota serves its own unsecured WiFi network for everyone to join. Publish the following command to disable this behaviour (5 means wait).

cmnd/sonoff1/WifiConfig 5