Now we have a working Raspberry Pi 3 with Z-Way it’s time to connect it to the backbone of Thuis: MQTT. For this I developed a custom userModule/app. In this post I’ll explain what it does, how to use it and how it works.

Goal

The app enables Z-Way to send and receive MQTT messages. Whenever the status of one of the selected devices changes it will be published to a topic. Based on these topics some other topics are available to change the status of the devices or request a status update.

How to use

Install

Z-Way MQTTZ-Way allows developers to create userModules and publish them to their App Store. Unfortunately they still didn’t reply to my submit, so it’s available straight away. Luckily they do have a beta-token mechanism, so it’s still possible to install it. I’ve used BaseModule by @maros as basis, so you’ll need to install this as well.

To install it follow these steps:

  1. Go to the Management page of your Z-Way
  2. Open the App Store Access tab
  3. Add mqtt_beta as token
  4. Now go to Apps, and then Online Apps
  5. Search for Base Module and install it
  6. Search for MQTT and install it

Configure

MQTT PublicationWhen you go to the settings of the app you’ll find quite some options. The first section are the basic settings needed for MQTT: the client ID and hostname/port/username/password of the MQTT broker. Next are the common topic prefix (we’ll use Thuis/device for now) and postfixes for requesting a status update and for setting the status of a device.

More interesting is the second section, which are the publications. When you add a publication you first select a type: a single device or tagged devices. We’ll use the latter for now. Now you will add some tags to select which devices will be part of this publication.

Next is deciding on the topic for the publication. You can use two placeholders: %deviceName% and %roomName%. They will be replaced by the actual values for the corresponding device. In this example we’ll use the topic %roomName%/%deviceName%.

The last option is wether or not to publish status updates as retained messages, we’ll turn it on.

Receive status updates

Now you’ve configured the Z-Way MQTT app so we can use it to receive updates on our devices. We’ll use mosquitto_sub to demonstrate this:

As you can see a devices called ‘Counter Switch’ in the room ‘Kitchen’ was turned on and off, the dimmer in the office was turned down, and the luminescence sensor in the kitchen gave an updated status.

Interact

There are two interactions available: requesting a status update and setting a value. Both can be demonstrated using mosquitto_pub. To request the status of the dimmer in the office publish a message to Thuis/device/office/dimmer/status with an empty message:

When you subscribe to the dimmer’s topic in the meanwhile you receive the new status:

Then to set the dimmer to 60% you send the message 60 to Thuis/device/office/dimmer/set:

The light will turn on and you’ll see the status changed:

Currently values between 0 and 100 are supported for dimmers (devices supporting the SwitchMultiLevel command class) and on/ off for switches (devices supporting the SwitchBinary command class).

How does it work

Z-Way apps are made in JavaScript. Their basic structure is defined in the developer manual. As said before I’m using the BaseModule as basis as it provides some useful functions for interacting with devices, for example filtering status updates to includes only updates with actual changes. The full project can be found on my GitHub as Zway-MQTT. Here I’ll explain some interesting parts of the code.

MQTT client

From a userModule you can call out to external utilities, but only when you give explicit permission. I would like to avoid this, so I searched for a pure JavaScript solution. Most JavaScript-based MQTT libraries use WebSockets, which is unfortunately not available in Z-Way. Luckily I found a module by @goodfield. He found and modified a MQTT client for use within Z-Way. I cleaned it up and started using it:

Publishing status updates

To receive updates we have to subscribe to updates sent by the Z-Way core. I’m using the modify:metrics:level event, which is the filtered version from BaseModule.

In updateDevice I retrieve the new value from the device and transform it if needed. Then I’ll look up all publications matching this device (for example because they are tagged with the configured tag) and execute a MQTT publish for them.

You’ll notice the createTopic call, this call takes care of merging the prefix and configured topics plus it replaces the placeholders. Device and room names are camel cased to have nice and valid topics.

Reacting on interaction

To react on status requests and new values the app will subscribe to all topics starting with the prefix. It will then filter out the actions. If it receives an action message it will try to find the corresponding publication and device bases on the topic. It will then take the requested action.

We can now easily talk to Z-Wave devices through MQTT messages. Later I’ll add support for more command classes, like thermostat and power usage. Someone already requested battery level updates through GitHub, which is an interesting addition as well.

Publishing activity from Z-Way to MQTT
Tagged on:             

4 thoughts on “Publishing activity from Z-Way to MQTT

Leave a Reply

Your email address will not be published. Required fields are marked *

Fork me on GitHub