Nested variables with InfluxDB

Hi everyone :slight_smile:

I have a question regarding nested variables within my influx-database.

  • First of all i am running a grafana cloud instance (version 8.5.2.) on my linux machine.

  • I want to have 3 variables that each access the previous ones. (For example: var1 is not nested; var2 is looking what value var1 has; var3 is looking what var1 and var2 has. With this sequence I want to filter my data so i can get the precise sensor and select which exact _tag i want to visualize.

  • My bucket inside influx has this structure:

  1. "example-bucket":
  2. _measurement: list of sensors
  3. _tags=(“stateofactive”, “stateofbattery”): 2 tags which show if the sensor is active and the battery percentage
  4. _field=(“Longitude”, “Latitude”, “measurementofthesensor”): 3 fields which show long and lat as well as the measurement of the specific sensor (f.ex. humidity => 30 %)
  • Right now i have created 3 variables which look like the following:

The first variable is working…

#Variable named Sensors, giving me every _measurement value (This variable is good)

import "influxdata/influxdb/schema"
schema.measurements(bucket: "example-bucket")

But these two are not, i constantly wont get the distinct values from the tags which should be nested…

#Variable named IsSensorActive. This one should give me every stateofactive tag value according to the _measurement which is selected (This variable is not working for me)

import "influxdata/influxdb/v1"
v1.measurementTagValues(
    bucket: "example-bucket",
    measurement: "${Sensors}",
    tag: "stateofactive",
)
#Variable named BatteryPercentage. This one should give me every stateofbattery tag value according to the _measurement & tag=stateofactive which is selected (This variable is not working for me)

import "influxdata/influxdb/v1"
v1.measurementTagValues(
    bucket: "example-bucket",
    measurement: "${Sensors}",
    stateofactive: "${IsSensorActive}",
    tag: "stateofbattery",
)

  • So these variables wont look at the variables which should be nested, therefore i dont get the selection. Another thing i tried is the code below, but this only gets me every tag value of stateofactive. But not regarding the _measurement i selected.
import "influxdata/influxdb/schema"
schema.tagValues(bucket: "example-bucket", tag: "stateofactive")
  • What can i do to solve this? I looked up nested variables inside the influxcommunity and documentation but did not get any results.

Lastly another question regarding the query inside a panel. If i want to query within these nested variables what is the correct syntax ? Because i have the following:

from(bucket: "example-bucket")
  |> range(start: -7d, stop:now())
  |> filter(fn: (r) => contains(value: r["_measurement"], set: ${Sensors:json}))
  |> filter(fn: (r) => r["stateofactive"] == ${IsSensorActive})
  |> filter(fn: (r) => r["_field"] == "measurementofthesensor")
  |> yield(name: "last")

and this wont work either

Thank you in advance :slight_smile: Greetings Grafu

Problem is solved.

The nested variables can be described as following:

from(bucket: "example-bucket")
|> range(start: v.timeRangeStart)
|> filter(fn: (r) => contains(value: r["_measurement"], set: ${var1:json}))
|> filter(fn: (r) => contains(value: r["tag1"], set: ${var2:json}))
|> filter(fn: (r) => contains(value: r["tag2"], set: ${var3:json}))
|> keyValues(keyColumns: ["tag3"])
|> group()
|> keep(columns: ["tag3"])
|> distinct(column: "tag3")

a linked subject to this was discussed here: