MQTT wildcards, and how to read them?

Hey all!

I'm trying the following...

MQTT input subscribed to /some/topic/+/state

...and I need to get the wildcard (+) name and the value of the topic. This name hold the name of a sensor and becouse I have loooots of sensors I want to keep it simple and parse them all the same way.

When I attach a debugger to the node, I can see the complete topic, but I just need the wildcard part.

/some/topic/13245/state : msg.payload : string[1]
"0"

So I need to get the "12345" part as a separate msg or variable.

You will need to extract the info from the string.

A change node with either a regex or JSONata will do the job. Split the string on "/" then from the resulting array, you need element 2.

1 Like

Thanks for the tip! Got as far as splitting some stuff up, but I can't seem to get the regex part right.
If I use a split, I get many messages, and need a way to discard all but the Nth of them.

no don't use a split node.... you need a change node as Julian said.
(or simple function node with msg.payload = msg.topic.split("/")[3]; I think (as you have a leading /

1 Like

That seems to be the best solution, I guess it's also faster than using a regex?

A Change node will be more efficient than a simple function node, as function nodes have a significant inherent overhead. However it is most unlikely that you need to worry about efficiency unless you are trying to handle thousands of messages per second, so do whatever you feel most comfortable with.

I theory but I very much doubt it is enough to spot.

It would be interesting if someone had time to do a test of the different ways - but sadly, not enough time for me to do it.