Computations
Computations give you the ability to derive new values and insights from your data while it is in transit. Because the data already passes through the broker, the broker executes the computation directly. There is no extra network hop and no separate compute infrastructure to operate. Instead of exporting raw data to a separate system to calculate a result and feed it back, you define the logic once in the namespace and the platform computes it on the connected brokers as data flows.
This approach reduces the effort to build and maintain extra systems. It keeps your derived values close to the definitions that describe them: the computation, the data it draws on, and the surrounding context all live in the same namespace.
A computation lets a namespace node derive its value from other tags, rather than read it directly from MQTT.
For example, you can take a raw temperature tag in Celsius and produce a new corrected_temperature tag in Fahrenheit.
Computations run on the connected brokers, close to the data, according to the declaration in your namespace. This guide shows how to create a computation, step by step.
Before You Begin: Assign a Traffic Origin
A computation runs on a connected broker. Before you create a computation, set the traffic origin for the site that contains the input tag.
-
In the namespace tree, select the site node, such as
berlin. -
In the panel header, click Edit.
-
Under Traffic Origin, select the broker that carries the traffic for the site, such as
HQ Broker. -
Click Save.
| If the input tag has no traffic origin, HiveMQ saves the computation without a broker assignment. The Data Sources tab shows the warning Cannot assign a broker. After you set the traffic origin, HiveMQ assigns the broker automatically. For more information, see Assign a Broker to a Calculation or Expression. |
Step 1: Create a Tag for the Computed Value
Create the tag that holds the result of the computation.
-
In the namespace tree, select the parent node, such as the
conveyor-beltfolder. -
Next to the parent node, click +, then click Tag.
-
Enter the Node Name and MQTT Topic Level, such as
corrected_temperature. -
Select the Data Type and Data Serialization Format, such as
DecimalandUTF8. -
Click Create Child Tag.
Step 2: Choose the Data Source
By default, a tag reads its value from MQTT. A computed tag draws its value from other tags instead, so you change its data source.
Open the new tag’s Data Sources tab and select the source type. You can compute a value in two ways:
-
Calculation: an easy-to-use, predefined set of functions. Best for common operations.
-
Expression: a free-form formula for more complex computations.
| Both calculations and expressions build on the same set of functions. See the functions reference for everything available. |
| A tag can also take its value from a Link, which mirrors the values of another node instead of computing a new one. A tag holds only one computation, expression, or link at a time. For all four source types, see Data Sources. |
This example uses an Expression to convert Celsius to Fahrenheit.
Step 3: Write the Expression
Click Expression and enter the formula in Expression Content. For the literals, operators, and syntax rules available in a formula, see Expression Language. To convert Celsius to Fahrenheit, use:
(celsius * 9/5) + 32
The platform parses the formula and lists each name it finds under Detected Variables. In this case, a single variable named celsius.
| The variable name is just a placeholder in the formula. In the next step, you map it to the tag that supplies its values. |
Step 4: Map Each Variable to a Tag
Before you can save the computation, you must map each detected variable to an existing tag.
-
Under the
celsiusvariable, click Add Source to open the Select an input for celsius dialog. -
In the dialog, enter a topic prefix or node name to find the input tag.
-
In the results, select the
temperaturerow to map the variable and close the dialog.
After you map every variable, click Create to save the computation.
| To compute over a tag’s stored history rather than its latest value (for example, a moving average), enable the Windowed option for the variable. See tag history for how values are retained. |
Step 5: Activate the Output Tag
The new corrected_temperature tag starts inactive.
To run the computation, activate the output tag:
-
In the panel header of
corrected_temperature, click Activate. -
In the confirmation dialog, review the affected nodes and usage.
-
In the dialog, click Activate.
HiveMQ also activates any inactive nodes that supply inputs to the computation. For more information, see Activate and Deactivate Nodes.
Result
With an active output and an assigned broker, the computation processes new input values.
Each time the temperature tag receives a new value, the broker recalculates the Fahrenheit result.
The computed tag behaves like any other tag. You can inspect its values in the Data Output and Charts tabs, and reference it from further computations.
For example, a Celsius input of 20.3 produces the Fahrenheit value 68.54 in the Data Output tab.
To inspect stored values or charts, enable history for the output tag.
See Configure History.
For the full list of functions available in calculations and expressions, see Functions.
Beyond a Single Value: Complex Expressions
The example above transforms one input, but expressions are not limited to a single value. An expression can combine multiple input tags, and draw on a tag’s stored history to compute higher-level insights, not just unit conversions.
A common example is reliability engineering. When you combine an asset’s state transitions over time, you can compute metrics such as Mean Time Between Failures (MTBF), Mean Time To Repair (MTTR), and availability directly in the namespace. Use functions such as BetweenState to measure how long an asset spends in each state.
For a detailed, worked walkthrough of how to build these reliability key performance indicators (KPIs) with expressions, see the HiveMQ blog post Real-Time Reliability KPIs: MTBF, MTTR, and Availability.