MQTT template variables
Use template variables to create dynamic dashboards where the MQTT topic changes based on user selections. Instead of hard-coding topic paths, you can use variables to let users switch between devices, rooms, or sensors from a drop-down at the top of the dashboard.
For general information about template variables in Grafana, refer to Templates and variables.
Before you begin
- Ensure you have configured the MQTT data source.
- Familiarize yourself with Grafana variable types.
Supported variable types
The MQTT data source doesn’t provide a variable query handler, so query-based variables aren’t available. You can use the following variable types to build dynamic topics.
Use variables in the topic field
Reference variables in the Topic field of the query editor using $variable or ${variable} syntax. Grafana replaces the variable with its current value before subscribing to the topic.
For example, if you create a Custom variable named room with the values bedroom, kitchen, and living_room, you can set the topic to:
home/$room/temperatureWhen a user selects kitchen from the drop-down, the plugin subscribes to home/kitchen/temperature.
Create a variable for MQTT topics
To create a variable that controls which topic the panel subscribes to:
- Open the dashboard and click Dashboard settings (gear icon).
- Click Variables in the left menu.
- Click Add variable.
- Set Variable type to Custom.
- Enter a Name (for example,
device). - In Custom options, enter a comma-separated list of values (for example,
sensor-01, sensor-02, sensor-03). - Click Apply.
- In your panel’s query editor, use the variable in the Topic field (for example,
factory/$device/metrics).
Combine multiple variables
You can use more than one variable in a single topic to build fully dynamic paths. For example, with variables location and metric:
sites/${location}/sensors/${metric}This lets users independently select both the location and the metric type from separate drop-downs.



