Custom thing type

I have many real world devices which are all similar, and talked to over mqtt. All have exactly the same interface to them.
With my cpp background I would typically make an abstract base class for the commonality, so this is my mindset.
The current code I have for a sonoff device that I will use for this example in my mqtt.things is:

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhab", password="password", clientID="openHAB2server" ]
{
    /////////////
    // Sonoffs //
    /////////////
    Thing topic lounge_airwick2 "Airwick" @ "Lounge" 
    {
    Channels:
        Type string : reachable "Reachable"            [ stateTopic="sonoff-airwick-lounge/tele/LWT" ]
        Type switch : power     "Power"                [ stateTopic="sonoff-airwick-lounge/stat/POWER",   commandTopic="sonoff-airwick-lounge/cmnd/POWER" ]
        Type number : rssi      "WiFi Signal Strength" [ stateTopic="sonoff-airwick-lounge/tele/STATE",   transformationPattern="JSONPATH:$.Wifi.RSSI"]
        Type string : ota       "OTA URL"              [ stateTopic="sonoff-airwick-lounge/tele/otaurl",  commandTopic="sonoff-airwick-lounge/cmnd/otaurl"]
        Type string : fw        "FW Version"           [ stateTopic="sonoff-airwick-lounge/tele/status2", commandTopic="sonoff-airwick-lounge/cmnd/status"]
        Type switch : upgrade   "Firmware Upgrade"     [ stateTopic="sonoff-airwick-lounge/tele/upgrade", commandTopic="sonoff-airwick-lounge/cmnd/upgrade"]
        Type switch : restart   "Restart"              [ stateTopic="sonoff-airwick-lounge/tele/restart", commandTopic="sonoff-airwick-lounge/cmnd/restart"]
	}
}

Extending this to many other similar devices means copy and pasting the Thing, over and over again and changing the topic prefix (sonoff-xxxx-xxxx).

Is there a way I can abstract this definition, or perhaps create a custom Thing that just requires this prefix, and the rest of the definition is populated as per the template I define?

Even something like this would be better for me:

Bridge mqtt:broker:mosquitto "Mosquitto" [ host="localhost", port=1883, secure=false, username="openhab", password="password", clientID="openHAB2server" ]
{
    /////////////
    // Sonoffs //
    /////////////
    Thing topic lounge_airwick2 "Airwick" @ "Lounge" 
    {
    Channels:
        Type string : hostname "sonoff-foo-bar"
        Type string : reachable "Reachable"            [ stateTopic=hostname+"/tele/LWT" ]
        Type switch : power     "Power"                [ stateTopic=hostname+"/stat/POWER",   commandTopic=hostname+"/cmnd/POWER" ]
        Type number : rssi      "WiFi Signal Strength" [ stateTopic=hostname+"/tele/STATE",   transformationPattern="JSONPATH:$.Wifi.RSSI"]
        Type string : ota       "OTA URL"              [ stateTopic=hostname+"/tele/otaurl",  commandTopic=hostname+"/cmnd/otaurl"]
        Type string : fw        "FW Version"           [ stateTopic=hostname+"/tele/status2", commandTopic=hostname+"/cmnd/status"]
        Type switch : upgrade   "Firmware Upgrade"     [ stateTopic=hostname+"/tele/upgrade", commandTopic=hostname+"/cmnd/upgrade"]
        Type switch : restart   "Restart"              [ stateTopic=hostname+"/tele/restart", commandTopic=hostname+"/cmnd/restart"]
	}
}

Open to other suggestions if there is a better way of course.

Thanks

Short answer is no, there is no way to do this.

I suppose you could follow some “discoverable” MQTT standard like Homie.

I think Tasmota or ESPEasy supports the Home Assistant standard which also supports discovery by the binding.

Many thanks for the replies.
I feared there was no templating mechanism but worth an ask.
Just to say, I used sonoff as a familiar example but I have other devices with fw I’ve written, mainly in the ESP IDF too.
Auto discover is something I’ve used before but decided to move back to config files. I found the names auto discover gives to things and items non intuitive for setting up rules.
It would be ideal if I could define a prototype for a “Thing topic”. I’d happily help so this for the project if desired by you and told which file(s) need working on.
Cheers for the suggestions though.

Oh, one thing I will ask additionlly, if I may. I feel the answer is probably going to be a no again:

Where I have

Bridge mqtt:broker:mosquitto {...

Is it possible to split this between multiple files, using the same bridge?
So I am thinking one file is for sonoff Things, one for esp32s running firmware foo Things, another for esp32s running firmware bar Things. But they all use the same bridge/mosquitto?

Yes you dont have to put the “Thing” inside the “Bridge {}”.

You have to specify the bridge in brackets ()

Thing mqtt:topic:tasmota-plug-1 "Tasmota Plug-1 MQTT" (mqtt:broker:127_0_0_1_1883) {
    Channels:
        Type string : state "State" [ stateTopic="tele/tasmota-plug-1/STATE" ]
        Type switch : power "Power" [ stateTopic="stat/tasmota-plug-1/RESULT", transformationPattern="JSONPATH:$.POWER", commandTopic="cmnd/tasmota-plug-1/Power", on="ON", off="OFF" ]

}
1 Like

@SpaceGlider that’s brilliant thank you. Certainly helps to reduce having one mega file!

Got it all working.

Thank you everyone for your help