Getting Raw Health Data From a Pebble

Published on Thu, 31 October, 2019 | 900 words
Tags: pebble sql android self-hosting lifelogging

It’s no secret that I love my Pebble. It’s still the best “smart watch” ever to be released in my humble opinion, and the later versions, such as the Pebble Time, have basic health tracking built-in, giving them most of the functionality of a fitness tracker but the look and feel (and functions) of a watch.

Thing is, not a lot supports the dear old Pebble any more. Lots of fitness apps are beginning to support multiple brands of fitness tracker, but the Pebble isn’t among them. And there’s no ‘export’ feature in the official Pebble app, or its Rebble-friendly successor. The data is stored on your watch and the Pebble app on your phone, and there’s no way of getting at it. Or is there?

There is an app called pebble-health-export. It’s available from the Rebble store, or as source code (in C) from Github. It’s odd, in that you need to set up a web server (presumably on Amazon or some other cloud platform) to receive the data, because the app runs on the watch and simply sends a CSV dump as an HTTP POST request. It’s quite configurable, and can be set to run at a certain time each day. I’ve had mixed results with it - although it’s quite good at exporting sleep history, it’s not too great on step count and I find it often misses steps.

There is, of course, another way. You can get the data directly from the app itself, and all the health data is stored in a SQLite3 database, which makes it very easy to query in something like Python. The files you need are stored in the Pebble app’s data directory in the subdirectory ‘databases’. There are several files containing data, but the nice one is simply called ‘health’ with no file extension.

Getting the file off the phone, on the other hand, may prove tricky. My task was simple as my phone is rooted and I simply open a root shell over ADB to get it. The file itself (at least on my phone) did have world read access, although the directory it was in wasn’t executable, so provided you know the actual location of the file you can get it through an ADB PULL like so:

adb pull /data/data/com.getpebble.android.basalt/databases/health ./health

Once you have the file, make sure SQLite3 is installed, and type

sqlite3 health

Inside the database you’ll find several tables, but two are of interest. These are minute_samples and activity_sessions. They contain pretty much all the data that can be displayed in the app. It’s worth mentioning how the data is stored for such a long time. Basically, the minute_samples table has a new row created every minute, featuring a timestamp, a step count, a VMC value (basically how much you’ve moved, although it’s much more complicated than that), a light sensor value, and a few other things. The app uses this to generate the daily summaries you see, and the minute-by-minute values are then deleted once the table gets too big.

Sample Queries

If you want step count, by the minute, then simply do this

SELECT date_utc_secs, step_count FROM minute_samples;

The data_utc_secs value is the number of seconds since Midnight, 1st January 1970, UTC. There’s a command to convert this into a local time in pretty much every programming language known to humans.

Obviously you can refine this query to get daily or hourly step counts.

SELECT SUM(step_count) WHERE data_utc_secs>=1546300800 AND data_utc_secs<=1546387199;

That will return the sum of all the steps taken between 0:00 and 23:59 on 1st January 2019, essentially the number of steps you took on this day.

If sleep is more your thing, you can get this from the activity_sessions table.

SELECT start_utc_secs, end_utc_secs, type FROM activity_sessions ORDER BY start_utc_secs ASC;

This returns all the ‘sessions’ the app knows about. The start_utc_secs and end_utc_secs are the start and end times of a period of time, and the ‘type’ value is an integer specifying what was going on during that period. I’ve figured out three of the values:

Value Meaning
1 Sleep
2 Deep (restful) sleep
5 Long walk

I’m guessing the intention was for the Pebble to recognise more activities in further revisions of the firmware. My data doesn’t contain any 3s or 4s, but then my Pebble is a Pebble Time, which doesn’t have a heart monitor, like the Pebble 2 does. Perhaps 3 and 4 are heart rate related.

So with all this in mind, it’s trivial to write a few quick Python scripts to dump your health data as CSV, or whatever format is supported by your chosen health tracker app or website. If you’re really dedicated (and as lazy as I am!) you can use the excellent app FolderSync to periodically push your database file to a server, where a cron script can dump and import the data into your chosen service automatically! This almost certainly requires a rooted phone, as you’ll need to give FolderSync access to the data of the Pebble app, which vanilla Android (sensibly) doesn’t allow. But it’s nice that options exist for those who, like me, want to track my activity but don’t want to say goodbye to their Pebble just yet.