Skip to content
Stand with Ukraine flag

Trip Distance from GPS Coordinates

  • Calculated Field
  • 1 install
  • v1.0.0
  • Aug 13, 2026
  • MIT license
  • v4.2+

Calculates the distance covered between consecutive GPS latitude/longitude fixes using the Haversine formula, filtering out GPS jitter below 5 meters. Reports the distance covered since the previous GPS fix, in kilometers — no dedicated distance sensor required. Ideal for fleet, asset, and vehicle trip tracking.

Type
  • Script
Category
  • Custom Formula
  • Geospatial
Use Cases
  • Asset Tracking
  • Fleet Tracking
  • Smart City

Who it’s for

For fleet managers and asset-tracking teams who only get raw GPS coordinates from a tracker — “how far did this vehicle move since its last GPS fix?”, “can I flag a trip leg without a dedicated distance sensor?”, “how do I filter out GPS jitter so a parked vehicle doesn’t rack up phantom distance?”

What it does

This SCRIPT calculated field computes the distance covered between the two most recent, time-matched GPS fixes from latitude/longitude telemetry.

trip-distance-pipeline

  • Distance between the two most recent, time-matched GPS fixes uses the Haversine formula (Earth radius 6371 km).
  • Movements smaller than 5 m (0.005 km) are treated as GPS jitter and reported as 0 — a parked asset doesn’t accumulate phantom distance.
  • Output: tripDistance in km, rounded to 3 decimal places, written as time series on every calculation. This is a per-interval value, not a running total — it reports only the distance covered since the previous GPS fix, not a cumulative odometer reading.
  • Writes nothing if fewer than two readings are available yet on either stream, or if the two most recent latitude and longitude readings don’t share matching timestamps.

How to set up

  1. Add the field to a device or, better, to a device or asset profile (e.g. “GPS Tracker”) — this applies it to every entity of that type at once, including trackers added later.
  2. Confirm the arguments point at your telemetry keys: latlatitude, lonlongitude (both Time series rolling, 15-minute window).
  3. Save. tripDistance starts reporting once at least two GPS fixes have arrived within the window.

Note: the field only computes when the latest latitude and longitude readings share the same timestamp — meaning the tracker published both coordinates in the same message. It does not forward-fill or merge across a gap in either stream; if the two most recent readings don’t align, the field waits for the next matching pair rather than comparing against a stale coordinate.

If you need a running total (e.g. distance covered per hour or per day), add a separate Time Series Data Aggregation calculated field that sums tripDistance over a fixed interval (e.g. HOUR). Note that this produces a rollup per interval, resetting at each boundary — it’s “distance covered this hour,” not a cumulative, ever-growing odometer. If you need a true lifetime total that never resets, use a rule chain that adds each new tripDistance value to a running total stored as a server attribute (enrichment → script → save attributes).

How to customize

  • To point the inputs at your keys — change the Time series key on lat / lon if your tracker uses different telemetry key names.
  • To rename the output — open the script and change the tripDistance key in the return object.
  • To convert to miles — multiply the result by 0.621371 before rounding.
  • To adjust the noise filter — change the noiseThresholdKm constant near the top of the script (default 0.005, i.e. 5 meters). Lower it for slow-moving assets where small movements matter; raise it for trackers prone to more GPS jitter.
  • To widen or narrow the lookback window — adjust the Time window field on both lat and lon arguments (must match on both).

Share Your Calculated Field with the Community

Built a useful data transformation? Export it as a JSON from ThingsBoard and publish it to the IoT Hub. Share it with thousands of ThingsBoard developers worldwide.