Zwavejs2mqtt snap package

Hi,

For you Ubuntu lovers and those who thinks using Snap packages is smooth and easy (so OK! To everyone having Snapd installed!);

I’ve made a Snap package of zwavejs2mqtt. I choose this instead of the bare zwave-js-server that communicates with HA, since zwavejs2mqtt includes this - and you’ll get a UI to work with while waiting for the HA UI interface to grow :slight_smile: Simply just disable the MQTT Gateway and enable the WS server.

To install the package, simply run

sudo snap install zwavejs2mqtt

This should download and install everything for you. Since the snap is strictly confined, you’ll have to grant access to raw_usb and hardware-observe so you can get a list of available ports in the UI and communicate with your USB device.

You’ll do this by running

sudo snap connect zwavejs2mqtt:raw-usb
sudo snap connect zwavejs2mqtt:hardware-observe

Set your network key with

sudo snap set zwavejs2mqtt network.key="...key here..."

(see zwavejs2mqtt.help for every option, but I recommend setting most of them in the UI).

For the first time I’ll advice you to run sudo zwavejs2mqtt.exec before daemonizing it, just to check that everything boots fine and that you’re able to configure everything - without any complains in the terminal.

Head to localhost:8091 (change localhost with the client IP if you’re running from another device) and make your changes in the UI and follow the output in the terminal for errors!

When everything seems fine, exit the command (on Ubuntu CTRL+X from the terminal) and run the following to daemonize it:

sudo zwavejs2mqtt.enable
3 Likes

Hi, how to modify it, as snap files are read only? Thank you.

/snap/zwavejs2mqtt/323/lib/node_modules/zwavejs2mqtt/server/config/app.js

1 Like

Hi @pw44 , why would you modify this file?

If you want to modify settings, modify the file in /var/snap/zwavejs2mqtt/current/settings.json. There is also some commands made available, e.g

sudo snap set zwavejs2mqtt server.host='0.0.0.0'
sudo snap set zwavejs2mqtt server.port=1234

See zwavejs2mqtt.help for more info.

Hi @joachimmg , thank you so much.

that did the trick.

when i snap set zwavejs2mqtt server.host=‘192.168.28.4’, where is it stored?

Thank you!

Hi, in /var/snap/zwavejs2mqtt/current/settings.json :slight_smile: (note this path starts with /var and is not the same as the read-only path /snap/zwavejs2mqtt/curreent).

current is a symlink to the revision of the snap you’re running, e.g. 329. So within the /var/snap/zwavejs2mqtt/ directory you’ll also have a backup of some of the previous settings from before an update of the snap. :slight_smile: But I prefer to use current so I know that I’m editing the «current files».

@joachimmg thank you. i setted server.host to 192.168.28,4, and searched, but did not find where it was stored.

@joachimmg
I’m trying to connect snap to a Z-Pi7 on a Raspberry Pi4, the tty is ttyAMA0

root@z-wavejs:~# dmesg | grep ttyAMA0
[ 1.781596] fe201000.serial: ttyAMA0 at MMIO 0xfe201000 (irq = 38, base_baud = 0) is a PL011 rev2

After installing it wants me to do a “sudo snap connect zwavejs2mqtt:serial-port”, but i don’t have a clue how.

Hi,

Sorry my delayed response. This doesn’t show up as a USB device? E.g /dev/ttyAMA0? I don’t have it myself installed on any device, so I haven’t been able to test it.

You might have to use the serial-port interface for it, yes. But I recommend trying to just set the path to /dev/ttyAMA0 first.

You might have to turn on hotplug support to use it:

Enabling hotplug

Hotplug is currently an experimental feature that needs to be enabled. First ensure you have a recent snapd installed, either via the core snap or the snapd snap, at least 2.39.

Then enable hotplug support with the following command:

$ sudo snap set system experimental.hotplug=true

You will need to either restart snapd (with sudo systemctl restart snapd, for example), or reconnect USB devices to initiate hotplug processing for connected devices.

No it doesn’t show up as a USB device nor as a PCI device because it’s connected to the GPIO of the Pi4

root@z-wavejs:~# lsusb
Bus 002 Device 002: ID 174c:1153 ASMedia Technology Inc. ASM1153 SATA 3Gb/s bridge
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 046d:c312 Logitech, Inc. DeLuxe 250 Keyboard
Bus 001 Device 003: ID 046d:c046 Logitech, Inc. RX1000 Laser Mouse
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
root@z-wavejs:~# lspci
00:00.0 PCI bridge: Broadcom Inc. and subsidiaries BCM2711 PCIe Bridge (rev 20)
01:00.0 USB controller: VIA Technologies, Inc. VL805 USB 3.0 Host Controller (rev 01)
root@z-wavejs:~#

