Fitbit-Notion Health Sync with AI Food Tracking

Fitbit-Notion Health Sync with AI Food Tracking
Overview
Comprehensive health tracking automation that syncs Fitbit data and analyzes food photos using AI, all automatically managed through GitHub Actions. Perfect for quantified-self enthusiasts who want seamless health data integration.
🚀 Key Features
📊 Comprehensive Fitbit Integration
- Activity Metrics: Steps, distance, calories, active minutes
- Sleep Analysis: Total hours, efficiency, start/end times, deep/light/REM stages
- Heart Rate Zones: Resting HR, fat burn/cardio/peak zone minutes
- HRV Tracking: Daily and deep sleep heart rate variability
- Body Metrics: Weight, BMI, body fat percentage (when available)
🍽️ AI-Powered Food Photo Analysis
- Google Drive Integration: Upload food photos to designated folder
- Original Timestamp Extraction: Uses EXIF data to capture when photos were actually taken
- Smart Meal Classification: Automatically categorizes by time (breakfast 6-11AM, lunch 11AM-4PM, dinner 5-11PM)
- Gemini 2.5 Flash AI: Provides concise food descriptions ("latte", "fried eggs with tomatoes")
- Multi-layered Timestamp Processing: EXIF → Drive metadata → filename parsing → upload time fallback
⚙️ Automation & Reliability
- Daily GitHub Actions: Runs every morning at 9 AM Zurich time
- Manual Sync Options: On-demand processing via GitHub Actions or local scripts
- Automatic Token Refresh: Handles OAuth token expiration automatically
- Historical Backfill: Fill in missing data for any date range
- Error Handling: Graceful handling of missing data and API failures
🗃️ Notion Database Integration
- Structured Schema: Automatically creates and updates database columns
- Rich Text Support: Food descriptions stored as formatted rich text
- Data Validation: Ensures data integrity and handles missing values
- Progress Tracking: Checkbox to track food photo processing status
🛠️ Technical Architecture
Backend & APIs
- Python 3.10+ with asyncio for efficient API calls
- Fitbit Web API (Server application type for intraday access)
- Notion API with official Python client
- Google Drive API for photo storage and retrieval
- Google Gemini 2.5 Flash for multimodal food recognition
Authentication & Security
- OAuth 2.0 flows for all service integrations
- GitHub Secrets for secure credential storage
- Automatic token refresh with .env file updates
- No data retention - personal health data never committed to repository
Image Processing
- Pillow (PIL) for EXIF data extraction
- Multi-source timestamp detection:
- EXIF DateTimeOriginal (most reliable)
- Google Drive image metadata
- Filename pattern parsing (IMG_20240723_142530.jpg)
- Upload time fallback with user warning
Infrastructure
- GitHub Actions for scheduling and automation
- Dotenv for local development environment management
- Request handling with proper rate limiting and error recovery
- Modular architecture with separate concerns for each API
🎯 Problem Solved
The Challenge
Manual health tracking is tedious and inconsistent. Existing solutions either:
- Don't integrate multiple data sources
- Require manual food logging
- Can't extract original photo timestamps
- Don't provide AI-powered food recognition
- Lack automation and reliability
The Solution
This system creates a fully automated health tracking pipeline that:
- Eliminates manual data entry through API integrations
- Preserves data context by extracting original photo timestamps
- Provides intelligent food recognition without manual descriptions
- Maintains data consistency through automated daily sync
- Scales effortlessly with GitHub Actions infrastructure
🔧 Technical Highlights
OAuth Token Management
Implemented robust token refresh system that automatically handles expiration:
def refresh_fitbit_tokens():
# Automatic token refresh with .env file updates
# Handles 401 errors gracefully
# Updates GitHub Secrets for Actions
Multi-layered Timestamp Extraction
Prioritized approach to get accurate photo timestamps:
def get_photo_timestamp(file_info, credentials):
# 1. EXIF DateTimeOriginal (when photo was taken)
# 2. Google Drive image metadata
# 3. Filename pattern parsing
# 4. Upload time fallback
AI Prompt Engineering
Optimized Gemini prompt for concise food descriptions:
prompt = """List only the food and drink items in this image.
No descriptions, no presentation details, no extra words.
Good examples:
- "cappuccino" (not "coffee with latte art")
- "fried eggs with tomatoes and cucumber"
Just the food items, separated by commas if multiple dishes."""
📈 Impact & Results
Personal Health Insights
- Zero manual effort for comprehensive health tracking
- Rich historical data for trend analysis and pattern recognition
- Food context linked to exact meal times and dates
- Complete picture combining biometrics with nutrition
Technical Achievements
- 100% automation - no daily maintenance required
- Multi-API integration with robust error handling
- Advanced timestamp processing preserving data accuracy
- Scalable architecture supporting additional health data sources
🚀 Getting Started
Prerequisites
- Fitbit Developer app (Server type)
- Notion integration and database
- Google Cloud project with Drive API
- Gemini API key from Google AI Studio
Quick Setup
- Clone and install:
pip install -r requirements.txt
- Run OAuth flows: Get tokens for all services
- Configure GitHub Secrets: Add all API credentials
- Update Notion schema:
python update_notion_schema.py
- Test sync:
python manual_sync_today.py
The system is designed for set-it-and-forget-it operation with comprehensive documentation for troubleshooting and customization.
🎓 Educational Value
Perfect example of:
- API integration patterns and authentication flows
- Computer vision applications in real-world scenarios
- Automation infrastructure with GitHub Actions
- Data pipeline design with error handling and recovery
- Multi-modal AI integration for practical use cases
This project demonstrates how modern APIs, AI, and automation can create powerful personal productivity tools that provide genuine value in daily life.
Project Gallery
