Sonoff and Home Assistant (Best $5 light automation)

by | Feb 15, 2018 | Home assistant, Smart Devices

Sonoff and Home Assistant are made for each other – both practical and reliable. Especially if you use customer firmware. This is a detailed guide how to make them work together.

Sonoff in itself is a great outlet and can be use independently with the useful app. But to unlock its true potential, you can flash custom firmware and connect it to your Home Assistant configuration.

Hardware Prerequisites:

  1. Sonoff outlet itself (£5 – £8)
  2. FTDI to upload firmware – this is to upload firmware
  3. Mini usb cable – to connect to FTDI
  4. Extension lead – you can use existing lead if you have it around
  5. Connectors – to connect FTDI to Sonoff
  6. Windows PC

1. Install and configure Arduino IDE

  • Arduino IDE is a tool that will allow you to modify and upload firmware to Sonoff

a) Download and install the latest version of Arduino IDE. (v. 1.8.5 as of 11/02/18)

b) Go to Arduino IDE folder and create “portable” folder

c) Launch Arduino IDE by opening “arduino.exe” file, go to preferences and paste the link below to “board manager url”

http://arduino.esp8266.com/versions/2.4.0/package_esp8266com_index.json

d) Now we need to install board manager. Go to “Tools” and board manager and type “esp82”. This will give you just one option which you need to install

e) Next, select the options as per image on the right in “Tools” menu for Board, Flash Mode, Flash Size, Debug port, Debug Level, IwIP Variant, Reset Method, Crystal Frequency, Flash Frequency, CPU Frequency, Upload Speed.

f) Replace in Arduino IDE folder portable\packages\esp8266\hardware\esp8266\2.3.0 file platform.txt with the Tasmota Source code file arduino\version 2.4.0\platform.txt

g) Copy all files from the Tasmota release Source code folder lib into your sketchbook\libraries folder

2. Download and customise Sonoff Firmware

a) Download Sonoff-Tasmota firmware

b) In Arduino IDE go to File > Open and open “sonoff.ino” file in “sonoff” folder of the firmware files you just extracted

c) Open tab “user_config.h” and enter your wifi network name and password

c) Configure MQTT Server

  • This is how your Sonoff will communicate with Home Assistant. If you don’t have this installed (it takes 5 min) follow this guide from Ben
  • In the same “user_config.h” file enter the IP address, port, username and password for MQTT server

 

c) Then you can disable some of the features so you have more space in your Sonoff (can be pain sometimes). In my case I disabled DOMOTICZ, USE_WS2812 and USE_IR_HVAC

c) Check if it works by pressing a checkmark on the top left corner in Arduino IDE

c) You should see “Done compiling” in your screen below. Do not worry if any error messages, just check that you have done all the steps above

2. Time to flash!

Now focus! This might not be very easy to start with, but once done you will be very proud of yourself.

Disconnect from Mains!

It is very important that you disconnect your Sonoff from main. It can shock you if you did not do that!

a) Unpack your Sonoff and open up the box to expose for connection with FDTI

a) Connect the wiring between your programmer (FTDI) and Sonoff with your connectors

  • See picture on the right of how to find the hole in Sonoff and connection with FTDI
  • On FTDI the correct holes will be annotated
  • Notice that you need to cross connect TX and RX cables

a) Holding the button pressed, connect the USB cable. If all done correct, there should be a quick LED light blink in FTDI.

d) Make sure that you selected the correct COM port (it should be appearing and disappearing once you insert the FTDI from your PC)

a) Press Upload button in Arduino IDE

a) Now enjoy as the LED will be blinking on FDTI and at the bottom of Arduino IDE you will see Upload Complete message

a) Once flashed, the LED on Sonoff should flash green once like in my video. Big respects if you managed to do it in the first go (took me at least 10 attempts)

3. Customise Shit out of it!

Well done on following all the steps! The last part left – you need to connect it to wifi and integrate with your Home Assistant configuration.

a) Connecting to wifi

  • If you entered your correct Wifi SSID and Password your sonoff will connect automatically
  • Alternatively, you can use WPS to connect to wifi. To do it, press “WPS” on your router and press the button on outlet 5 times after you plugged it in. That is it.

b) Find sonoff IP address

  • This is only needed if you want to change any config for your Sonoff
  • Go to your router admin panel (usually 192.168.1.1) and go to attached devices or something similar.
  • You will see one of the new devices which will have sonoff in its description
  • Alternatively you can use any of the ip scanners in your phone.

b) Access Sonoff Web-interface

  • Go to your favourite browser and enter the IP address you just identified
  • You will see the web interface where you can change settings including MQTT server details

b) Connect to Home assistant

  • Go to your configuration.yaml file and enter the following code:
switch: 
  - platform: mqtt
    name: "OneExtra"
    icon: mdi:camera
    command_topic: "cmnd/sonoff8/power"
    state_topic: "stat/sonoff8/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: trueextra

Now you have a new switch which you can turn on and off

- id: switch_off_cabinet_lights
  alias: 'switch off cabinet'
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d000121a8d5
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 7
      seconds: 0
  condition:
    condition: state
    entity_id: 'switch.oneextra'
    state: 'on'
  action:
    service: switch.turn_off
    entity_id: switch.oneextra

Was it helpful?

Did you use the tutorial? Have you noticed any errors? Can we improve?

Please add your comments below so we can help you further or improve the content.