Automating myTracks Driving Log Filtering with iOS Shortcuts

I’ve found, over years of collecting driving tracks for my driving log, that having the logging be as automatic as possible is crucial. The more logging relies on remembering to start and stop a GPS log, the more likely it is that some data will end up not getting recorded.

When I was primarily using Garmin nüvi and DriveSmart GPS units to record my driving, the logging was effectively automatic; any time I had the GPS turned on it was logging, and I only had the GPS on while in the car. When I used a Bad Elf GPS Pro+, I could either trigger recording with car power, or leave it running in the car all day with its own speed filter preventing it from recording when the car wasn’t moving.

Using myTracks on iOS has been more of an issue, though. It’s certainly possible to leave it running all day, but running on my phone means it doesn’t get left in the car, so it records my walking motion outside of a car as well.

To get rid of the walking data and only keep the driving tracks, I’ve written a filter that removes points below a certain rolling average speed threshold. This is helpful but imperfect; GPS data is noisy (particularly when walking indoors with a weak GPS signal), and it’s easy to get series of walking points that falsely get kept as driving unless the minimum speed threshold is set fairly high. But higher speed thresholds mean more low-speed driving gets removed, which leads to gaps in places like drive-thrus, parking lots, and heavy traffic. No matter what the filter is set to, I need to do some manual cleanup of the driving data.

Fortunately, iOS can run actions in response to events via the Automation tab of its Shortcuts app. This meant that if I was able to connect some phone events to the beginning and end of driving, I could potentially run some actions that could effectively remove non-driving track points from GPS logs.

Strategy

To make this work, there were two main things I needed to determine: what actions I wanted the automation to do, and what events I wanted to use to trigger the automation.

Automation Actions

The myTracks app does have support iOS Shortcuts, with has actions for Start Recording and Stop Recording. However, myTracks can’t run these tasks in the background (the app has to open up), so the phone has to be unlocked in order for these tasks to run. This meant remembering to unlock the phone every time I entered or exited a car, which made the automation much less effective. (Indeed, when I was experimenting with this, I had several driving tracks fail to record because the phone wasn’t unlocked.)

Instead, my strategy was to use the Append to Text File action, and use that to add a timestamp to an iCloud text file every time I started or stopped driving. If I then left myTracks running all day, I could compare the all-day GPX track to this time log text file to determine which of those points occurred while I was not driving, allowing me to remove those points.

Automation Events

So how could my phone tell me when I’ve started and stopped driving—that is, when to run my Start and Stop automations?

One potential idea was to start logging when the phone connected to my car via Bluetooth and stop when it disconnected. Since it would have been connected to the car only when the car was running, it would be a pretty good signal that I was driving. Unfortunately, iOS automation has a trigger for connecting to a particular Bluetooth network, but not for disconnecting from it, so that didn’t end up being helpful. (This would have also be a pain when driving more than one car, such as with rental cars, since I would have had to update the automation for each car’s unique Bluetooth connection).

Automation does have When turning on driving focus and When turning off driving focus events that I could have potentially used. However, this would have resulted in too many false positives—driving focus often comes on when I’m not driving, like when I’m passenger in someone else’s car, or when I’m riding around an airport in a taxiing airplane.

The best solution I found was to use the When CarPlay connects and When CarPlay disconnects events. Whenever my phone is connected to CarPlay, I can be pretty sure I’m driving. CarPlay is widely available on newer cars; nearly every one of my recent rental cars has been equipped with CarPlay (19 of the 20 rental cars I’ve had to date in 2022), so it’s now reasonable to use this as a reliable driving signal. (For the few rental cars which don’t have CarPlay, I can still revert to using my old speed filter to process tracks.)

Implementation

Setting Up timelog.csv on iCloud

I decided to use a Comma Separated Values (CSV) text file to store my statuses and timestamps. To synchronize it between my iPhone and computers, I stored the file on iCloud. (Since this CSV file is small, the free tier of iCloud I have is more than sufficient.)

