Last active
September 16, 2024 21:31
-
-
Save ferventcoder/f2e4df46977c52feae7c95579adc4728 to your computer and use it in GitHub Desktop.
Converting Fitbit Weight Measurement Data to Withings Import Format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Convert-FitbitToWithingsWeightMeasurements { | |
[CmdletBinding()] | |
Param( | |
[Parameter()] | |
[String] | |
$Folder =(Get-Location).Path | |
) | |
# Create a collection (a List for memory efficiency) to hold output | |
$WithingsWeightMeasurements = New-Object System.Collections.Generic.List[System.Object] | |
# Search for all files that start with weight and end with .json | |
$JsonFilesCount = 0 | |
$WeightMeasurementsCount = 0 | |
Get-ChildItem $Folder -Filter weight-*.json | | |
Foreach-Object { | |
$JsonFilesCount += 1 | |
# Get JSON content and then add them to the collection | |
Get-Content $_.FullName | Out-String | ConvertFrom-Json | ForEach-Object { | |
$WeightMeasurementsCount += 1 | |
$WithingsWeightMeasurements.Add([PSCustomObject]@{ | |
Date = $([DateTime]$_.date).ToString('yyyy-MM-dd') + " " + $([DateTime]$_.time).ToString("hh:mm:ss") | |
Weight = $_.weight | |
FatMass = if($_.fat -le 1) {""} else {[Math]::Round(($_.weight * ($_.fat / 100)),2)} | |
}) | Out-Null | |
# - Date must be in yyyy-mm-dd hh:mm:ss format (example: 2013-11-22 08:35:00) | |
# - Values must be in US numbers format (with a point as decimal mark) | |
# - Numerical values must be in the same unit set in your dashboard | |
# - Fat mass (optional - format : kg) (OR 'lb' based on "same unit set in your dashboard") | |
# Fat isn't always available from Fitbit either | |
} | |
} | |
# - Do not exceed 300 rows in a file | |
# Split to 299 value arrays | |
$ImportSizeLimit = 299 | |
Write-Host "Captured $WeightMeasurementsCount weight measurements from $JsonFilesCount exported Fitbit files. Splitting data to $ImportSizeLimit batches for import." | |
$WeightMeasurementImportFiles = New-Object System.Collections.ArrayList | |
$WeightItemsTemp = New-Object System.Collections.ArrayList | |
$WithingsWeightMeasurements | ForEach-Object { | |
$WeightItemsTemp.Add($_) | Out-Null | |
If ($WeightItemsTemp.Count -ge $ImportSizeLimit) { | |
$WeightMeasurementImportFiles.Add($WeightItemsTemp.ToArray()) | Out-Null | |
$WeightItemsTemp.Clear() | |
} | |
} | |
# Capture the last set of items | |
if ($WeightItemsTemp.Count -gt 0) { | |
$WeightMeasurementImportFiles.Add($WeightItemsTemp.ToArray()) | Out-Null | |
$WeightItemsTemp.Clear() | |
} | |
# Create output folder | |
$OutputFolder = Join-Path -Path (Get-Location).Path -ChildPath "FilesToImport" | |
If (-Not (Test-Path -Path $OutputFolder)) { | |
Write-Host "- Creating folder for generated files at $OutputFolder" | |
New-Item -Path $OutputFolder -ItemType Directory | Out-Null | |
} | |
# Write each file | |
for ($i = 0; $i -lt $WeightMeasurementImportFiles.Count; $i++) { | |
$WeightMeasurementImportFilePath = Join-Path -Path $OutputFolder -ChildPath "Withings-Weight-Import-$i.csv" | |
Write-Host "- Exporting $($i+1)/$($WeightMeasurementImportFiles.Count) weight measurement import batches to $WeightMeasurementImportFilePath." | |
# - File must have a header | |
# - File must be in a CSV format with commas as column separators and without field separators | |
$WeightMeasurementImportFiles[$i] | Export-Csv $WeightMeasurementImportFilePath -NoTypeInformation -Force -Delimiter ',' -UseQuotes Never | |
} | |
Write-Host "Conversion of Fitbit data complete. Please see instructions at https://support.withings.com/hc/en-us/articles/201491477-Health-Mate-Online-Dashboard-Importing-data for importing." | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PS /code/FitbitToWithingsWeightConversion> . ./Convert-FitbitToWithingsWeightMeasurements.ps1 | |
PS /code/FitbitToWithingsWeightConversion> Convert-FitbitToWithingsWeightMeasurements ./ImportDataOriginal/ | |
Captured 1898 weight measurements from 82 exported Fitbit files. Splitting data to 299 batches for import. | |
- Exporting 1/7 weight measurement import batches to /Users/rob/code/mine/FitbitToWithingsWeightConversion/FilesToImport/Withings-Weight-Import-0.csv. | |
- Exporting 2/7 weight measurement import batches to /Users/rob/code/mine/FitbitToWithingsWeightConversion/FilesToImport/Withings-Weight-Import-1.csv. | |
- Exporting 3/7 weight measurement import batches to /Users/rob/code/mine/FitbitToWithingsWeightConversion/FilesToImport/Withings-Weight-Import-2.csv. | |
- Exporting 4/7 weight measurement import batches to /Users/rob/code/mine/FitbitToWithingsWeightConversion/FilesToImport/Withings-Weight-Import-3.csv. | |
- Exporting 5/7 weight measurement import batches to /Users/rob/code/mine/FitbitToWithingsWeightConversion/FilesToImport/Withings-Weight-Import-4.csv. | |
- Exporting 6/7 weight measurement import batches to /Users/rob/code/mine/FitbitToWithingsWeightConversion/FilesToImport/Withings-Weight-Import-5.csv. | |
- Exporting 7/7 weight measurement import batches to /Users/rob/code/mine/FitbitToWithingsWeightConversion/FilesToImport/Withings-Weight-Import-6.csv. | |
Conversion of Fitbit data complete. Please see instructions at https://support.withings.com/hc/en-us/articles/201491477-Health-Mate-Online-Dashboard-Importing-data for importing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was to get my data into Withings and subsequently into Health. I posted this over at https://support.withings.com/hc/en-us/community/posts/360031291453-Import-Fitbit-Aria-Scale-Info.