Automatically translate this page?

How to calculate daily engine hours?

How to calculate vehicle engine hours on a daily basis

Imagine that you have a vehicle with a GPS tracking device installed. And you want to calculate daily vehicle utilization time. With flespi analytics, you can generate such report with the GET /gw/devices/{selector}/intervals REST API call

We recommend to carefully read conceptual information on flespi analytics before continuing with the next steps.

After connecting the device to flespi (you may follow steps 1 and 2 of this guide), the first task is to look inside device messages with Toolbox and determine message parameters that will be used to detect when the vehicle engine is ON. Let's say the wire is connected to the digital input 4 and is available in the "din" parameter as its fourth bit. It means that the expression we will use to detect active engine state will be "din&8", where 8 is two to the power of four minus one (0b00001000).

The interval selector will have type="datetime" as we need to separate statistics results on a daily basis according to selected (in separate parameter) timezone. Here is a sample configuration for the selector:

"type": "datetime",
"split": "day",
"merge_message_after": true,
"max_messages_time_diff": 600

We would like the interval to end not at last message inside the current day but at the first message of the following day. This is why merge_message_after is set to 'true'. This will allow us to correctly calculate the duration of engine running and combine results with 100% precision if we decide to split not on a daily basis, but on a weekly or monthly basis.

We also would like to skip gaps when there is no data from the device for more than 600 seconds — this is what "max_messages_time_diff" is used for.

As we just need the total daily stats, we do not need more interval selectors that can split one day into work hours or rides based on this ignition sensor. This is possible but we want just one number per day.

Next task is to correctly configure counters. We will have just one counter of type="expression" and method="duration" that will calculate the duration for the message when the engine is ON until the next message for all messages in the interval:

    "name": "engine_hours"
    "type": "expression",
    "expression": "din&8"
    "method": "duration"

The resulting set of intervals will look similar to this:

"begin": 1522732482,
"end": 1522792804,
"duration": 60322
      "engine_hours": 0
"begin": 1522792804,
"end": 1522879226,
"duration": 86422,

      "engine_hours": 6123
"begin": 1522879226,
"end": 1522965618,
"duration": 86392,

      "engine_hours": 13475

We recommend you to play with interval selectors and counters to find mathematics suitable for your needs.

See also
The guide on how to set up a calculator to receive live customizable MQTT messages when device parameters change according to a complex condition.
Handling events, accessing intervals, and attaching custom data to calculators