Skip to content

Wi-Fi Smart Plug with Energy Monitoring for Home Assistant

Automation based on power consumption

Immagine you have an item/process that draws power that you would like to monitor, receive a notification when it starts or stops, or calculate the amount of power consumed.  In this article, I’ll show you how you can use the Sonoff S31 smart plug to achieve any or all of those.  The process itself isn’t specific as it can be applied to anything that is powered by 120v.  The S31 shape allows you to have two units plugged into one receptacle as opposed to many existing ones on the market that cover up the second receptacle and they are rather inexpensive at < $20 per plug. 

Some examples:

    • Monitoring of clothes washer with notification of completion
    • Monitoring of dishwasher with notification of completion
    • Automating of your toaster/toaster oven to automatically cook your breakfast with notification of completion
    • Monitoring of modem (or other networking equipment) with automatic reset on loss of internet

What you’ll need:

Thanks for your support in using the Amazon referral links above.

 

What you should already have:

  • Home Assistant
  • MQTT Server

 

Disassembly

In order to flash the Sonoff, you need to disassemble the S31 to get access to the pads.  It’s not as easy as the tool-free design of the Sonoff Basic, but it’s pretty close as it requires removing a total of three screws.

    1. Here is the S31.  I used a wedge tool to pry off the gray cover.

  1. With the cover removed, the next step is to slide off the two corner beads.
    Sonoff S31 Corner beads removed
  2. This will uncover the three screws that need to be removed.
    Sonoff S31 being taken apart / disassembled for flashing Tasmota firmware
  3. Remove the screws
    Sonoff S31 being taken apart / disassembled for flashing Tasmota firmware
  4. Separate the cover from the plug.
  5. The business side of the S31.

 

Connecting to the Sonoff S31

Next up is wiring up to the S31.  The pads are already identified on the PCB, just ignore the extra ones:

 

Pin definition on my FTDI is DTR, RX, TX, VCC, CTS, GND.  In order to flash it, I connected only the four bold ones, be sure to connect the TX on the FTDI to the RX on S31 and the RX on the FTDI to the TX on S31.  In order to connect these, I first tried to hold the four wires in place while uploading.  This failed miserably.  After wasting 20 mins, I just soldered the Dupont jumpers right onto the pads in 30 seconds and went on my way.

 

 

Flashing Sonoff S31 with Tasmota Firmware

Now that your S31 is wired up, you can proceed with flashing the Sonoff.  If you’ve never flashed a Sonoff or any other device, I’d recommend checkout out some videos to familiarize yourself with the process.  Make sure to follow all of the configuration steps in Arduino IDE so that you don’t brick your device.

I use the user_config_override file to keep my settings as the Tasmota firmware gets updated.  Although I have found it necessary to rebuild this file due to compilation errors.  These are the items I configure, but ultimately it’s completely up to you:

  • STA_SSID1
  • STA_PASS1
  • WIFI_CONFIG_TOOL – WIFI_WAIT
  • SYS_LOG_HOST
  • MQTT_HOST
  • MQTT_USER
  • MQTT_PASS
  • MQTT_POWER_RETAIN – 1
  • MQTT_FULLTOPIC
  • MQTT_TOPIC
  • MQTT_CLIENT_ID
  • TELE_PERIOD – 60
  • HOME_ASSISTANT_DISCOVERY_ENABLE – 1
  • WEB_PASSWORD
  • FRIENDLY_NAME
  • NTP_SERVER1
  • APP_TIMEZONE
  • APP_LEDSTATE – LED_POWER_MQTTPUB
  • APP_POWERON_STATE – POWER_ALL_SAVED

Here is my UART in action. Super exciting…

 

Setting up Tasmota Firmware

Once it’s flashed, go to the IP address of the Sonoff.  The first step is to configure the module by changing it from Sonoff Basic to Sonoff S31 and hit Save.  If you’ve configured all of the settings before upload, you should be all set.

In order to pull your data into Home Assistant, you need to set up an MQTT sensor.  In your sensor.yaml file, I’ve added the following:

In order to get the data visible on my HA frontend, I’ve added this info to my groups.yaml file:

 

 

Now you can go wild setting up automations.  I recommend reading Phil’s post about washer automation for a great example.

Published inHome AssistantHome Automation

One Comment

  1. Grant Lewis Grant Lewis

    Hey, thanks for this write-up. I hope you don’t mind a couple of questions. I have my compiled Sonoff/Tasmota firmware in the Arduino IDE and have my four FTDI wires soldered to the S31 (TX to RX and RX to TX as you describe).

    1) A jumper shipped on my FTDI sets the voltage to either 5 or 3.3 Which do I use?
    2) Should the FTDI work out of the box on a Win7 PC? No driver installation necessary?
    3) I can’t find a clear explanation of the button presses, etc. that put the Sonoff into flash mode. For instance, do I continue holding down the button on the outlet during the flashing process or release?
    4) So far every attempt at flashing results in this warning: espcomm_send_command: wrong direction/command: 0x00 0x08, expected 0x01 0x08. Any ideas of what might be going on there?

    Thanks very much.

Leave a Reply

Your email address will not be published. Required fields are marked *