This article explains how to configure a calculator to detect trips between geofences. The calculator will generate intervals for each trip that occurs when a device moves from one geofence to another.
Such a calculator can be used when you would like to set up some locations and control all the movements outside of these locations. For example:
Ships navigation between harbors
Courier trips between warehouses, bases and clients
Dump truck rides between a quarry and clients
Any vehicle movements outside of bases
To achieve this we will create a calculator, define the geometry of our bases and clients as geofences and assign these geofences to the device and/or calculator to track changes in real time.
Configuration
The calculator configuration consists of one interval selector to detect when the device is outside of geofences and several interval counters to capture trip details.
Interval Selector
We use an expression selector with method="boolean" to detect when device is outside of all geofences:
{
"type": "expression",
"method": "boolean",
"expression": "geofence() == null",
"merge_unknown": true,
"merge_message_before": true,
"merge_message_after": true
}
The selector configuration explained:
expression="geofence() == null" - evaluates to true when device is outside of all assigned geofences
method="boolean" - creates interval while expression evaluates to true
merge_unknown=true - prevents points without coordinates from breaking current interval
merge_message_before=true - includes the last message inside source geofence
merge_message_after=true - includes the first message inside destination geofence
Interval Counters
We need several counters to capture trip details:
{
"counters": [
{
"type": "expression",
"name": "from_geofence",
"expression": "geofence()",
"method": "first"
},
{
"type": "expression",
"name": "to_geofence",
"expression": "geofence()",
"method": "last"
},
{
"type": "expression",
"name": "distance",
"expression": "mileage()",
"method": "summary"
},
{
"type": "route",
"name": "route"
}
]
}
The counters configuration explained:
from_geofence - stores the name of geofence from which trip started
to_geofence - stores the name of geofence where trip ended
distance - calculates total trip distance
route - stores the trip route in encoded format
Usage
Create a calculator with the configuration above
Create geofences and assign them to the calculator or devices
Assign devices to the calculator
The calculator will automatically generate intervals for trips between geofences.
Each interval will contain:
begin - when device left the source geofence
end - when device entered the destination geofence
duration - total trip time
from_geofence - name of source geofence
to_geofence - name of destination geofence
distance - trip distance in kilometers
route - encoded route points
You can access intervals via REST API or subscribe to MQTT topics to receive real-time notifications about trips.
For example, to receive real-time notifications about trips subscribe to:
flespi/interval/gw/calcs/{calc_id}/devices/{device_id}/created
The interval message will look like:
{
"begin": 1621234567,
"end": 1621234789,
"duration": 222,
"from_geofence": "office",
"to_geofence": "warehouse",
"distance": 12.5,
"route": "_p~iF~ps|U_ulLnnqC_mqNvxq`@"
}