Tag Archives: InfuxDB

Configuring Grafana with Selectable Variables

This is a usage note that collects together a bunch of information that was collected while working on a Grafana Dashboard for an IoT project.

The projects is using Grafana v9.4.9 and querying data in an Influx Database, using the Flux query language.

Getting Location values

The data that is being collected is tagged with a string that describes the location of the sensor. This is separate to the name of the device (dev_id), and this was a deliderate choice so that the device could be maintained and possibly be replaced with a new one, and continue to collect data for a particular locatgion. (The new sensor would also have different callibration data.)

The following query in the variable definition will extract a list of the locations.

import "influxdata/influxdb/v1"
v1.tagValues(
    bucket: v.bucket,
    tag: "location",
    predicate: (r) => true
)

Using Location values

The variable can be used is the dashboard query by using “${location:json}”. The following will filter the data for the selected locations.

from(bucket: "paeiot-bucket")
 |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
 |> filter(fn: (r) => r["_measurement"] == "sensor")
 |> filter(fn: (r) => r["_field"] == "temperature_10")
 |> filter(fn: (r) => contains(value: r["location"], set: ${location:json}))
 |> drop(columns: ["dev_id", "_field", "latitude", "longitude"])
 |> aggregateWindow(every: v.windowPeriod, fn: mean)
 |> yield(name: "mean")

Notice that this query also selects out the range of the data to display as selected from the time range of the dashboard.

Selecting Location Values

To allow the locations of interest to be selected on the dashboard as required, in the definition of the variable screen, select the “Show on Dashboard” option – “Label and Value”. This will automatically add the variable, possible values and selected values to the dashboard, and allow the user to selct between them.

Thanks

A quick thanks to Randall, from the PAE IoT Experimenters for providing the initial screenshot.