How to get topic of MQTT


i’m using openHAB2.
and, I have a question using MQTT binding.

I have an item that subscribes to all topic with MQTT binding.
Therefore, this item can subscribe the data that published to multiple topics.

I know that header of MQTT protocol contains topic information.

So, how to get a topic information from openHAB2? not payload.

for example,

String exam "[%s]" {mqtt = "<[mqtt:#:state:default]"}

this item subscribes to all topics.

Suppose the topic “/openHAB2/topics” publish data.
Is there a way to get information on the topic “/openHAB2/topics” in openHAB2?

1 Like

Nope, not possible I am afraid.
What are you trying to do?


I want subscribe as above.

When published through several topics, rule tries to update multiple items according to the topic.

“/openHAB2/A” is update item A
“/openHAB2/B” is update item B.

What are you trying to accomplish, at a higher level. What is doing the publishing? Why do you think this approach is easier than configuring each Item to subscribe to their topic individually and not need a Rule? Are you aware of the MQTT Eventbus? Assuming you have control over the topics, this may already be something built in without your need to write a Rule.

Sorry for the delay in my replying.

My team is trying to use mqtt in the above way. So I asked the above question…

Sadly I don’t know about mqtt-eventbus. I saw the document, but I didn’t understand it. What role does mqeventbus.cfg play? Can topic information be extracted through this setting?

Any my question is why? There may be a better way to accomplish the same thing without needing to extract the topic, which is impossible.

The event bus was developed to sunchronize two OHs with each other. As part of that it will automatically update or command Items that match the name in the topic. So if you configured your publisher to use the Event Bus topic naming scheme you might be able to update and command the Items without needing to configure each of the Items with MQTT.

The “correct” solution is to configure the Items with the appropriate mqtt topic and not use rules but the Event bus config might be useful.

Hopping on an old topic…

We have an RF device that publishes to a different topic which varies by button press. Because each button has a different “code”, defining an Item for every new device for each particular button is problematic.

homie/xyz/serial01/EV1527/02e1a2 {"CMD":"ON"}
homie/xyz/serial01/EV1527/02e1a8 {"CMD":"ON"}

Using homie/xyz/serial01/# or homie/xyz/serial01/+/+ returns the JSON payload data, but would also need to know the “wildcard” portions of the topic to handle properly.


Assuming MQTT 2, you can create a Channel on the Broker Thing (not a separate Thing) with the wild card subscription, you can then trigger a Rule with the Channel trigger. There will be an implicit variable called receivedEvent that will have the full topic name and the message separated by a #.

It’s somewhat loosely documented here.

You will still have to deal with parsing out the topics and the messages and doing the appropriate thing in the Rule, but you wouldn’t need to create any Items at all.

I’ve not done this myself so this is all theory talking, but I believe this should work.

This is also the key to creating the equivalent to the MQTT EventBus using the MQTT 2 binding.

1 Like

This publish trigger channel works great, I use it for both my nodered and snips communication if you have any questions.

Thanks Rich. And thanks Johannes for the assistance offer. First order of business is giving mqtt2 a whirl. Still using OH1 binding.

Great news!
@JGKK Can you provide code sniplet(s) of config(s) to get the full topic in receivedEvent ?

this logs the topic and the payload of any received message if you substitute the trigger channel with the one that you created in your mqtt broker thing:

rule "Topic and Payload"
    Channel "mqtt:broker:yourBroker:yourTriggerChannel" triggered
    var rawevent = receivedEvent.toString.split(" ").get(2).split("#")
    var topic = rawevent.get(0)
    var payload = rawevent.get(1)
    logInfo("Topic", topic + ", " + payload)

Regards Johannes