Skip to content

Using variables to create a template dashboard

Overview

Variables allow you to create more interactive and dynamic dashboards by replacing hard-coded elements such as assets and aspects in your metric queries with variables. A variable is a placeholder for a value that you can use in metric queries and panel titles.

If you have multiple assets that share the same asset type, it is useful to create a reusable dashboard. This makes it possible to quickly browse through all of the assets/aspects by using variables. When you change a value by selecting from the drop-down list at the top of the dashboard, the panel’s metric queries change accordingly.

Types of Variables

  • Query Variables: Dynamically fetch values from a data source, enabling flexible data selection.
  • Custom Variables: Hold predefined values for specific use cases.
  • Text Box Variables: Allow users to input custom values directly.
  • Constant Variables: Store a fixed value that remains static.

Key Concepts

  • Asset: Represents a physical or logical entity in an IoT environment, such as a machine or sensor. Assets have unique identifiers and attributes like name, description, and type.
  • Aspect: A specific characteristic or feature of an asset, often containing multiple variables for individual data points or measurements.

Variables help users dynamically select assets, aspects, and properties, offering a customizable dashboard experience.

Setting Up Variables in the Dashboard

Steps to Add and Configure Variables

The following sections describes the steps to add and configure variables:

  1. Go to Dashboard Settings:
  2. Open the required dashboard.
  3. Click on the gear icon to access settings.
  4. Navigate to the "Variables" section.

  5. Add a Variable:

  6. Click on "Add variable".
  7. Choose the variable type:

    • Query
    • Custom
    • Text Box
    • Constant
  8. Configure the variable:

  9. Name: Provide a unique identifier for your variable.
  10. Label: Add a descriptive label for easier identification.
  11. Description: Optionally, include details to explain the variable's purpose.

Query Variables Configuration

Query variables are the most versatile in Grafana, dynamically fetching values from a data source to enable filtering and customization.

  • Data Source: Select the data source for the variable.
  • Query: Enter a syntax-specific query to retrieve relevant values.
  • Regex: Use a regular expression to filter the results (e.g., pump.* to include assets named pump_1, pump_2, etc.).
  • Sort: Specify sorting of results (for example, alphabetically or numerically).
  • Refresh: Determine when the variable updates its values (e.g., on dashboard load or time range change).
  • Multi-value: Allow multiple values to be selected.
  • Include All Option: Add an "All" option to select all values at once.

Adding and Using IoT Variables

IoT dashboards commonly use Asset and Aspect variables, enabling dynamic value substitution in panels.

  1. Adding an Asset Variable:
  2. In Dashboard Settings, click "Variables" and then click "Add Variable". Enter the name (e.g., Asset) and select a data source.
  3. In the "Query Options", enter * as the query to fetch all assets, or use a regex filter (e.g., pump.*).
  4. Review values under Preview of values to verify correctness.
  5. Click "Add" to include the variable in the dashboard, which will display an “Asset” dropdown.

  6. Adding an Aspect Variable:

  7. In Dashboard Settings, click "Variables" and then click "Add Variable**. Enter the name (e.g., Aspect).
  8. For Query, use assetName to retrieve aspects related to the selected asset.
  9. Use regex as needed to filter specific aspects.
  10. Preview values to ensure accuracy, then click "Add."

  11. Using Asset and Aspect Variables:

  12. In a new data query, use $Asset and $Aspect for dynamically updating the selected asset and aspect.
  13. Adjust queries as required by replacing static values with the relevant variable placeholders.

Additional Features

  • Sorting Variable Values: Sort options can be set to alphabetically or numerically to organize variable values.
  • Transformation Options with Variables:
    • Navigate to "Transformation Data".
    • Select "Add transformation" and then click "Add field from calculation".
    • Use variables in the operation field for flexible data manipulation.

Last update: December 5, 2024

Except where otherwise noted, content on this site is licensed under the Development License Agreement.