i allready tried “sudo snap connect zwavejs2mqtt:ttyAMA0 ttyAMA0” and “sudo snap connect zwavejs2mqtt:/dev/ttyAMA0 /dev/ttyAMA0” but without luck

With the docker zwavejs2mtqq i can connect to it without problems

Hi,

that is not the correct way. Since snap is running within an isolated environment you need to give it access to specific interfaces.

You either need to use the raw-usb or the serial-port interface. And you’re connecting them like this:

sudo snap connect zwavejs2mqtt:raw-usb

or

sudo snap connect zwavejs2mqtt:serial-port

Using the serial port

The latter one, which I think you need to use, requires hot-plug support to be enabled first:

sudo snap set system experimental.hotplug=true
sudo systemctl restart snapd

If you have a valid serial device and its available to snapd it should be listed under «slots» by the following command:

snap interface serial-port

My list is like this

name:    serial-port
summary: allows accessing a specific serial port
plugs:
  - zwavejs2mqtt
slots:
  - snapd:aeotecz-stickgen5zw0 (allows accessing a specific serial port)

Look at it as plugs (e.g zwavejs2mqtt) is the consumer and slots (your device) as the producer, that can be connected accordingly.

Now connect to the one you have, in my case it’s snapd:aeotecz-stickgen5zw0, to Z2M:

sudo snap connect zwavejs2mqtt:serial-port <slot name>

(swap <slot name> with the name of your device)

Set the device-path in the settings

Now remember to edit zwave details in the settings. This can be done by accessing http://<hostname>:8091/settings where hostname could be localhost if it’s a local instance or the IP of the host if its running on another machine.

The service must of course be running to access it, either

  • as a background daemon: sudo zwavejs2mqtt.daemonize
  • in the foreground: sudo zwavejs2mqtt.exec

The second will exit the application when you exit the command, while the daemon will auto-start on boot and keep the process running in the background. Do not use both at the same time!

However if you just installed Z2M and are debugging and restarting the app frequently, the foreground method is nice.

You’ll get the output directly in your terminal - most likely with colors.

For the background service you can access logs with

sudo snap logs zwavejs2mqtt -f

(note the -f = follow)

Set the device-path through the shell

An alternative method to set the device is to edit the settings.json file within the shell of the application, like this:

sudo snap run --shell zwavejs2mqtt
cat <<< $(jq '.zwave.port="/dev/ttyACM0"' $SNAP_DATA/settings.json) > $SNAP_DATA/settings.json

(swap out /dev/ttyACM0 with your device, e.g /dev/ttyAMA0)

Now exit the shell and restart zwavejs2mqtt with

exit
sudo snap restart zwavejs2mqtt

Hi, sorry - to enable the daemon, you will use sudo zwavejs2mqtt.enable and not sudo zwavejs2mqtt.daemon. Sorry for the confusion!

Hi @Goldwing, just out of curiosity, did you manage to get it working?

I hope it is OK to tack on to this thread - I am having a similar issue.

Trying to run zwavejs2mqtt as a snap package in an LXD container on an Ubuntu host.

The device is a USB Aeon Labs Z-Stick S2. On the physical host…

djwhyte@server1:~$ lsusb
Bus 002 Device 005: ID 18d1:9302 Google Inc. 
Bus 002 Device 003: ID 0764:0501 Cyber Power System, Inc. CP1500 AVR UPS
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 006: ID 0624:0249 Avocent Corp. Virtual Keyboard/Mouse
Bus 001 Device 005: ID 413c:a001 Dell Computer Corp. Hub
Bus 001 Device 004: ID 10c4:ea60 Silicon Labs CP210x UART Bridge
Bus 001 Device 003: ID 0480:a00c Toshiba America Inc 
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Command I used to pass this device through to the LXD container (which is the mechanism I successfully used to pass the device through to my hass container for the now deprecated version of the Zwave integration)…

lxc config device add zwave z-stick unix-char source=/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0 path=/dev/ttyUSB0 mode=666

The device is indeed available in the container but from within the snap package there is no access to the device…

