Hey đź‘‹ new reader,
Pluralsight gave me some free

1 month
subscriptions

, get them before
they are gone!

Automatic Sleep Tracking

· 3 mins read Edit Post
How I track my Sleep to my Google calendar.

When I sleep in, I want to know. When I am productive, I want to know.

I started using the iOS app Sleep Cycle to track my sleep and as an alarm. It takes 66 days to form a habit, so after 66 days of using the app, I looked at my calendar and I couldn’t tell when I woke up and went to sleep each night. I wanted to know when I slept-in on weekends, or got up early.

Screenshot showing how my sleep is automatically added to my Google calendar.

I researched Sleep Cycle and found they did not have an API I could use. I paid the 69 pence for premium to access their online dashboard and found I could access the underlying data. This was a long-winded method to retrieving data and wasn’t a sustainable method - if they changed their front-end codebase, I would need to update my script.

I experimented with tools to connect different services, such as IFTTT and loved them!

I noticed that Sleep Cycle syncs its data with Apple iOS health. I wanted to access that data and insert it into my calendar. IFTTT doesn’t directly integrate with iOS Health, so I had to look around for other services that used that data and integrated with IFTTT.

I settled on the Jawbone UP app (although I have never owned a Jawbone device) and used IFTTT, Zapier & Google Calendar to connect everything.

The Problem

The most difficult issue was getting the data into the correct format for Google Calendar to process. I was almost regretting my decision to not create my own program, but then I remembered Google Sheets.

My Solution

I wrote a small script to parse the sleep data into the correct format for Google Calendar to process and I was away!

function checkIfNewRow() {

  if(latest_sleep_row != latest_processed_row) {

    // Process the row
    addToFinalSheet();

    // Append to the processed sheet
    sheetLatest.appendRow([latest_sleep]);

  } else {
    // do nothing, as there is nothing new
  }
}

Sleep Cycle - Steps

These are the steps to complete the integration process:

  1. Make an IFTTT account.

  2. Create new IFTTT trigger. Using this formula.

  3. Download the UP app.

  4. To test the IFTTT trigger, go into the UP app and Track Sleep for over 2 hours, to force the IFTTT trigger.

  5. Edit the IFTTT Google Sheets Spreadsheet to have the sheets that I have in mine here. Note include 1 value to each sheet row.

  6. Create a new Google Sheet script for the IFTTT spreadsheet and add this script in it.

  7. Now at this point. Your sleep should be automatically tracked and new items post processed and appended to Sheet2. Now to add them to your calendar…

  8. Create a Zapier account (I have a whole new account, dedicated for sleep, as this will run everyday). Use this recipe to append the sleep to your calendar in the correct format.

  9. Test everything works by force adding sleep to UP > 2 hours.

  10. You (hopefully) should see it appear in your Google calendar after the IFTTT trigger happens and Zapier trigger happens (30m max). You can force IFTTT and Zapier triggers to happen by going to the triggers and pressing run.

Google Sheets Script found here.

Google Sheets SpreadSheet found here.

Fitbit - Steps

Months later, I managed to get my hands on a Fitbit Flex and tracking my sleep was MUCH simpler! Use this IFTTT trigger and it starts logging whenever you sleep! Using Sleep Cycle was an interesting process where I utilised services to achieve a goal. I’m glad I did it!

return home