Automatically translate this page?

Expressions — the way to analyze message

How to use expressions to extract information from telematics message using own mathematics

Analytics system uses expressions everywhere — in intervals selectors, in interval counters, and even in message and interval validators. Expression operates with a message and the result of an expression is a number.

In the case of boolean-type expressions (e.g. validation or interval test expressions), non-zero resulting value means the expression is valid. An expression consists of numbers, operators, parameters references, and functions. If an expression contains a reference to a parameter that is not present in the message or division by zero, its result is undefined or OFF in most boolean expressions.

For example, we sequentially analyze the following messages from devices:

{
  "device.name": "123456789012345",
  "timestamp": 1490347944.893743,
  "din": 9,
  "channel_id": 123,
  "altitude": 568.49,
  "direction": 297,
  "hdop": 0.9,
"speed": 10,
  "lat": -21.328481,
  "lng": 47.562136,
  ...
}

In an expression, all references to message parameters that contain a number or a boolean value will equal to this value. All references to message parameters that contain non-number value (e.g. text) will equal to 1 if this parameter is defined for the message. For example expression "channel_id + device.name" will equal to 124.

To reference the value of the same parameter but for the previous message, you need to prepend hash (#) to the parameter name. For example, you can use "speed - #speed" to calculate to speed change from message to message. 

Important note on using references to previous message parameters: as the system does not know the value of the previous parameter for the very first message, it means that the very first message in such reference will always return unknown or OFF value.

Another form of parameters specification is by preceding them with dollar sign ($). In that case if parameter not present in the message zero value will be put instead. For example "abs($speed)>=0" will be always valid and non-zero.

You can use mathematical operators in expression and add brackets to control the order of operations. The priority of operations is the same as in the C programming language and, moreover, all operation notation is also very similar to what C language defines. You can use the following operators:

operator
explanation
+
sum
-
diff
/
divide
*
multiply
|
binary OR
&
binary AND
&&
logical AND
||
logical OR
^
binary XOR
==
equal
!=
not equal
<
less
<=
less or equal
>
greater
>=
greater or equal

If you need more operators, please contact us

Here are some sample expressions and their resulting values:

expression
evaluation
1 + 2 * 4 / 2
5
(1 + 2) * 4 / 2
6
(4 & 1) > 0
0, cause 4 AND(binary) 1 is 0
(4 && 1) > 0
1, cause 4 AND(logical) 1 is 1

It is also possible to use some functions inside expressions, for example, "abs(speed-#speed)>10" will always equal to 1 if instant speed value change between two sequential messages is different by more than 10 km/h in any direction. Here is the full list of functions:

function
explanation
abs(X)
an absolute value of X
round(X)
round X to the closest integer
ceil(X)
round X up to the largest integer
floor(X)
round X down to the smallest integer
min(X, Y)
return a minimum value between X and Y
max(X, Y)
return a maximum value between X and Y
if(X, Y, Z)
if X is non-zero, evaluate to Y otherwise evaluate to Z

If you need more functions, please contact us.

Analytics system is being in active development at the moment and all REST API calls described here are subject to change. If you intend to use this functionality for commercial projects, please let us know so we can make sure to notify you before applying incompatible API changes.


See also
How to calculate vehicle engine hours on a daily basis
Instructions on how to use flespi analytics engine to extract trips from raw device messages