When repeating a panel using a Dashboard Variable, how do I use the variable in the query?

  • What Grafana version and what operating system are you using?

8.3 on Linux

  • What are you trying to achieve?

I have a Bar Gauge panel that repeats based on a Dashboard array Variable. I want the gauge to use the current value of the variable in the query, and use the returned value for the gauge.

  • How are you trying to achieve it?

I created a dashboard variable that returns an array containing each instance of a Redis key (i.e. $my_var = [0,1,2,3] if there are 4 instances of the key). My Redis database will contain a variable number of branches based on the number if installed sensors, and each branch will contain a key with the value I want to use.

I created a Bar Gauge panel that’s set to repeat based on that variable. So in the case above, it will repeat 4 times. I put $my_var in the panel title, and the title is correct for each repeated panel (Sensor 0, Sensor 1, …).

I created a Redis GET query to retrieve the value from a Redis key. The variable is used in the key name (GET api:$my_var:voltage). I also have two more queries that retrieve the MIN and MAX values, but those queries don’t use the variable.

In the “Value Options” section, in the “Fields” drop-down, I’m choosing my query as the field to display. However, in the drop-down, it displays as “api:0:voltage” instead of “api:$my_var:voltage”.

  • What happened?

Only the first panel in the repeating series works properly, the rest show “No data”. It seems as if every repeated panel is trying to use the results from a query “api:0:voltage” instead of “api:$my_var:voltage”.

  • What did you expect to happen?

I expect each panel to query the value from the correct Redis key and use that value in the Gauge. i.e. On the second panel, which has the title “Sensor 1”, I expect it to retrieve the value from “api:1:voltage” and display that value in the gauge.

1 Like

Is it not possible to use a Dashboard Variable inside of the “Field” property of a gauge?

I tried manually editing the panel JSON. Here is the relevant portion of the JSON:

"options": {
    "reduceOptions": {
      "values": false,
      "calcs": [
        "lastNotNull"
      ],
      "fields": "/^api:0:voltage$/"
    },
    "orientation": "horizontal",
    "displayMode": "lcd",
    "showUnfilled": true
  },
  "targets": [
    {
      "command": "get",
      "datasource": {
        "type": "redis-datasource",
        "uid": "AB23G85"
      },
      "keyName": "api:$my_var:voltage",
      "query": "",
      "refId": "A",
      "type": "command"
    },
    {
      "command": "get",
      "datasource": {
        "type": "redis-datasource",
        "uid": "AB23G85"
      },
      "hide": false,
      "keyName": "api:voltage_threshold_min",
      "query": "",
      "refId": "B",
      "type": "command"
    },
    {
      "command": "get",
      "datasource": {
        "type": "redis-datasource",
        "uid": "AB23G85"
      },
      "hide": false,
      "keyName": "api:voltage_threshold_max",
      "query": "",
      "refId": "C",
      "type": "command"
    }
  ],

The query (“target”) is correct, it’s using the variable in the query. But the “fields” property in the “options” object is using a static value of “0”.

I tried modifying the “fields” property to "/^api:$my_var:voltage$/", but then none of the panels displayed data. When I changed it to "/^api:1:voltage$/", the second panel started working and the first panel stopped working. From doing this, I can tell the query is working properly; it’s pulling the correct value from Redis for each panel, based on the dashboard variable.

So I just need to know how to get a dashboard variable to work in the “fields” property of the gauge panels.

It is possible, I do it all the time.

first check that you are using the correct syntax:

Variable syntax | Grafana documentation

and that you have selected enable multiple options:

and of course, the repeat panel is referencing the variable. also you may need to save first then reload the page to be able to see the repeated panels with their specific values.

Grafana will interpolate whatever value is in you array inside the query, for influxdb I use the values like this:


 |> filter(fn: (r) => r["field"] == "${Rows2}")

where :
Rows2 = [value1, value2, value3]

If I do inspect query it looks like this:
Panel1:

 |> filter(fn: (r) => r["field"] == "value1")

Panel2:

 |> filter(fn: (r) => r["field"] == "value2")

Panel3:

 |> filter(fn: (r) => r["field"] == "value3")

On most recent versions of grafana (9.4) I noticed that if I create a new variable like that It does not always take the “All” values of the array if you have the variable hidden. you need to unhide, select all, save and hide it again to have the variable with “All” values selected.