I previously wrote a post on DIY Home automation with Node-Red, but the project was hosted in the cloud and the communications were dependent on the internet. So, I decided to completely rethink my design and came up with a practical solution that I use everyday.
Benefits of This Approach
Only surface rewiring will get the work done. You don’t need to replace your lights, fans and appliances. Any device that is already using an automation setup will still work with this setup.
A large amount of data generated from smart home devices can be misused if a third party server in some other country owns it. This solution respects your privacy by not sending your data anywhere. You own it.
Low cost hardware with open source and free software makes a great combination for cheap but highly reliable solution.
Security issues about smart CCTVs not using end-to-end encryption or sometimes no encryption (unbelievable) were recently in the news. So all data is stored locally.
Regular updates, security fixes for the software, thanks to the open source community that maintains these projects.
Easily replaceable and up-gradable hardware.
All communications between devices happen locally but the dashboard can be accessed remotely to control the devices from anywhere.
Network Map of the Setup
Any hardware that can run Home Assistant directly or using Docker containers. I am using a Raspberry Pi 4B.
Edge IoT devices like ESP8266, ESP32 or equivalent devices that can communicate via the MQTT protocol. I am using ESP-01, compatible relay boards and a compatible USB flasher
There are several ways to install Home assistant on a Raspberry Pi. But, I am installing it using Docker way because I have other self-hosted apps already running. So, installing the Home Assistant OS is not an option.
Install Docker on Raspberry Pi
I am using the convenience script.
Install and Run the Home Assistant Container
On Raspberry Pi 3
On Raspberry Pi 4
Tip: Use “docker ps -a” to see all your running containers and their status.
Setting up the Home Assistant
After your Home Assistant container shows a healthy status, visit the IP Address of your Pi followed by port number 8123 (for e.g. mine is http://192.168.0.119:8123).
Tip: You can also visit http://raspberrypi.local:8123 (works most of the time).
It will ask you to create an user account. It’s not an online account. This account is just used to log in to Home Assistant.
After you create an account It will automatically detect If you have printers, smart lights, chromecast devices, smart speakers that are already setup. You can add them to your dashboard if you wish.
Setting up a MQTT Broker
MQTT is one of the best protocols used for home automation, as it allows two way communication, message quality control and more. Setting up a MQTT broker will allow us to control our smart devices from the Home Assistant and also see device status. I recommend reading MQTT for dummies get a basic understanding of the MQTT protocol.
I am installing the Eclipse Mosquitto MQTT broker (without docker).
Make mosquitto auto-start on boot
Connect the Home Assistant to the MQTT Broker
Home Assistant dashboard -> Configuration -> Integrations.
Add Integration -> Search for “MQTT” and complete the setup by entering the IP Address of your MQTT client and it’s port. If you are following this tutorial the IP Address will be 127.0.0.1 and the port will be 1883 (127.0.0.1:1883).
Setting up the Arduino IDE
Setting up the ESP-01
Use the following C++ program for the ESP-01 that helps it connect to your WiFi and the MQTT server. I have added useful tips as comments.
Tip: You can also consider installing Tasmota as it has many configuration options that can be set using it’s own easy to use web dashboard.
To flash the ESP-01 (to run the above code), the ESP USB flasher should be modified as described here. After this you can flash the ESP-01 with the above code and connect the circuit as shown below.
Adding a Switch on the Dashboard
As we are running our Home Assistant in a Docker container we have to ssh into the Home Assistant container by using the command and modify the configuration.yaml file.
This is an example config and you can add more devices and categories if you like. Extended configuration options are explained in the Home Assistant Docs.
Tip: Notice how the Home Assistant publishes command to the “bathroom/lights/com” and the ESP-01 is subscribed to the same topic.
Access your Home Assistant Remotely
I found that most people use Port forwarding and DDNS to access a resource outside a local network. But, I will go for a reverse proxy becasue I am too lazy to get an SSL certificate and also because I am behind a NAT.
Here are the some good reverse proxy providers to choose from:
- Nabu Casa - This method is recommended in the Home Assistant Docs.
- PageKite - I will use this for a demo.
Setting up Pagekite for remote access is fairly simple and can be done completely from the command line. Just use the commands below.
Once you confirm your details with pagekite, the proxy client will start running and you can now visit SUBDOMAIN-THAT-YOU-WANT.pagekite.me and access your Home Assistant over the Internet. This URL can be used when you sign in on the Mobile apps of Home Assistant.
Customize your Dashboard
To demonstrate customization I applied a custom theme using the configuration.yaml file.