Karl’s Home Automation Project – Part 1: Home Assistant & YAML, MQTT, Sonoff, and Xmas Lights
Karl here. I am here to write about my home automation project. First thing I want to say is that I am very cost conscious and I don’t mind putting in extra effort into the setup of things to keep costs down. I did invest a lot of time and had to do a lot of reading to get my project going. It took while and I received a lot of groans from my wife while testing. I am still in the process of tweaking things.
I started watching a series of videos on YouTube from Bruh Automation. He introduced me to Home Assistant. It got me really excited. He uses a Raspberry Pi as a server but I already had a Wintel Pro CX-W8 Smart TV Box which I use as a server. I run 3 Minecraft Servers, Emby Server, iSpyConnect DVR (2 IP Cameras), Unifi wifi controller, and now MQTT Server, and Home Assistant. Below is screenshot of mostly idle.
If it weren’t for iSpy it would be around 5-10% most of the time. Emby transcoding is the only thing that is stressful and it is not used much. The reason I mention this is because after purchasing a Raspberry Pi with power supply and case, you are not far off from getting a z8300 box. Only downfall is dreaded Windows update auto reboot. I finally looked into it and disabled it. If you decide to use a Windows box, I would make sure you are running 64bit windows. One advantage to using a Raspberry Pi is there is an image on Home assistant with the basics pre-configured and just need to write it to an SD card.
I won’t go into too much detail on server side, as I installed Python, Mosquitto, and Home Assitant (I followed the guide on their site for Windows)
Python was a breeze to install and just ran the executable and went with defaults. I already had it installed for something else and I am running 3.5.2 64-bit. There are newer versions now. Mosquitto was the most difficult. I followed this guide but substituted Win32OpenSSL_Light-1_0_2j.exe approx 2MB. A k version is available now. Home assistant was easy and used pip.
It was a little before Christmas when I started researching home automation. I had been reading about these inexpensive Sonoff devices here on CNX and I found a project on Github for some custom firmware by arendst that enabled them to be controlled by MQTT. (While getting the link it looks like a new project has started with some additional features here). My wife really likes decorating for Xmas and we have 3 trees and lots of lights. She mentioned getting some timers and boom I had my opportunity and ordered them the same night. After receiving It took me a couple nights and I had a simple automation turning Xmas lights on and off at specific times and life was good. I got an extra one to play with until Xmas was over. I redeployed the rest around the house after Xmas.
I really had no idea what this was and it took me a while to grasp. You can use a cloud based MQTT if you would like, but I prefer to run my own. MQTT is a service that relays messages between devices. There are 2 main items topics and payloads. To be able to tell a switch to turn on you send payload “on” to a topic, for example, “cmnd/testbench/power”. The light turns on and it replys back to a topic “stat/testbench/POWER” confirming that the light is on and the message is received. Because we are sending “on” to the topic each device using MQTT will need its own topic. Topics are case sensitive. I made a batch file to subscribe to all topics for troubleshooting so I could monitor the messages. The # indicates all sub topics.
"C:\Program Files (x86)\mosquitto\mosquitto_sub" -t stat/# -t tele/# -t cmnd/# -t home/# -v"
I picked the Sonoff basic but there are also different varieties that add additional features which are supported by arendst software.
Arendst has been very active with this project and adding/tweaking daily. When I first flashed the device, I did find a defect and notified him and he had it fixed and uploaded within the hour. He has very detailed instructions on the Wiki. First step before flashing is soldering headers. (I link to bent headers…which I initially thought I made a mistake but turned out it was good. They are easy to straighten) A USB to TTL adapter is also needed to upload from Arduino IDE. I recommend one like this because it provides both 3.3 and 5V. After downloading and setting Arduino up, I only set my WiFi password and SSID in the sketch. After it boots the first time, it connects to your wireless network. Find the IP address in your router, and pop the IP address in your browser to finish the configuration. Set the MQTT server credentials and topic and your done. I never setup credentials on the MQTT server so it accepts any login. Finally after everything is programmed you need to connect it to mains. Beware do not connect mains while TTL is connected. I bought some extension cords locally. Cut them in half and stripped back a ¼ inch of the insulation. Extension cords use stranded wire so I tinned them with solder to avoid any stray strands from shorting out. Then I screwed them down on the terminals making sure polarity was correct.
YAML is unforgiving. It is the formatting that you configure Home Assistant in. A single space will stop Home Assistant from starting. Luckily on this last update if you restart Home Assistant through the browser it will test the configuration file before actually restarting. I purposefully put an extra space on line 54 to show it is easy to find any mistakes.
I also recommend Notepad++ for editing in windows. You can break your configuration down into different files but I like one. Notepad ++ allows you to collapse the parts you aren’t currently working on.
I recommend adding one thing at a time and restarting to make it easier to find errors. And making a copy of the last working config before adding more. In the config below there are 5 sonoff’s and an automation to turn the lights on and off at specific times. This is extremely basic. I also recommend setting up one new device and be conscious of naming. When you get your config working properly on your first new device I copy the config to a new blank text window and do a find/replace.
Below is the screen capture of collapsed parts, and and full config (minus personal info).
# Name of the location where Home Assistant is running
# Location required to calculate the time the sun rises and sets
# Impacts weather/sunrise data (altitude above sea level in meters)
# metric for Metric, imperial for Imperial
# Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
# Show links to resources in log and frontend
# Enables the frontend
# Uncomment this to add a password (recommended!)
# Turn on built in mqtt broker
# Checks for available updates
# Discover some devices automatically
# Allows you to issue voice commands from the frontend in enabled browsers
# Enables support for tracking state changes over time.
# View all events in a logbook
# Track the sun
# Weather Prediction
- alias: Living Room
name: "Living Room"
- alias: Karson
- alias: Master Bedroom
name: "Master Bedroom"
- alias: McKayla
- alias: Test Bench
name: "Test Bench"
- alias: Lights On
- alias: Lights Off
Notice the test bench is on later firmware and the MQTT topic is slightly different
So now I have a smart home, right? Not in my opinion. I can turn lights on and off with a schedule or with my smart phone or at the light by pressing the button on the Sonoff. To me this is not smart. Setting a schedule is OK, but then you have the lights on unnecessarily and wasting electricity. Only real option is to press a button on the Sonoff but what difference is that than flipping a switch. Taking your phone out takes way too long, and I feel like it is going backwards. Below are estimated costs so far. By far the Windows Box will be the most expensive part if you choose to go that way. You can re-purpose just about anything that runs Linux to be a server. One other option is to run Linux on an S905x.
Cost of server not included nor shipping.
|USB to TTL||1||$2.54||$2.54|
If you find this entertaining or want me to go more in depth on a specific aspect let me know in the comments. I have been finding my time setting it up very satisfying. I am able to do some hardware and software work. I hope this might get your interest in home automation going, and find out it is not hard nor expensive. I would like to state none of the products linked were provided by the sites. I purchased with my own money.
The plan right now is to do a 3 part post. In the next post, we will integrate some inexpensive motion sensors and door sensors using 433mhz, then finally modifying the sensors to include a light intensity sensor.
Continue reading “Part 2: 433 MHz / WiFi MQTT Bridge, Door & PIR Motion Sensors“.