Skip to content
Stand with Ukraine flag

Math Function

Use this node to compute a mathematical result from message data, metadata, or entity attributes and save it back to the message or directly to the database — for example, converting raw ADC readings to engineering units, accumulating a running total in a server attribute, or computing a heat index from temperature and humidity without writing a script node.

Select one of the built-in operations or CUSTOM for arbitrary expressions:

OperationArgsDescription
ADD2x + y
SUB2x − y
MULT2x × y
DIV2x / y
SIN / SINH1Sine / hyperbolic sine
COS / COSH1Cosine / hyperbolic cosine
TAN / TANH1Tangent / hyperbolic tangent
ACOS / ASIN / ATAN1Inverse trig functions
ATAN22atan2(y, x) — polar angle
EXP / EXPM11eˣ / eˣ−1
SQRT / CBRT1Square root / cube root
LOG / LOG10 / LOG1P1Natural log / log₁₀ / log(1+x)
CEIL / FLOOR1Ceiling / floor
FLOOR_DIV / FLOOR_MOD2Floor division / floor modulo
ABS1Absolute value
MIN / MAX2Minimum / maximum
POW2x ^ y
SIGNUM1Sign: −1, 0, or 1
RAD / DEG1Degrees ↔ radians conversion
GET_EXP1Unbiased IEEE 754 exponent
HYPOT2√(x² + y²)
CUSTOM0–16Custom expression via exp4j

Write any mathematical expression using the predefined argument variable names shown in the configuration form. Available built-in constants: pi/π (3.14159…), e (2.71828…), φ (1.61803…). Available functions: all standard trig/log/sqrt/abs functions plus ln(x), lg(x) (log₁₀), logab(a, b) (logₐ b).

Expression examples:

Use caseExpression
Fahrenheit → Celsius(x - 32) / 1.8
Pythagorean theoremsqrt(x^2 + y^2)
Circle areapi * r^2

Each argument specifies where to read its value from:

SourceDescription
CONSTANTFixed numeric value
MESSAGE_BODYField from message data
MESSAGE_METADATAField from message metadata
ATTRIBUTEOriginator attribute (with scope)
TIME_SERIESLatest time-series value for the originator

Argument keys support templatization — use ${metadataKey} and $[dataKey] to build key names dynamically.

Each argument can have a default value used when the source is missing or invalid.

The computation result can be written to one or more destinations:

DestinationDescription
MESSAGE_BODYKey in the message data
MESSAGE_METADATAKey in the message metadata
ATTRIBUTEOriginator attribute (Server or Shared scope)
TIME_SERIESTime-series data point (current timestamp)

For ATTRIBUTE and TIME_SERIES destinations, you can optionally also write the result to the message body and/or metadata for downstream nodes.

Set Result value precision to the number of decimal places (half-up rounding). 0 gives an integer.

  1. For each argument, retrieve the value from its configured source. If a value is missing and no default is set, route via Failure.
  2. Convert all values to double. If conversion fails, route via Failure.
  3. Perform the mathematical calculation.
  4. Round the result to the configured precision.
  5. Write the result to all configured destinations.
  6. Route the message via Success.
ConnectionCondition
SuccessCalculation completed and result saved.
FailureMissing argument without default, non-numeric value, division by zero, invalid operation, or unexpected error.

Example 1 — Accumulate water consumption

Section titled “Example 1 — Accumulate water consumption”

State: originator has server attribute totalConsumption = 12543.8. Data: { "deltaConsumption": 15.7 }.

{
"operation": "ADD",
"arguments": [
{ "name": "x", "type": "ATTRIBUTE", "key": "totalConsumption", "attributeScope": "SERVER_SCOPE", "defaultValue": 0 },
{ "name": "y", "type": "MESSAGE_BODY", "key": "deltaConsumption" }
],
"result": {
"type": "ATTRIBUTE",
"key": "totalConsumption",
"attributeScope": "SERVER_SCOPE",
"resultValuePrecision": 2,
"addToBody": true,
"addToMetadata": false
}
}

Outgoing data: { "deltaConsumption": 15.7, "totalConsumption": 12559.5 }. Attribute updated to 12559.5. Routes via Success.


Example 2 — Custom formula: signal quality

Section titled “Example 2 — Custom formula: signal quality”

Data: { "distance": 250, "frequency": 2400, "signalStrength": -45 }.

{
"operation": "CUSTOM",
"customFunction": "100 * e^(-x / 1000) * (1 + 0.5 * sin(2 * pi * y / 1000)) * sqrt(abs(z))",
"arguments": [
{ "name": "x", "type": "MESSAGE_BODY", "key": "distance" },
{ "name": "y", "type": "MESSAGE_BODY", "key": "frequency" },
{ "name": "z", "type": "MESSAGE_BODY", "key": "signalStrength" }
],
"result": {
"type": "MESSAGE_BODY",
"key": "signalQuality",
"resultValuePrecision": 2,
"addToBody": true,
"addToMetadata": false
}
}

Outgoing data includes: "signalQuality": 675.98. Routes via Success.

{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "TbMathNodeConfiguration",
"type": "object",
"required": ["operation", "arguments", "result"],
"additionalProperties": false,
"properties": {
"operation": { "type": "string" },
"customFunction": { "type": "string" },
"arguments": {
"type": "array",
"items": {
"type": "object",
"required": ["name", "type", "key"],
"properties": {
"name": { "type": "string" },
"type": { "type": "string", "enum": ["CONSTANT","MESSAGE_BODY","MESSAGE_METADATA","ATTRIBUTE","TIME_SERIES"] },
"key": { "type": "string" },
"attributeScope": { "type": "string" },
"defaultValue": { "type": "number" }
}
}
},
"result": {
"type": "object",
"required": ["type", "key"],
"properties": {
"type": { "type": "string", "enum": ["MESSAGE_BODY","MESSAGE_METADATA","ATTRIBUTE","TIME_SERIES"] },
"key": { "type": "string" },
"attributeScope": { "type": "string" },
"resultValuePrecision": { "type": "integer" },
"addToBody": { "type": "boolean" },
"addToMetadata": { "type": "boolean" }
}
}
}
}