0% found this document useful (0 votes)
222 views

A Practical Guide To Home Automation Using Open Source Tools

Automation

Uploaded by

Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

A Practical Guide To Home Automation Using Open Source Tools

Automation

Uploaded by

Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Opensource.

com

A practical guide to
home automation using
open source tools
OPENSOURCE.COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ABOUT OPENSOURCE.COM

What is Opensource.com?

OPENSOURCE.COM publishes stories about creating,


adopting, and sharing open source
solutions. Visit Opensource.com to learn more about how the open source
way is improving technologies, education, business, government, health, law,
entertainment, humanitarian efforts, and more.

Submit a story idea: opensource.com/story

Email us: open@opensource.com

2 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ABOUT THE AUTHOR

STEVE OVENS

STEVE OVENS is a dedicated IT professional and Linux advocate.


Prior to joining Red Hat, he
spent several years in financial, automotive, and movie
industries. Steve currently works for Red Hat as
an OpenShift consultant and has certifications
ranging from the RHCA (in DevOps), to Ansible,
to Containerized Applications and more. He
spends a lot of time discussing technology
and writing tutorials on various technical
subjects with friends, family, and anyone
who is interested in listening.
Follow me at @linuxovens

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 3


CONTENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CHAPTERS

Why I use Home Assistant for open source home automation 5

Cloud control vs local control: What to choose for 7


your home automation

How to choose a wireless protocol for home automation 10

Set up Home Assistant to manage your open source 12


smart home

Integrate devices and add-ons into your home 15


automation setup

How to set up custom sensors in Home Assistant 19

Protect your Home Assistant with these backups 23

4 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . . . . . WHY I USE HOME ASSISTANT FOR OPEN SOURCE HOME AUTOMATION

Why I use Home Assistant


for open source home automation
Home automation can be a slippery slope. The right open source tools can get you on firmer footing.

HOME AUTOMATION is a slippery slope;


