Export Mi Band 5 data from Mi Fit to Google Sheets
Tags: javascript, googleappsscript, gas, googlesheets
Published: 2021-01-26 06:18:08

***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.

 ![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/spreadsheet-tools-script-editor.png)

This will open up the Google Apps Script IDE:

![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/google-apps-script-ide.png)

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](https://github.com/googleworkspace/apps-script-oauth2#setup) and choose the latest version 

	```html
	1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
	```

	![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/library-script-id.png)

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](https://console.cloud.google.com/apis/dashboard) and complete these steps:

1. Click on **Select a project** on the top bar then click on **New project**
	
	![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/api-console.png)

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

6. Click **CREATE CREDENTIALS** at the top and click on **OAuth client ID**
	![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/create-credentials.png)

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

8. Find your Script ID by going back to the google scripts editor under **Project Settings** in the left menu
	![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/Script-id.png)

9. Under **Authorized redirect URIs**, add this url replacing SCRIPT_ID with your script ID from the previous step and click **CREATE**:
	 ```html
	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:

 {% gist https://gist.github.com/chemical-machine/a262c7ab149477ae5312eb69f6317ff8 %}

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". 
![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/mi-fit-tab.png)
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
![](https://chemicalmachine.gitlab.io/posts/mi-fit-post/verify-proceed.png)
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_