Maker.io main logo

Smart Home Automation, Iteration 2: Home Assistant and ESPHome

2021-05-18 | By Nate_Larson

License: See Original Project

In the first iteration of automating the smart light switch and outlets in my home electronics lab, I pulled together a quick proof-of-concept using an ESP32 MicroMod and a SparkFun Qwiic PIR sensor breakout. The original method used Blynk and Tasker to trigger a Google Assistant routine. I began the project trying to figure out how I could use hardware to automatically trigger a Google Assistant routine, but I was struggling with trying to directly interface with it, and with time I began to wonder why I needed to involve Google at all.

I have several smart products throughout my house and creating interactions between the various ecosystems has always been an obstacle, so when I learned of an open-source home automation platform that ran on a Raspberry Pi, my interest piqued! I knew that to create a more robust solution in my quest to automate my home lab, setting up the Home Assistant platform would be my next course of action.

I decided to use the 2GB version of the Raspberry Pi 4 to run Home Assistant due to its ability to boot directly from a USB drive. Home Assistant logs a lot of data, and I’ve heard tell that all this disk activity can quickly exhaust the life of an SD card. I already had a SATA HDD sitting around unused that was salvaged from an old laptop. All I needed was a SATA to USB 3.0 adapter so I could also make use of the faster USB 3.0 ports featured on the Pi 4.

Adapter_1

Installation of Home Assistant was simply following the instructions here: https://www.home-assistant.io/installation/raspberrypi and installing onto my spare disk drive using the USB to SATA adapter just as one would an SD card. Once the drive was written with the Home Assistant operating system, I disconnected the drive from my laptop, plugged it into one of the USB 3.0 ports on the Pi, connected an ethernet cable to the Pi for the network connection, and plugged in the power adapter.

The software took a few moments to load on the first boot, but soon I was able to connect to the web interface at http://homeassistant.local:8123/ to begin the initial setup. If you are unable to find your installation using this hostname, try scanning your network for the IP address of your Pi using a program such as Angry IP Scanner and put that in your address bar followed by “:8123” which is the port for Home Assistant’s web server. Once connected, it was a simple matter of following the onboarding instructions to get the software setup and all my smart home devices connected to the system.

Setup_2

Once the Home Assistant platform was set up, I had to get to work integrating the PIR sensor. I connected the SEN-17373 SparkFun PIR breakout board to an ESP8266 SparkFun Thing for this project. To easily integrate this sensor with Home Assistant, I installed the ESPHome add-on from the Add-on Store within the Supervisor tab of Home Assistant.

Addon_3

Once ESPHome was installed, I started the add-on and opened the web UI. Clicking the plus button in the lower right corner opens the wizard to create a new node. I named this node occupancy_detector, selected SparkFun ESP8266 Thing for the device type, and provided the WIFI SSID and password along with a password for OTA updates within the wizard. With this, my node was created so I could begin specifying the code.

Home_4

Clicking edit opens the YAML code window for this node. The code for this node is quite simple, following the ESPHome PIR example; essentially, I just needed to tell the code which pin the sensor was connected to along with the device class that will match up with Home Assistant.

Assistant_5

Upon completion of the code, the YAML file was saved and closed. Typically, at this point, one would just validate their code, connect their board to one of the USB ports on the Pi and then click upload; however, since my Home Assistant Pi is now tucked away in my server closet, I needed to take a slightly different route. After validating the code, I compiled the code by clicking the three dots on the node.

Compile_6

When the code was finished compiling, I downloaded the binary, so I could load the file to the board. This process only needs to be done for the initial code, as any later changes can be sent via OTA updates.

Updates_7

With the binary file ready to load, I installed the ESPHome-flasher tool on my computer, and then, since the SparkFun Thing board does not have a USB data interface, I connected the board to my laptop using an FTDI Basic by following the hookup guide. Using the flasher tool, I selected the serial port of my board, chose the binary firmware file, and flashed the code.

Home Assistant detected the new device right away and provided notification of the new device found so I could configure it and check the logs for the node to verify it was working properly.