you have been
warned! In this multipart series, I will discuss home automa-
Self-Hosted [6] podcast), I discovered alternative firmware
projects, such as Tasmota [7] and ESPHome [8], available
for certain wireless chipsets. I’ll cover the different chipsets
tion using the open source project Home Assistant. This in- and protocols (Zigbee, WiFi, Z-Wave, and the like) in a fu-
troductory article will cover my journey to Home Assistant [1], ture article, but suffice it to say, you are not stuck buying
what the application does, and why it’s important. products that are dependent on the cloud. There are online
stores such as CloudFree [9] that sell devices pre-flashed
How my journey began with Tasmota. There are even companies like Shelly [10] that
Some time ago, when I set out on this journey, my goal was produce high-quality products with an optional cloud compo-
not lofty. I was solving a need. You see, I have a fairly sizable nent, but the buyer maintains local control.
homelab [2]. Nothing on the scale of some notable YouTu- Anyway, back to my story. I bought a plug, flashed the firm-
bers, but I have eight machines ranging from 16GB RAM all ware to run Tasmota, and remotely power-cycled my switch
the way up to 96GB. I have a Netgear 10G Ethernet switch on occasion, and that was the end of it, right? Well, if it was,
as the backbone of my networking infrastructure. However, I imagine I would not be writing this article. I currently have
I have a small problem. Every once in a while, this switch’s 43 Internet of Things (IoT) devices—from sensors I built to
state table fills up, and then it crashes, taking the network smart bulbs, smart LED strips, infrared blasters, and more.
with it. This is a known issue with this model (although it was Remember how I said home automation is a slippery slope?
not known to me ahead of time). The only way to resolve the Did I mention that I have had virtually no experience with
issue, without replacing the switch, is to power it off for a few soldering, electronic theory/repairs, or anything remotely relat-
seconds and then power it back on. ed to home automation? In fact, outside of Linux and related
This would not seem like a big deal. Especially because technology, I am one of the least “handy” people I know. Sure,
I live in an apartment, so I don’t even have to travel up or I can punch a hole in a wall or put some screws in a board,
down a flight of stairs to do this. However, I work for Red Hat but up to the age of 30, the
as a senior OpenShift consultant, which means I am often on only tools that I ever owned
the road (at least pre-COVID). I use my lab almost every day were a Dremel and some
for work-related activities, and my family uses the network as screwdrivers for installing
well for things like playing games, watching our Blu-ray col- computer components.
lection, etc. So, it’s a giant pain to have the network go down Why mention this? So
when I don’t have physical access. My solution was to get a you understand exactly
smart plug and join it to a completely different network with how far behind square one
nothing else on it, completely firewalled off from other equip- I was when I started. If I can
ment. If the Netgear switch needs to be rebooted, I should be take this on, so can you!
able to access the smart plug remotely to reboot the switch.
Centralized local
My journey away from the cloud control
Being privacy-conscious, perhaps bordering on the tinfoil hat Great, I have a ton of IoT
breed, I am immediately uncomfortable with a “cloud”-con- devices, and I can toggle
nected anything. We run Plex [3], Kodi [4], Nextcloud [5], things on and off from a
and a host of other services because, well, I am “that guy.” phone. For a while, I was
But I’m not “anti-cloud.” In fact, a large part of my day job satisfied with simply call-
is working with the big three: AWS, Google Compute, and ing up the Tasmota web
Azure. But when it comes to standing up services that I rely UI and using its sparse but
on, I have an almost irrational need to host things locally. functional controls.
After doing a significant amount of digging and speak- Even my wife, God bless
ing to my coworker Alex Kretzschmar (who also hosts the her willingness and patience (Steve Ovens, CC BY-SA 4.0)

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 5


WHY I USE HOME ASSISTANT FOR OPEN SOURCE HOME AUTOMATION. . . . . . . . . . . . . . . . . . . . . . . . .

while I constantly learned and attempted new things, got in bed? All of these data points are of limited use on their
used to using the Tasmota interface. After a while, as the own, but together, you can use them to set the brightness of
number of devices grew, this method of managing things be- a light at dusk, turn a fan on because it’s above 26ºC (80ºF)
came untenable and did not scale well at all. in the room, but don’t turn on Steve’s desk lamp because
I began to poke around to learn how other people were his side of the bed is occupied.
managing their “smart homes.” Let’s be honest; at this point, If you can imagine it, you can accomplish it using Home
it wasn’t a smart home as much as a small but growing num- Assistant and the right sensor inputs.
ber of lights we could remotely control.
The big open source projects in this area are openHAB [11] Sustainable and open source
and Home Assistant [12]. This series won’t compare them in One thing I like about Home Assistant is that it seems
any manner. Partly, it’s because I am not qualified for this, as to be reaching a critical mass of adoption from various
I have only been using Home Assistant for the last year or vendors. But more important to me is that the company
so, but also because for me to feel comfortable making such behind it, Nabu Casa [13], creates a focal point for the
a comparison, I would have to set up openHAB as a drop-in community. The company hired developers directly from
replacement and, frankly, I have too much invested in Home the community, and it does not penalize you if you don’t
Assistant to explore this avenue. pay a monthly fee. The revenue Nabu Casa generates
comes from value-added items, like being able to access
About Home Assistant your local Home Assistant easily and from anywhere in
You may be wondering what Home Assistant is and what it the world.
has to offer. Imagine a house that reacts to its inhabitants, like This is important because it means that there is a path
in sci-fi movies. Perhaps you have a couple of bedside lamps, forward for the project, and it has a sustainable income for
a TV, some floor lighting, a fan, and a few other gizmos in your future development. Unlike “open core” models, Home As-
bedroom. As you walk into the bedroom, the lights turn on. sistant is fully open source, which means that if the commu-
Big deal, you say; we have had this technology for some time. nity disagrees strongly enough with Nabu Casa’s direction,
Of course, you are right. But what if it’s late and your signifi- it could fork Home Assistant at its current state and take a
cant other is already in bed? You might be in for a rather cold divergent path. Because my family has chosen to make this
greeting if the lights come on as you enter the bedroom. Yikes! piece of software integral to how we interact with our devic-
What if, instead, when you walk into the room at night, es, that Home Assistant has a revenue stream and is fully
the floor LED lights turn on to a soft glow—enough so that open source are invaluable to me.
you can move around without killing yourself but not bright In the next article, I will discuss some of the common stan-
enough to wake a sleeping partner. You climb into bed, and dards for IoT devices as well as the benefits of local control.
a few minutes later, the floor lighting turns off. This is what Future articles will walk through the basics of installing Home
Home Assistant can do for you. Assistant, setting up essential add-ons (e.g., MQTT), Node-
“But wait!” I hear you screaming at your monitor. “I can RED, the Community Store, making backups, and more.
just ask Alexa or Google to turn off my lights for me.” You’re In the meantime, comment or tweet [14] at me if you would
correct again, of course. However, what happens when like to explore something specific in-depth. If I have used it,
these services suffer some sort of disruption? Also, you may I can talk about it.
need to consider how loudly you need to speak to trigger
the listening device. And remember that thing I said about Links
privacy? Do you really want these big companies to learn [1] https://www.home-assistant.io/
your habits? Even discarding the privacy issue, consider that [2]  https://opensource.com/article/19/3/home-lab
others may use your home as well. This means visitors need [3]  https://www.plex.tv/
to know what commands you have available for controlling [4]  https://opensource.com/article/19/1/manage-your-media-
your smart devices. kodi
Lots of “smart” products on the market can set timers, [5] https://nextcloud.com/
schedules, and scenes, but none of them can really react [6] https://selfhosted.show/
to any given situation. This is where a home automation hub [7] https://tasmota.github.io/docs/
like Home Assistant comes into play. [8] https://esphome.io/
At its core, Home Assistant is software that helps centralize [9] https://cloudfree.shop/
all of the sensors, gizmos, and gadgets you have in your home. [10] https://shelly.cloud/
With all of these products working together in concert, you can [11] https://www.openhab.org/
set all kinds of conditions that would not otherwise be possible. [12] https://opensource.com/article/18/3/smart-home-assistant
Has someone entered the room? Is it a certain time of the [13] https://www.nabucasa.com/
day? Is the room warmer than a certain value? Is someone [14] https://twitter.com/linuxovens

6 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . CLOUD CONTROL VS LOCAL CONTROL: WHAT TO CHOOSE FOR YOUR HOME AUTOMATION

Cloud control vs local control: What


to choose for your home automation
Cloud may be more convenient, but local control gives you more privacy and other options in your
Home Assistant ecosystem.

THERE ARE A LOT of factors to consider when


investing in a home automation
ecosystem. In my first article in this series, I explained why
I picked Home Assistant [1], and in this article, I’ll explain
some of the foundational issues and technologies in home
automation, which may influence how you approach and
configure your Internet of Things (IoT) devices.

Cloud connectivity
Most devices you can buy today are tied to some type of
cloud service. While the cloud brings a certain level of con-
venience, it also opens a host of problems. For starters,
there are privacy issues related to a company having access
to your personal habits—when you are home, what shows
you watch, what time you go to bed, etc. Although most peo-
ple are not as concerned about these issues as I am, privacy
should still be a consideration, even if it is a small one.
Cloud access also creates issues around being reliant on
something outside your control. In 2019, Sonos came under
fire for remotely bricking [2] older smart speakers. Speak-
ers usually continue to work for years after their warranty (Amazon screenshot by Steve Ovens, CC BY-SA 4.0)
ends; in fact, they usually function until they physically break.
There’s also the case of Automatic, which produced a cloud- Some of these decisions are purely financial, but there are
based car tracker. When it announced in May 2020 that it more than a few anecdotal cases where a company blocks a
would be shutting down [3] its services, it advised customers person’s access to a device they purchased simply because
to “please discard your adapter by following standard elec- they did not like what the user said [7] about them. How crazy
tronic recycling procedures.” is that?
Being dependent on a third-party provider for critical func- Another consideration with cloud connectivity is a de-
tionality can come back to bite you. IFTTT [4], a popular ser- vice’s responsivity if its signals must travel from your home
vice for programming events based on external conditions, to a cloud server (which may be halfway around the world)
recently altered its free plan’s terms and conditions [5] to and then back to the device. This can lead to a two-second
severely limit the number of events you can create—from (or more) delay on any action. For some people, this is not
an unlimited number to three. This is even though IFTTT a deal-breaker. For others, that delay is unbearable.
charges device manufacturers for certification with its sys- Finally, what happens if there is an internet outage? While
tem, which allows products like Meross smart bulbs [6] to most modern home internet connections are quite reliable,
proudly display their compatibility with IFTTT. they do happen. Some large [8], well-known cloud service

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 7


CLOUD CONTROL VS LOCAL CONTROL: WHAT TO CHOOSE FOR YOUR HOME AUTOMATION . . . . . . . .

providers [9] have experienced outages this year. Are you • Q


 oS 0: The message is sent to the broker in a fire-and-
OK trading convenience for possibly having your automa- forget way. No attempt is made to verify that the broker
tions break and losing control of your smart devices for pe- received the message.
riods of time?

Local control
There are several ways you can regain control over your
smart devices. Commercially, you could try something like
Hubitat [10], which is a proprietary platform that emphasizes
local control. I have no experience with these devices, as I
don’t like to rely on an intermediary.
In my home, I standardized on WiFi (although I may branch (© 2015 HiveMQ, reused with permission)
out to Zigbee [11] in the future) and Home Assistant [12]. Us-
ing WiFi means I need to buy or make my devices based on • Q
 oS 1: The message is posted, and the broker replies
their compatibility with alternative open source firmware, such once the message is received. Multiple messages can be
as Tasmota [13] or ESPHome [14]. I admit that neither of these sent before the broker replies. For example, if you are try-
options is “plug-and-play friendly” unless you buy devices from ing to raise the projector’s brightness, multiple brightness
sources like Shelly [15], which is very friendly to the community, bars may be inadvertantly adjusted before the broker tells
or CloudFree [16], which has Tasmota installed by default. the sender to stop publishing messages.
(As a small aside, I have both flashed my own devices and
purchased them from CloudFree. There are some savings
with the DIY approach, but I buy pre-flashed devices for my
father’s house because this eliminates a lot of hassle.)
I won’t go into more detail about alternative firmware, how
to flash it, and so on. I simply want to introduce you to the
idea that there are options for local control.

(© 2015 HiveMQ, reused with permission)


Achieving local control with MQTT
A local control device probably uses either a direct API [17] • Q
 oS 2: This is the slowest but safest level. It guarantees
call, where Home Assistant talks directly to the device, or that the message is received only once. Similar to TCP,
Message Queuing Telemetry Transport (MQTT) [18]. if a message is lost, the sender will resend the message.
MQTT is one of the most widely used protocols for local
IoT communication. I’ll share some of the basics, but the
Hook Up has an in-depth video [19] you can watch if you
want to learn more, and HiveMQ has an entire series [20] on
MQTT essentials.
MQTT uses three components for communication. The
first, the sender, is the component that triggers the action.
The second, the broker, is kind of like a bulletin board where
messages are posted. The final component is the device
(© 2015 HiveMQ, reused with permission)
that will perform the action. This process is called the pub-
lish-subscribe model. In addition, MQTT has a retain flag that can be enabled on
Say you have a button on the wall that you want to use the messages, but it is not set by default. Going back to the
to turn on the projector, lower the blinds, and turn on a fan. bulletin board analogy, it’s like if someone posts a message
The button (sender) posts the message ON to a specific sec- to a bulletin board, but another person walks up to the board,
tion of the broker, called a topic. The topic might be some- takes the message down, reads it, and throws it away. If a
thing like /livingroom/POWER. The fan, the projector, and third person looks at the bulletin board five minutes later,
the blinds subscribe to this topic. When the message ON is they would have no knowledge of the message. However,
posted to the topic, all of the devices activate their respective if the retain flag is set to true, it’s like leaving the message
functions, turning on the projector, lowering the blinds, and pinned on the board until a new message is received. This
starting the fan. means that no matter when people come to read messages,
Unlike a message board, messages have different Quality they will all know the latest message.
of Service (QoS) states. The HiveMQ website has a good In home automation terms, whether or not the retain flag
explanation of the three QoS levels [21]. In short: is set depends completely on the use case.

8 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . CLOUD CONTROL VS LOCAL CONTROL: WHAT TO CHOOSE FOR YOUR HOME AUTOMATION

In this series, I will use Home Assistant’s Mosquitto MQTT [5] https://ifttt.com/plans
broker [22] add-on. Most of my devices use MQTT; however, [6]  https://www.amazon.ca/meross-Dimmable-Equivalent-
I do have a couple of non-critical Tuya devices that require Compatible-Required/dp/B07WN2J3C7
a cloud account. I may replace them with locally controllable [7]  https://www.techrepublic.com/article/iot-company-bricks-
ones in the future. customers-device-after-negative-review/
[8]  https://www.theverge.com/2020/9/28/21492688/microsoft-
Wrapping up outlook-office-teams-azure-outage-down
Home Assistant is a large, wonderful piece of software. It is [9]  https://www.cnn.com/2020/08/30/tech/internet-outage-
complex in some areas, and it will help you to be familiar with cloudflare/index.html
these fundamental technologies when you need to trouble- [10] https://hubitat.com/
shoot and coordinate your setup. ]11] https://zigbeealliance.org/
In the next article, I will talk about the “big three” wireless [12] https://www.home-assistant.io/
protocols that you are likely to encounter in smart devices: [13] https://tasmota.github.io/docs/
Zigbee, Z-Wave, and WiFi. Don’t worry—I’m almost done [14] https://esphome.io/
with the underlying theories, and soon I’ll get on with install- [15] https://shelly.cloud/
ing Home Assistant. [16] https://cloudfree.shop/
[17] https://en.wikipedia.org/wiki/API
Links [18] https://en.wikipedia.org/wiki/MQTT
[1] https://opensource.com/article/20/11/home-assistant [19] https://www.youtube.com/watch?v=NjKK5ab0-Kk
[2]  https://www.bbc.com/news/technology-51768574 [20] https://www.hivemq.com/tags/mqtt-essentials/
[3]  https://www.cnet.com/roadshow/news/automatic- [21] https://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-
connected-car-service-dead-may-coronavirus/ quality-of-service-levels/
[4] https://ifttt.com/ [22] https://mosquitto.org/

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 9


