Analytics Builder 10.16.0 | Using Analytics Builder for Cumulocity IoT | Block Reference | Calculation | Expression
Evaluates an expression to perform arithmetic or logical calculations or string operations.
On change of input values (once all in-use inputs have been received), the expression specified in the parameter is re-calculated.
The expression language is much like EPL (see EPL reference), but is restricted to float, integer, string and boolean types. All numeric literals are treated as float type values, even if they have no fractional part. Integer values can only be obtained as the result of functions such as floor(). Similar to EPL, integer and float are not implicitly convertible within an expression. If the result of an expression is an integer value, it is converted to a float automatically (there might be a loss of precision). Boolean values can be specified using the Boolean literals true and false. Boolean literals are case insensitive, so for example, TRUE and True are allowed. String values can be specified by enclosing string literals in double quotes, for example "my value". Special characters are encoded with a backslash (\). The following special characters (along which their encoding) are supported in string literals:
*Double quotes - \"
*Backslash - \\
*Newline - \n
*Tab - \t
The values of the inputs are available as input1, input2, input3, input4 and input5. The input values can be of type float, string, boolean and any. Logical, relational, numerical and equality operators can be used on the values of the supported types. Logical operators are case insensitive, so for example, AND and And are allowed. Built-in methods on the float, integer, string and boolean types can be called, including x.abs() (absolute value of x), x.pow(y) (raise x to the power y), x.sin() (sine of x in radians), x.round() (rounds x to the nearest integer), and s.ltrim() (remove whitespace from the start of the string s). Built-in static methods of the supported types can be called by specifying the type name, followed by a dot (.) and the method name, for example, float.max(input1, input2) (find the larger of two input values). Built-in constants on the supported types can be accessed by specifying the type name, followed by a dot (.) and the constant name, for example, float.E (Euler's constant). Values of type any are unpacked at runtime to evaluate the expression. After unpacking, the value must be of type float, string or boolean. The type checker tries to validate the expressions during the validation phase, but this is not always possible with the any type. So if an expression contains the any type, even if it passes the validation phase, it can still fail at runtime due to a wrong type of variable being passed or an unsupported operation being performed. For a full list of built-in methods and constants, consult the API Reference for EPL (ApamaDoc).
Some examples:
*Convert Fahrenheit to Celsius: (input1 - 32) * 5/9
*Convert days to seconds: input1 * 86400
*Average of 4 inputs: (input1 + input2 + input3 + input4) / 4
*Threshold comparison: input1 > 3.1412 (but also see the Threshold block)
*Pythagoras to compute the hypotenuse of a right-angled triangle: (input1.pow(2) + input2.pow(2)).sqrt()
*Comparison to 3 decimal places: (input1 * 1000 - (input1 * 1000).fractionalPart()) = (input2 * 1000 - (input2 * 1000).fractionalPart())
*Range check: input1 >= 1 and input1 <= 10
*String comparison: input1 = "my value"
*Larger value: float.max(input1, float.PI)
*Remainder of integer division: input1.round() % input2.round()
An expression - a string representation of an EPL expression.
Similar to EPL expressions, but with the differences as described above.
Input Port Details
First input, to be used as input1 in the expression.
Second input, to be used as input2 in the expression.
Third input, to be used as input3 in the expression.
Fourth input, to be used as input4 in the expression.
Fifth input, to be used as input5 in the expression.
Output Port Details
Result of the expression.