Search code examples
grafanainfluxdbgrafana-templatinggrafana-variable

Grafana + InfluxDB Flux - query for displaying multi-select variable inputs


We've set up a Grafana cloud + InfluxDB 2.0 (Flux language) cloud instance. As part of this, we've created a custom variable list with "device IDs", called devices.

In a panel, we wish to display parameter data where the user can select one or more device IDs from the devices list to have them displayed in the panel. This works fine for single device ID selection, but not for multiple devices.

How should the query be modified to display data from a variable number of devices based on a multi-select entry in the dropdown in Grafana?

from(bucket: "test-bucket-new")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "${devices}")
  |> filter(fn: (r) => r["_field"] == "Speed")
  |> aggregateWindow(every: v.windowPeriod, fn: mean)
  |> yield(name: "mean")

Solution

  • It seems the below solves it:

    from(bucket: "test-bucket-new")
      |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
      |> filter(fn: (r) => contains(value: r["_measurement"], set: ${devices:json}))
      |> filter(fn: (r) => r["_field"] == "Speed")
      |> aggregateWindow(every: v.windowPeriod, fn: mean)
      |> yield(name: "mean")