HOW TO CHOOSE A WIRELESS PROTOCOL FOR HOME AUTOMATION . . . . . . . . . . . . . . . . . . . . . . . . . . .

How to choose a wireless protocol


for home automation
Which of the three dominant wireless protocols used in home automation—WiFi, Z-Wave, and
Zigbee—is right for you? Consider the options in part three of this series.

IN THE SECOND ARTICLE in this series, I talk-


ed about local control vs.
cloud connectivity [1] and some things to consider for your
Plug in the new device, launch a vendor-provided application
or a web browser, enter your credentials, and you’re done.
It’s important to note that almost all moderate- to low-
home automation setup. priced IoT devices use the 2.4GHz wireless spectrum. Why
In this third article, I will discuss the underlying technology does this matter? Well, 2.4GHz has been around so long
for connecting devices to Home Assistant [2], including the that virtually all devices—from cordless phones to smart
dominant protocols that smart devices use to communicate bulbs—use this spectrum. In most countries, there are gen-
and some things to think about before purchasing smart de- erally only about a dozen channels that off-the-shelf devices
vices. can broadcast and receive on. Like overloading a cell tower
when too many users attempt to make phone calls during
Connecting devices to Home Assistant an emergency, channels can become overcrowded and sus-
Many different devices work with Home Assistant. Some ceptible to outside interference.
connect through a cloud service, and others work by commu- While well-behaving smart devices use little-to-no band-
nicating with a central unit, such as a SmartThings Hub [3], width, if they struggle to send/receive messages due to over-
that Home Assistant communicates with. And still others have crowding on the spectrum, your automation will have mixed
a facility to communicate over your local network. results. A WiFi access point can only communicate with one
For a device to be truly useful, one of its key features must client at a time. That means the more devices you have on
be wireless connectivity. There are currently three dominant WiFi, the greater the chance that someone on the network
wireless protocols that smart devices use: WiFi, Z-Wave, will have to wait their turn to communicate.
and Zigbee. I’ll do a quick breakdown of each including their
pros and cons. Pros:
A note about wireless spectra: Spectra are measured • Ubiquitous
in hertz (Hz). A gigahertz (GHz) is 1 billion Hz. In general, • Tend to be inexpensive
the larger the number of Hz, the more data can be transmit- • Easy to set up
ted and the faster the connection. However, higher frequen- • Easy to extend the range
cies are more susceptible to interference and do not travel • Uses existing network
very well through solid objects. Lower frequencies can travel • Requires no hub
further and pass through solid objects more readily, but the
trade-off is they cannot send much data. Cons:
• Can suffer from interference from neighboring devices or
WiFi adjacent networks
WiFi [4] is the most widely known of the three standards. • Uses the most populated 2.4GHz spectrum
These devices are the easiest to get up and running if you • Your router limits the number of devices
are starting from scratch. This is because almost everyone • Uses more power, which means less or no battery-pow-
interested in home automation already has a WiFi router or ered devices
an access point. In fact, in most countries in the western • Has the potential to impact latency-sensitive activities like
world, WiFi is considered almost on the same level as run- gaming over WiFi
ning water; if you go to a hotel, you expect a clean, tem- • Most off-the-shelf products require an internet connection
perature-controlled room with a WiFi password provided at
check-in. Z-Wave
Therefore, Internet of Things (IoT) devices that use the Z-Wave [5] is a closed wireless protocol controlled and main-
WiFi protocol require no additional hardware to get started. tained by a company named Zensys. Because it is controlled

10 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . . . . . . . . HOW TO CHOOSE A WIRELESS PROTOCOL FOR HOME AUTOMATION

by a single entity, all devices are guaranteed to work togeth- employee, you won’t be able to understand each other. Both
er. There is one standard and one implementation. This of you can speak and hear each other, but the meaning will
means that you never have to worry about which device you be lost.
buy from which manufacturer; they will always work. Similarly, the Zigbee standard allows all devices on a Zig-
Z-Wave operates in the 0.9GHz spectrum, which means bee network to “hear” each other, but different implemen-
it has the largest range of the popular protocols. A central tations mean they may not “understand” each other. Fortu-
hub is required to coordinate all the devices on a Z-Wave nately, more often than not, your Zigbee devices should be
ecosystem. Z-Wave operates on a mesh network [6] to- able to interoperate. However, there is a non-trivial chance
pology, which means that every device acts as a poten- that your devices will not be able to understand each other.
tial repeater for other devices. In theory, this allows a When this happens, you may end up with multiple networks
much greater coverage area. Z-Wave limits the number that could interfere with each other.
of “hops” to 4. That means that, in order for a signal to Like Z-Wave, Zigbee employs a mesh network topology
get from a device to a hub, it can only travel through four but has no limit to the number of “hops” devices can use
devices. This could be a positive or a negative, depending to communicate with the hub. This, combined with some
on your perspective. tweaks to the standard, means that Zigbee theoretically can
On the one hand, it reduces the ecosystem’s maximum support more than 65,000 devices on a single network.
latency by preventing packets from traveling through a sig-
nificant number of devices before reaching the destination. Pros:
The more devices a signal must go through, the longer it can • Open standard
take for devices to become responsive. • Form mesh network
On the other hand, it means that you need to be more • Low-powered and can be battery powered
strategic about providing a good path from your network’s • Can support over 65,000 devices
extremities back to the hub. Remember, the lower frequen- • Can communicate faster than Z-Wave
cy that enables greater distance also limits the speed and
amount of data that can be transferred. This is currently not Cons:
an issue, but no one knows what size messages future smart • No guaranteed compatibility
devices will want to send. • Can form separate mesh networks that interfere with
each other
Pros: • Uses the oversaturated 2.4GHz spectrum
• Z
 -Wave compatibility guaranteed • Transmits only 10 to 30 meters
• F
 orm mesh network
• L
 ow powered and can be battery powered Pick your protocol
• M
 esh networks become more reliable with more devices Perhaps you already have some smart devices. Or maybe
• U
 ses 0.9GHz and can transmit up to 100 meters you are just starting to investigate your options. There is a lot
• L
 east likely of the three to have signal interference from to consider when you’re buying devices. Rather than focus-
solid objects or external sources ing on the lights, sensors, smart plugs, thermometers, and
the like, it’s perhaps more important to know which protocol
Cons: (WiFi, Z-Wave, or Zigbee) you want to use.
• C
 losed protocol Whew! I am finally done laying home automation ground-
• C
 osts the most work. In the next article, I will show you how to start the ini-
• M
 aximum of four hops in the mesh tial installation and configuration of a Home Assistant virtual
• C
 an support up to 230 devices per network machine.
• U
 ses 0.9GHz, which is the slowest of all protocols
Links
Zigbee [1]  https://opensource.com/article/20/11/cloud-vs-local-home-
Unlike Z-Wave, Zigbee [7] is an open standard. This can be automation
a pro or a con, depending on your perspective. Because it is [2] https://opensource.com/article/20/11/home-assistant
an open standard, manufacturers are free to alter the imple- [3]  https://www.smartthings.com/
mentation to suit their products. To borrow an analogy from [4] https://en.wikipedia.org/wiki/Wi-Fi
one of my favorite YouTube channels, The Hook Up [8], Zig- [5]  https://www.z-wave.com/
bee is like going through a restaurant drive-through. Having [6]  https://en.wikipedia.org/wiki/Mesh_networking
the same standard means you will always be able to speak [7] https://zigbeealliance.org/
to the restaurant and they will be able to hear you. Howev- [8] https://www.youtube.com/channel/
er, if you speak a different language than the drive-through UC2gyzKcHbYfqoXA5xbyGXtQ

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 11


SET UP HOME ASSISTANT TO MANAGE YOUR OPEN SOURCE SMART HOME . . . . . . . . . . . . . . . . . . . .

Set up Home Assistant to manage


your open source smart home
Learn how to install and configure Home Assistant in the fourth article in this series on home automation.

IN THE FIRST ARTICLE [1] in this series, I intro-


duced Home Assistant [2] and
why you might be interested in it. In short, Home Assistant
When you do this, make sure to change the firmware
to UEFI:

is an automation hub for some of the most common smart


devices on the market today. It enables centralized coordi-
nation of disparate hardware. By using it, you no longer have
to choose suboptimal tech from a single vendor to manage
your smart home from a single app. It also means you will no
longer struggle with a hundred different apps that all function
slightly differently to manage all your devices. One program
to rule them all… or at least that’s the dream.
In the second and third articles, I looked at some of the de-
cisions to make when developing home automation, namely
local vs. cloud control [3], and whether to choose Zigbee,
Z-Wave, or WiFi [4], just to hit the high points. This fourth ar-
ticle will be much more hands-on than the previous ones by
walking you through setting up a virtual machine (VM) with
the Home Assistant-provided image.

Set up the VM (Steve Ovens, CC BY-SA 4.0)


I won’t cover all of the methods available for installing Home
Assistant (HA). I run HA in a virtualized environment, and Important note: You cannot change the firmware type after
the official installation page [5] provides VMDK, VHDX, VDI, the VM has been created. If you choose BIOS, the VM will
QCOW2, and OVA downloads. I have a libvirt-based [6] not boot!
homelab, so I chose the QCOW2 image. If you need to expand the amount of disk available to HA’s
Regardless of which hypervisor you use, you need to VM, shut down the VM and run:
make sure that the boot type is set to UEFI instead of the
traditional BIOS. In Arch Linux or Fedora, you need to in- qemu-img resize hassos_ova-4.13.qcow2 +40G
stall the package edk2-ovmf to have the option available
in Virt-Manager [7]. In Ubuntu, the package is called ovmf. Upon boot, the new space will be automatically allocated to
After the package is installed, restart libvirt. the appropriate partitions.
When you create your VM, select the HA image you down- The first boot can take a significant amount of time, as HA
loaded. You can accept the default options that Virt-Manager pulls down the latest versions of software from the internet
selects until the confirmation screen. Make sure you check and prepares them for initial launch and configuration. To
the box that says Customize configuration before install. be on the safe side, walk away for 10 minutes or so before
attempting to pull up the webpage for the first time. In my
experience, it often requires less than five minutes, but 10
minutes is a good amount of time for the system to spin up
and settle.

First-time setup
You should now be able to access the HA interface by point-
ing a browser to http://homeassistant.local:8123. How-
ever, this relies on your router to support automatic DNS
registration. You can also access the webpage via its IP. In
(Steve Ovens, CC BY-SA 4.0) my case, that’s http://192.168.122.90:8123.

12 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . SET UP HOME ASSISTANT TO MANAGE YOUR OPEN SOURCE SMART HOME

after sunrise,” HA uses this location information to determine


what time to activate the lights. Unfortunately, you have to
use the graphical map to set this information, and it may not
function properly without an active internet connection.
After you have completed this, you will see a confirmation
screen.

(Steve Ovens, CC BY-SA 4.0)


Enter your username and password for the administrative (Steve Ovens, CC BY-SA 4.0)
HA account. Then it will prompt you to select a location. Rather than setting up devices here, I prefer to click Finish
and use the full UI to configure my devices. This is optional,
of course. This screen may prepopulate some integrations,
depending upon whether HA has automatically discovered
devices on your network.

Initial user settings


After you finish the initial configuration, you will see the Over-
view page. HA’s default interface is called Lovelace [8]. It
is a powerful YAML-described [9] interface. This means that
even if you can’t choose certain user interface (UI) elements
in the graphic interface, you can open the built-in YAML edi-
tor and add them yourself.
Lovelace’s default view has a single card that displays the
weather based on the location you entered. Click your user-
name (stratus in this example) in the bottom-left panel.

(Steve Ovens, CC BY-SA 4.0)


As you can see, your location is used for “sun-based au-
tomations.” This means it uses your longitude and latitude
to determine sunrise and sunset times and your time zone.
If, for example, you have an automation that says, “turn on
desk light 20 minutes before sunrise and turn off an hour (Steve Ovens, CC BY-SA 4.0)

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 13


SET UP HOME ASSISTANT TO MANAGE YOUR OPEN SOURCE SMART HOME . . . . . . . . . . . . . . . . . . . .

This brings up another screen with several options. Find Clicking on the Check Configuration button will check all
Advanced Mode and make sure it is toggled on. the HA configuration files for syntax errors. If it finds no er-
rors, you will see a message in green that says Configura-
tion Valid!

(Steve Ovens, CC BY-SA 4.0)

Looking ahead
Now that HA is set up and configured, you are ready to
start really digging into it. In future articles, I will explain
how to:

• Install and configure add-ons


• Create snapshots and run HA updates
• Install the Home Assistant Community Store (HACS)
• Configure entities via the built-in options
• Work with MQTT
(Steve Ovens, CC BY-SA 4.0) • Create automation flows with NodeRed
This setting is toggled per user, so if you have other adminis-
trative users, only this one (e.g., stratus) will have advanced And much more.
settings turned on. There are quite a few options exposed
when toggling Advanced Mode, but the one you want is the Links
ability to run a syntax check against the HA configuration [1] https://opensource.com/article/20/11/home-assistant
files. To see this in action, navigate to Server Controls by [2]  https://www.home-assistant.io/
clicking on the Configuration option in the bottom-left panel, [3]  https://opensource.com/article/20/11/cloud-vs-local-home-
and then click on Server Controls. automation
[4]  https://opensource.com/article/20/11/home-automation-
part-3
[5] https://www.home-assistant.io/hassio/installation/
[6] https://libvirt.org/
[7] https://virt-manager.org/
[8] https://www.home-assistant.io/lovelace/
[9] https://en.wikipedia.org/wiki/YAML

(Steve Ovens, CC BY-SA 4.0)

14 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . INTEGRATE DEVICES AND ADD-ONS INTO YOUR HOME AUTOMATION SETUP

Integrate devices and add-ons


into your home automation setup
Learn how to set up initial integrations and install add-ons in Home Assistant in the
fifth article in this series.

IN THE FOUR PREVIOUS ARTICLES in this series


about home automation, I have
discussed what Home Assistant is [1], why you may want
From there, click the Add Integration button in the bot-
tom-right corner and search for Zigbee:

local control [2], some of the communication protocols [3]


for smart home components, and how to install Home As-
sistant [4] in a virtual machine (VM) using libvirt. In this fifth
article, I will talk about configuring some initial integrations
and installing some add-ons.

Set up initial integrations


It’s time to start getting into some of the fun stuff. The whole
reason Home Assistant (HA) exists is to pull together vari-
ous “smart” devices from different manufacturers. To do so,
you have to make Home Assistant aware of which devices it
should coordinate. I’ll demonstrate by adding a Sonoff Zig-
bee Bridge [5].
I followed DigiBlur’s Sonoff Guide [6] to replace the stock
firmware with the open source firmware Tasmota [7] to de-
couple my sensors from the cloud. My second article [8] in (Steve Ovens, CC BY-SA 4.0)
this series explains why you might wish to replace the stock Enter the device manually. If the Zigbee Bridge was physical-
firmware. (I won’t go into the device’s setup with either the ly connected to the Home Assistant interface, you could se-
stock or custom firmware, as that is outside of the scope of lect the device path. For instance, I have a ZigBee CC2531
this tutorial.) USB stick that I use for some Zigbee devices that do not
First, navigate to the Configuration menu on the left side communicate correctly with the Sonoff Bridge. It attaches di-
of the HA interface, and make sure Integrations is selected: rectly to the Home Assistant host and shows up as a Serial
Device. See my third article [9] for details on wireless stan-
dards. However, in this tutorial, we will continue to configure
and use the Sonoff Bridge.

(Steve Ovens, CC BY-SA 4.0) (Steve Ovens, CC BY-SA 4.0)

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 15


INTEGRATE DEVICES AND ADD-ONS INTO YOUR HOME AUTOMATION SETUP . . . . . . . . . . . . . . . . . . .

The next step is to choose the radio type, using the infor- You can name the device(s) and assign an area (if you set
mation in the DigiBlur tutorial. In this case, the radio is an them up).
EZSP radio:

(Steve Ovens, CC BY-SA 4.0)


Finally, you need to know the IP address of the Sonoff
Bridge, the port it is listening on, and the speed of the con-
nection. Once I found the Sonoff Bridge’s MAC address, I
used my DHCP server to ensure that the device always uses
the same IP on my network. DigiBlur’s guide provides the
port and speed numbers. (Steve Ovens, CC BY-SA 4.0)
The areas displayed will vary based on whether or not you
have any configured. Bedroom, Kitchen, and Living Room
exist by default. As you add a device, HA will add a new
Card to the Integrations tab. A Card is a user interface (UI)
element that groups information related to a specific entity.
The Zigbee card looks like this:

(Steve Ovens, CC BY-SA 4.0)


Once you’ve added the Bridge, you can begin pairing devic-
es to it. Ensure that your devices are in pairing mode. The
Bridge will eventually find your device(s).

(Steve Ovens, CC BY-SA 4.0)

Later, I’ll come back to using this integration. I’ll also get
into how to use this device in automation flows. But now, I
will show you how to add functionality to Home Assistant to
make your life easier.

Add functionality with add-ons


Out of the box, HA has some pretty great features for home
automation. If you are buying commercial-off-the-shelf
(CoTS) products, there is a good chance you can accomplish
everything you need without the help of add-ons. However,
you may want to investigate some of the add-ons, especially
(Steve Ovens, CC BY-SA 4.0) if (like me) you want to make your own sensors.

16 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . INTEGRATE DEVICES AND ADD-ONS INTO YOUR HOME AUTOMATION SETUP

There are all kinds of HA add-ons, ranging from Android that’s external to the HA machine, I find low value in doing
debugging (ADB) tools to MQTT brokers to the Visual Stu- this. Since I am using MQTT just to communicate with my
dio Code editor. With each release, the number of add- IoT devices, and HA is the primary method of coordinating
ons grows. Some people make HA the center of their local that communication, there is a low risk in having these com-
system, encompassing DHCP, Plex, databases, and other ponents vertically integrated. If HA is offline, the MQTT bro-
useful programs. In fact, HA now ships with a built-in media ker is almost useless in my arrangement.
browser for playing any media that you expose to it.
I won’t go too crazy in this article; I’ll show you some of the Install community add-ons
basics and let you decide how you want to proceed. Home Assistant has a prolific community and passionate de-
velopers. In fact, many of the “community” add-ons are de-
Install official add-ons veloped and maintained by the HA developers themselves.
Some of the many HA add-ons are available for installation For my needs, I install:
right from the web UI, and others can be installed from alter-
native sources, such as Git.
To see what’s available, click on the Supervisor menu on
the left panel. Near the top, you will see a tab called Add-on
store.

(Steve Ovens, CC BY-SA 4.0) (Steve Ovens, CC BY-SA 4.0)


Below are three of the more useful add-ons that I think Grafana (graphing program) and InfluxDB (a time-series
should be standard for any HA deployment: database) are largely optional and relate to the ability to
customize how you visualize the data HA collects. I like to
have historical data handy and enjoy looking at the graphs
from time to time. While not exactly HA-related, I have my
pfSense firewall/router forward metrics to InfluxDB so that I
can make some nice graphs over time.

(Steve Ovens, CC BY-SA 4.0)


The File Editor allows you to manage Home Assistant con-
figuration files directly from your browser. I find this far more
convenient for quick edits than obtaining a copy of the file,
editing it, and pushing it back to HA. If you use add-ons like
the Visual Studio Code editor, you can edit the same files.
The Samba share add-on is an excellent way to extract
HA backups from the system or push configuration files or
assets to the web directory. You should never leave your
backups sitting on the machine being backed up. (Steve Ovens, CC BY-SA 4.0)
Finally, Mosquitto broker is my preferred method for ESPHome is definitely an optional add-on that’s warranted
managing an MQTT [10] client. While you can install a broker only if you plan on making your own sensors.

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 17


INTEGRATE DEVICES AND ADD-ONS INTO YOUR HOME AUTOMATION SETUP . . . . . . . . . . . . . . . . . . .

