---
title: "InfluxDB template variables | Grafana documentation"
description: "Guide for template variables in InfluxDB"
---

# InfluxDB template variables

Instead of hard-coding details such as server, application, and sensor names in metric queries, you can use variables. Grafana displays these variables in drop-down select boxes at the top of the dashboard to help you change the data displayed in your dashboard. Grafana refers to such variables as template variables.

For additional information using variables and templates, refer to the following documentation:

- [Variables](/docs/grafana/latest/dashboards/variables/)
- [Templates](/docs/grafana/latest/dashboards/variables/)
- [Add and manage variables](/docs/grafana/latest/dashboards/variables/add-template-variables/)
- [Variable best practices](/docs/grafana/latest/dashboards/variables/add-template-variables/#variable-best-practices)

## Use query variables

By adding a query template variable, you can write an InfluxDB metadata exploration query. These queries return results such as measurement names, key names, and key values.

For more information, refer to [Add a query variable](/docs/grafana/latest/dashboards/variables/add-template-variables/#add-a-query-variable).

To create a variable containing all values for the `hostname` tag, use the following query format in the **Query** variable:

SQL ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```sql
SHOW TAG VALUES WITH KEY = "hostname"
```

## Chain or nest variables

You can also create nested variables, sometimes called [chained variables](/docs/grafana/latest/dashboards/variables/add-template-variables/#chained-variables).

For example, if you have a variable named `region`, you can configure the `hosts` variable to display only hosts from the selected region using the following query:

SQL ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```sql
SHOW TAG VALUES WITH KEY = "hostname"  WHERE region = '$region'
```

You can also fetch key names for a given measurement:

SQL ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```sql
SHOW TAG KEYS [FROM <measurement_name>]
```

If you have a variable containing key names, you can use it in a **GROUP BY** clause. This allows you to adjust the grouping by selecting from the variable list at the top of the dashboard

## Use filters

InfluxDB supports the **Filters** variable type. This variable type allows you to define multiple key/value filters, which Grafana then automatically applies to all your InfluxDB queries.

For more information, refer to [Add filters](/docs/grafana/latest/dashboards/variables/add-template-variables/#add-ad-hoc-filters).

## Choose a variable syntax

The InfluxDB data source supports two variable syntaxes for use in the **Query** field:

- `$<varname>` - This syntax is easy to read and write but does not allow you to use a variable in the middle of a word or expression.
  
  SQL ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
  
  ```sql
  SELECT mean("value") FROM "logins" WHERE "hostname" =~ /^$host$/ AND $timeFilter GROUP BY time($__interval), "hostname"
  ```
- `${varname}` - Use this syntax when you want to interpolate a variable in the middle of an expression.
  
  SQL ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy
  
  ```sql
  SELECT mean("value") FROM "logins" WHERE "hostname" =~ /^[[host]]$/ AND $timeFilter GROUP BY time($__interval), "hostname"
  ```

When you enable the **Multi-value** or **Include all value** options, Grafana converts the labels from plain text to a regex-compatible string, so you must use `=~` instead of `=`.

### Templated dashboard example

To view an example of a templated dashboard, refer to this [InfluxDB example dashboard](https://play.grafana.org/d/f62a0410-5abb-4dd8-9dfc-caddfc3e2ffd/eccb2445-b0a2-5e83-8e0f-6d5ea53ad575).
