Admin formulas
For examples that show how formulas are used in application features, including the variables available in each context, see Formula use cases
Formula Operands
The Formula class evaluates a formula definition as a tree of operands. Every operand is an object with a type field. Operation and function operands also include an operator field and an operands array containing child operands.
Operand Shape
type Operand =
| Constant
| Variable
| ArithmeticOperation
| ComparisonOperation
| LogicalOperation
| ConditionalFunction
| StringFunction
| AggregateFunction
| ArrayFunction
| StatisticalFunction
| null;
null is part of the shared TypeScript type, but the Formula class throws Operand is null when it encounters one.
Constant
Constants return their configured value.
Type | Operator | Inputs | Result |
|---|---|---|---|
| N/A |
| Returns |
JSON Example:
{ "type": "constant", "value": 42 }
Variable
Variables read values fetched during prepare() or passed to run({ variables }).
Type | Operator | Inputs | Result |
|---|---|---|---|
| N/A |
| Reads the root variable named by |
Input notes:
path[0]identifies the variable fetcher or runtime variable value.paramsare passed to the variable fetcher and are part of the variable cache key.Paths with more than one segment require the root variable value to be an object.
Array callback variables are injected internally with names beginning with
$, such as$arrayItemand$accumulator.
JSON Example:
{ "type": "variable", "path": ["entity", "custom_data", "temperature"] }
Arithmetic
Arithmetic operations evaluate two numeric child operands.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSON Example:
{
"type": "arithmeticOperation",
"operator": "+",
"operands": [
{ "type": "variable", "path": ["entity", "custom_data", "temperature"] },
{ "type": "constant", "value": 5 }
]
}
Comparison
Comparison operations evaluate two child operands and return a boolean.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSON Example:
{
"type": "comparisonOperation",
"operator": ">=",
"operands": [
{ "type": "variable", "path": ["entity", "custom_data", "temperature"] },
{ "type": "constant", "value": 70 }
]
}
Logical
Logical operations evaluate child operands using JavaScript truthiness.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
Because the implementation uses JavaScript logical operators directly, AND and OR return the selected operand value, not necessarily a boolean.
JSON Example:
{
"type": "logicalOperation",
"operator": "AND",
"operands": [
{ "type": "variable", "path": ["entity", "custom_data", "isOnline"] },
{
"type": "comparisonOperation",
"operator": ">",
"operands": [
{ "type": "variable", "path": ["entity", "custom_data", "batteryLevel"] },
{ "type": "constant", "value": 20 }
]
}
]
}
Conditional
Conditional functions choose which child operand to evaluate based on earlier operands.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
| Evaluates |
|
|
| Evaluates |
JSON Example:
{
"type": "conditionalFunction",
"operator": "if",
"operands": [
{ "type": "variable", "path": ["entity", "custom_data", "isOnline"] },
{ "type": "constant", "value": "online" },
{ "type": "constant", "value": "offline" }
]
}
String
String functions operate on text-like values.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
| Evaluates each operand and joins the values with no separator. |
|
|
| Converts non-null inputs to strings and returns |
JSON Example:
{
"type": "stringFunction",
"operator": "concat",
"operands": [
{ "type": "variable", "path": ["entity", "custom_data", "name"] },
{ "type": "constant", "value": " - " },
{ "type": "variable", "path": ["entity", "custom_data", "status"] }
]
}
Aggregate
Aggregate functions operate on one array operand. They only include entries that are numbers and not NaN in their calculations.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
| Sum of numeric entries. Starts at |
|
|
| Sum of numeric entries divided by the array length. |
|
|
| Maximum numeric entry. Starts at |
|
|
| Minimum numeric entry. Starts at |
JSON Example:
{
"type": "aggregateFunction",
"operator": "sum",
"operands": [
{
"type": "constant",
"value": [
{ "type": "variable", "path": ["entity", "custom_data", "mondayRunHours"] },
{ "type": "variable", "path": ["entity", "custom_data", "tuesdayRunHours"] },
{ "type": "variable", "path": ["entity", "custom_data", "wednesdayRunHours"] }
]
}
]
}
Array
Array functions operate on array values and, for callbacks, create scoped variables for each iteration.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
| Returns |
|
|
| Returns |
|
|
| Returns |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Callback variable examples:
{ "type": "variable", "path": ["$arrayItem"] }
{ "type": "variable", "path": ["$accumulator"] }
JSON Example:
{
"type": "arrayFunction",
"operator": "filter",
"operands": [
{
"type": "constant",
"value": [
{ "type": "variable", "path": ["entity", "custom_data", "morningTemperature"] },
{ "type": "variable", "path": ["entity", "custom_data", "afternoonTemperature"] },
{ "type": "variable", "path": ["entity", "custom_data", "eveningTemperature"] }
]
},
{
"type": "comparisonOperation",
"operator": ">",
"operands": [
{ "type": "variable", "path": ["$arrayItem"] },
{ "type": "constant", "value": 80 }
]
}
]
}
Statistical
Statistical functions operate on one array operand, except percentile, which also takes a comparison value. These functions expect array contents to be numeric because they perform numeric sorting and arithmetic directly.
Type | Operator | Inputs | Result |
|---|---|---|---|
|
|
| Arithmetic mean of the array. |
|
|
| Median of the numerically sorted array. |
|
|
| Mode of the numerically sorted array. |
|
|
| Standard deviation of the array. |
|
|
| Variance of the array. |
|
|
| Percentage of entries below |
JSON Example:
{
"type": "statisticalFunction",
"operator": "mean",
"operands": [
{
"type": "constant",
"value": [
{ "type": "variable", "path": ["entity", "custom_data", "morningTemperature"] },
{ "type": "variable", "path": ["entity", "custom_data", "afternoonTemperature"] },
{ "type": "variable", "path": ["entity", "custom_data", "eveningTemperature"] }
]
}
]
}