customise a Sonoff smart relay (esphome 3b)
‘own’ your own devices
THE FOLLOWING PROCESS IS STILL USEFUL – HOWEVER SONOFF DEVICES CAN WORK WITHOUT MODIFICATION IN HOME ASSISTANT (SEE HERE) AND THE EWElink app. THE ADVANTAGE OF THE FOLLOWING, WHERE YOU REPROGRAM A SMART DEVICE, IS TO GAIN MORE CONTROL
ESPhome can be used to reprogram a ESP chip so that Home Assistant can control it. I use it to re-program a Sonoff S20 smart plug or a Sonoff relay or a Sonoff light switch. There are nicely priced devices including the Sonoff RF Bridge that will control legacy 433MHz devices. Here are some examples:
- I control my hot water immersion heater with a Sonoff Pow2. I programmed the heater to turn off when the water was hot – instead of turn off at a certain time. I also programmed a ‘hot water boost’ feature where I could turn on the water and it would switch off as soon as the water was hot. With the help of graphs I had a much better idea of the cost of heating water in this way.
- I plugged a printer upstairs into a Sonoff smart socket. The printer takes a few minutes to warm up so I’m able to print something without repeatedly going up and downstairs. Also, in case I forget, the smart socket is set to switch off every night.
program a Sonoff wifi smart plug and control it with Home Assistant and the ESPHome plug-in.
A Sonoff smart plug is a mains socket that you can turn on remotely like any smart plug. The procedure below replaces its firmware with new firmware so that you can use it in Home Assistant. The result is a huge hike in capability and convenience. The process is a bit scary the first time through – so watch others do it on the youtube below until you’re comfortable!
compile the ESPhome code into firmware
What we’re trying to do is to add ‘code’ to the ESP chip and replace what’s currently there. Copy and paste this code into the ESPhome interface and add your wifi password and network settings. The code adds a wifi signal meter for the helluvit. We can re-edit this code for future projects.
# Enter the name you want to use substitutions: devicename: sonoff_plug esphome: name: {devicename} platform: ESP8266 board: esp01_1m wifi: ssid: "YOUR_SSID" password: "YOUR WIFI PASSWORD" manual_ip: # Set this to the IP you want for the ESP static_ip: 192.168.1.111 # Set this to the IP address of the router. Often ends with .1 gateway: 192.168.1.1 # The subnet of the network. 255.255.255.0 works for most home networks. subnet: 255.255.255.0 # Enable useful feedback logger: # Enable a connection to the Home Assistant API api: password: "YOUR Home ASSISTANT API password" ota: password: "" # This esnsures that the button on the smart device works as a manual override binary_sensor: - platform: gpio pin: number: GPIO0 mode: INPUT_PULLUP inverted: True name: ${devicename} button on_press: switch.toggle: relay output: - platform: esp8266_pwm id: sonoff_led pin: number: GPIO13 inverted: True # This turns on an LED when the plug is active light: - platform: monochromatic name: ${devicename} LED output: sonoff_led id: led switch: - platform: gpio pin: GPIO12 id: relay - platform: template name: ${devicename} relay 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 # The following can be omitted - platform: restart name: ${devicename} restart sensor: - platform: wifi_signal name: ${devicename} wifi signal update_interval: 600s - platform: uptime name: ${devicename} uptime
‘flash’ (or ‘put’) the above code into a Sonoff device:
Here’s the project write up for the above steps – by Juan who deserves thanks for outlining the process efficiently: https://www.juanmtech.com/how-to-get-started-with-esphome-and-sonoff/