Performs mathematical operations and functions on numeric data. Arguments can be retrieved from multiple sources (constants, message data, message metadata, attributes, or time series data) and results can be saved to the message or directly to database as attributes or time series.
Configuration
Supported functions
Function | Number of arguments | Description | Reference |
---|---|---|---|
ADD | 2 | x + y | |
SUB | 2 | x - y | |
MULT | 2 | x * y | |
DIV | 2 | x / y | |
SIN | 1 | Returns the trigonometric sine of an angle. | Math.sin |
SINH | 1 | Returns the hyperbolic sine of a double value. The hyperbolic sine of x is defined to be (ex - e-x)/2 where e is Euler’s number. | Math.sinh |
COS | 1 | Returns the trigonometric cosine of an angle. | Math.cos |
COSH | 1 | Returns the hyperbolic cosine of a double value. The hyperbolic cosine of x is defined to be (ex + e-x)/2 where e is Euler’s number. | Math.cosh |
TAN | 1 | Returns the trigonometric tangent of an angle. | Math.tan |
TANH | 1 | Returns the hyperbolic tangent of a double value. | Math.tanh |
ACOS | 1 | Returns the arc cosine of a value; the returned angle is in the range 0.0 through pi. | Math.acos |
ASIN | 1 | Returns the arc sine of a value; the returned angle is in the range -pi/2 through pi/2. | Math.asin |
ATAN | 1 | Returns the arc tangent of a value; the returned angle is in the range -pi/2 through pi/2. | Math.atan |
ATAN2 | 2 | Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). First argument is Y (ordinate coordinate), second argument is X (abscissa coordinate). | Math.atan2 |
EXP | 1 | Returns the value ex, where e is the base of the natural logarithms. | Math.exp |
EXPM1 | 1 | Returns ex-1. Note that for values of x near 0, the exact sum of expm1(x) + 1 is much closer to the true result of ex than exp(x). | Math.expm1 |
SQRT | 1 | Returns the correctly rounded positive square root of a double value. | Math.sqrt |
CBRT | 1 | Returns the cube root of a double value. | Math.cbrt |
GET_EXP | 1 | Returns the unbiased exponent used in the representation of a double. | Math.getExponent |
HYPOT | 2 | Returns √(x² + y²) without intermediate overflow or underflow. | Math.hypot |
LOG | 1 | Returns the natural logarithm (base e) of a double value. | Math.log |
LOG10 | 1 | Returns the base 10 logarithm of a double value. | Math.log10 |
LOG1P | 1 | Returns the natural logarithm of the sum of the argument and 1. Note that for small values x, the result of log1p(x) is much closer to the true result of ln(1 + x) than the floating-point evaluation of log(1.0+x). | Math.log1p |
CEIL | 1 | Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. | Math.ceil |
FLOOR | 1 | Returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. | Math.floor |
FLOOR_DIV | 2 | Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient. | Math.floorDiv |
FLOOR_MOD | 2 | Returns the floor modulus of the long arguments. | Math.floorMod |
ABS | 1 | Returns the absolute value of a double value. | Math.abs |
MIN | 2 | Returns the smaller of two double values. | Math.min |
MAX | 2 | Returns the greater of two double values. | Math.max |
POW | 2 | Returns the value of the first argument raised to the power of the second argument. | Math.pow |
SIGNUM | 1 | Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero. | Math.signum |
RAD | 1 | Converts an angle measured in degrees to an approximately equivalent angle measured in radians. | Math.toRadians |
DEG | 1 | Converts an angle measured in radians to an approximately equivalent angle measured in degrees. | Math.toDegrees |
CUSTOM | 0-16 | Use this function to specify complex math expressions. For example, transform Fahrenheit to Celsius using (x - 32) / 1.8) | exp4j |
Using CUSTOM function
When you select the CUSTOM operation, you can write your own mathematical expression instead of using predefined operations. This allows you to perform complex calculations that combine multiple operations in a single node.
Key points:
- You can use up to 16 arguments with predefined names (displayed on the configuration form)
- Use these predefined argument names directly in your expression
- The expression is evaluated using the exp4j library - refer to its documentation for detailed syntax rules
- Supports standard mathematical operators:
+
,-
,*
,/
,^
(power), and parentheses - Supports standard functions (
sin()
,cos()
,sqrt()
,abs()
, etc.) plus custom functions:ln(x)
(natural logarithm),lg(x)
(base 10 logarithm),logab(a, b)
(logarithm of b with base a) - Available built-in constants:
π
orpi
(3.14159265358979323846),e
(2.7182818284590452354),φ
(1.61803398874)
Expression examples:
Use case | Expression | Description |
---|---|---|
Fahrenheit to Celsius | (x - 32) / 1.8 |
Convert temperature from °F to °C |
Pythagorean theorem | sqrt(x^2 + y^2) |
Calculate hypotenuse using square root function |
Circle area | pi * r^2 |
Calculate area using the built-in π constant |
Arguments
Arguments for mathematical operations can be retrieved from the following sources:
- Constant - A fixed numerical value specified directly in the configuration
- Message - Reads a value from a field in the JSON message payload
- Metadata - Reads a value from the message metadata
- Attribute - Fetch the attribute value from the database for the message originator
- Time series - Retrieve the latest time series value from the database for the message originator
Each argument can have a default value that will be used if the source value is missing.
Result
The calculation result can be saved to one or more destinations:
- Message - Save the result to a specified key in the message data
- Metadata - Save the result to a specified key in the metadata
- Attribute - Save as an originator attribute (with configurable scope: Server or Shared)
- Time series - Save as a time series data point with the current timestamp
When saving to attributes or time series, you can optionally also add the result to the message data and/or metadata for use in subsequent rule nodes.
You can specify the number of digits after the decimal point for the result. The value will be rounded to the specified precision using half-up rounding mode (rounds towards the nearest neighbor, and if both neighbors are equidistant, rounds up).
JSON Schema
Message processing algorithm
- For each argument, the node retrieves the value from the configured source (constant, message data, metadata, attribute, or time series). If a value cannot be retrieved and no default value is specified, processing fails.
- All argument values are converted to numeric (double) format. If conversion fails, processing fails.
- The node performs the mathematical calculation using the retrieved argument values according to the selected operation.
- The result is formatted according to the configured precision (rounded to the specified number of decimal places).
- The result is saved to the configured destination(s): message, metadata, attribute, and/or time series.
- When processing completes successfully, the message is forwarded via the
Success
connection.
Output connections
Success
- The mathematical operation completed successfully and the result was saved.
Failure
- An error occurred during processing, such as:
- Missing required argument without a default value
- Failed to convert an argument value to a number
- Division by zero
- Invalid mathematical operation (e.g., square root of negative number for real numbers)
- Other unexpected error
- An error occurred during processing, such as:
Examples
Example 1 — Accumulating water consumption
A water meter device reports incremental consumption. You need to update the total consumption stored as an attribute.
Incoming message
Data:
1
2
3
{
"deltaConsumption": 15.7
}
State of the system
Originator has a server attribute: totalConsumption
= 12543.8
Node configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"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 message
Data:
1
2
3
4
{
"deltaConsumption": 15.7,
"totalConsumption": 12559.5
}
Routed via the Success
connection.
Result
The originator attribute totalConsumption
is updated from 12543.8 to 12559.5 (12543.8 + 15.7). The new total is also added to the message data for use in subsequent rule nodes.
Example 2 — Complex signal processing calculation
Calculate a signal quality indicator using a custom formula that combines distance, signal strength, and environmental factors. The formula uses trigonometric functions, logarithms, and built-in constants.
Formula: quality = 100 * e^(-distance / 1000) * (1 + 0.5 * sin(2 * pi * frequency/1000)) * sqrt(abs(signalStrength))
Incoming message
Originator: DEVICE
Data:
1
2
3
4
5
{
"distance": 250,
"frequency": 2400,
"signalStrength": -45
}
Node configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
"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 message
Data:
1
2
3
4
5
6
{
"distance": 250,
"frequency": 2400,
"signalStrength": -45,
"signalQuality": 675.98
}
Routed via the Success
connection.
Result
The signal quality indicator is calculated using exponential decay for distance, sinusoidal modulation for frequency effects, and square root transformation of signal strength. The formula demonstrates the use of:
- The built-in constant
e
(Euler’s number) for exponential calculations - The built-in constant
pi
for trigonometric calculations - Functions:
sin()
,sqrt()
,abs()
- Complex expression with multiple operations
The result (675.98) is saved to the message data for further processing.