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 a report with the POST /gw/devices/{selector}/calculate REST API call or by configuring calculator with corresponding configuration and assigning device to it for automatic calculation in background.

We recommend 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 = 2^(4-1) = (0b00001000).

The interval selector will have type="datetime" as we need to separate statistics results on a daily basis according to the 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 want the interval to end not with the last message of the current day but with 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.

The 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 play with interval selectors and counters to find mathematics suitable for your needs.

Next step will be to copy this configuration to calculator in order to enable automatic intervals calculation in the background.


See also
How to add data generated by calculators to device messages