Raspberry Pi 4 doing domotics

    

I already run Domoticz using a Synology package and a couple of docker containers. But this is complicated to maintain. And the Syno App doesn’t seem to be updated frequently. Not being said the management of the USB sticks is quite random.

So I decided to get myself a Raspberry Pi 4 and run everything from there.

Overview

To build the whole solution requires connecting quite a few hardware and software bits. I personally understand things better when I see them.

The Raspberry Pi 4 OS

I installed raspbian. Because it is build for the Raspberry. Because I don’t want to struggle with some other Linux distro. Because it is Debian-based ; which is usually nice and known to me.

# cat /etc/os-release 
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

ConBee: the universal Zigbee USB gateway

All my cheap Xiaomi IoT use the ZigBee protocol. So I bought a ConBee II USB stick to get rid of the Xiaomi gateway and allow Domoticz to get the metrics.

You need a deCONZ daemon to talk to the ConBee II and get the metrics into Domoticz. The official installation guide is available here .

Create a user to run the deCONZ daemon unpriviledged:

# useradd -c "deCONZ daemon user" -d /home/deconz -g dialout \
  -s /usr/sbin/nologin -u 8666 deconz

Import the Phoscon public key and install the software:

# wget -O - http://phoscon.de/apt/deconz.pub.key | apt-key add -

# echo "deb http://phoscon.de/apt/deconz $(lsb_release -cs) main" \
  > /etc/apt/sources.list.d/deconz.list

# apt update
# apt install deconz
(...)
Setting up deconz (2.05.88-raspbian-buster-stable) ...
enable systemd deconz-update service
Important: /boot/cmdline must not contain console=serial0 (RaspBee only)
use raspi-config to configure serial port as described in documentation (RaspBee only):
https://github.com/dresden-elektronik/deconz-rest-plugin#software-requirements

Configure headless support for the daemon ; disable the GUI:

# systemctl enable deconz
Created symlink /etc/systemd/system/multi-user.target.wants/deconz.service → /lib/systemd/system/deconz.service.
# systemctl disable deconz-gui

Tune the service to run as a dedicated user and port:

# systemctl edit deconz

# cat /etc/systemd/system/deconz.service.d/override.conf
[Service]
User=
User=8666
ExecStart=
ExecStart=/usr/bin/deCONZ -platform minimal --http-port=8666

# systemctl restart deconz

You can now browse to http://raspberryip:8666/. Select the “Phoscon-GW” icon. Input the Gateway name and login password.

You may create groups to place your IoT. I just created one so that the wizard don’t bug me. In the end, browse to Mainpage / Settings / Gateway and check the hardware, firmware, … If needed, this is where you can backup configuration and update the ConBee II firmware.

Connect the lights, switches and sensors to the ZigBee network.

Aeotec Z-Stick Gen5: the Z-Wave gateway

This stick is used to pair/unpair devices that use the Z-Wave protocol. Domoticz will bind to the USB device to get all the metrics and informations.

In my experience, it works better if you start with unparing the device ; then pair it. But it has to be noted that I am migrating from Syno to Pi and the network configuration must be stored on the key.

Plug the Z-Stick to a Raspberry Pi USB port to enable automation software to access the Z-Wave network.

Domoticz: the Home Automation System

There does not seem to be any packages for Domoticz. The standard installation method seem to be using an installer shell script. By default, running that script with require root access, to add package dependencies, and will install Domoticz as you current user. I want a dedicated user to run Domoticz ; neither root, nor myself.

# useradd -c "Domoticz daemon user" -d /home/domoticz -g dialout \
  -s /usr/sbin/nologin -u 8667 domoticz

# curl -L https://install.domoticz.com | bash

# chown -R domoticz:root /home/domoticz

# rm /etc/init.d/domoticz.sh
# cat /etc/systemd/system/domoticz.service
[Unit]
  Description=domoticz_service
[Service]
  User=domoticz
  Group=dialout
  ExecStart=/home/domoticz/domoticz -www 8667 -sslwww 0
  WorkingDirectory=/home/domoticz
  PermissionsStartOnly=true
  ExecStartPre=setcap 'cap_net_bind_service=+ep' /home/domoticz/domoticz
  Restart=on-failure
  RestartSec=1m
  #StandardOutput=null
[Install]
  WantedBy=multi-user.target

# systemctl daemon-reload
# systemctl start domoticz

I only configured HTTP as HTTPS with be hosted on the network gateway. Until then, you may browse to http://raspberryip:8667 and discover that none of the two previous IoT networks are available…

Access the ZigBee data

You will need a particular Domoticz plugin to talk to the deCONZ daemon:

# cd /home/domoticz/plugins
# sudo -u domoticz git clone https://github.com/Smanar/Domoticz-deCONZ.git
# chmod +x Domoticz-deCONZ/plugin.py

# apt install libpython3-dev

# systemctl restart domoticz

# sudo -u domoticz python3 Domoticz-deCONZ/API_KEY.py 127.0.0.1:8666 create
Your new API key is : xxxxxxxxxx

