Post processing of data
How to post process data
Table of Contents
What is post processingRules & expressionsArithmetic operationsBitwise & binary operatorsConditional operatorsExamplesErrorsWhat is post processing
Post processing of items is a simple way of applying mathematical expressions, logic arithmetic and conditional functions to data.
Every time a new value for an item is read, it will be stored in the SIA internal database. Before storing that value, it can be modified with scaling, mathematical expressions or arithmetic logic in order to store in a different format by using the post processing function.
This can be done by simply adding a JavaScript-like expression in the item settings, under the Setup section, in the Post Processing field.
The Post Processing field also allows the use of variables (system, device, item, etc).
See System variables, Item variables and Instance parameters for more info about referencing to items and its variables.
Rules & expressions
The syntax behind post processing is based on Javascript. To assign and return a value from the post processing function the value has to be assigned such as below:
value = value * 100
or in simplified format
value * 100
or to assign the value a string
value = "We have used "+ value +" %ITEM.unit% today" //We have used "+ value +" kWh today
assuming the unit for the item is kWh.
Note that the value is pointing to the value coming directly from the item and needs to be wrapped into "+ value +" to indicate a variable is being used inside a string.
The following sections shows some examples of the arithmetic logic, conditions statement and mathematical expressions and functions that can be used within the post processing function.
Arithmetic operations
The following table shows the arithmetic operations that is allowed within a post processing function.
Operation | Syntax |
---|---|
Addition / Substraction | value + x value - x |
Multiplication / Division | value * x value / x |
Modulus | value % x |
Exponentiation / Power of | Math.pow(value, x) |
Absolute value | Math.abs(value) |
Cosine / Sine / Tangent | Math.cos(value) Math.sin(value) Math.tan(value) |
Hyperbolic Cosine / Sine / Tangent | Math.cosh(value) Math.sinh(value) Math.tanh(value) |
Arc Cosine / Sine / Tangent | Math.acos(value) Math.sin(value) Math.atan(value) Math.atan(value) |
Hyperbolic Arc Cosine / Sine / Tangent | Math.acos(value) Math.sin(value) Math.atan(value, x) |
Exponential | Math.exp(value) |
Natural log | Math.log(value) |
Common log | Math.log10(value) |
Square root | Math.sqrt(value) |
Integral part | Math.sqrt(value) |
Round to nearest integer | Math.round(value) |
Round down to nearest integer | Math.floor(value) |
Round up to nearest integer | Math.ceil(value) |
Sign of a number | Math.sign(value) |
Truncate fractional digits | Math.trunc(value) |
Bitwise & binary operators
For bitwise manipulation of the following operators can be used
Operation | Syntax |
---|---|
Bitwise left shift operator | value << x |
Bitwise right shift operator | value >> x |
Bitwise unsigned right shift operator | value >>>> x |
AND | value & x |
OR | value | x |
XOR | value ^ x |
Conditional operators
Conditional logic can also be applied to post processing functions.
Operation | Syntax |
---|---|
if statement | if (CONDITION) { value = x; } |
else statement | ... } else { value = x; } |
else if statement | ... } else if (CONDITION) { value = x; } |
Inline statement | value == x ? value : y |
Equality operator |
if(value == x) if (value) |
Inequality operator |
if (value != x) if (!value) |
Logical AND | if (value == y && x == z) |
Logical OR | if (value == y || x == z) |
Ignore the new value | if (value == 1) { value = null } |
Examples
The following tables shows some examples of the post processing.
Description | Original value | Post processing expression | Result |
---|---|---|---|
Temperature reading coming in Fahrenheit (°F), storing as Celsius (°C) | 59 | (value - 32) * 5/9 |
15 |
Converting string as a JSON object. | "SIA brings data alive!" | "{ \"message\": " + value + "}" |
{"message": "SIA brings data alive! "} |
Converting number to a JSON object | 42 | "{ \"random\": " + value/2 + "}" |
{ "random": 21} |
Checking for a conditional statement up against another items (UID: ALARM_LIMIT) value | 35 | if(value > %ALARM_LIMIT%) { value = 1; } |
0 if the value is larger than ALARM_LIMIT 35 if the value is lower than ALARM_LIMIT |
Ignore a reading if a condition is a met from another item. | 50 | if(value == 50) value = false |
None as the reading is skipped |
Another item (UID: SEND_DATA) value controlling transmission of data | 21 | if(%SEND_DATA% == 0) return false |
21 if SEND_DATA is 1 Ignored if SEND_DATA is 0 |
Bit shifting (2 bits) | 20 | value >> 2 |
Shift the decimal value 20 (0b010100) with two bits resulting in decimal 5 (0b000101) |
Bit extracting (2nd bit) using bitshift & AND'ing | 20 | (value >> 2) & 0x1 |
Shifts the value 2 bits as above example. AND's with 0x1 to get the bit |
Errors
Errors in the post processing script will be shown under the given item in the list.
The error will be represented as a yellow warning box with the syntax or function error. The format of the error representation is shown below.
Postscript error: Error message Line: X