ESPHome is a tool that allows to easily create custom firmware for ESP8266 and ESP32 boards.
On today’s video, I'm going to show you step by step how to use the ESPHome add-on
in Home Assistant to flash a custom firmware to a Sonoff basic switch.
We're also going to look at some of the available components that can be customized via ESPHome
so you can integrate them and easily control them via Home Assistant.
All right, so to flash a firmware to a Sonoff switch, you would need a couple of things.
A USB to TTL (Transistor-Transistor Logic) converter and also 4 male to female pin wires.
You can find links for those items in the description below.
Now, to be able to install the ESPHome add-on, you would first need to add the ESPHome repository.
So, open Home Assistant,
go to Hass.io, Add-on Store and then under Add new repository, add the following URL:
https://github.com/esphome/hassio.
Then, scroll to the bottom of the list, click on ESPHome and then click on Install.
If you have remote access set up for your Home Assistant, using DuckDNS, you would need
to enable SSL and also set up a port forwarding rule on your router for the ESPHome add-on.
So, scroll to the bottom and under Config, change the SSL from false to true.
Save the changes then access your router settings and set up the port forwarding rule for the
ESPHome add-on.
Because all routers are different, I wouldn’t be able to tell you exactly how to do this
step.
However, the configuration should be somewhat similar to this: (Show ports on router page).
After you have the port forwarding rule set up, go back to Home Assistant and start the
ESPHome add-on.
Give it a few seconds for it to load and then, click on Open Web UI.
A login window comes up where you would need to sign in using your Home Assistant credentials
to access the ESPHome Dashboard.
Now that the Add-on is installed, you can start creating the configuration file for
the Sonoff switch.
So, click on the Plus “+” icon on the right to add a new node.
Enter a unique name for the device.
Make sure that the name is all in lowercase and with no spaces.
The characters allowed are letters, number and instead of spaces, you can use underscore.
Under Device Type, you can select the device that you are going to be flashing from the
drop-down.
The Sonoff Basic has an ESP8266 chip so select Generic ESP8266.
Then enter your WiFi credentials and lastly, click on Submit.
OK, so the configuration file for the Sonoff switch is now created.
However, it only has some basic settings.
If you were to flash the firmware to the Sonoff switch now, it would just connect to the WiFi
and nothing else.
So, you would need to edit the config file and add a few more components like for example
the switch settings to control the relay to turn the Sonoff switch on and off.
Let’s go over some of the available settings that you can add.
Click on Edit to open the config file and enter the following at the bottom:
The binary_sensor will check for anytime the button on the Sonoff switch is pressed
The switch would trigger the relay on and off
The status_led is useful to show the status of a device.
Letting us know of any warning or errors The wifi_signal sensor monitors the received
signal strength The uptime sensor tracks the time (in seconds)
the device has been online The version text_sensor provides information
of the current version of the ESPHome firmware installed on the device
Ok, so click on Save and then click on Validate.
This will check that the configuration is set up correctly.
After it’s validated, click on the More Options icon on the right, and then click
on Compile.
When the process is done, click on Download Binary.
All right, so you installed the ESPHome add-on, created the configuration file, compiled it
and downloaded to the computer.
Now, to flash the firmware to the Sonoff Basic switch, you can use the ESPHome Flasher which
makes it super easy to flash the firmware from Windows or Mac.
Now open the case for the Sonoff switch and to be able to connect it to the computer,
use the USB to TTL Converter and the 4 male to female jumper wires.
Now, this is very important.
Make sure that you don’t have the Sonoff switch connected to an electrical outlet.
The only way the Sonoff switch should be getting power is from the 3.3v coming from the USB
converter.
Ok, starting on the USB to TTL converter, connect the female ends of the wires to GND,
TX, RX, and 3.3V. Then connect the male ends of the wires to the Sonoff switch but connect
the TX wire to the RX PIN hole and the RX wire to the TX PIN hole.
Now, press and hold the onboard button on the Sonoff switch, and connect the USB to
TTL converter to the computer.
Continue holding the onboard button for 3 to 5 seconds, so the switch goes into programming
mode.
Lunch the ESPHome Flasher tool.
Under Serial Port, you should now have the Sonoff switch coming up.
Then, for the Firmware, search and select the binary file that you downloaded from ESPHome.
Lastly, click on Flash and give it a couple of minutes for the process to complete.
When the process finishes, restart the Sonoff switch by only unplugging and replugging the
USB connection to the computer.
If the switch connected to the WiFi successfully it would then show up online in the ESPHome
dashboard.
Now, to use the Sonoff switch with Home Assistant, you’d need to integrate it.
So, in Home Assistant, go into Configuration and then, Integrations.
At the top, you should have the new ESPHome device discovered.
Click on Configure, and then Submit.
If the device was not discovered automatically, you can also manually add it.
Look for the ESPHome integration and then click on Configure.
Then enter the name_of_the_node and then .local at the end.
Or, if you set up a static IP address for the Sonoff switch, you can also enter the
assigned IP address for that device.
Lastly, click on Submit, and you should now have the Sonoff switch integrated.
Click on it and the options that you set up under the ESPHome config file, it would now
show up as available entities that you can use with Home Assistant.
All right, so the Sonoff switch has the ESPHome firmware installed, and it’s integrated
with Home Assistant.
Now, if you want to make updates to the firmware, you can edit the config file in the ESPHome
Dashboard and do an OTA (Over The Air) update.
For example, let’s make a change to the current firmware, Let’s say we want the
green LED on the Sonoff switch to either be on or off depending on the state of the relay.
So, if the switch is turned on, the LED would also be on and then off if the switch is turned
off.
Go to the ESPHome Dashboard and open the Sonoff switch config file and replace the current
switch and satus_led with the following:
Ok, so we set up an output that uses the GPIO13, which the PIN that the Sonoff switch uses
for the LED.
Then, we added a light component linked to the output that controls GPIO13.
Lastly, we set up 2 switched.
A GPIO switch that uses the GPIO12 which is the PIN used for the relay.
The other switch is a template that will turn on and off both the relay and the led at the
same time.
Now, for the binary_sensor, under on_press, you need to change the ID name for the switch.toogle
from “relay” to “relayandled”, which is the ID name that we set up for the template
switch on this example.
Click on Save and then Close.
Now, select Validate to check that the configuration is ok.
Lastly, click on Upload to compile the firmware and uploaded automatically to the Sonoff switch.
Once the OTA update finishes, you could then turn the switch on and off, and the LED would
also turn on and off depending on the state of the relay.
The last thing that I want to show you today is how to open the ESPHome Dashboard directly
in Home Assistant instead of another tab.
In your Home Assistant Config folder, create a new file named pane_iframe.yaml.
Then open the file and enter the following:
This will create an option on the sidebar of Home Assistant named ESPHome.
The URL is the Home Assistant local IP address, or if you set up remote access, then you would
enter the DuckDNS URL plus the ESPHome port at the end.
Save the file and then open the configuration.yaml file and enter the following:
panel_iframe: !include panel_iframe.yaml
Save the file and then restart Home Assistant to apply the changes.
When Home Assistant is back online, you would now have the ESPHome option on the sidebar,
and when you click on it, it will open the ESPHome dashboard directly in Home Assistant.
All right, guys that all I have for today.
What do you guys think about ESPHome?
Let me know in the comments below.
If you like this video, give it a thumbs up.
Don’t forget to subscribe if you haven’t done so already and I’ll see you in the
next video.