Zigbee2Mqtt / MQTT Thing templates

Playing around with Z2M, I am wondering about a few things:

Thing definition / channels “database”

For a given device, I can define a new Thing with channels based on the MQTT topics. Z2M provides great doc, that with the UI make it very helpful to dev, test and troubleshoot. Is there a “database” for the standard devices ? So I could cimply copy/paste the standard channels for a given device ?

Thing templating

It is common do have several devices of a given type (I mention A1Z here but it could be any). Do I need to define every single of my NOUS A1Z as a new Thing and define (again) all the channels or is there a way to create a template and feed only the device ID to the template ?

In the case of Z2M, the ID is actually the device’s name and Thing differ only from:

  • Thing ID
  • ID (ie Z2M name / MQTT topic)
  • Name

I’m doing so…
create one thing, go to code tab and copy the code.
create a new thing, and paste the code with changed friendly name…
Not a template but faster then clicking through the ui
Greets…

Yes, I guess this is option 1 but that does not feel great, it introduces lots of duplicate code and is prone to errors due to the volume. I bet you already ran into issue because you forgot to replace the ID on one line, or such an error.

No, not for MQTT.

But I beleive that Z2M supports the Home Assistant MQTT standard and OH can discover devices that follow that standard (among others). If you do that you don’t need to manually create any Things, just accept them from the Inbox.

Indeed, you have to define each and every Thing individually if you are not using Discovery to autocreate the Things.

I played around a bit with Things when I migrated to Zwave-js (Zwave to Mqtt) and grouped my OH “Things” by function. After the first one I just copied the code tab and changed the node. Probably not for everyone. Also have a summary page of battery widgets

UID: mqtt:topic:f06f8352c2:Batteries
label: Battery Levels
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:253a91e196
location: Battery Levels
channels:
  - id: Basement_Door_Sensor_Battery_n
    channelTypeUID: mqtt:number
    label: Basement Door Sensor Battery
    configuration:
      stateTopic: zwave2/Basement_Door_Status/battery/endpoint_0/level
      transformationPattern:
        - JSONPATH:$.value
  - id: Master_Bath_Sensor_Battery_n
    channelTypeUID: mqtt:number
    label: Master Bedroom Sensor Battery
    configuration:
      stateTopic: zwave2/Bath_Sensors/battery/endpoint_0/level
      transformationPattern:
        - JSONPATH:$.value
  - id: Key_fob_Battery_Level_n
    channelTypeUID: mqtt:number
    label: Key Fob Battery Level
    configuration:
      stateTopic: zwave1/Controller_-_8/battery/endpoint_0/level
      transformationPattern:
        - JSONPATH:$.value
      unit: "%"
etc, (I have 17 devices with a battery reading.)

Confirmed, that is what I use for my 10+ devices. Though the most recent one has a bug in the channel code that had me go back to the manual method :frowning: not sure if this is a z2mqtt issue or an home assistant issue or an openHAB issue :confused:

here my 2 cents:
Due to the reasons mentioned above and in spite of being a big UI-fan I use file-based definitions for my mqtt-things (sorry Rich :grimacing:)
All the channels and especially the meta-data (Alexa…) is defined for all things at once.
And you find many examples here in the community e.g. here

Although I’m currently not using the MQTT/HA binding/transform I have been following its development and @ccutrer has done a lot of work over the last couple of months to improve its usefulness and applicability both for Zwave to MQTT (Zwave-js) and Zigbee2MQTT. It is a good option.

1 Like

Like @apella12 I moved my Zwave configuration to Zwave-js. Now all 70+ of my Zwave devices are mqtt things created via the UI. It was quick and easy despite having devices from at least 4 different manufactures and dozens of different actual devices.

I didn’t use full thing templates because I have so many different things. Instead, I created a bank of each different channel I needed:


Then for each new thing I just create the generic thing via the UI, copy and paste the channels I need, change the node numbers and it’s done. If I need to go back and add a channel I didn’t use previously, it’s just another quick cut and paste into the code tab.

With a good editor you can use search and replace…
works good for me.
Greets…

Do you need separate steps for metadata like state descriptions, Alexa parameters, widget configs or is this possible all at once?

Maybe we should open a new thread for the Zwave-js. I did not know about that one and it sounds also very interesting. The question from this thread will remains also so the Zwave-js are not totally out of topic but it sounds worth making a home for the topic so it stands out.
I found also this thread and it looks like a good place to keep going the non “template” discussion.

I am still resisting the UI temptation for a few reason:

  • I don’t really “see” what of where the UI does things
  • I do like having .items and .rules as code/text, I am using git to track down my changes
  • As mention, I heavily use (some kind of…) search/replace and I find it very useful

The reason why I asked the question is that I now have a few Thing defined and tested, and I now need to copy/paste the definition for a gazillion of the same devices (but with other ids obviously) with very little changes.

Reading this thread, I am still hesitiating between 2 options:

  • Use tera and make a script/repo so I can quickly generate the definitions as text for my gazillion devices, pointing to a template for the device and a yaml with the raw infos for each device. Advantage: less errors, Drawback: maybe a bit overkill for a thing I will personally do likely only once
  • Use copy/paste and replace carefully to build the list. Advantage: simple. Drawback: error prone.

Thank you all for the interesting feedback based on all the various experience, this is very valuable.

Imho the nice option would be to create the template and the logic inside the same file as a comment and then generate the file content outside of the comment block.
Sounds like a pretty nice side project and if done properly can also be used to build items files.
If someone would build that I can imagine using it.

On the other hand:
I created a dummy entry with an arbitrary id and copy pasted it as often as I needed it.
Then I always selected one of the blocks and then did a search replace in the current selection.
Took a little bit longer but to be honest the zwave thing definitions are relatively stable …

Is it allowed to make corrections to the channel uid in the thing.thing file afterwards? Do I have to unlink the item before.

Those are all further downstream steps at the item level. I do have a full documentation of all of my custom metadata namespaces which includes a template for the metadata yaml:


There’s a copy icon in the upper right corner of that template so I that while I am referencing the doc I can also quick copy the template and then go to the metadata config page to paste and configure.

1 Like

Yes, just edit the thing file and then edit the items file. There is no need to edit the items file first.

1 Like