Trip Distance from GPS Coordinates
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.
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.
- 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:
tripDistancein 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
- 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.
- Confirm the arguments point at your telemetry keys:
lat→latitude,lon→longitude(both Time series rolling, 15-minute window). - Save.
tripDistancestarts reporting once at least two GPS fixes have arrived within the window.
Note: the field only computes when the latest
latitudeandlongitudereadings 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
tripDistanceover 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 newtripDistancevalue 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/lonif your tracker uses different telemetry key names. - To rename the output — open the script and change the
tripDistancekey in the return object. - To convert to miles — multiply the result by
0.621371before rounding. - To adjust the noise filter — change the
noiseThresholdKmconstant near the top of the script (default0.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
latandlonarguments (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.