root@zwave:~# ls -al /dev/ttyUSB0 
crw-rw-rw- 1 root root 188, 0 May 13 12:50 /dev/ttyUSB0
root@zwave:~# sudo snap run --shell zwavejs2mqtt
Missing plug: «serial-port»
root@zwave:/root# ls -al /dev/ttyUSB0 
---------- 1 root root 0 May 13 12:50 /dev/ttyUSB0
root@zwave:/root#

This results in zwavejs2mqtt not being able to open the USB device with the following continuously repeated in the logfile…

2022-05-14T00:00:20.152Z DRIVER   starting driver...
2022-05-14T00:00:20.154Z DRIVER   opening serial port /dev/ttyUSB0
2022-05-14T00:00:29.168Z DRIVER   Failed to open the serial port: Error: Permission denied, cannot open /dev/tty
                                  USB0
2022-05-14T00:00:29.169Z DRIVER   destroying driver instance...
2022-05-14T00:00:29.170Z DRIVER   driver instance destroyed

Initially, I followed the steps available at ZwaveJS2Mqtt and thus issued the commands…

snap connect zwavejs2mqtt:raw-usb
snap connect zwavejs2mqtt:hardware-observe

And after reading this thread, I also issued the commands (though not sure I actually needed to)…

snap set system experimental.hotplug=true
systemctl restart snapd
snap connect zwavejs2mqtt:serial-port

This is current status…

root@zwave:~# snap interface serial-port
name:    serial-port
summary: allows accessing a specific serial port
plugs:
  - zwavejs2mqtt
root@zwave:~# snap interface raw-usb
name:    raw-usb
summary: allows raw access to all USB devices
plugs:
  - zwavejs2mqtt
slots:
  - snapd

Any hints on how to get this working?

Cheers :slight_smile:

Interestingly - I removed the device from my LXC container. I deleted the snap from the container. Re added the device and the snap and it seems to be working this time. :man_shrugging:

1 Like

Glad you got it working.

The serial-port is not often needed. raw-usb should be enough, but if you’re using a HAT for a RPi you have to alter your system:

Hi Joachim,

Your latest update to the snap broke things on my end. None of the executables (i.e. zwavejs2mqtt.help, zwavejs2mqtt.enable) seem to exist any more.

I’m running Ubuntu 20.04 if that matters.

Edit: now the snap is disabled.

Edit 2: I can’t remove to re-install, it tells me auto-refresh in progress.

Tried to abort the update job, now it’s just hanging.

Edit 3: rebooted and the snap status became “Undone”, and I was able to do a snap refresh

Hi Robert,

Sorry my late reply. Everything seems to be working great on all my 10+ instances, on a variety of OS’es.

My guess is that your Snapd instance is hanging on the update process. You can try to sudo snap abort and then do a new update sudo snap refresh zwavejs2mqtt :slight_smile:

Hi Joachim,

Yes, I eventually figured that out.

I can’t seem to log in via github to post an issue, so I guess I’ll post it here:

I’m getting log spam every 15 seconds from apparmor:

[50482.661687] audit: type=1400 audit(1657038762.162:13541): apparmor="DENIED" operation="exec" profile="snap.zwavejs2mqtt.zwavejs2mqtt" name="/usr/bin/ip" pid=24916 comm="sh" requested_mask="x" denied_mask="x" fsuid=0 ouid=0
[50482.661748] audit: type=1400 audit(1657038762.162:13542): apparmor="DENIED" operation="exec" profile="snap.zwavejs2mqtt.zwavejs2mqtt" name="/usr/bin/ip" pid=24916 comm="sh" requested_mask="x" denied_mask="x" fsuid=0 ouid=0
[50482.661770] audit: type=1400 audit(1657038762.162:13543): apparmor="DENIED" operation="exec" profile="snap.zwavejs2mqtt.zwavejs2mqtt" name="/usr/bin/ip" pid=24916 comm="sh" requested_mask="x" denied_mask="x" fsuid=0 ouid=0
[50482.661789] audit: type=1400 audit(1657038762.162:13544): apparmor="DENIED" operation="exec" profile="snap.zwavejs2mqtt.zwavejs2mqtt" name="/usr/bin/ip" pid=24916 comm="sh" requested_mask="x" denied_mask="x" fsuid=0 ouid=0

It’s 3 times per process every 15 seconds. Disabling MQTT doesn’t fix it.

Thanks,
Rob

Upgrading to Ubuntu 22.04 fixed it.

How do I set the websocket listen address to be a particular interface versus 0.0.0.0?

1 Like