On macOS, the iCloud folder is located at “~/Library/Mobile Documents/com~apple~CloudDocs/” (which I symlinked at ~/iCloudDrive/). On Windows, I had to install iCloud, and its folder is located at C:\Users\username\iCloudDrive\. Under this folder, I created a Timelog subfolder and put a timelog.csv file into it (~/iCloudDrive/Timelog/timelog.csv).

I opened the CSV file, added a header row of status,time (my column names), and saved and closed it.

Writing Shortcuts for Appending to Timelog

I wrote two shortcuts; one for when I start driving and one for when I stop driving. Each shortcut appends a row with two comma-separated values to timelog.csv:

Thus, a start row would look like 1,2022-11-17T09:04:46-05:00, and a stop row would look like 0,2022-11-17T10:09:00-05:00.

Within the Shortcuts app, I created a shortcut named Timelog Start. I clicked Add Action, and searched for Append to Text File, which brings up the following screen:

A screenshot of the Append to Text File action settings.

In the Text block, I typed 1,. While the cursor is in the text box, there’s a bar near the bottom of the screen that starts with Select Variable; swiping sideways on it eventually brings up a 🗓️ Current Date option. Clicking that brings up an options dialog; I selected a Date Format of ISO 8601, and turned ISO 8601 Time on.

Back in the action, there’s a to field which is defaulted to Shortcuts; this is meant to be the folder the text file is located in. Since I wanted to save it in the Timelog folder instead, I clicked on it and browsed to iCloud Drive, then my Timelog folder.

The File Path, of course, was timelog.csv.

Finally, I enabled Make New Line at the bottom of the action.

I repeated the above to create a Timelog Stop shortcut. It’s nearly identical to the Timelog Start shortcut; the only difference is that the text block instead started with 0,.

Timelog start and stop shortcuts

Setting up Automation for CarPlay

In the Shortcuts app, there’s an Automation tab at the bottom. Within Automation, clicking the + in the upper right corner brought me to the New Automation Screen, and I chose to Create Personal Automation.

First, I created an automation for CarPlay > When CarPlay Connects. On the Actions screen, I clicked Add Action and searched for Run Shortcut; I then selected the Timelog Start shortcut I’d created. So I would get an indication that the shortcut had run, I also added a second action to Vibrate Device.

Next, I created a similar automation for CarPlay > When CarPlay Disconnects, which would run the Timelog Stop shortcut and vibrate.

Timelog start and stop automations. The Timelog Start shortcut runs on CarPlay connect, and the Timelog Stop shortcut runs on CarPlay disconnect.

Testing this out worked great; every time my phone connected to or disconnected from CarPlay, a line was added to the CSV file in my iCloud folder, just as I wanted.

A screenshot of timelog.csv, showing a number of start and stop entries.

Processing GPX with the Timelog

I wrote a filter_timelog script to filter my all-day myTracks GPX files based on the known start/stop times from the timelog.csv file in my iCloud folder. Effectively, it would split the track (GPX trk) into segments (GPX trkseg) consisting of points between timestamps with 1 and 0 statuses; any points with times between between 0 and 1 status timestamps would be discarded. (If the track starts before the first 0 or ends after the last 1, those points at the beginning or end respectively are kept.)

Diagram showing the timelog as a square wave between 0 and 1 over time. A set of GPX file points is set alongside the square wave. In places where the square wave is 0, the points are discarded; in places where the square wave is 1, the points are kept.

Once the all-day track is split into driving-only segments, the updated GPX data can then be fed into my import process as usual.

Results

Overall, this has worked really well. CarPlay does tend to take a few seconds to connect once the car is started and the phone is plugged in (longer on some cars than others), so there have been a few cases where it missed the first few seconds of driving—but it still captures more of my drives than than my speed filter, so it’s an unambiguous improvement.

Tags: