utilities/gadgetbridge_to_influxdb#7: Track sync times

Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: v0.4
Created: 26-Aug-23 11:21


It'd be useful to have a graph showing when data was last received from the watch - the might later turn into an alert if things haven't synced for quite some time

Toggle State Changes


assigned to @btasker


mentioned in commit b68ad532889c8df88d020b8a2f3a6ddf9da89370

Commit: b68ad532889c8df88d020b8a2f3a6ddf9da89370 
Author: B Tasker                            
Date: 2023-08-26T12:24:38.000+01:00 


Track time of last observed sync (utilities/gadgetbridge_to_influxdb#7)

This adds two fields

  • last_seen: The (nanosecond) timestamp of the last observed datapoint
  • last_seen_age: number of nanoseconds since that timestamp

This currenty relies on the periodic sampling in MI_BAND_ACTIVITY_SAMPLE.

Values are grouped by device ID, so if multiple devices are registered in gadgetbridge, there'll be an entry for each

+24 -0 (24 lines changed)

The calculation is currently pretty simple

    # Create a field to record when we last synced
    now = time.time_ns()
    last_dates_query = ("SELECT DEVICE_ID, max(TIMESTAMP) "
        f"WHERE TIMESTAMP >= {query_start_bound} "

    res = cur.execute(last_dates_query)
    for r in res.fetchall():
        row_ts = r[1] * 1000000000 # Convert to nanos
        row_age = now - row_ts
        row = {
                "timestamp": now,
                "fields" : {
                    "last_seen" : row_ts,
                    "last_seen_age" : row_age
                "tags" : {
                    "device" : devices[f"dev-{r[0]}"],
                    "sample_type" : "sync_check"


However, the reliance on MI_BAND_ACTIVITY_SAMPLE means we may only see a record periodically (if the wearer is asleep, there's not going to be much in there).

It might actually be worth moving away from relying on a query for this. Above that codeblock is all the queries used to extract metrics - we could conceivably capture timestamps from those and then have this block take the most recent. That way, the timestamp of any supported activity/metric would update the sync time.


mentioned in commit ed97a5ab40f1dc8eb8fe9f0fdb19cf21a747d71e

Commit: ed97a5ab40f1dc8eb8fe9f0fdb19cf21a747d71e 
Author: B Tasker                            
Date: 2023-08-26T12:36:33.000+01:00 


Use timestamps from all supported events to calculate last_seen and last_seen_age (utilities/gadgetbridge_to_influxdb#7)

This pivots away from relying on a single query to assess when data was last synced

+44 -26 (70 lines changed)

mentioned in issue #8

This is working


(That gap has been addressed in #11)

I've added these stats to the dashboard


The queries are variations of

SELECT last("last_seen_age") AS "sync_age" FROM "telegraf"."autogen"."gadgetbridge" WHERE  $timeFilter