Create simple home automation artefacts using ESP modules and HomeAssistant

Anuradha Wickramarachchi
An Idea (by Ingenious Piece)
5 min readJan 26, 2021

--

Home automation can be expensive and complex. However, with the arrival of IoT platforms from vendors such as Tuya, Phillips, etc have made home automation a little simpler. However, the cost still remains a little higher and the customisations are far limited. Last but not least, none of the mentioned services comes for free. You are likely being charged with your privacy.

IoT platforms provide free of charge Alexa/Google Assistant integrations. Which means you’re either charged for several years at purchase or your data is more profitable for vendors.

Both HomeAssistant and ESPHome are Free and Open-source

Enough with motivation! Let’s try to make a simple door sensor. This example will be really handy if you have many windows, doors and buying smart attachments can be really expensive.

Essential Components

Home Assistant

HomeAssistant is a smart home platform which is free for everyone. It is secure and can be deployed at the premise. For the deployment, I am using a Raspberry Pi 4 device. You can read more about HomeAssistant for automation in the following articles.

In simple terms,

  1. Download HomeAssistant image from official page.
  2. Deploy in your favourite environment. VirtualBox is easy for beginners.
  3. Find it’s IP address from VirtualBox or by the router page (If it is running in a Raspberry Pi). If your OS supports MDNS, visit http://homeassistant.local:8123
  4. Wait for setup to complete and create an account. Then you’re good to go!
Image by Author

ESPHome Plugin

When you have a running HomeAssistant instance, setting up ESPHome is very simple. However, this is not essential. But in this article, we shall follow the HomeAssistant approach since it makes more sense and easily followed.

  1. Open Supervisor (if not visible, go to Userpage and enable Advanced mode).
  2. Search Add-ons for ESPHome under Supervisor tab
  3. Install and click add to sidebar using the toggle switch you’ll see!
Image by Author

Circuitry Needed

We need several components for our example. It is always advisable to let main currents be handled by the qualified professional. Hence, as the lighting source, I will be using a smart bulb running on Tuya IoT platform. We will be designing the door sensor by ourselves. This should usually cost around $25 for the bulb and $1–5 for the sensor. To build the sensor all we need is the following.

  1. ESP32/8266 board
  2. Pre-punched prototyping copper board
  3. Reed switch (I will be using a normally closed reed switch)
  4. 18650 battery (3.7v 2600mAh) and a holder
  5. Wires and solder
  6. Optional casing equipment such as acrylic sheets and spacers

Building the Circuit

Since we are building a basic binary sensor I will just show the breadboard diagram I prepared using Fritzing. Note that the Wemos D1 Mini has an input range of 2.8~5.5v in 5V pin (It will not tolerate 5.5v unless mentioned in the datasheet). I used esp32 version of the exact board in the following diagram.

Circuit Diagram by Author

The above diagram is quite simple. Now let’s see how we can program this using ESPHome Add-on in Home Assistant.

Programming the ESP32

Open ESPHome Add-on from the supervisor. Then click on the big plus icon in the bottom right corner. Then follow the wizar. Don’t worry about the fields as you’ll later be able to edit them.

Screenshot by Author

Now you’ll see the list of devices you have created. Click on edit and you should see a YAML editor. Fill it in as follows.

esphome:
name: door_sensor
platform: ESP32
board: esp-wrover-kit
wifi:
ssid: YOUR_WIFI_SSID
password: YOUR_WIFI_PASSWORD
# Enable fallback hotspot in case wifi connection fails
ap:
ssid: "Door Sensor Fallback Hotspot"
password: "some random generated text"
captive_portal:# Enable logging
logger:
# Enable Home Assistant API
api:
password: YOUR_PASSWORD
ota:
password: YOUR_PASSWORD

binary_sensor:
- platform: gpio
pin:
number: 4
mode: INPUT_PULLDOWN
inverted: True
name: "Main Door"
device_class: door
filters:
- delayed_on: 10ms

Flashing the Program

In my case, the raspberry pi is running Home Assistant and I do not wish to connect anything via USB. It is far located. So the convenient approach will be to compile it on Home Assistant from UI. Then flash it to the board using my laptop. Here’s how you’d do it.

  • Compile from the web UI.
Image by Author
  • Download and flash it to the board using ESPHome flasher tool.

You can download the tool here.

Image by Author

Now everything is ready! You will see a new sensor detected by the Home Assistant. Add it to the UI.

All you saw was some circuit diagram. However, since I wanted this in action I made the following setup. Works fine, yet ugly! The yellow and orange wires extending from the board goes to the reed switch.

Image by Author

Now, whenever you open and close the door you will see that in Home Assistant. You can use Node-red to create automation. Well, that's for a future article!

Have a good one! Cheers. :)

--

--