Functions

Functions are the building blocks of computed values in Contextualize. When a namespace node does not carry a raw value but instead derives one — through its Calculation or Expression data source — you use functions to describe how that value is produced. For example, a node can compute a simple moving average from a tag’s readings, sum the parts produced over a window, or measure how long a machine stays in a fault state.

Functions operate on the variables that reference your live data (tags, fields, and other computed nodes) and combine with the arithmetic, comparison, and logical operators of the expression language. The result becomes the value of the computed node, which is then published back into the namespace like any other node.

Function names are case-insensitive. For example, SMA, sma, and Sma all refer to the same function. The same applies to every function on this page.

Available Functions

The following functions are available, grouped by category. Select a function name to jump to its detailed reference.

Table 1. Available functions
Function Category Description

ABS

Arithmetic

Returns the absolute (non-negative) value of a number.

ROUND

Arithmetic

Rounds a number to a given number of decimal places.

SUM

Arithmetic

Adds two values together.

MINUS

Arithmetic

Subtracts the second value from the first.

DIV

Arithmetic

Divides the first value by the second.

PRODUCT

Arithmetic

Multiplies two values together.

COUNT

Aggregation and statistical

Counts the number of values in a window.

FIRST

Aggregation and statistical

Returns the first (oldest) value in a window.

LAST

Aggregation and statistical

Returns the last (newest) value in a window.

MEDIAN

Aggregation and statistical

Returns the median value of a window.

SMA

Aggregation and statistical

Calculates the simple moving average of a window.

STDDEV

Aggregation and statistical

Calculates the standard deviation of a window.

EMA

Aggregation and statistical

Calculates the exponential moving average of a window.

MAX

Aggregation and statistical

Returns the larger of two values.

MIN

Aggregation and statistical

Returns the smaller of two values.

MAX_WINDOW

Aggregation and statistical

Returns the maximum value in a window.

MIN_WINDOW

Aggregation and statistical

Returns the minimum value in a window.

SUM_WINDOW

Aggregation and statistical

Returns the sum of all values in a window.

PRODUCT_WINDOW

Aggregation and statistical

Returns the product of all values in a window.

EVENT_TIME

State and event

Identifies a payload field as the event timestamp source.

INGESTION_TIME

State and event

Returns the broker ingestion timestamp of the current datapoint.

TimeAtState

State and event

Emits a timestamp when the payload matches a state.

BetweenState

State and event

Aggregates values while a state is active.

TimeElapsed

State and event

Measures elapsed time across a state (used inside BetweenState).

CountBetween

State and event

Counts datapoints during a state (used inside BetweenState).

Arithmetic Functions

Arithmetic functions perform numeric computations on individual scalar values such as tags, fields, or other computed nodes.

ABS

Returns the absolute value of a number. Negative values are converted to their positive equivalent.

Syntax: ABS(x)

  • x — A numeric value (BIG_INT or BIG_DECIMAL).

Returns: The absolute value, with the same numeric type as the input.

ABS(-15.7)            // Returns 15.7
ABS(setpoint_error)   // Magnitude of the deviation from setpoint

ROUND

Returns a number rounded to a specified number of decimal places. Rounding uses standard mathematical rounding (HALF_UP), where 0.5 rounds away from zero.

Syntax: ROUND(x, n)

  • x — The number to round (BIG_DECIMAL or BIG_INT).

  • n — The number of decimal places (a non-negative integer).

Returns: The rounded number as a BIG_DECIMAL.

ROUND(3.14159, 2)                 // Returns 3.14
ROUND(efficiency_ratio * 100, 2)  // Converts a ratio to a percentage with 2 decimals
ROUND(-2.5, 0)                    // Returns -3 (HALF_UP rounds away from zero)
Use ROUND to control calculation precision and to avoid confusing trailing zeros (for example, 0.9850000000) when displaying percentages and ratios.

SUM

Adds two values together.

Syntax: SUM(a, b)

  • a — The first value (numeric).

  • b — The second value (numeric).

Returns: The total of the two values, as a numeric scalar.

SUM(good_parts, scrap_parts)   // Total parts produced
SUM(base_load_kw, peak_load_kw)

MINUS

Subtracts the second value from the first.

Syntax: MINUS(a, b)

  • a — The value to subtract from (numeric).

  • b — The value to subtract (numeric).

Returns: The difference, as a numeric scalar.

MINUS(85, 20)                       // Returns 65
MINUS(total_parts, defective_parts) // Number of good parts

DIV

Divides the first value by the second.

Syntax: DIV(a, b)

  • a — The dividend (numeric).

  • b — The divisor (numeric).

Returns: The quotient, as a numeric scalar.

Dividing by zero produces no value (null) or an error, so guard against a zero divisor where it can occur.
DIV(100, 4)                       // Returns 25
DIV(good_parts, total_parts)      // Quality rate as a ratio