NodeRED is my preferred automation flow-handling solu- Don’t forget to start the plugin.
tion. Although HA has some built-in automation, I find a visu- Most add-ons follow a similar procedure, so you can use
al flow editor is preferable for some of the logic I use in my this approach to set up other add-ons.
system.
Wrapping up
Configure add-ons Whew, that was a lot of screenshots! Fortunately, when you
Some add-ons (such as File Editor) require no configuration are doing the configuration, the UI makes these steps rela-
to start them. However, most—such as Node-RED—require tively painless.
at least a small amount of configuration. Before you can start At this point, your HA instance should be installed with
Node-RED, you will need to set a password: some basic configurations and a few essential add-ons.
In the next article, I will discuss integrating custom Internet
of Things (IoT) devices into Home Assistant. Don’t worry; the
fun is just beginning!

Links
[1]  https://opensource.com/article/20/11/home-assistant
[2]  https://opensource.com/article/20/11/cloud-vs-local-home-
automation
[3] https://opensource.com/article/20/11/home-automation-
part-3
[4] https://opensource.com/article/20/12/home-assistant
[5]  https://sonoff.tech/product/smart-home-security/zbbridge
[6]  https://www.digiblur.com/2020/07/how-to-use-sonoff-
zigbee-bridge-with.html
(Steve Ovens, CC BY-SA 4.0) [7] https://tasmota.github.io/docs/
IMPORTANT: Many people will abstract passwords through [8]  https://opensource.com/article/20/11/cloud-vs-local-home-
the secrets.yaml file. This does not provide any additional automation
security other than not having passwords in the add-on con- [9]  https://opensource.com/article/20/11/wireless-protocol-
figuration’s YAML. See the official documentation [11] for home-automation
more information. [10] https://en.wikipedia.org/wiki/MQTT
In addition to the password requirement, most of the add- [11] https://www.home-assistant.io/docs/configuration/secrets/
ons that have a web UI default to having the ssl: true option
set. A self-signed cert on my local LAN is not a requirement,
so I usually set this to false. There is an add-on for Let’s
Encrypt, but dealing with certificates is outside the scope of
this series.
After you have looked through the Configuration tab,
save your changes, and enable Node-RED on the add-on’s
main screen.

(Steve Ovens, CC BY-SA 4.0)

18 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HOW TO SET UP CUSTOM SENSORS IN HOME ASSISTANT

How to set up custom sensors


in Home Assistant
Dive into the YAML files to set up custom sensors in the sixth article in this home automation series.

IN THE LAST ARTICLE in this series about home


automation, I started digging into
Home Assistant. I set up a Zigbee integration [1] with a Sonoff
Zigbee Bridge and installed a few add-ons, including Node-
RED, File Editor, Mosquitto broker, and Samba. I wrapped
up by walking through Node-RED’s configuration, which I
will use heavily later on in this series. The four articles be-
fore that one discussed what Home Assistant is [2], why
you may want local control [3], some of the communication
protocols [4] for smart home components, and how to install
Home Assistant [5] in a virtual machine (VM) using libvirt.
In this sixth article, I’ll walk through the YAML configuration
files. This is largely unnecessary if you are just using the
integrations supported in the user interface (UI). However,
there are times, particularly if you are pulling in custom sen-
sor data, where you have to get your hands dirty with the (Steve Ovens, CC BY-SA 4.0)
configuration files.
Let’s dive in.
Make sure Show in
Examine the configuration files sidebar is toggled
There are several potential configuration files you will want on. I also always tog-
to investigate. Although everything I am about to show you gle on the Watchdog
can be done in the main configuration.yaml file, it can help setting for any add-
to split your configuration into dedicated files, especially with ons I use frequently.
large installations. Once that is com-
Below I will walk through how I configure my system. pleted, launch File
For my custom sensors, I use the ESP8266 chipset, which Editor. There is a
is very maker-friendly. I primarily use Tasmota [6] for my folder icon in the top-
custom firmware, but I also have some components run- left header bar. This
ning ESPHome [7]. Configuring firmware is outside the is the navigation icon.
scope of this article. For now, I will assume you set up The /config folder is
your devices with some custom firmware (or you wrote where the configu-
your own with Arduino IDE [8]). ration files you are
concerned with are
The /config/configuration.yaml file stored. If you click on
Configuration.yaml is the main file Home Assistant reads. the folder icon, you
For the following, use the File Editor you installed in the will see a few import-
previous article. If you do not see File Editor in the left ant files:
sidebar, enable it by going back into the Supervisor set- The following is a
tings and clicking on File Editor. You should see a screen default configuration.
like this: yaml: (Steve Ovens, CC BY-SA 4.0)

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 19


HOW TO SET UP CUSTOM SENSORS IN HOME ASSISTANT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

(Steve Ovens, CC BY-SA 4.0)


The notation script: !include scripts.yaml indicates that
Home Assistant should reference the contents of scripts. (Steve Ovens, CC BY-SA 4.0)
yaml anytime it needs the definition of a script object. You’ll I have three main types of binary sensors [10]: door, mo-
notice that each of these files correlates to files observed tion, and power. A binary sensor has only two states: on or
when the folder icon is clicked. off. All my binary sensors send their data to MQTT. See my
I added three lines to my configuration.yaml: article on cloud vs. local control [11] for more information
about MQTT.
input_boolean: !include input_boolean.yaml My binary_sensor.yaml file looks like this:
binary_sensor: !include binary_sensor.yaml
sensor: !include sensor.yaml - platform: mqtt
state_topic: "BRMotion/state/PIR1"
As a quick aside, I configured my MQTT settings (see Home name: "BRMotion"
Assistant’s MQTT documentation [9] for more details) in the qos: 1
configuration.yaml file: payload_on: "ON"
payload_off: "OFF"
mqtt: device_class: motion
discovery: true
discovery_prefix: homeassistant - platform: mqtt
broker: 192.168.11.11 state_topic: "IRBlaster/state/PROJECTOR"
username: mqtt name: "ProjectorStatus"
password: superpassword qos: 1
payload_on: "ON"
If you make an edit, don’t forget to click on the Disk icon to payload_off: "OFF"
save your work. device_class: power

- platform: mqtt
state_topic: "MainHallway/state/DOOR"
name: "FrontDoor"
qos: 1
payload_on: "open"
payload_off: "closed"
device_class: door

Take a look at the definitions. Since platform is self-explan-


atory, start with state_topic.
(Steve Ovens, CC BY-SA 4.0)

The /config/binary_sensor.yaml file  tate_topic, as the name implies, is the topic where the
• s
After you name your file in configuration.yaml, you’ll have device’s state is published. This means anyone subscribed
to create it. In the File Editor, click on the folder icon again. to the topic will be notified any time the state changes. This
There is a small icon of a piece of paper with a + sign in its path is completely arbitrary, so you can name it anything
center. Click on it to bring up this dialog: you like. I tend to use the convention location/state/ob-
ject, as this makes sense for me. I want to be able to
reference all devices in a location, and for me, this layout is
the easiest to remember. Grouping by device type is also a
valid organizational layout.

20 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HOW TO SET UP CUSTOM SENSORS IN HOME ASSISTANT

• name is the string used to reference the device inside {


Home Assistant. It is normally referenced by type.name, "Time": "2020-12-23T16:59:11",
as seen in this card in the Home Assistant Lovelace [12] "DHT11": {
interface: "Temperature": 24.8,
"Humidity": 32.4,
"DewPoint": 7.1
},
"BH1750": {
"Illuminance": 24
},
"TempUnit": "C"
}

