How to get Grafana dashboard variables into flux query?

Hi,

in influxdb 1.x I used a dashboard variable to repeat a row of a dashboard.
In influxdb 1.x during creation of the query I get a popup window to choose variables from.

Now in influxdb 2.0 and flux queries I don’t know how to access the dashboard variables to get repeated rows as well:

Grafana_InfluxDB20_repeatrows

I tried also:

|> filter(fn: (r) => r["host"] == ${myvariable})

but got this error:

invalid: compilation failed: error at @8:37-8:38: invalid expression: invalid token for primary expression: ILLEGAL

Can someone please tell me how to get the variable into my flux query?

Thanks a lot…

I don’t know if it works the same way in Grafana, but in InfluxDB 2.0 it just does:

r["host"] == v.myvariable

Hi Franky,

I tried this as well, but it gives me:

invalid: error @8:37-8:38: undefined identifier v

if I just enter v. then I get a list of variables I can choose from like

v.defaultBucket
v.organization
v.timeRangeStart
v.timeRangeStop
etc.

But my dashboard variable is not within this list.

I think you should rather ask the question in the Grafana forum, because I think this is a Grafana problem, how the variables come from the dashboard into the flux queries and how they are called there?

Hi Frank,

yes - I did already.

Try it with:

r["host"] == "${myvariable}"

or:

r["host"] == "myvariable"

Hi Franky,

the 1st one I tried already (I mentioned in my 1st post).
The 2nd one gives me:

invalid: error @8:37-8:49: undefined identifier myvariable

Meantime I found it by myself, instead of:

|> filter(fn: (r) => r["host"] == ${myvariable})

it shoud be:

|> filter(fn: (r) => r["host"] == "${myvariable}")

This is what Franky suggested but I forgot the quotes in my test.
So I just missed the quotes - hope that helps someone

1 Like

This above mentioned line is the solution (the quotes are important)

Hey @Franky1 and @T_Burns,

I have managed to replicate the call of Grafana variables as you stated.
Now i want to build it into a part of a text. What i want to do is a Query that looks like this:

Variable="${MyVariable}"

from(bucket: “MyBucket”)

|> range(start: v.timeRangeStart, stop: v.timeRangeStop)

|> filter(fn: (r) =>

r._measurement == "MyMeasurement" and

r.Generic_Name == "Weight_Indicator_Machine_**{Variable}**" 

)

I couldn’t make it work. Do you have any ideas how this might work?

I hope, it is not working for all. The above syntax does not give me any result though there is no error.

|> filter(fn: (r) => r["host"] == "${data_nodes_prod}")

Instead if I give the following pattern it works fine. I don’t understand what’s the problem exactly.

|> filter(fn: (r) => contains(value: r["host"], set: ${data_nodes_prod:json}))

Any views?

1 Like

great, thanks! using the contains(value: r["host"], set: ${data_nodes_prod:json} syntax worked a treat. thanks!

filter(fn: (r) => r[“host”] =~ /${data_nodes_prod:regex}/

that has superior performance to “contains” and does the same thing