PRODUCT

Multiplies two values together.

Syntax: PRODUCT(a, b)

  • a — The first factor (numeric).

  • b — The second factor (numeric).

Returns: The product, as a numeric scalar.

PRODUCT(availability, performance)   // Combine two OEE factors
PRODUCT(unit_cost, units_produced)   // Total production cost

Aggregation and Statistical Functions

Aggregation and statistical functions reduce a window of time-series values — the set of datapoints collected for a tag over a period — into a single result. Functions that take a window operate on a tag or computed value that produces a window; MAX and MIN instead compare two individual scalar values.

COUNT

Counts the number of values in a window.

Syntax: COUNT(window)

  • window — A windowed data source, such as a tag’s readings over a period.

Returns: The number of values in the window, as an integer.

COUNT(production_events)   // Number of production events in the window
COUNT(sensor_readings)     // Number of readings collected

FIRST

Returns the first (oldest) value in a window.

Syntax: FIRST(window)

  • window — A windowed data source.

Returns: The earliest value in the window.

FIRST(efficiency_window)   // Efficiency at the start of the window
FIRST(batch_weight)        // Initial batch weight

LAST

Returns the last (newest) value in a window.

Syntax: LAST(window)

  • window — A windowed data source.

Returns: The most recent value in the window.

LAST(temperature_window)   // Most recent temperature reading
LAST(quality_window)       // Latest quality measurement

MEDIAN

Returns the median (middle) value of a window.

Syntax: MEDIAN(window)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL).

Returns: The median value, as a BIG_DECIMAL.

MEDIAN(cycle_times)     // Median cycle time, resistant to outliers
MEDIAN(response_times)  // Median response time

SMA

Calculates the simple moving average of the values in a window. Every value in the window contributes equally to the average.

Syntax: SMA(window)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL).

Returns: The arithmetic mean of the window’s values.

SMA(temperature_window)   // Average temperature over the window
SMA(production_rate)      // Average production rate

STDDEV

Calculates the standard deviation of the values in a window, a measure of how much the values vary around their mean.

Syntax: STDDEV(window)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL). The window must contain at least two values.

Returns: The standard deviation, as a BIG_DECIMAL.

STDDEV(measurement_window)   // Measurement variability
STDDEV(hourly_rates)         // Production consistency metric

EMA

Calculates the exponential moving average of a window. Unlike SMA, an exponential moving average weights more recent values more heavily, so it responds faster to change.

Syntax: EMA(window, period)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL).

  • period — The averaging period, as a positive integer.

Returns: The exponential moving average, as a BIG_DECIMAL.

EMA(temperature_window, 10)   // 10-period exponential average of temperature
EMA(power_window, 20)         // Smoothed power consumption trend

MAX

Returns the larger of two values.

Syntax: MAX(a, b)

  • a — The first value (numeric scalar).

  • b — The second value (numeric scalar).

Returns: The larger of the two values.

To find the maximum across a window of time-series values, use MAX_WINDOW.
MAX(temp_zone_a, temp_zone_b)   // Higher of two zone temperatures
MAX(target_rate, actual_rate)

MIN

Returns the smaller of two values.

Syntax: MIN(a, b)

  • a — The first value (numeric scalar).

  • b — The second value (numeric scalar).

Returns: The smaller of the two values.

To find the minimum across a window of time-series values, use MIN_WINDOW.
MIN(pressure_a, pressure_b)   // Lower of two pressures
MIN(planned_runtime, actual_runtime)

MAX_WINDOW

Returns the maximum value across all values in a window.

Syntax: MAX_WINDOW(window)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL).

Returns: The largest value in the window.

MAX_WINDOW(vibration_window)   // Peak vibration in the window
MAX_WINDOW(temperature_window) // Highest temperature reached

MIN_WINDOW

Returns the minimum value across all values in a window.

Syntax: MIN_WINDOW(window)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL).

Returns: The smallest value in the window.

MIN_WINDOW(pressure_window)   // Lowest pressure in the window
MIN_WINDOW(level_window)      // Minimum tank level

SUM_WINDOW

Returns the sum of all values in a window.

Syntax: SUM_WINDOW(window)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL).

Returns: The total of the window’s values.

SUM_WINDOW(parts_produced)   // Total parts produced over the window
SUM_WINDOW(energy_samples)   // Accumulated energy across the window

PRODUCT_WINDOW

Returns the product of all values in a window.

Syntax: PRODUCT_WINDOW(window)

  • window — A windowed numeric data source (BIG_INT or BIG_DECIMAL).

Returns: The product of the window’s values.

PRODUCT_WINDOW(yield_factors)   // Combined yield across sequential steps

State and Event Functions