There are a few things to note here. First, as the sensor data
(Steve Ovens, CC BY-SA 4.0)
is stored in a time-based data store, every reading has a
• q os, short for quality of service, refers to how an MQTT cli- Time entry. Second, there are two different sensors attached
ent communicates with the broker when posting to a topic. to this output. This is because I have both a DHT11 tem-
• payload_on and payload_off are determined by the firm- perature sensor and a BH1750 light sensor attached to the
ware. These sections tell Home Assistant what text the same ESP8266 chip. Finally, my temperature is reported in
device will send to indicate its current state. Celsius.
• device_class: There are multiple possibilities for a device Hopefully, the Home Assistant definitions will make a
class. Refer to the Home Assistant documentation [13] little more sense now. value_json is just a standard name
for more information and a description of each type given to any JSON object ingested by Home Assistant. The
available. format of the value_template is value_json.<compo-
nent>.<data point>.
The /config/sensor.yaml file
This file differs from binary_sensor.yaml in one very For example, to retrieve the dewpoint:
important way: The sensors within this configuration
file can have vastly different data inside their payloads. value_template: '{{ value_json.DHT11.DewPoint}}'
Take a look at one of the more tricky bits of sensor data,
temperature. While you can dump this information to a file from within
Home Assistant, I use Tasmota’s Console to see the data it
Here is the definition for my DHT temperature sensor: is publishing. (If you want me to do an article on Tasmota,
please let me know in the comments below.)
- platform: mqtt As a side note, I also keep tabs on my local Home Assistant
state_topic: "Steve_Desk_Sensor/tele/SENSOR" resource usage. To do so, I put this in my sensor.yaml file:
name: "Steve Desk Temperature"
value_template: '{{ value_json.DHT11.Temperature }}' - platform: systemmonitor
resources:
- platform: mqtt - type: disk_use_percent
state_topic: "Steve_Desk_Sensor/tele/SENSOR" arg: /
name: "Steve Desk Humidity" - type: memory_free
value_template: '{{ value_json.DHT11.Humidity }}' - type: memory_use
- type: processor_use
You’ll notice two things right from the start. First, there are
two definitions for the same state_topic. This is because While this is technically not a sensor, I put it here, as I think
this sensor publishes three different statistics. of it as a data sensor. For more information, see the Home
Second, there is a new definition of value_template. Assistant’s system monitoring [14] documentation.
Most sensors, whether custom or not, send their data in-
side a JSON payload. The template tells Home Assistant The /config/input_boolean file
where the important information is in the JSON file. The This last section is pretty easy to set up, and I use it for a
following shows the raw JSON coming from my home- wide variety of applications. An input boolean is used to track
made sensor. (I used the program jq to make the JSON the status of something. It’s either on or off, home or away,
more readable.) etc. I use these quite extensively in my automations.

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 21


HOW TO SET UP CUSTOM SENSORS IN HOME ASSISTANT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

My definitions are: These can be used to determine whether I am home, if a


guest is in my house, and so on. Clicking on one of these
steve_home: badges allows me to toggle the boolean, and this object can
name: steve be read by automations to make decisions about how the
steve_in_bed: “smart devices” react to a person’s presence (if at all). I’ll
name: 'steve in bed' revisit the booleans in a future article when I examine Node-
guest_home: RED in more detail.

kitchen_override: Wrapping up
name: kitchen In this article, I looked at the YAML configuration files and
kitchen_fan_override: added a few custom sensors into the mix. You are well on
name: kitchen_fan the way to getting some functioning automation with Home
laundryroom_override: Assistant and Node-RED. In the next article, I’ll dive into
name: laundryroom some basic Node-RED flows and introduce some basic
bathroom_override: automations.
name: bathroom Stick around; I’ve got plenty more to cover.
hallway_override:
name: hallway Links
livingroom_override: [1]  https://opensource.com/article/21/1/home-automation-5-
name: livingroom homeassistant-addons
ensuite_bathroom_override: [2] https://opensource.com/article/20/11/home-assistant
name: ensuite_bathroom [3]  https://opensource.com/article/20/11/cloud-vs-local-home-
steve_desk_light_override: automation
name: steve_desk_light [4]  https://opensource.com/article/20/11/home-automation-
projector_led_override: part-3
name: projector_led [5]  https://opensource.com/article/20/12/home-assistant
[6] https://tasmota.github.io/docs/
project_power_status: [7] https://esphome.io/
name: 'Projector Power Status' [8]  https://create.arduino.cc/projecthub/Niv_the_anonymous/
tv_power_status: esp8266-beginner-tutorial-project-6414c8
name: 'TV Power Status' [9] https://www.home-assistant.io/docs/mqtt/broker
bed_time: [10] https://www.home-assistant.io/integrations/binary_sensor/
name: "It's Bedtime" [11] https://opensource.com/article/20/11/cloud-vs-local-home-
automation
I use some of these directly in the Lovelace UI. I create little [12] https://www.home-assistant.io/lovelace/
badges that I put at the top of each of the pages I have in the UI: [13] https://www.home-assistant.io/integrations/binary_sensor/
[14] https://www.home-assistant.io/integrations/systemmonitor

(Steve Ovens, CC BY-SA 4.0)

22 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . PROTECT YOUR HOME ASSISTANT WITH THESE BACKUPS

Protect your Home Assistant


with these backups
Make sure you can recover quickly from a home automation failure with a solid backup and
hardware plan in the seventh article in this series.

IN THE LAST TWO ARTICLES in this series on home


automation with Home Assistant
(HA), I walked through setting up a few integrations [1] with
the files that are hidden or otherwise protected. For exam-
ple, backup software for a computer (such as CloneZilla)
makes an exact replica (in some cases) of the hard drive
a Zigbee Bridge and some custom ESP8266 [2] devices to ensure no files are missed. Home Assistant knows how
that I use for automation. The first four articles in the series to create snapshots and does it for you. You just need to
discussed what Home Assistant is [3], why you may want worry about storing the files somewhere.
local control [4], some of the communication protocols [5]
for smart home components, and how to install Home As- Set a good backup strategy
sistant [6] in a virtual machine (VM) using libvirt. Before I get into how to deal with snapshots in Home Assis-
Now that you have a basic home automation setup, it is tant, I want to share a brief story from a recent client. Re-
a good time to take a baseline of your system. In this sev- member when I mentioned that simply having a single copy of
enth article, I will talk about snapshots, backups, and backup your files doesn’t give you any indication that a problem has
strategies. Let’s get right into it. occurred? My client was doing all of the right things when it
came to backups. The team was using the proper methodolo-
Backups vs. copies gy for backups, kept multiple files going back a certain period
I’ll start by clearing up some ambiguity: A copy of something of time, ensured there were more than two copies of each
is not the same as a backup. Here is a brief overview of the backup, and was especially careful that backups were not be-
difference between a copy and a backup. Bear in mind that ing stored locally on the machine being backed up. Sounds
this comes from the lens of an IT professional. I work with great, doesn’t it? They were doing everything right. Well, al-
client data day in and day out. I have seen many ways that most. The one thing they neglected was testing the backups.
backups can go sideways, so the following descriptions may Most people put this off or disregard it entirely. I admit I am
be overkill for home use. You’ll have to decide just how im- guilty of not testing my backups frequently. I do it when I re-
portant your Home Assistant data really is. member, which is usually once every few months or so.
In my client’s case, a software upgrade created a new re-
• C
 opies: A copy is just what it sounds. It is when you high- quirement from the backup program. This was missed. The
light something on your computer and hit Ctrl+C and paste it backups continued to hum along, and the automated checks
somewhere else with Ctrl+V. Many people may view this as passed. There were files after every backup run, they were
backing up the source, and to some extent, that is true. How- larger than a certain amount, and the magic file checks [7]
ever, a copy is merely a representation of a point in time. If it’s reported the correct file type. The problem was that the file
taken incorrectly, the newly created file can be corrupt, lead- sizes shrunk significantly due to the software change. This
ing to a false sense of security. In addition, the source may meant the client was not backing up the data it thought.
have a problem—meaning the copy will also have a problem. This story has a happy ending, which brings me to my point:
If you have just a single copy of a file, it’s often the same as Because the client was doing everything else right, we could
having nothing at all. When it comes to backup, the saying go through the backups and identify the precise moment when
“one is none” is absolutely true. If you do not have files going something changed. From this, we linked this to the date of an
back over time, you won’t have a good idea of whether the upgrade some weeks back. Fortunately, there was no emer-
system creating the backups has a problem. gency that precipitated the investigation. I happened to be do-
• B
 ackups and snapshots: In Home Assistant, it is a bit ing a standard audit when I discovered the discrepancy.
