Howto Setup Zigbee2mqtt on OpenWrt

Yeah it’s running as root, or as privileged+ root when in a container.. weird :frowning:

are you running in VM ?

Generally I run everything on Docker, but I also tested with on-host zigbee2mqtt and it didnt work like that either. :frowning:

UPDATE of some info: for those who use openwrt and current highest version domoticz supported for your device is V4 -the highest version supported for the zigbee2mqtt plugin is 0.2.1
if you loaded the newest one already you can downgrade using

git reset --hard c48d5cf3d64da9cff94bbf3bf2119430e296a712

some issue you may run across is unsupported devices. so you need to add them manually which is not that hard

example say the cheap ewelink/tekber smart plugs

if your device loads properly into zigbee2mqtt ,but not loading into domoticz - ( enable zigbee2mqtt debug) check your domoticz logs activate your device ( press on/off button) it gives you a report - this "device" not currently supported in my case it was ZB-SW01

simply go to ---> /etc/domoticz/plugins/zigbee2mqtt/adapters/

and add your device to the manufacture base it belongs too in this case the tekber/ewelink zigbee device go to ewelink folder

nano __init__.py

and add this line

'ZB-SW01': OnOffSwitchAdapter, # eWeLink Zigbee smart plug

now the cheap zigbee plug is supported in domoticz using the older version 0.2.1 zigbee2mqtt plugin

if you're running and older zigbee2mqtt (<=18 ) and the device is not supported and you can not install a newer version nodejs
then you can update the device.js
cd /opt/zigbee2mqtt/node_modules/zigbee-shepherd-converters

nano devices.js

add this after ----> const devices = [

    //ewelink
     {
     zigbeeModel: ['ZB-SW01'],
        model: 'ZB-SW01',
        vendor: 'eWeLink',
        description: 'Smart switch - 1 gang',
        supports: 'on/off',
        fromZigbee: [fz.generic_state, fz.ignore_onoff_change],
        toZigbee: [tz.on_off],
      },

okay hope that helps those who wish to install zigbee2mqtt on openwrt

I thought I add another zigbee device report --
a tuya WHD02 this device was supported in domotizc by default but not with my version of zigbee2mqtt you need to add to devices.js

cd /opt/zigbee2mqtt/node_modules/zigbee-shepherd-converters

nano devices.js

     {
     zigbeeModel: ['TS0001'],
        model: 'TS0001',
        vendor: 'tuya',
        description: 'Smart switch - 1 gang',
        supports: 'on/off',
        fromZigbee: [fz.generic_state, fz.ignore_onoff_change],
        toZigbee: [tz.on_off],
      },

update : since domoticz does not currently support python 3.10 and only supports 3.4 to 3.8. so you are still currently limited to openwrt19.07.10. but you can make domoticz2022.1 for openwrt see https://forum.openwrt.org/t/domoticz-2022-1-with-functioning-python-plugin/ ( there are precompiled package for cortexA7 cpu ) and for node packages see https://github.com/krywenko/openwrt_19.07.7_cortexA7_node including zigbee2mqtt node package

I am also using Zigbee2MQTT on OpenWRT. To do that, I create a chroot and install it in there. The chroot is made by using debootstrap. I'd like to share this script which can be used to create the userland for a Raspberry Pi 3 or 4:

#!/bin/sh

THIS_CHROOT="/root/zigbee2mqtt"

#chroot is not there, build it
if [ ! -d "$THIS_CHROOT" ]; then
  debootstrap --arch arm64 bullseye "$THIS_CHROOT"
  
  chroot "$THIS_CHROOT" /bin/bash -c "apt install -y curl nodejs build-essential git vim mosquitto"
fi

#inside the chroot, install nodejs, zigbee2mqtt
chroot "$THIS_CHROOT" /bin/bash -c "mount -t proc proc /proc && \
mount -t sysfs sys /sys && \
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && \
apt-get install -y nodejs && \
mkdir /opt/zigbee2mqtt && \
git clone --depth 1 https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt && \
cd /opt/zigbee2mqtt && \
npm ci"

To create the image for a RPI3B I use this script:

#!/bin/bash

#Documentation: https://openwrt.org/docs/guide-user/additional-software/imagebuilder

HOST="rpi3.lan"
OUTPUT="$(pwd)/images"
BUILDER="https://downloads.openwrt.org/releases/22.03.2/targets/bcm27xx/bcm2710/openwrt-imagebuilder-22.03.2-bcm27xx-bcm2710.Linux-x86_64.tar.xz"

KERNEL_PARTSIZE=$((2*1024)) #Kernel-Partitionsize in MB
ROOTFS_PARTSIZE=$((5*1024)) #Rootfs-Partitionsize in MB

# download image builder
if [ ! -f "${BUILDER##*/}" ]; then
	wget "$BUILDER" || exit 1
	tar xJvf "${BUILDER##*/}" || exit 1
fi

mkdir "$OUTPUT"
cd openwrt-*/ || exit 1

# list all targets for this image builder, consider 'make help' as well
#make info

# clean previous images
make clean || exit 1

#adjust partition sizes
sed -i "s/CONFIG_TARGET_KERNEL_PARTSIZE=.*/CONFIG_TARGET_KERNEL_PARTSIZE=$KERNEL_PARTSIZE/g" .config || exit 1
sed -i "s/CONFIG_TARGET_ROOTFS_PARTSIZE=.*/CONFIG_TARGET_ROOTFS_PARTSIZE=$ROOTFS_PARTSIZE/g" .config || exit 1

# Packages are added if no prefix is given, '-packagename' does not integrate/removes a package
make image  PROFILE="rpi-3" \
           PACKAGES="luci-base luci-ssl luci-mod-admin-full luci-theme-bootstrap \
                     htop debootstrap debian-archive-keyring gpgv2 block-mount \
                     openssh-sftp-server \
                     mosquitto-client-ssl mosquitto-ssl screen socat unshare kmod-usb-serial-cp210x ip-full kmod-veth \
                     shadow-useradd tar" \
            BIN_DIR="$OUTPUT" || exit 1
1 Like

Hello, I ran your first script in a Raspb4.
Once finished the long installation, what the next step is ?? In my folder /opt/ is not the zigbe2mqtt folder

Thanks for your help

Hello @kaciker,
In case /root/zigbee2mqtt exists it should contain a Debian "userland", which is what is all files that can be considered the operating system without the Linux-Kernel. To get a Bash-shell into that Debian-userland execute chroot /root/zigbee2mqtt /bin/bash. To test if you are really inside the chroot execute the command apt, which is the Debian package manager not present in OpenWRT. If the command exists it will output some info, which is all we need as confirmation for being inside the chroot. From there cd /opt/zigbee2mqtt and execute zigbee2mqtt from there interactively.

I can only guess without having more details:

  • Did you check /root/zigbee2mqtt/opt/zigbee2mqtt is present or the real root-directory of OpenWRT?
  • Did you see the script creating a chroot environment or did it fail within the chroot when downloading NodeJS or the git-repository?

If none of the questions point you in the right direction, can you please provide the output of the command and paste it in this thread?

1 Like

@Torxgewinde was my fault, I'm not familiar with chroot, I followed your last instructions, and set the configuration file with a ezsp driver IP sonoff wifi. z2M is working perfectly in its last version 1.28.2.
Amazing work ¡¡¡¡ :smiling_face_with_three_hearts:

Can you teach me to create the command to start the z2m from the openwrt command line, in order to set in the openwrt boot?
I tried with chroot /root/zigbee2mqtt cd /opt/zigbee2mqtt && npm start but is not working

@kaciker Good to hear that it works :+1:

There are several options to run a command. Here i like to use screen, so I can see at a later stage the output of the command without flooding syslog.

Since npm and NodeJS raise red flags in regards of lax security, I did not exactly run it as follows. I left out the steps to use namespaces and unshare, but it should work as follows:

Put into /etc/rc.local:

#start the script run.sh in chroot and create a screen-session called "zig":
screen -d -S zig -m sh "chroot /root/zigbee2mqtt bash /root/run.sh"

The script /root/zigbee2mqtt/root/run.sh can contain the following:

#!/bin/bash

function cleanup {
	echo "Inside: cleaning up 🧹"
	umount /proc
	umount /sys
}
trap cleanup EXIT INT

mount -t proc proc /proc
mount -t sysfs sys /sys

cd /opt/zigbee2mqtt || exit 1
npm start || exit 1

exit 0

Optional: To see the output enter the screen session with: screen -dRR zig. To leave the session without closing it press <CTRL>+a d

Option #2: If you are interested in isolating Z2M using unshare there is another post: Notes on using namespaces with unshare. Using unshare is uncommon, lxc and docker are a lot more common. On OpenWRT there is also ujail, but I haven't tried it enough yet to make it work and it lacks some documentation IMHO (Documentation on using procd-ujail / ujail?).

HTH!

1 Like

Hi, I have found this thread, looking for a way to install zigbee2MQTT in by openWRT router.
I would like to install it there as the router is an allways on device quite reliable and if the router is down, anyway you are not going to have LAN or WLAN connections among the IOT devices, so it would not improve the up time to install it in other device.

My router is a belkin RT3200 [OpenWrt Wiki] Linksys E8450 (aka. Belkin RT3200)

I tried to install it there. I had to install some modules first in order to follow instalation instructions, ar, git, git-http, sudo, node and node-npm.

My first problem was trying to install it in /opt/zigbee2MQTT. I could not do the git clone there.
The root partition seems to be quite scarce of space.

I tried in /tmp that is where openWRT modules like wireguard seems to be installed.
There is about 45 MB free.

I could do the git clone but during npm install I received messages that there is no left space, it gets full.

Is there a solution?
Is there a way to reduce the space needed?
I see that in the repository there are many files probably not needed in this environment.

Is there a way to do the npm install and javascript compiling in a desktop environment and only install the compiled files?

A way of expanding disk space? What is that chroot that I read about here?

And the most important: do you think it would be a capable enough device for running Z2M an MQTT?
There won't be too much iot devices, and not too active, some TRV valves, some wifi thermometers... may be about 12 or 15 at most.

I removed the directory where I cloned z2M and the installed packages, git, node, etc... but I think I have not recoverd all free space I had at the beginning.

Does npm install copy files to other places that are not removed when the install fails and after removing npm and node themselfs?

Thank you for any advice.

easiest method get a usb hub plug it into the usb port of the router and insert a usb drive of some form, for extra storage and still have room for your zigbee stick

opkg install block-mount kmod-fs-ext4 kmod-usb-storage e2fsprogs kmod-usb-ohci kmod-usb-uhci fdisk

if you have node and npm installed form openwrt packages. git cloning zigbee2mqtt is the easiest and faster route

1 Like

Thank you, I don't have a usb zigbee stick, It runs in a zigbee concentrator and would be access via tcp serial. That concentrator would be the only router probably in the zigbee net (if it gives enough coverage for all the house a not too big flat).

Main concern is would this router have enough power to run zigbee2MQTT, does it consume too much resources in memory and processor?
Thir router has only a 2.0 usb, would it have enough speed? I suppose it will, as zigbee has only a 250 Kbps thoughtput.

You have to install all that to simply mount the usb and acess it as a drive?
May I install node and git in the usb drive?
I have used luci to install them and it does not let you select the directory to install them.

I would like to install all what is needed to run z2m in that external drive, node, node-npm, git, git-http and z2t itself.

zigbee2mqtt uses it own zigbeestick not sure about what the "concentrator" is. which sounds like a a zigbeehub of someform.. so i can not comment on that and even if it works with zigbee2mqtt.. but perhaps domoticz support the zigbee hub so no need for zigbee2mqtt to be installed . But as to storage you install the storage as an overlay it gives additional space equal to the size of the stick across all directories . so you just install as you would normally ..

Excuse me, typo, I wanted to say the zigbee coordinator, that in this case would be only a zigbee wirless to ethernet bridge, as zigbee2mqtt is the coordinator and would connect to it via TCP serial.

It is an athom ZG01 which can act as zigbee coordinator but it would provide Zigbee 2 tasmota, different message format than z2M (and not as easy to use in HA).

I have no experience with overlay storage (well yes, in android and do not like it two much, because you don't know where each file is stored).
I would prefer to add is as a mount point and install all z2M related files in that stick. So if it gets disconnected or fails, I would know that only zigbee would be affected.

Is it possible?

I will try, would seed a usb pen and test.

when it done as an overlay . if you pulled the usb stick it just reverts to what the router was originally before it was inserted .. if you have node and node npm installed currently then and git clone make and gcc then not a problem to install as directory.. install the above opkg I mentioned above once installed you see under system "mount points" install as overlay or as directory.. if you want you can build in the package on your computer follow direction for openwrt build first

and then nxhack

it take probably about 24 hrs or so

Thanks for your advices.
Well npm node an git will fit in the system as it is now, but I would prefer to left space just in case I need to install other things later, so would like to install them in the usb better.
making a hardlink prior or soft link to a directory in the usb would let install npm in the usb instead in local system using luci?

My concern about overlays is not knowing where each package would be installed, if I install them in the usb, I know what will fail if the usb fails.

If I understand you well, if you add an overlay of a directory all that you install in that directory after configuring the overlay would be written to the usb instead of local system, even if there is space left in the local system.

Is it like that? I have to read a bit more about overlay ...

If I compile all in an external PC, would I only need to upload the "compiled" z2M javascript?
Would it take much less space?

I seems quite complicated to compile it... will see.

when compiled the opkg zigbee2mqtt package is 5.5 meg as you can see from my github dir for sunix devices
https://github.com/krywenko/openwrt_19.07.7_cortexA7_node/blob/main/node-zigbee2mqtt_1.24.0-1_arm_cortex-a7_neon-vfpv4.ipk
you can do what I mentioned above install as overlay install all your necessary files to build it then compile zigbee2mqtt . then tar compress it and scp off the device to your computer . then remove all the gcc make and packages you no longer need then remove the overlay . after that you can untar the zigbee2mqtt to a USb then install it as mount point. you need to run zigbee2mqtt atleast once on the overlay as it does not finish the compiling until the after the first start

1 Like

Sounds a good plan, use a usb as overlay and install all packages needed and do the compilation, seems less complicated than compiling in the PC.

But I am not sure or being able to do the compilation and then what files are needed to run z2m and what are not.

Will make some tests and investigate it a bit. Thank you.