Formulas

 
Formulas can be used to calculate parameter values.
The formula can begin with any arithmetic operand or "=" sign.
If the formula begins with an arithmetic operand, then the value of the parameter is calculated.
In Figure 1, the formula is used to set the parameter IN1.
The final value of the parameter will be calculated using the formula IN1 * 0.5.
 
Picture 1.
 
 
In Picture 2, upon receiving parameter IN1, the described sensor will be assigned the value calculated by the formula IN2 *+ 20.
 
Picture 2.
 
 
Simple formulas
Formulas in data processors are based on the syntax of the PHP language and are executed on the server when processing data to convert it into the required format.
The formula can be simple and begin directly with the sign of the arithmetic operation. The operations add ( + ), subtract ( - ), multiply ( * ), divide ( / ) are supported, and parentheses are also supported to ensure the correct sequence of operations.
For example:
*10 - the original value will be multiplied by 10
/1000 - the original value will be divided by 1000
/100+7 - the original value will be divided by 100 and 7 will be added to the resulting result
/100+(7*3-2)*4
Data from other data fields, speed and number of satellites can be substituted into the formula.
To substitute values ​​into a formula, they must be enclosed in %% escape characters.
The variable used for speed is %S%
For the number of satellites %P% or %satsinview%
For the unixtimestamp of the current package, use the %ts% variable
For the unixtimestamp of the previous packet, the variable %ts1%.The value of the current sensor can be substituted by the %value% variable.
To get the difference in seconds relative to the previous packet, you need to calculate %ts1%-%ts%
For all other values, the names are taken from the device package as they are sent by the driver.
For example: %DIN1%    %DIN2%    %HDOP%
 
 
Example of a formula with variables:
*%DIN1% - multiply the original value by the value sent in the DIN1 field
/%HDOP% - divide the original value by the value from the HDOP field
 
Complex formulas
More complex formulas begin with an = sign and do not use the original value. But it can be substituted into the formula itself using the %value% variable or a variable with the field name.
For example:
=(1+2) - the final value will be 3
=(7/%value%) - the final value will be equal to 7 divided by the initial value
 
Conditional statements
Formulas can use conditions that provide branching calculation logic depending on the values ​​of any parameters or results of operations.
The notation for conditional statements looks like this: condition ? value_if_yes :  value_if_no
This notation is usually called a ternary operator.
Logically this expression is identical to the expression:
if condition return value_if_yes else return value_if_no
Conditions can use comparison operators equal to ( == ), greater than ( > ), less than ( < ), greater than or equal to ( >= ), less than or equal to ( <= ), not equal to ( != )
 
For example:
- if the value of the DIN1 field is 1 then the final value will be 22 otherwise the final value will be 33
=(%temp%>30)? %temp%*10 : %temp%*20 - if the value of the temp field is greater than 30, then the final value will be equal to the value of the temp field multiplied by 10, otherwise the final value will be equal to the value of the temp field multiplied by 20.
 
Functions
Formulas may use some PHP functions to transform data.
These are the functions unpack, pack, array_sum, substr, round, hex2bin, dechex, hexdec, decbin, number_format, max, min, abs, str_replace, time, strrev, bytesrev, explode, sqrt, acos, asin, cos, sin, pow.
 
The values ​​passed to these functions and the values ​​returned can be read in the documentation PHP https://www.php.net/manual/ru/function.acos.php
 
For example:
=substr("%FIELD%", 1, 2) - the final value will be equal to 2 characters from the string passed to the FIELD field starting from position 1
=round(%AIN1%/1000) - the final value will be equal to the rounded value of the quotient of the AIN1 field value divided by 1000
 
Division by 0
To avoid unpredictable results when processing sensor values, you should avoid variables as a divisor or use conditional statements to test them for 0
For example:
=1000/%DIN1% - may generate a calculation error if the value of the DIN1 field is not defined or is equal to 0. In this case, the formula must be rewritten as follows:
=(%DIN1%==0) ? 0 : 1000/%DIN1% - if the value of the DIN1 field is 0 then the final value is 0 otherwise the final value is equal to the quotient of 1000 divided by the value of the DIN1 field
 
• Formula for converting an unsigned number to a signed number using the example of Teltonics
If a bluetooth temperature sensor connected to Teltonika EYE Sensor sends a negative temperature,
then it can be transformed by the following formula.
=((%BLE_T1%>3000)?((%BLE_T1%-65535)/100):(%BLE_T1%/100))
 
Example of a temperature sensor with formula
 
 
• =bytesrev('%value%')
To reverse hex bytes you should use this bytesrev('%value%') function:
Example
0123d848010000e5 -> e500000148d82301
 
• =strrev('%value%')
To reverse text, use the strrev('%value%') function:
Example
0123d848010000e5 -> 5e000010848d3210
 
• =hexdec('%Param1%') - converting a 16-digit number to decimal.
 Example:
 PAram1=0A, in this case the function will return the value 10.
 
• =decbin('%Param1%') - convert decimal to double.
substr('%Data%',x,y) - search for a substring length in "y" in the "Data" string starting from position "x"
Examples:
Param1=A6B1
substr(decbin(hexdec('%Param1%')),9,1), the formula will return the value 0.
 
• =explode(',','%value%')[2]
explode - Splits text using a delimiter and returns an array of strings.
=explode('separator','%value%')[2]
separator - separator
value is the string to be separated.
 
 
acos - The acos function returns the arc cosine of a number, that is, the angle whose cosine is equal to this number. Angle is measured in radians.
 
asin - The asin function returns the arcsine of a number, that is, the angle whose sine is equal to this number. Angle is measured in radians.
 
cos - is one of the basic trigonometric functions, defined as the ratio of an adjacent leg to the hypotenuse in a right triangle.
 
sin - the bottom of the basic trigonometric functions, which for an acute angle in a right triangle is defined as the ratio of the opposite leg to the hypotenuse, i.e. the bottom of the basic trigonometric functions, which for an acute angle in a right triangle is defined as the ratio of the opposite leg to the hypotenuse.
 
 
Convert little endian byte sequence to big endian
 
We have a sequence in hex '1500000124286b01'
It needs to be converted to the sequence 6B2824010000
 
=strtoupper(substr(unpack('H*',strrev(hex2bin($value)))[1],2,12))