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.
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.
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.
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 #.
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.
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"
when
Channel "mqtt:broker:yourBroker:yourTriggerChannel" triggered
then
var rawevent = receivedEvent.toString.split(" ").get(2).split("#")
var topic = rawevent.get(0)
var payload = rawevent.get(1)
logInfo("Topic", topic + ", " + payload)
end