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:
- Go to Dashboard Settings:
- Open the required dashboard.
- Click on the gear icon to access settings.
-
Navigate to the "Variables" section.
-
Add a Variable:
- Click on "Add variable".
-
Choose the variable type:
- Query
- Custom
- Text Box
- Constant
-
Configure the variable:
- Name: Provide a unique identifier for your variable.
- Label: Add a descriptive label for easier identification.
- 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 namedpump_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.
- Adding an Asset Variable:
- In Dashboard Settings, click "Variables" and then click "Add Variable". Enter the name (e.g.,
Asset
) and select a data source. - In the "Query Options", enter
*
as the query to fetch all assets, or use a regex filter (e.g.,pump.*
). - Review values under Preview of values to verify correctness.
-
Click "Add" to include the variable in the dashboard, which will display an “Asset” dropdown.
-
Adding an Aspect Variable:
- In Dashboard Settings, click "Variables" and then click "Add Variable**. Enter the name (e.g.,
Aspect
). - For Query, use
assetName
to retrieve aspects related to the selected asset. - Use regex as needed to filter specific aspects.
-
Preview values to ensure accuracy, then click "Add."
-
Using Asset and Aspect Variables:
- In a new data query, use
$Asset
and$Aspect
for dynamically updating the selected asset and aspect. - 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.
Except where otherwise noted, content on this site is licensed under the Development License Agreement.