Note the API key that is provided by the script. Domoticz requires it. Browse to the Hardware section of Domoticz and add a new item:

Browse to the Devices section to see the already registered devices. Refresh and/or wait a few seconds if needed. The Log section may be helpful.

Access the Z-Wave data

Browse to the Hardware section of Domoticz and add a new item:

When done, click on the “Setup” button to access the list of know IoT.

Configure simple and persistent USB devices

You may have noticed the weird serial port name if the previous section. This is because I tweaked the OS so that the USB device will have to same reference at every reboot. If you do not do this, ConBee and Z-Stick may exchange their device name at every Pi reboot. Which requires you to reconfigure Domoticz. And this sucks!

So I replicated for the Z-Stick what the clever deCONZ people did for the ConBee II.

Get the IDs for both USB sticks:

# lsusb -v | grep 'idVendor\|idProduct'
(...)
idVendor 0x1cf1 Dresden Elektronik
idProduct 0x0030
(...)
idVendor 0x0658 Sigma Designs, Inc.
idProduct 0x0200 Aeotec Z-Stick Gen5 (ZW090) - UZB
(...)

Copy the udev rules file that comes with the deCONZ software and adapt to the Z-Wave stick.

# cat /etc/udev/rules.d/70-aeotec.rules
# Aeotec Z-Wave USB Stick Gen5
ATTRS{idVendor}=="0658", ATTRS{idProduct}=="0200", ENV{ID_MM_DEVICE_IGNORE}="1"
ATTRS{idVendor}=="0658", ATTRS{idProduct}=="0200", ENV{MTP_NO_PROBE}="1"
KERNEL=="ttyACM?", SUBSYSTEMS=="usb", DRIVERS=="usb", ACTION=="add", ATTRS{idVendor}=="0658", ATTRS{idProduct}=="0200", SYMLINK+="ttyZWaveGen5", GROUP="dialout", MODE="0660"

Reboot to ensure everything works as expected. In my case, I get stuff like that:

# ls -alh /dev/tty*
(...)
crw-rw---- 1 root dialout 166, 0 Nov 20 00:50 /dev/ttyACM0
crw-rw---- 1 root dialout 166, 1 Nov 20 00:46 /dev/ttyACM1
crw-rw---- 1 root dialout 204, 64 Nov 19 23:28 /dev/ttyAMA0
lrwxrwxrwx 1 root root 7 Nov 19 23:28 /dev/ttyConBee2 -> ttyACM0
lrwxrwxrwx 1 root root 7 Nov 19 23:28 /dev/ttyZWaveGen5 -> ttyACM1

It is unclear to me why the Aeotec Z-Stick would not work if connected to an USB2 or USB3 port of the Raspberry. But this seems to be well known and referenced by Google. The Z-Stick has to be connected via an external USB switch. I’m using a cheap USB2 4 ports. And only then can I access the Z-Wave data. One thing to note is that the Z-Stick attaches to ACMx instead of AMAx when connected to the hub.

Change the Domoticz look

Tastes are personal. And I don’t like the default Domoticz look. Hopefully, you can get themes from the Internet:

# cd /home/domoticz/www/styles/

# sudo -u domoticz git clone https://github.com/DewGew/Domoticz-ThinkTheme ThinkTheme
Cloning into 'ThinkTheme'...
(...), done.

Browse to the Settings section, select ThinkTheme and apply.

PS: thank you DewGew.

Bluetooth Temperature and Humidity sensor

I also own the Xiaomi Mi Temperature and Humidity sensor. This has an LCD to show metrics and Bluetooth connectivity to transfer data. And there is a tool to get those data:

# cd /home/scripts
# sudo -u domoticz git clone https://github.com/pFenners/mijia-sensor-domoticz.git
Cloning into 'mijia-sensor-domoticz'...
(...), done.

Browse to the Hardware section of the Domoticz web page.

Create a dummy switch:

Click the Create Virtual Sensors button and write down the IDX:

Finding the Bluetooth MAC Address for the sensor

You need to identity the BT Mac Address of the sensor:

# hcitool lescan
(...)
xx:xx:xx:xx:xx:xx MJ_HT_V1

Configure and run the script

Edit the script and enter the MAC Address and IDX:

# vim /home/scripts/mijia-sensor-domoticz/domoticz_mijia.py
(...)
print("\n1: updating")
update("xx:xx:xx:xx:xx:xx","idx")

Run the script once to check it works:

# sudo -u domoticz python3 \
  /home/scripts/mijia-sensor-domoticz/domoticz_mijia.py

Finally, add the script to crontab for a regular update:

# crontab -e -u domoticz
*/10 * * * * /usr/bin/python3 \
  /home/scripts/mijia-sensor-domoticz/domoticz_mijia.py \
  2>&1 1>/dev/null

Conclusion

That’s all for the moment. Next steps would be enabling authentication, publishing the Web GUI using TLS on relayd(8), configuring the notifications and scenes, sending some data to a remote InfluxDB server, …

Until then, take care!