Recently, I have started to use parts of my SmartHome to the Zigbee standard.
In the past, I still used sensors and actuators based on the simple 433Mhz radio. Unfortunately, however, there are fewer and fewer sensors and actuators for this "radio standard".
Last but not least, the very reasonable prices for Zigbee accessories at Ikea were the decisive argument for me to switch to this standard.
In addition to these advantages, there are of course other benefits of the Zigbee standard. For example, actuators that are permanently connected to the power supply (e.g. a Zigbee-capable socket) automatically act as repeaters (called "routers" in Zigbee jargon). This means that even very large Zigbee networks (e.g. over several floors) can be set up.
Battery-powered Zigbee devices can achieve very long battery life thanks to the more suitable architecture. A window or door sensor, for example, can be operated for several years with a single CR2032 button cell.
All right, but how can you integrate Zigbee sensors and actuators into your own SmartHome integrate?
A very cool and helpful open source project for this is Zigbee2MQTT.
As the name suggests, zigbee2mqtt is a type of adapter that evaluates the data received via Zigbee and transmits it to your SmartHome forwarded.
How to install zigbee2mqtt on your RaspberryPi I have described in the following article.
I know the following notes are always kind of annoying and seem unnecessary. Unfortunately, many people who knew "better" have lost eyes, fingers or other things due to carelessness or injured themselves. Data loss is almost negligible in comparison, but even these can be really annoying. Therefore, please take five minutes to read the safety instructions. Because even the coolest project is not worth injury or other trouble.
Affiliate links/advertising links
The links to online shops listed here are so-called affiliate links. If you click on such an affiliate link and make a purchase via this link, Nerdiy.de will receive a commission from the relevant online shop or provider. The price does not change for you. If you make your purchases via these links, you support Nerdiy.de in being able to offer other useful projects in the future. 🙂
Before you start with this article, you should have prepared a RaspberryPi so that it is accessible via the network and controllable via SSH. You should also have a MQTT server already installed be.
The following articles describe what needs to be done to prepare the RaspberryPi.
- RaspberryPi – setup for nerdiys!
- RaspberryPi – The first configuration!
- RaspberryPi – Control the RaspberryPi via SSH
In the following lists you will find all the parts you need to implement this article.
|SONOFF Zigbee 3.0 USB Dongle Plus Buy at Amazon
|USB extension cable Buy at Amazon
|Raspberry Pi Buy at Amazon
|Raspberry Pi power supply Buy at Amazon
|Raspberry Pi case Buy at Amazon
|Micro SD card 64GB Buy at Amazon
Log in to the RaspberryPi via SSH
To start, you must first log in to the Rasp Pi with Putty via SSH. How to do this is in the article RaspberryPi - Control the RaspberryPi via SSH described.
Update package management
The package management in Linux is a "central place" through which various software packages can be installed. In order for this to work reliably, the lists and sources of the package management should be updated before each installation of new packages.
sudo apt-get update && sudo apt-get upgrade
Preparing the installation on the Raspberry Pi
The installation of zigbee2mqtt can be started with a few simple bash commands.
But before you can start with the actual installation of zigbee2mqtt you have to install a few required dependencies/libraries.
To do this, execute the following command. This will prepare the installation of NodeJs.
sudo curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
The following command then installs NodeJs including various compilers.
sudo apt-get install -y nodejs git make g++ gcc
Then you need to install the Yarn Package Manager. To do this, execute the following commands on your Raspberry Pi.
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list<br>
sudo apt-get update sudo apt-get install yarn -y
You can then use the following commands to download the zigbee2mqtt repository and set the required directory rights.
sudo git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
sudo chown -R pi:pi /opt/zigbee2mqtt
Set Zigbee2mqtt configuration
After downloading the zigbee2mqtt files, you must now adapt the configuration file to your system.
To ensure that your zigbee2mqtt adapter (coordinator) can be addressed correctly, you must enter the correct path in the configuration file.
Of course, the adapter should already be flashed with the zigbee2mqtt firmware and connected to the Raspberry Pi. You can find information on this in the following blog article, for example.
As soon as the Zigbee Coordinator is connected, you should find out the device path and save it temporarily.
You can display the device path of the connected adapters with the following command.
If you only have one adapter connected, only the device path to your Zigbee adapter will be listed here. If several entries are listed here, it is easiest to first remove all other USB devices, call up the list again and then copy the only existing entry.
In the example shown, the device path you are looking for is the following.
It is now best to save this in a text file. You will need the device path to enter it later in the configuration file.
Now create or open the configuration file of zigbee2mqtt by entering the following command.
The content of the file should look like this. However, you will need to adapt the positions below accordingly.
# MQTT settings mqtt: # MQTT base topic for Zigbee2MQTT MQTT messages base_topic: zigbee2mqtt # MQTT server URL server: 'mqtt://localhost' # MQTT server authentication, uncomment if required: # user: my_user # password: my_password # Serial settings serial : # Location of the adapter (see first step of this guide) port: /dev/ttyACM0 advanced: network_key: GENERATE ikea_ota_use_test_url: true legacy_api: false log_level: debug frontend: port: 8038 host: 0.0.0.0 auth_token: secretPassword123 url: http ://localhost
It is important that you adapt a few settings in the configuration file to your situation.
Customize Mqtt server:
Set the domain name for your MQTT server correctly. To do this, you must change the 'mqtt://localhost' accordingly (if the mqtt server is not running on the same RaspberryPi as zigbee2mqtt).
Customize password to the dashboard:
To protect access to the zigbee2mqtt dashboard, you should set the password to a value known only to you. To do this, change the part auth_token: secretPassword123 accordingly.
Adjust the path to your Zigbee Coordinator:
Sets the device path for the entry
- port: /dev/ttyACM0
to the correct value. You should have copied the appropriate device path in the previous step.
After you have completed all configurations, you can now start the installation of zigbee2mqtt.
You can then start the installation of zigbee2mqtt by entering the bash command
changes to the directory and starts the installation with
If you want you can test the installation of zigbee2mqtt at this point.
To do this, simply execute the following commands.
This will start zigbee2mqtt and you can view the log output.
Zigbee2mqtt then runs as long as the console window is open and connected or you end the program run with CTRL-C.
Configure Zigbee2mqtt as a system service
To ensure that zigbee2mqtt runs without an open console window, you should install a system service.
To do this, create a new configuration file for the system service by entering the following command.
sudo nano /etc/systemd/system/zigbee2mqtt.service
Then insert the text below and save the file using the shortcut keys CTRL-X, Y and Enter.
[Unit] Description=zigbee2mqtt After=network.target [Service] ExecStart=/usr/bin/npm start WorkingDirectory=/opt/zigbee2mqtt StandardOutput=inherit # Or use StandardOutput=null if you don't want Zigbee2MQTT messages filling syslog, for more options see systemd.exec(5) StandardError=inherit Restart=always User=pi [Install] WantedBy=multi-user.target
Set up autostart of Zigbee2mqtt
You can use the following command to activate the autostart of zigbee2mqtt. This will start zigbee2mqtt automatically after starting the RaspberryPi.
Thanks to the already configured system service, you only need to execute the following command to start zigbee2mqtt together with the Raspberry Pi.
sudo systemctl enable zigbee2mqtt.service
You can use the following command to restart your zigbee2mqtt instance.
Enter the following command to restart zigbee2mqtt.
sudo systemctl restart zigbee2mqtt
You can use the following command to stop your zigbee2mqtt instance.
Enter the following command to stop zigbee2mqtt.
sudo systemctl stop zigbee2mqtt
Show log from Zigbee2mqtt
The following command displays the existing log file of zigbee2mqtt. This view is also updated automatically. For example, you can monitor the start process and find errors or display error messages.
Enter the following command.
sudo journalctl -f -u zigbee2mqtt
More articles on the topic
Once you have set up zigbee2mqtt, you should of course now integrate sensors and actuators into your Zigbee network.
The zigbee2mqtt dashboard can be used very well for this purpose. The dashboard is also a very convenient way to administer zigbee2mqtt. The article in which the dashboard is briefly introduced can be found here.
In the following category I have listed some Zigbee devices which can be controlled or read out with the NodeRed code offered there.
I have summarized further articles on the subject of Zigbee or zigbee2mqtt in the following category.
Have fun with the project
I hope everything worked as described for you. If not or you have questions or suggestions please let me know in the comments. I will then add this to the article if necessary.
Ideas for new projects are always welcome. 🙂
PS Many of these projects - especially the hardware projects - cost a lot of time and money. Of course I do this because I enjoy it, but if you think it's cool that I share the information with you, I would be happy about a small donation to the coffee fund. 🙂