Home Assistant makes the Raspberry Pi fit for the Z-Wave world
Home Automaton
Z-Wave components, a RaZberry module, and the free Home Assistant software make the Raspberry Pi a powerful smart home control center.
What is causing the smart home hype? Do you really need socket outlets that can be switched on by your cell phone, and if so, what is it all good for at the end of the day? Starting a Raspberry Pi project to find the answer to such questions has always proved to be a workable approach, thus far. Even if my answer to the "Do I need it?" question was, "No," one thing is for sure, open source and Raspberry Pi can do it.
Of course, a targeted solution needs to meet a number of criteria. It should be based on standards for which sufficient numbers of different components exist, while being open source. All components need to harmonize with the Raspberry Pi, work as self-sufficient systems without any cloud contact, and avoid transmitting unnecessary data on the network. Ideally, I would want to control the whole setup in a web interface that adapts to the small format of a smartphone display.
Every home automation installation includes sensors, actuators, and a control center that evaluates everything and makes link-ups possible. Sensors and actuators not only need to be available over the counter, they should communicate with the control center wirelessly, but securely and with encryption. Rewiring the house is out of the question. Furthermore, the specifications state that the individual components must be economical and run on one battery for a long time. Last but not least, the overall solution needs to remain future-proof and expandable for many years.
Component Selection
From the large selection of manufacturers for smart home components, the Z-Wave standard quickly crystallized as a basis that meets all criteria. Under the umbrella of the Z-Wave Alliance [1], more than 600 manufacturers and service providers offer thousands of different components that comply with the standard and communicate with each other.
The consortium of various companies enables cost-effective and flexible installation and integrates the Raspberry Pi. With the help of the RaZberry module [2], the Raspberry Pi can access the Z-Wave network. Z-Wave uses radio frequencies between 850 and 950MHz, which ensures a high range and penetration depth and avoids interference.
That still leaves the question of suitable software. The included with the RaZberry module, based on the proprietary Z-Way [3] solution, is disqualified in the context of the project because of its proximity to the operator's cloud. Nevertheless, the software is suitable for first steps and experiments and can be put into operation without much knowledge about Linux or Raspberry Pi OS [4].
A nicer solution would integrate on a Raspberry Pi that is already installed and active in the house. In this way, the usefulness of a Raspberry Pi – used as a monitoring solution, for example – can be expanded without having to purchase another small-board computer.
For this specific use case, Home Assistant [5] is a good choice of software. You can run it in a virtual Python environment, and the configuration is controlled by simple configuration files in the YAML markup language [6]. The system supports a massive range of components, including the RaZberry module.
Home Assistant
For a test setup, I integrated a Fibaro smart wall plug (on/off; measures power and consumption) (Figure 1), a multisensor (temperature, brightness, motion, humidity) (Figure 2), and a radiator thermostat into a rudimentary smart home system. The RaZberry module and Home Assistant software take care of control.
The Home Assistant needs to install in a Python virtual environment. Anyone who has ever dealt with chroot environments under Linux will see the similarities. You set up an environment within the running system that includes all the required libraries and binaries. This setup also facilitates backups of the Home Assistant installation: An image is kept up and running immediately after a simple restore.
In terms of additional packages from the Raspberry Pi package management system, you only need Python and the libudev-dev development package to build the Z-Wave modules. For Home Assistant, set up a separate user account, (e.g., homeassist). Because communication with the RaZberry module is over a serial interface, you also need to add the homeassist user to the dialout, gpio, and i2c groups.
You can do all of this with the command
$ sudo useradd -rm homeassistant -G dialout,gpio,i2c
Next, you should open the /boot/config.txt
configuration file for the Raspberry Pi system in a text editor and add the enable_uart=1
line, edit the /boot/cmdline.txt
file, and remove the console=serial0,115200 console=tty1
section from the configuration.
To enable the settings, restart the system, then install Home Assistant in a virtual Python environment:
$ python3 -m venv homeassistant $ cd homeassistant $ source bin/activate $ python3 -m pip install wheel $ python3 -m pip install homeassistant $ hass --open-ui
The command from the last line handles the basic configuration of Home Assistant and completes the basic installation.
Note that when you activate components in the Home Assistant configuration file, any modules that are still needed will be reinstalled when you restart, so don't set up the system in a place that doesn't have network access yet – the Raspberry Pi needs an Internet connection until it is fully configured.
So that the system automatically starts the Home Assistant service at boot time, create another service unit for systemd by copying the contents of Listing 1 and saving it to /etc/systemd/system/
in the homeassist@homeassist.service
file. To enable the service, type:
Listing 1
homeassist@homeassist.service
[Unit] Description=Home Assistant After=network-online.target [Service] Type=simple User=%i ExecStart=/home/%i/homeassistant/bin/hass -c "/home/%i/.homeassistant" [Install] WantedBy=multi-user.target
sudo systemctl enable homeassist
The service will load automatically at boot time in the future. Finally, start the service manually with
sudo systemctl start homeassist
just this once.
The first start of Home Assistant takes some time, because the system creates the configuration.yaml
configuration file and initializes the components. Also, when you add new entries to the configuration that require additional modules later on, Home Assistant needs a somewhat longer pause for thought while it retroactively installs the corresponding modules.
After completing the initialization, the system saves configuration.yaml
to .homeassistant/
, the homeassistant user's home directory. Its contents looks something like:
group: !include groups.yaml automation: !include automations.yaml script: !include scripts.yaml scene: !include scenes.yaml
In the same directory, the program also writes a logfile named home-assistant.log
, which provides information about what is currently happening. If you have a problem somewhere, you will find further details there. On the Raspbian installation for this test setup, for example, I still had to install two packages – libtiff5 and libopenjp2-7 – because Home Assistant complained about their absence.
In a web browser, open the administration interface on http://<RasPi-IP>:8123 (Figure 3). Initially, Home Assistant will prompt you to enter a new username and password, which you will later use to authenticate yourself to the service. Additionally, the page asks for language, location, and time zone.
RaZberry
The communication between the Raspberry Pi and the RaZberry module happens over the GPIO serial interface. For the installation, you only have to plug the RaZberry onto the GPIO bar of the Raspberry Pi, starting at the upper edge of the board (Figure 4).
For the connection, first create an AES key,
$ cat /dev/urandom | tr -dc '0-9A-F' | fold -w 32 | head -n 1 | sed -e 's/\(..\)/0x\1,/g' -e 's/, $//'
and enter it into configuration.yaml
as shown in Listing 2. The usb_path
is /dev/ttyACM0
for USB-connected Z-Wave sticks and dev/ttyAMA0
for the hardware attached on top (HAT) module.
Listing 2
configuration.yaml
[...] zwave: usb_path: /dev/tty<AMA0> network_key: "<0x41, 0xF3, 0xA6, 0x9A, 0x60, 0x56, 0x43, 0xE1, 0x3D, 0x15, 0xBD, 0xF7, 0x5C, 0x5D, 0x0B, 0x8E>" [...]
After a reboot, you can reach the Z-Wave controller from the Integrations submenu of the web interface. There, in typical Z-Wave fashion, you need to teach the components their installation instructions (Figure 5). In most cases, you need to switch temporarily the component in question to learning mode by pressing a button. The controller should then find the sensor or actuator, parse its data, and assign the controller permanently to its own Z-Wave network. After all, several Z-Wave networks might need to share a frequency. The GUI is very intuitive to use. In case of problems, see the OZW_Log.txt
file in /home/homeassistant/.homeassistant/
for details of the actions performed by the Z-Wave module.
Each component registers in the system's Home Assistant universe. In the overview, you can see the states and measured values of all integrated sensors and actuators (Figure 6). The individual components can then be managed and controlled in detail, and the individual nodes can be linked together in the scope of automation setups.
The configuration and automation can be set up manually from the configuration.yaml
and automations.yaml
files, respectively. However, the graphical user interface under the Configuration | Automations menu offers considerably more convenience. Depending on the intended use and connected components, you can implement anything from a simple to an extremely comprehensive scenario. The test setup is limited to a simple function that switches on the light as soon as a motion detector is tripped (Listing 3).
Listing 3
automations.yaml
- id: '1590605895467' alias: snap burg description: 'switch light on movement' trigger: - above: '3' entity_id: sensor.aeon_labs_zw100_multisensor_6_burglar platform: numeric_state condition: [] action: - data: {} entity_id: switch.fibaro_system_fgwpe_f_wall_plug_gen5_switch service: switch.turn_on
When you create the automation from the GUI, the corresponding data ends up in the automations.yaml
file. Listing 3 demonstrates the structure of a simple automation: The trigger
is set off when a certain measured value of a sensor is reached. In the example, I want the motion detector to report an activity value greater than 3 (above: '3'
). The assignment is made with the entity_id
of the sensor component of the multisensor and can also be adapted in the configuration if required.
The trigger now initiates an action
in compliance with a condition
. In the test setup, I want the Fibaro connector to switch to On; the example does not set a condition. For example, it would be conceivable for Home Assistant to switch on the light only after the sun has dropped below the horizon. The condition in this case would query the entity_id: sun.sun
and respond to state: "below_horizon"
[7]. Incidentally, the entity for the position of the sun is one of the automatically added sensors.
The example described here gives you an idea of the wide range of possibilities offered by automation. The multisensor in particular offers numerous options for triggering actions based on light incidence, temperature, or motion. The example still lacks a function to turn off the light again automatically. To do this, you need to add a sensor-controlled or time-controlled shutdown to the routine you have already created or create another automatic routine.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.