Home Automation Project: Automating Hydroponics with Linux
I have a black thumb. No, it's not due to a hammer-related incident, I just can't seem to grow anything in a garden — or even a pot. Throughout the years I've purchased hundreds (maybe thousands!) of dollars worth of seeds and gadgets to help me grow a garden, and every year, it falls apart.
Thankfully, this year I started gardening in the middle of winter. In Michigan. My only option was to use hydroponics in the basement, and my whole gardening experience changed. Hydroponic gardening is AWESOME!!!
My first hydroponics adventure was a deep water culture setup with an analog timer plug for timing the lights. (Honestly, the deep water culture setup is worthy of an article itself, but it's not really Linux related, just super cool!)
During the winter, this was perfect. It didn't require anything other than the power outlet, and apart from a 6-hour power outage messing up the timer, it worked flawlessly all winter.
* Move hydroponics to backyard (south side of house)
* Build greenhouse to protect lights from rain
* Time lighting to supplement sunshine
The last bit is where it gets complicated. My crops (basil, and tomatoes) do well with about 16 hours of light per day. The thing is, we don't get that much sun here in Michigan, even in the middle of summer. The plants will do well with less sun, but like Mark Watney, I plan to science the heck out of my garden! I could try to use my analog timer to estimate the sunrise and sunset times, but that changes every day, so I need something smarter. That's where home automation comes in. Unfortunately, home automation often comes with some unsettling features.
Kvetching About Clouds
I have a SmartThings hub in my house. I have Amazon Alexas in my house. I have Google Home devices in my house. I even have a Jibo in my house. Unfortunately, every single one of those devices depends on a solid internet connection and a constant data stream back and forth to the cloud. That bothers me on several fronts. One, the security concern about personal data going to and from 3rd-party companies. But also, if my internet connection goes down, my smart house gets really dumb, really quick.
From a technologist's standpoint, the security and privacy aspect should be the most bothersome. And I admit, it does bother me. But having a smart home go dumb when the Internet goes down is surprisingly disruptive. So, either problem is a valid reason to question traditional home automation.
I really don't want my plants to die because the Internet goes down, so I searched for an option that will do all the automation locally. And thanks to a recommendation from my friend Adam, I found it. Home Assistant is a Linux-based home automation platform that runs locally. And while the learning curve is frustrating, once you start with it, you'll likely never go back!
Meet Home Assistant
Home Assistant is a software package that integrates with a wide variety of home automation devices. It will also integrate with cloud devices like Amazon Echo and Google Home, but it doesn't depend on cloud services in order to function. In fact, it doesn't need an internet connection at all. Granted, it's nice to have an internet connection for installing updates and such — but all the automation is done in house. On a Raspberry Pi running Linux. Awesome.
It's possible to install Home Assistant on any Linux machine, but they've been focusing on creating a distribution called hass.io, which handles system updates — and includes a fancy web-based GUI that allows for configuration of the automation platform. It's important to keep in mind that the GUI is still maturing, but it's very usable. I wanted to get the full experience with Home Assistant, so I followed the installation directions and created a MicroSD card with the OS on it, and popped it into one of the Raspberry Pi devices I had sitting around my office.
I have to admit, from the moment I powered on the Pi after inserting the freshly imaged SD card — I fell in love. It seems like a minor thing, but I love not needing to hook up a keyboard and monitor to my Pi, even temporarily, to configure it.
I plugged in an ethernet cable, powered on the Pi, and then went back to my computer and opened a browser to http://hassio.local:8123 — and was met with this screen.
Setting up the system was painless. Just click next a few times, create your user account, and that's it. My only advice is to remember how awesome the installation is, because later on there will be some frustrations that seem unwarranted.
Wave Goodbye to Z-Wave
Z-Wave is a really neat protocol that uses the 900mhz frequency range (fairly unused now that landline cordless phones have faded from fashion). Many home automation devices rely on Z-Wave to communicate, and so I purchased a Z-Wave USB adapter so I could add the functionality to my new Home Assistant server.
I plugged in the USB device (An Aeotec Gen5 Z-Stick), and Home Assistant discovered it on reboot. I had a cool plug-in outlet I no longer use, and planned to connect it to my new Z-Wave enabled Home Assistant server, only to discover that my nifty plug was Zigbee, not Z-Wave.
Zigbee is a similar, yet incompatible home automation network protocol. To add insult to injury, no store within 100 miles had a Z-Wave plug in stock, so I had to come up with another solution. Mainly because I'm too impatient to wait for an Amazon delivery, but also because Z-Wave plugs are close to $50 each.
Thankfully, Wemo is a popular wireless system that uses traditional Wi-Fi to connect. Their devices can be controlled via smartphone app, but also happen to be compatible with Home Assistant. The best part? At my local Home Depot, I got a plug-in outlet and a wall mounted switch, both for $30!
They don't require a special hub, and while they can be controlled via the cloud, they don't require that feature to be enabled, so they can function completely locally. I'm a little curious if any data goes out to the cloud when that feature is disabled, but at the very least it will function locally, and it's compatible with Home Assistant. Sorta.
When I configured the Wemo Mini Smart Plug using the Wemo app on my Google Pixel 2, Home Assistant informed me it found Wemo devices on my network. So cool!
Unfortunately, while Home Assistant discovered Wemo on my network, it didn't find any devices at all. I spent hours trying to figure out if maybe the plug itself wasn't compatible, or if I needed to add specific driver information somewhere.
It turns out the solution was simple and very frustrating. I had to reboot the Home Assistant server. Three. Times. Then it happily found the smart plug, which I'd named Hydroponics Plug in Wemo.
Why did it require three reboots? I have no idea. I'm not the only person to experience the weirdness though, so when in doubt, the old "turn if off and back on" seems to work. At least if you do it three times.
First off, I should note that while Home Assistant has some rough edges with things like detecting Wemo devices, and the complexity of setting up automations (you'll see below), I plan to fully transition my home away from SmartThings. The local control, Open Source platform, and nerdy roll-you-own aspect make for the perfect system. Basically, don't let my initial struggles stop you, Home Assistant is worth the hassle.
My first use case was to set up that lighting schedule I mentioned above. Yes, I clicked the fancy sliders on and off over and over because it's cool to hear the plug click-in response, but after my playing, I wanted to get my lighting to work automatically as the summer progressed.
I need my plants to have light/sun from 7 a.m. to 11 p.m.. In the winter, I just timed my basement grow lights for the entire time. Now, I wanted the lights to come on at 7 a.m., but then turn off when the sun comes up. Then, when the sun goes down, I want the lights to come back on until 11 p.m. It will save electricity, plus it's just a neat project since the sun rises and sets at a different time every day!
First, I set up the 7 a.m. and 11 p.m. triggers. There's a screen in the GUI under Preferences/Automations that allows you to create an automation task. The tasks have three aspects: trigger, conditions, and action. The trigger and actions work like you'd expect. The conditions are optional, but allow you to create more complex rules.
I'm just using a trigger/action combo for my lighting. Here you can see my first automation, which is to turn the lights off at 11 p.m. The trigger part isn't too complicated, basically I chose time and then entered 23:00 for the time.
The action part is where things get dicey. Clearly, I want to turn the light off at 11 p.m. The option to call service was in the drop down menu, and made sense. So I chose that, and then from the drop down box of available services, switch.turn_off, seemed logical. But then, rather than a drop down list of devices, there's a text field where I had to write JSON formatted text describing the device!
On the screenshot below, you can see where I got the official name of the plug (devices are listed if you click on that link, but you better write it down, because you have to type it by hand!). Also, the JSON code requires you to know to type entity_id paired with the device name. Even after I figured out what to fill in there, I accidentally wrote entityid without the underscore, and spent an hour trying to figure out why my automation wouldn't work. Seriously, it's so close to awesome, but it feels like the person designing the automation creation tool gave up on the last field!
Once I figured out how to get a basic automation created, setting up the other actions was pretty straightforward.
For instance, this one here shows a trigger based on the sunset.
I decided to make the lights go on 15 minutes before sunset, because it's pretty dim around dusk, so the extra lights won't hurt. My total automations in order to set up the hydroponics summer lighting ended up being four.
In order to test them without waiting for the sun to set, if you click on the automation, it brings up a window that allows you to trigger the action to make sure it works like expected.
That handy triggering mechanism is what helped me figure out my typo with the JSON code.
For now, the simple lighting automation is enough to take advantage of the summer sun without losing hours of light for my hydroponics vegetables. As fall approaches, however, temperature will become an issue too. Thankfully, there are plenty of tools and automations that can help me eek the last bit of sunshine out of my backyard before moving everything into the basement for the long winter.
While I haven't purchased the sensor(s) yet, it will be pretty simple to configure a thermometer in the greenhouse that will trigger a space heater if the temperature falls too low overnight. Heck, I heated my office with a Bitcoin miner last winter. Perhaps that will be my space heater in the greenhouse once the temperature drops a bit.
I installed Home Assistant in order to solve a problem with my hydroponics system. Since installing it, however, I keep coming up with neat ways to automate my house that never seemed feasible with my previous home automation tools.
Once I completely transition to Home Assistant, I'll probably connect Alexa and Google Home into the mix so I can use voice control (although, there is some local voice control using Snips.AI available for Home Assistant I might check out). It will be refreshing to know that if my Internet connection goes down, the only thing that will stop working is the fancy proprietary addons, not the core system itself.
Are you doing home automation? I'd love to hear about problem solving you're doing in your own environment. Not only because I'm curious about such things, but because I have no problem building on the genius of others — it's the Open Source way!
While I haven't added any Z-Wave devices to my system yet, that doesn't mean it's not a great protocol. It can be confusing to install new devices, but Home Assistant has a great web interface for adding devices to your system. Sometimes it takes a couple tries, but once a device is adopted into your system, it usually works without a flaw. Plus, Z-Wave devices that are powered by wall current (as opposed to battery operated) also repeat the Z-Wave signal, which means they form a sort of grid network that allows you to expand your system far beyond the range covered by your USB stick.
Learn from my mistake, however, and if you use Z-Wave, be sure not to accidentally buy Zigbee devices! Or, you could always add Zigbee support to Home Assistant. Thanks to it being Open Source, there's a fairly straightforward way to do that too!
delivered to your inbox.