New_8

With this working, all that was left was to set up the automation that will trigger the devices in the room when motion is detected, and then turn them off when motion is no longer detected for a period of time. Since all the smart devices I was looking to control within the room are switch entities, I decided to modify the motion-activated light blueprint that is included by default with Home Assistant. These blueprints serve as a basic structure for automation, allowing the user to just specify the devices they want to control without needing to worry about code syntax, structure, or recreate the same code for multiple animations.

To modify the motion-activated light blueprint, I first installed the File editor add-on.

Install_9

Then, using the file editor add-on’s web UI, I navigated to /config/blueprints/automation/homeassistant/motion_light.yaml and copied all the text in the file. I then created a new file in the homeassistant directory, which I named motion_switch.yaml. I was then able to paste all the copied code into this new file, replace the word light with the word switch throughout the code as shown below, and save the file. While doing this, one must be mindful not to change any of the indentations, since, similar to Python, proper indentation is critical for functional YAML code.

Copy Code
blueprint:
name: Motion-activated Switch
description: Turn on a switch when motion is detected.
domain: automation
source_url: https://github.com/home-assistant/core/blob/dev/homeassistant/components/automation/blueprints/motion_light.yaml
input:
motion_entity:
name: Motion Sensor
selector:
entity:
domain: binary_sensor
device_class: motion
switch_target:
name: Switch
selector:
target:
entity:
domain: switch
no_motion_wait:
name: Wait time
description: Time to leave the switch on after last motion is detected.
default: 120
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds

# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent

trigger:
platform: state
entity_id: !input motion_entity
from: "off"
to: "on"

action:
- alias: "Turn on the switch"
service: switch.turn_on
target: !input switch_target
- alias: "Wait until there is no motion from device"
wait_for_trigger:
platform: state
entity_id: !input motion_entity
from: "on"
to: "off"
- alias: "Wait the number of seconds that has been set"
delay: !input no_motion_wait
- alias: "Turn off the switch"
service: switch.turn_off
target: !input switch_target

Anytime changes are made to a file in Home Assistant, it is best to check that the configuration is valid before doing anything else. Any error could prevent Home Assistant from booting. There is a built-in tool that will check the configuration within the server controls section. Once my configuration was validated, I clicked restart under the server management heading to ensure the new blueprint was loaded.

Blueprint_10

Once Home Assistant restarted, I went to the blueprints section under the configuration tab, found my new motion-activated switch blueprint, and clicked on the associated create automation text.

Home_11

Then, after naming my new automation and selecting my motion sensor entity, I selected the area I wanted to control. This will control any switch I assign to this area without having to select multiple devices or entities individually. With my area set, I only needed to set the time to wait after no motion is detected before the switches should be turned off. With that completed, I saved the automation.

Name_12

Now, whenever I walk into the room, the light and all the outlets that power my lab equipment turn on automatically. Then, a couple of minutes after I leave the room, everything turns off. So, I no longer need to worry about leaving my soldering iron on. Best of all, the PIR sensor used for this is sensitive enough that it can detect my fingers moving while I type, so I don’t need to worry about everything turning off while I am still in the room. I’m also finding Home Assistant to be a great resource for all sorts of home automation routines, and I like that all the data it logs is stored locally and securely within my own home. I expect that all my home automation projects, moving forward, will be integrated with Home Assistant.

制造商零件编号 WRL-13231
ESP8266 THING
SparkFun Electronics
¥150.59
Details
制造商零件编号 SEN-17373
SPARKFUN PIR BREAKOUT - 1UA (EKM
SparkFun Electronics
¥273.84
Details
制造商零件编号 ADP001
SATA HARD DRIVE TO USB ADAPTER
Pimoroni Ltd
¥67.16
Details
制造商零件编号 DEV-09873
BRIDGE USB 2.0 MODULE
SparkFun Electronics
¥137.97
Details
制造商零件编号 SC0448
RASPBERRY PI 4 CASE FAN
Raspberry Pi
¥42.13
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum