Home Assistant makes the Raspberry Pi fit for the Z-Wave world

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, 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, 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 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.

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 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. 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.

Figure 1: The Fibaro adapter plug not only switches on the connected device but measures power and consumption.
Figure 2: The multisensor registers temperature, humidity, and brightness and reacts to motion.

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:

sudo systemctl enable homeassist

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

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.

Figure 3: Home Assistant uses a clear-cut web interface.

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).

Figure 4: A first-generation Raspberry Pi is sufficient for Home Assistant. The RaZberry module sits on a section of GPIO pins.

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
  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.

Figure 5: Integrate the Z-Wave devices into the Home Assistant system in learning mode.

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.

Figure 6: The overview shows the measured values and states of all Z-Wave sensors and actuators.

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". 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.

Conclusions and Outlook

The smart home system consisting of Z-Wave components, a Raspberry Pi, the RaZberry module, and Home Assistant implements a newcomer-friendly home automation setup. Only standardized components are used and controlled by open source software that does not require a connection to the cloud.

For advanced users, the solution also opens up the possibility of programming more extensive automations that do not require prefabricated industrial components. This flexibility allows more ambitious hobbyists to further expand the smart home and integrate additional functions at a manageable cost.

Listing download for MakerSpace 03 2023