tricky to differentiate between a copy and a backup. First, The moral of the story? Without proper backup strategies,
Home Assistant uses the term “snapshot” to refer to what we would have had a much harder time tracking down this
we traditionally think of backups. In this context, a backup problem. Also, in the event of a failure, we would have had
is very similar to a copy because you don’t use any type of no recovery point.
backup software, at least not in the traditional sense. Nor- A good backup strategy usually entails daily, weekly, and
mally, backup software is designed specifically to get all monthly backups. For example, you may decide to keep all

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 23


PROTECT YOUR HOME ASSISTANT WITH THESE BACKUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

your daily backups for two weeks, four weekly backups, and of the disk and the size of the backup. I recommend keep-
perhaps four monthly backups. This, in my opinion, is overkill ing at least four backups on your machine if you can afford
for Home Assistant after you have a stable setup. You’ll have the space.
to choose the level of precision you need. I take a backup
before I make any change to the system. This gives me a
known-good situation to revert to.

Create snapshots
Great, so how do you create a snapshot in Home Assistant?
The Snapshots menu resides inside the Supervisor tab on
the left-size panel.

(Steve Ovens, CC BY-SA 4.0)

You can retrieve these files from Home Assistant with File
Browser if you set up the Samba share extension.

(Steve Ovens, CC BY-SA 4.0)

You have two options for creating a snapshot: Full snapshot


or Partial snapshot. A Full snapshot is self-explanatory. You
have some options with a Partial snapshot.
(Steve Ovens, CC BY-SA 4.0)

Save these files somewhere safe. The name you give the
backup is contained in the metadata inside Home Assis-
tant, and the file names are randomly generated. After I
copy them to a different location, I usually rename them
because when I test the restoration process on a different
machine, the new file name does not matter.

My homelab strategy
I run my Home Assistant instance on top of KVM on a Linux
host. I have had a few requests to go into a little more detail
on this, so feel free to skip past this section as it’s not directly
related to HA.
Due to the nature of my job, I have a fairly large variety
of hardware, which I use for a homelab [8]. Sometimes this
is because physical hosts are easier to work with than VMs
for certain clustering software. Other times, this is because
I keep workloads isolated to specific hardware. Either way,
this means I already have a certain amount of knowledge
built up around managing and dealing with KVM. Not to
(Steve Ovens, CC BY-SA 4.0)
mention the fact that I run almost exclusively open source
Any component you install in Home Assistant will populate software (with a few exceptions). Here is a basic layout of
in this menu. Choose a name for your backup and click my homelab:
Create. This will take some time, depending on the speed

24 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM


. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . PROTECT YOUR HOME ASSISTANT WITH THESE BACKUPS

Using the Samba add-on, I pull a copy of the snapshot onto


my GNOME desktop. I configure Nextcloud using GNOME’s
Online Accounts settings.

(Steve Ovens, CC BY-SA 4.0) (Steve Ovens, CC BY-SA 4.0)


The network-attached storage (NAS) has dual 10GB net- Nextcloud takes a copy and puts it on my NAS. Both my
work cards that feed into uplink ports. Two of the KVM hosts desktop and the NAS use SpiderOak One Backup [9] cli-
have 10GB network interface cards (NICs), while the hosts ents to ensure the backups are linked to more than one
on the right have regular 1GB network cards. host. In the unlikely event that I delete a device from my
For Home Assistant, this is well into overkill territory. How- SpiderOak account, the file is still linked to another device.
ever, this infrastructure was not designed for HA. HA runs just I chose SpiderOak because it supports a Linux client, and
fine on a Raspberry Pi 4 (4GB version) at my parents’ house. it is privacy-focused and has no insight into what files it
The VM that hosts Home Assistant has three vCPU cores stores. The files are encrypted before being uploaded, and
of a Broadwell Core I5 CPU (circa 2015) with 8GB of RAM. only the owner has the ability to decrypt them. The down-
The CPU tends to remain around 25% usage, and I rarely use side is that if you lose or forget your password, you lose
more than 2.2GB of RAM. This is with 11 add-ons, including your backups.
InfluxDB and Grafana, which are heavier applications. Finally, I keep a cold copy on an external hard drive. I have
While I do have shared storage, I do not use it for live a 14TB external drive that remains off and disconnected
migration or anything similar. Instead, I use this for backend from the NAS except when backups are running. It’s not on
storage for specific mount points in a VM. For example, I the diagram, but I also occasionally replicate to a computer
store the data directory from Nextcloud on the NAS, divorc- at my in-laws’ house.
ing the data from the service. I can also take snapshots of the VM during critical oper-
At any rate, I have a few approaches to backups with ations (such as Home Assistant’s recent upgrade from us-
this setup. Naturally, I use the Home Assistant snapshotting ing a numbered point release to a month-based numbering
function to provide the first layer of protection. I tend to store system).
only four weeks’ worth of data on the VM. What do I do with I use a similar pipeline for most things that I back up, al-
the files themselves? Here is a diagram of how I try to keep though I recognize it is a bit overkill. Also, this whole process
my backups safe: has the flaw that it relies on me. Aside from SpiderOak and
Nextcloud, I have not automated this process. I have scripts
that I run, but I do not run them in a cron or anything like that.
In hindsight, perhaps I should work on that.
This setup may be considered extreme, but the built-in
versioning in Nextcloud and SpiderOak, along with mak-
ing copies in multiple locations, means that I am unlikely to
suffer a failure that I can’t recover from. At the very least, I
should be able to dig up a close reference.
As a final precaution, I make sure to keep the important
information about how I set things up on my private wiki on
Wiki.js [10]. I keep a section just for home automation.

(Steve Ovens, CC BY-SA 4.0)

HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM 25


PROTECT YOUR HOME ASSISTANT WITH THESE BACKUPS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Wrapping up
Backups are essential when you’re using Home Assistant,
as it is a critical component of your infrastructure that al-
ways needs to be functioning. Small downtime is accept-
able, but the ability to recover from a failure quickly is
crucial. Granted, I have found Home Assistant to be rock
solid. It has never failed on its own; any problems I have
had were external to HA. Still, if you are going to make HA
a central part of your house, I strongly recommend putting
a good backup strategy in place.

Links
[1]  https://opensource.com/article/21/1/home-automation-5-
homeassistant-addons
[2] https://opensource.com/article/21/1/home-assistant-6-
custom-sensors
[3] https://opensource.com/article/20/11/home-assistant
[4] https://opensource.com/article/20/11/cloud-vs-local-home-
automation
[5]  https://opensource.com/article/20/11/home-automation-
part-3
(Steve Ovens, CC BY-SA 4.0)
[6]  https://opensource.com/article/20/12/home-assistant
When you get into creating Node-RED automations (in the [7] https://linux.die.net/man/5/magic
next article), I suggest you keep your own notes. I take a [8] https://opensource.com/article/19/3/home-lab
screenshot of the flow, write a brief description, so I know [9] https://spideroak.com/
what I was attempting to achieve, and dump the flow to [10] https://wiki.js.org/
JSON (for brevity, I omitted the JSON from this screenshot):

(Steve Ovens, CC BY-SA 4.0)

26 HOME AUTOMATION USING OPEN SOURCE TOOLS . CC BY-SA 4.0 . OPENSOURCE.COM

You might also like