In flespi the geofence status of the device can be tracked either via the plugin or via the calculator.
With the plugin you can add to the device message a special parameter that contains the name of the geofence device is currently in. Such a parameter is written at the moment when the message is being registered and depends on the geofences linked to the device or plugin exactly at that moment.
While with the calculator you can define, link or change geofences any time and the flespi analytics system will automatically recalculate device messages and reflect their geofence in/out status in the calculator intervals.
In this article we will focus on controlling the geofences using a calculator.
There are two distinct options how geofence related events can be tracked with the calculator:
By using a special selector of type=”geofence” which will create an interval when a message is inside a geofence. This is the simplest option if you just need to control geofence visits.
By using a selector of type=”expression” and expression=”geofence()” with method=”change”. Such a selector will create intervals and track device state both inside and outside of geofences. Thus with a single calculator you are able to control both geofence entrance and exit events.
In this article we will describe exactly the second option. However if you need just to control geofences visit or track time spent there we’d suggest to use the first option with a selector of type=”geofence” due to its simplicity.
Calculator configuration
The calculator configuration consists of one interval selector and several interval counters.
Interval Selector
We use an expression selector with method="change" to detect any change in geofence status:
{
"type": "expression",
"method": "change",
"expression": "geofence()",
"merge_unknown": true,
"merge_message_after": true
}
The selector configuration explained:
expression="geofence()" - evaluates to geofence name when device is inside a geofence or to null when device is outside of all geofences
method="change" - creates new interval each time the expression result changes (e.g. when device enters or exits a geofence)
merge_unknown=true - prevents points without coordinates from breaking current interval
merge_message_after=true - includes the first message after the interval into the interval to have complete intervals
Interval Counters
We need several counters to properly identify the type of event and store geofence names:
{
"counters": [
{
"type": "expression",
"name": "enter_geofence",
"expression": "geofence()",
"method": "first"
},
{
"type": "interval",
"name": "exit_geofence",
"expression": "previous(\"enter_geofence\")"
},
{
"type": "interval",
"name": "geofence",
"expression": "if(enter_geofence != null, enter_geofence, exit_geofence)"
},
{
"type": "interval",
"name": "type",
"expression": "if(enter_geofence != null, \"enter\", \"exit\")"
}
]
}
The counters configuration explained:
enter_geofence - stores the name of entered geofence (will be null for intervals outside of geofences)
exit_geofence - stores the name of geofence that device just left (will be null for intervals inside geofences)
geofence - contains either enter_geofence or exit_geofence value depending on the event type
type - contains "enter" for intervals inside geofences and "exit" for intervals outside geofences
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 when the device enters or exits any geofence assigned to the calculator or device.
Each interval will contain:
begin - when device entered/exited geofence
end - when device exited/entered next geofence
type - "enter" or "exit" depending on the event
geofence - name of geofence that was entered or exited
You can access intervals via REST API, subscribe to MQTT topics to receive real-time notifications about geofence events or forward them to your server using webhooks.
For example, to receive real-time notifications when device entered or existed the geofence subscribe to MQTT topic:
flespi/interval/gw/calcs/{calc_id}/devices/{device_id}/activated
The interval message will look like:
{
"begin": 1621234567,
"end": 1621234789,
"duration": 222,
"type": "enter",
"geofence": "office",
"enter_geofence": "office",
"exit_geofence": null
}