Webhook icon
Schedule icon
LoopUntil icon
Script icon
Process icon
WorkingDirectory icon
Commands icon
If icon
SlackIncomingWebhook icon
MailSend icon
Log icon

Watch device pings against site fences and escalate long dwells

Evaluate each device ping against site geofences with haversine math, detect entry and exit from stored state, and escalate long dwells to Slack or email.

Categories
Infrastructure

Fences only work if somebody is watching the line, and a ping on its own says nothing without the state that came before it. This flow takes one location ping at a time, works out where the device sits relative to the warehouse and the retail fence, compares that with the last state it recorded and turns the difference into an entry, an exit or no event at all. The dwell clock and the site name ride along in the payload, so an alert says how long and where rather than only that something happened.

How it works

  1. on_location_ping (io.kestra.plugin.core.trigger.Webhook) accepts a body with device_id, lat, lon and ts. daily_state_sweep (io.kestra.plugin.core.trigger.Schedule, cron 0 6 * * *) sits disabled as the net to switch on for a morning replay of the fixture.
  2. poll_latest_ping (io.kestra.plugin.core.flow.LoopUntil) runs first, outside the workspace: read_latest_ping (io.kestra.plugin.scripts.python.Script) materializes the ping stream, picks the newest valid line for the watched device and emits found, the chosen ping and the full stream through the outputs protocol. The poller retries every five seconds until found is true, and failOnMaxReached is false, so a silent device becomes a NO_SIGNAL verdict instead of a failed run.
  3. workspace (io.kestra.plugin.core.flow.WorkingDirectory) opens one shared directory holding only runnable script tasks, and write_fixtures (io.kestra.plugin.scripts.shell.Commands) drops sites.json with the warehouse center and its 250 meter radius beside the retail center and its 500 meter radius, and state.json with the last recorded state for four tracked devices.
  4. evaluate_geofence (io.kestra.plugin.scripts.python.Script) prefers the webhook body when it carries a complete ping and otherwise falls back to the ping the poller emitted. It measures the haversine distance to every site center, walks the trailing run of pings inside the same fence to find the session start, compares that with the stored inside or outside state, writes the updated state back to state.json, and prints one ::{"outputs": ...}:: line carrying verdict, event, device_id, dwell_minutes and site.
  5. route_dwell_alert (io.kestra.plugin.core.flow.If) fires when event is entry and dwell_minutes is above dwell_alert_minutes, and alert_slack (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) posts the device, the site, the dwell and the verdict to the ops channel. The other branch holds route_asset_exit (io.kestra.plugin.core.flow.If), which mails email_custodian (io.kestra.plugin.email.MailSend) for an exit of a high value asset and records every routine result with log_quiet (io.kestra.plugin.core.log.Log).
  6. The errors handler pings Slack when the run itself breaks, because a geofence that fails quietly looks exactly like a quiet night.

What you get

  • verdict (STRING): DWELL_ALERT for an entry already past the dwell limit, EXIT_ESCALATION for the exit of a high value asset, OBSERVED for routine movement, UNKNOWN_DEVICE when the ping comes from a device that is not provisioned, NO_SIGNAL when no usable ping or site geometry is available.
  • event (STRING): entry, exit or none, straight from the state machine, alongside the device_id (STRING) the decision is about, taken from the webhook body or the polled fixture.
  • dwell_minutes (FLOAT): minutes since the session start inside the current fence, and zero whenever the device is outside.
  • site (STRING): the fence the device sits in, or the fence it just left on an exit, empty otherwise.

Who it's for

  • Fleet, retail and warehouse teams that need to know when a truck, cage or pallet enters a fenced site, how long it stays and when high value stock walks out.
  • Platform engineers who already push pings to a webhook and want the geofence decision, the alert routing and the audit trail in one versioned file.

Why orchestrate this with Kestra

Haversine math is easy and the state around it is not. Kestra supplies the webhook, the disabled daily sweep, the shared workspace, the poll loop that waits for a ping without failing the run, and the branch that separates a long dwell alert from a high value exit mail from a quiet observation. The state file is written back by the same task that reads it, every decision is a typed output a dashboard can consume, and the errors handler keeps a broken run visible instead of letting a silent failure look like a clean night.

Prerequisites

  • A Kestra instance with the Slack and email plugins available to the worker, a Slack incoming webhook for the dwell alert and for failure alerts, and an SMTP relay reachable from the worker.
  • A webhook key for the telemetry gateway and the fence table for your sites, seeded through sites.json or pointed at your own store.

Secrets

  • GEOFENCE_PING_WEBHOOK_KEY: key segment guarding the webhook URL, so only your gateway can post pings.
  • SLACK_WEBHOOK_URL: Slack incoming webhook used for the dwell alert and for failure alerts.
  • SMTP_FROM, SMTP_HOST, SMTP_USERNAME, SMTP_PASSWORD: SMTP relay and credentials used by email_custodian.

Quick start

  1. Add the six secrets to your namespace and import the flow.
  2. Run it once against the fixture with device_id set to TRK-001 and read verdict DWELL_ALERT, event entry, site warehouse and dwell_minutes 45.0 in the outputs.
  3. Run it with device_id set to TRK-002 to see EXIT_ESCALATION and the custodian mail branch fire instead, then point your gateway at /api/v1/{tenant}/executions/webhook/{namespace}/geofence-entry-exit-processor/{GEOFENCE_PING_WEBHOOK_KEY} with the ping body.

How to extend

  • Replace the sites.json heredoc with a query or an object store read, keeping the same locate function and the same verdicts.
  • Raise or lower dwell_alert_minutes per run, give each fence its own dwell limit inside sites.json, or add a third fence such as a depot.
  • Switch on daily_state_sweep for a scheduled replay of the day's pings instead of only webhook traffic.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.