Additional parameter in MQTT Persistence

My setup:
I have installed openHAB on a Pi and everything is fine. Now I want to send itemstate updates via mqtt to a Broker. I have used the MQTT Persistence and after a short time it works well.

As topic I use the second parameter “topic=%2$s” where I safe the room and the sensortype in the item alias.
Example: <bath/motion> as item alias for the motion sensor in the bath and my topic for mqtt is ready.

Now my problem is that I want to send the “hostname” from my Pi at the first position in the topic.
Example: topic=“hostname”/“room”/“sensortype”.

Of course I can make it as String, but I want it dynamically as variable, so that openhab automatically set the hostname if I write something like “$HOSTNAME”.
Is that possible?

I think there are two ways to do this.
First I can write it in the item alias.
Or maybe there is an extra parameter (5$) for the hostname and I can write directly in the mqtt-persistence. OK, I know that doesn’t work in the moment, but maybe somebody can expand the mqtt binding for this wonderful feature. :slight_smile:

Maybe someone has another better approach. I am very open for creative ideas. :wink:

Thanks in advance!

I am not sure if I entirely understand you. Do you want to do this from mqtt messages you send from openhab? or from mqtt messages that go to openhab

I want to do this from mqtt messages I send it FROM openHAB to a Broker.

I will make a little example in an application case:
I have two appartments with openHAB home automation. There is a Pi with openHAB in each appartment. In addition, I still have a Broker where I will send the itemstates of every motion sensor from each apartment.
Now I would like to have the same configuration files on each Pi. So I can’t set the “hostname or IP” in the configuration files of the Pi.
If I am a Subscriber on the Broker now, I only see at the topics “roomName”/“sensortype” (corridor/motion for example) but not from whom the message comes. Unfortunately there is no parameter in the MQTT protocol for the Publisher. So I want it make directly in the topic like “hostnameOfThePi”/“roomName”/“sensortype”.

Is there a way for it?

I hope with the example it is more clearly what I mean.
But if something is still unclear, then ask quietly.

An alternative method to do what you seek is: The MQTT-EventBus integration

Check an example here: [OH2] Control ESP8266 Relay using MQTT Eventbus

Docs entry:

You can define the topics in any style that you want.

This may be better for your use case (instead of using MQTT Persistence)

Thank you for the fast answer.

I already thought about the MQTT-Eventbus in the planning phase, but unfortunately this does not meet my requirements.

In my use case is the subscriber no openHAB instance! And there is no way to send the HOSTNAME in the topic.

Furthermore I do not want all events to be sent.
I only want to publish selected events. So I think MQTT-Persistence is the only way to do that.

OK thanks, I get it.
I am afraid I can’t help you with that. I would think maybe a JSON carrying the required variable would be a last resort but that isn’t really a friendly way, nor exactly what you want

I have make an Issue in eclipse/smarthome on GitHub.

If somebody have a create idea to solve my problem, it can still post here anyway.