Use part of MQTT Topic as Variable

Hey Guys,

I finished my Setup for a nice Dashboard which I want to use for about 32 Sensors (basically the same Dashboard 32 times).
Now I am trying to figure out the Variables, which would seem like they are the best practice to go about this.
The Sensors send MQTT Data into Mosquitto ->NodeRed-> Influx and then Grafana. Using the Adhoc Filter I can select every Topic (which means +100 Options because each Sensors send Data on multiple Topics).

Could it be possible that the User just selects part of the Topic?
So if the Topic is built like A/B/C/D/E, the User would just have to select the Value for B for the whole Dashboard.

For example, the Topic is always :
Machine/(Variable which User Selects)/Data/Energy/Meter1
Machine/(Variable which User Selects)/Data/Runtime/timer1
and the User would just select one Variable and it fills in both of these Topics so the same Machine is selected.

Is this possible? All the examples in the documentation dont have mqtt topics but rather already separated fields.
If this is needed, could I split up the Topics in Nodered and feed the Data into Influx this way to be able to select it individually?
Right now, my Nodered template node consists of
“network,device={{topic}} value={{payload}}”
would it even be possible to split up the Topics this way?

I am Grateful for any Help I receive

Hi @hazenjaqdx3 your question intrigued me, since MQTT support in Grafana is still in its infancy, so I was wondering what your use case is. If I’ve understood what you wrote, your question is really about how to query an Influx database, where the value of a tag (device) is derived an MQTT topic. (but the fact the data comes from MQTT is largely irrelevant).

I think you can certainly do what you want, and you have two options:

You can use a regular expression in your WHERE clause. Something like

SELECT value FROM network WHERE device =~ /^Machine\/${variable}\/ ... 

I’m only half-guessing what your query probably looks like (and half-guessing how the / in the topic needs to be escaped inside the regular expression) but hopefully this gives you some ideas.

The other option is indeed to split the topic into separate tags within NodeRed. That would be a better overall solution. (but is probably outside the scope of the Grafana forum)

1 Like

Edit:
Sorry I just saw that the Annotations which I need will not change (I need them Machine Specific) therefore this whole Idea cannot work for us anyways. Thank you for the Help though!