Global state topic for MQTT Things in openHAB 3

Dear openHABians,

I’m currently setting up a new openHAB3 environment.
I decided to use zigbee2mqtt for ZigBee devices and want to create a matching mqtt.things file for openHAB.

This is my mqtt.things file I set up for an air quality sensor:

Bridge mqtt:broker:mosquitto1 [ host="127.0.0.1", secure=false, username="openhab", password="XXX" ]

//Frient Air Quality Sensor - Develco AQSZB-110
Thing mqtt:topic:frientairqual1 "Frient Air Quality Sensor" (mqtt:broker:mosquitto1){
    Channels:
    Type number : voc "Measured VOC value" [ stateTopic="zigbee2mqtt/0x0015bc00360010b9", transformationPattern="JSONPATH:$.voc"]
    Type number : temperature "Measured temperature value" [ stateTopic="zigbee2mqtt/0x0015bc00360010b9", transformationPattern="JSONPATH:$.temperature"]
    Type number : battery "Remaining battery in %" [ stateTopic="zigbee2mqtt/0x0015bc00360010b9", transformationPattern="JSONPATH:$.battery"]
    Type switch : battery_low "Indicates if the battery of this device is almost empty" [ stateTopic="zigbee2mqtt/0x0015bc00360010b9", transformationPattern="JSONPATH:$.battery_low"]
    Type string : air_quality "Measured air quality" [ stateTopic="zigbee2mqtt/0x0015bc00360010b9", transformationPattern="JSONPATH:$.air_quality"]
    Type number : linkquality "Link quality (signal strength)" [ stateTopic="zigbee2mqtt/0x0015bc00360010b9", transformationPattern="JSONPATH:$.linkquality"]
}

Is it possible to set a “global stateTopic” for a Thing or to use variables? So I need to set the device-id one time per thing?

Thank you for your support!
Jonas

AFAIK, it isn’t possible to have one global state topic per Thing.

I wrote a tool to reduce repetitive typing such as this by templating the thing and items file and generating them. It’s handy when having lots of similar devices. GitHub - jimtng/openhab-itemsgen: Generate .things and .items files for OpenHAB using pre-set templates.

Also you might want to look into using “attribute” output:

advanced:
  output: attribute

You can do it with one (!) string channel by linking multiple Items to this channel.
Then add the JSONPATH statement as profile. Type of Item can differ from string, but the channel must be of type string, as it keeps the complete JSON object.

2 Likes

Hi Udo,
this sounds good, but I think I don’t understand it.
Can you please give an example?
Thank you!

Sure.

Thing:

Thing mqtt:topic:frientairqual1 "Frient Air Quality Sensor" (mqtt:broker:mosquitto1){
    Channels:
    Type string : json "json object" 	[ stateTopic="zigbee2mqtt/0x0015bc00360010b9" ]
}

Items:

Number AQS_voc			"VOC"				{channel="mqtt:topic:frientairqual1:json"[profile="transform:JSONPATH", function="$.voc"] }
Number AQS_temperature	"Temperature"		{channel="mqtt:topic:frientairqual1:json"[profile="transform:JSONPATH", function="$.temperature"] }
Number AQS_battery		"Remaining Battery"	{channel="mqtt:topic:frientairqual1:json"[profile="transform:JSONPATH", function="$.battery"] }
Number AQS_linkQuality	"Link Quality"		{channel="mqtt:topic:frientairqual1:json"[profile="transform:JSONPATH", function="$.linkquality"] }
String AQS_airQuality	"Air Quality"		{channel="mqtt:topic:frientairqual1:json"[profile="transform:JSONPATH", function="$.air_quality"] }
Switch AQS_batteryLow	"Battery Low"		{channel="mqtt:topic:frientairqual1:json"[profile="transform:JSONPATH", function="$.battery_low"] }

Please be aware that this configuration is old style Text format, but it’s also possible to do the same configuration via UI

1 Like