DEV Community

G Oh
G Oh

Posted on

Export Mi Band 5 data from Mi Fit to Google Sheets

NOTE: From my experimentation, every sync tends to cause my Mi Fit app on my phone to momentarily disconnect from my band and sometimes causes the app to stop running in the background. So be aware of that and use this script at your own risk.

This requires you to have a google account linked to your Mi Fit App

In order to export your Mi Fit data to Google Sheets, you will first need to create a spreadsheet from Google Drive. Once you created your spreadsheet, go to Tools > Script Editor to open the Google Apps Script page.

This will open up the Google Apps Script IDE:

Name the project whatever you wish by clicking the "Untitled project" and add the Oauth2 library by doing the following:

  1. Click the "+" button on Libraries in the sidebar

  2. Add the following script ID for the OAuth2 library and choose the latest version

    1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
    

Before you can use the library, you must register your script in the API Console. To do this, go to console.cloud.google.com/apis and complete these steps:

  1. Click on Select a project on the top bar then click on New project

  2. Give a name to the project and click the CREATE button

  3. Once the project is created, before you can create a client ID you will need to configure the consent screen:

    1. Click OAuth consent screen in the left sidebar
    2. For User Type check External and hit CREATE
    3. Fill out the App info with a name and email and click "Save and Continue"
  4. Once the OAuth consent screen is click on Credentials in the side bar

  5. Click CREATE CREDENTIALS at the top and click on OAuth client ID

  6. For Application type choose "Web Application" and give it a name

  7. Find your Script ID by going back to the google scripts editor under Project Settings in the left menu

  8. Under Authorized redirect URIs, add this url replacing SCRIPT_ID with your script ID from the previous step and click CREATE:

    https://script.google.com/macros/d/SCRIPT_ID/usercallback
    

Once you created the OAuth client, copy the following code below into code.gs in your script editor overwriting what was there and copy both the Client ID and Client Secret and paste them into the following code replacing YOUR_CLIENT_ID and
YOUR_CLIENT_SECRET on line 1 and 2 and on line 3 change the year to year you first synced data from your band:

Once you have done that, go back to your spreadsheet and reload it. You should now have an extra tab labeled "Mi Fit" at the top next to "Help".

To use the script you need to first click authorize and sign in to the Google account linked to your Mi Fit app.

The first time you attempt to authorize you will get this popup

Simply hit "Go to <project name>" to continue. Click Authorize again and a sidebar will pop up asking you to Sign with Google. Choose the google account you linked to your Mi fit App. Once it's logged in, click sync and you should have the your data.

The Spreadsheet will feature the following columnns and data:

  • Steps
  • Walk or Run Distance
  • Walk Speed
  • Walk Average Heart Rate
  • Sleep Duration (Total)
  • Resting Heart Rate
  • Sleep Duration separated by Sleep Stage
  • PAI score
  • Sleep Stress (Average of all stress measurements taken during sleep)*
  • Average Stress of the day*
  • Morning Readiness (My attemp at mimicking the ways other HRV apps take 1 HRV measurements in the morning)*

    *Must have All Day Stress tracking enabled

Latest comments (5)

Collapse
 
farwamehmood1421 profile image
FarwaMehmood1421

Hey, G Oh. While sync-ing the sheets I am getting the error:

TypeError: Cannot read property 'user_id' of undefined.

Kindly fix the problem for me.

Collapse
 
npocean profile image
npocean

Good day! For some reason the heart rate data doesn't show. Would you happen to have any idea why this is happening? Thanks!

Collapse
 
zont_zo_it_x profile image
midas

Thanks for sharing! I am looking to use this for a school project. Problem is, I am getting this error after trying to sync in google sheets:

TypeError: Cannot read property 'tp' of undefined

Do you have any idea how I can fix this?

Collapse
 
palms profile image
Guilherme Palmerio

Found it!

The error you're encountering indicates an attempt to access the 'tp' property of something that is undefined. In your code, this happens on the line where you try to access element.summary.pai.tp:

Change:
var totalPai = element.summary.pai.tp;

To:
var totalPai = element.summary.pai && element.summary.pai.tp ? element.summary.pai.tp : '';

Apparently, the element.summary.pai object is undefined in some cases, leading to the mentioned error. To fix this, you can add checks to ensure that the properties are present before accessing them.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.