State and event functions detect state transitions in your data and measure time or counts between those transitions. Use them to track how long equipment operates in a specific condition, to mark when a state is reached, or to count events while a state is active.

A state is described by a boolean predicate evaluated against the fields of an input tag — for example, voltage > 100 && id > 30. The state is active while the predicate is true and ends when it becomes false.

EVENT_TIME

Identifies a payload field as the event timestamp source and returns its value as a timestamp.

Syntax: EVENT_TIME(field)

  • field — A single payload field configured with the DATETIME data type.

Returns: A DATETIME.

The referenced field must be configured as a DATETIME. EVENT_TIME does not accept fields typed as integer or decimal.

Usage: Use as the timestamp source inside TimeAtState and TimeElapsed.

EVENT_TIME(tsField)
EVENT_TIME(event_timestamp)

INGESTION_TIME

Returns the broker ingestion timestamp of the current datapoint — the time the platform received the message.

Syntax: INGESTION_TIME()

  • No arguments.

Returns: A DATETIME (epoch milliseconds).

Usage: Use as the timestamp source inside TimeAtState and TimeElapsed.

TimeAtState(voltage > 100, INGESTION_TIME())
BetweenState(motor_temp > 85.0, TimeElapsed(INGESTION_TIME()))

TimeAtState

Emits a timestamp when the payload matches a specified state. If the predicate evaluates to false, the datapoint is ignored and no value is emitted.

Syntax: TimeAtState(predicate, timestampSource)

  • predicate — A boolean predicate evaluated against input fields, such as voltage > 100 && id > 30.

  • timestampSourceEVENT_TIME(field) or INGESTION_TIME().

Returns: A DATETIME when the predicate is true; otherwise no value is emitted.

Constraints:

  • Must be used as the root-level function.

  • Cannot be nested inside other operations.

  • All referenced fields must belong to the same input tag.

// Emit the event timestamp when voltage exceeds 100 and id exceeds 30
TimeAtState(voltage > 100 && id > 30, EVENT_TIME(tsField))

// Emit the broker ingestion timestamp when motor temperature is critical
TimeAtState(motor_temp > 85.0, INGESTION_TIME())

BetweenState

Accumulates values while a state is active (the predicate is true) and emits an aggregated result when the state deactivates (the predicate becomes false). The deactivation condition is automatically the negation of the activation predicate.

Syntax: BetweenState(predicate, aggregation)

  • predicate — A boolean predicate evaluated against input fields.

  • aggregation — The aggregation to apply across the active state: TimeElapsed or CountBetween.

Returns: Depends on the aggregation function (an integer for both TimeElapsed and CountBetween), emitted when the state deactivates.

Constraints:

  • Must be used as the root-level function.

  • Cannot be nested inside other operations.

  • All referenced fields must belong to the same input tag.

// Measure how long voltage stays above 100 using event timestamps
BetweenState(voltage > 100, TimeElapsed(EVENT_TIME(tsField)))

// Count how many datapoints had voltage above 100
BetweenState(voltage > 100, CountBetween())

TimeElapsed

Computes the elapsed time, in milliseconds, across the timestamps accumulated while a state is active (the latest timestamp minus the earliest).

Syntax: TimeElapsed(timestampSource)

  • timestampSource — A DATETIME source: EVENT_TIME(field) or INGESTION_TIME().

Returns: An integer (elapsed time in milliseconds).

Usage: Only valid as the aggregation inside BetweenState.

// Elapsed time the machine spent above the high-temperature threshold
BetweenState(motor_temp > 85.0, TimeElapsed(EVENT_TIME(tsField)))

// Elapsed time based on ingestion timestamps
BetweenState(pressure > 200, TimeElapsed(INGESTION_TIME()))

CountBetween

Counts the number of datapoints recorded while a state is active. The datapoint that deactivates the state is not included in the count.

Syntax: CountBetween()

  • No arguments.

Returns: An integer (the count of datapoints where the predicate evaluates to true).

Usage: Only valid as the aggregation inside BetweenState.

// Count datapoints recorded during the high-voltage state
BetweenState(voltage > 100, CountBetween())

// Count readings during a high-pressure state
BetweenState(pressure > max_safe_pressure, CountBetween())

Data Types

Functions operate on the following data types.

Type Description

BIG_INT

Arbitrary-precision integers, used for counting and indexing.

BIG_DECIMAL

Arbitrary-precision decimals, used for measurements and calculations.

BOOLEAN

True or false values, used for conditions, flags, and state predicates.

STRING

Text values, used for labels and states (limited support).

DATETIME

Timestamp values for date and time data. Supports multiple serialization formats, including Unix timestamps in milliseconds, Unix timestamps in seconds, and ISO-8601 strings. Used by EVENT_TIME, INGESTION_TIME, and TimeAtState.

WINDOW

Time-series collections of values, used by the aggregation and statistical functions.