Disclosure: I'm the developer of ScooterLog, and this post is published from my account. It was drafted and published by an AI agent working on my behalf from the app's source code. ScooterLog is an independent app, not affiliated with, endorsed by or sponsored by Ola Electric.
ScooterLog keeps a charge history and trip log for connected electric scooters. One chart in it answers a question every rider eventually asks: is the battery getting worse?
The raw data cannot answer that directly. All it has is timestamped readings of battery percent, odometer and charging state. This post is about turning those into one number per month that is honest enough to compare month against month, and about the three decisions that keep that number from lying.
The number: full-charge range, per month
For each calendar month, take the rides in that month and compute
full_range_km = km ridden / percent used * 100
That is "how far would 100% have taken me this month". If it drifts down over many months, that is a reasonable sign of battery wear. If it jumps around, it is weather, riding mode and how hard you ride.
Here is the function, trimmed to what matters:
def monthly_range(user_id, vehicle_id, months=12):
"""Real range on 100% per IST calendar month, from that month's own rides
(km / % used). A month needs >= 15% ridden to count."""
since = _now_ms() - months * 31 * 86400000
# ... SELECT source_ms, battery_soc, odometer, charging_state, lat, lng
# FROM history WHERE user_id=? AND vehicle_id=? AND source_ms>=?
# ORDER BY source_ms
by = {}
for t in _trips(rows):
if t.get('pct_used') and t['pct_used'] > 0:
m = time.strftime('%Y-%m', time.gmtime((t['start_ms'] + IST_MS) / 1000))
km, pct, n = by.get(m, (0.0, 0.0, 0))
by[m] = (km + t['km'], pct + t['pct_used'], n + 1)
return [dict(month=m, full_range_km=round(km / pct * 100), km=round(km), rides=n)
for m, (km, pct, n) in sorted(by.items()) if pct >= 15]
Three decisions are hiding in there.
Decision 1: sum first, then divide
Each ride has its own km and percent. It is tempting to compute km per percent for each ride and average the results. Don't. A 2 km errand that used 1% and a 40 km ride that used 14% are not equal witnesses: the first one's percent is rounded to a whole number, so its ratio can be off by a large factor, while the second one's is off by a few percent at most.
Summing kilometres and summing percent across the month, then dividing once, weights every ride by how much battery it actually used. Small rides add a little noise to a big total instead of equal noise to an average.
Decision 2: refuse a month that has not ridden enough
A month with only 3% of riding gives you km / 3 * 100, which is a rounding error multiplied by thirty. So the function drops any month where the summed percent is under 15:
if pct >= 15
The chart then shows nothing for that month, and the front end says so in plain words ("Appears once a month has about 15% of riding.") instead of drawing a dubious bar. A missing bar is better than a precise-looking wrong one, especially on a chart whose whole point is a trend.
The same floor is used when the app estimates km per 1% from rides for the range planner, so the two screens never disagree about how much data is enough.
Decision 3: bucket by the rider's calendar, not the server's
The server stores timestamps as UTC milliseconds. A ride that starts at 11 pm IST on the 30th is already the 1st in UTC. Bucket by UTC and that late-evening ride lands in the wrong month, which on a month-sized chart quietly moves part of one month's range into the next.
The fix is one line: shift the start time by the rider's offset before converting.
IST_MS = 19800 * 1000 # +05:30
m = time.strftime('%Y-%m', time.gmtime((t['start_ms'] + IST_MS) / 1000))
Adding the offset and then reading the result as if it were UTC (gmtime) gives the local calendar month without any time-zone database. It is hard-coded to IST because that is where the app's riders are. For a global product you would store each rider's zone and apply that instead; I would rather say that plainly than pretend the constant is general.
A ride is assigned to the month it started in. A ride that crosses midnight on the last day of the month is counted whole, not split, because its kilometres and its percent belong together.
What the chart says, and what it refuses to say
The front end takes the first and last months on the chart and writes a sentence from them: the last month's range, how many rides it rests on, and the change against the first month. Then it adds the caveat that is the honest part:
A steady fall across months points to battery wear; one month can also move with weather and riding mode.
It deliberately says "points to", not "proves". Two months are two points; the app does not fit a line or put a number on wear. And with a single qualifying month it shows that month's figure and says the trend appears from next month, rather than comparing a month with itself.
Known limits
- The query window is
months * 31days back from now, so the oldest month on the chart can be cut part way through. The 15% floor limits the damage, but a partial first month can still read a little off. - Rides come from sparse readings, so a ride's distance comes from the odometer difference and its percent from the battery difference between its first and last readings. Anything that hides those (a missed reading, a half-seen charge) shifts a month. That is why the ride reconstruction has its own rules about when to trust a ride, and why this function only sums what those rules accepted.
- A scooter ridden mostly in an eco mode one month and a fast mode the next will show a range change that is not battery wear at all.
Takeaways
- Sum numerators and denominators across the period, then divide once.
- Set a minimum amount of data and show nothing below it.
- Bucket by the user's calendar. Shifting a UTC timestamp by a fixed offset and reading it with
gmtimeis enough when the zone is fixed. - Write the caveat into the sentence the user reads, not only into the docs.
ScooterLog is on Google Play. It is a log and planner: it does not control the scooter and is not a security service. ScooterLog is an independent app, not affiliated with, endorsed by or sponsored by Ola Electric.
AI Disclosure: this article was drafted and published by an AI agent on the developer's behalf, from the app's source code.
Top comments (0)