# Gadgetbridge MQTT for Termux Publish fitness data from Gadgetbridge to Home Assistant via MQTT directly from your Android phone using Termux. ## Features - 📊 Publishes steps, heart rate, sleep, battery, weight, and more - 🔄 Auto-syncs with your fitness band every 5 minutes - 🏠 Home Assistant auto-discovery via MQTT - �� Runs entirely on your phone (no server needed) - 🚀 Auto-starts on boot ## Quick Setup ### 1. Install Apps from F-Droid - **[Termux](https://f-droid.org/packages/com.termux/)** - Terminal emulator - **[Termux:Boot](https://f-droid.org/packages/com.termux.boot/)** - Auto-start scripts on boot - **[Gadgetbridge](https://f-droid.org/packages/nodomain.freeyourgadget.gadgetbridge/)** - Fitness tracker app ### 2. Run Setup in Termux Open Termux and run: ```bash # Grant storage access termux-setup-storage # Install Python pkg update -y && pkg install -y python # Download and run setup curl -sL https://git.olli.info/Oliver/GadgetbridgeMqtt/raw/branch/main/setup.py | python ``` Or step-by-step: ```bash termux-setup-storage pkg update -y && pkg install -y python wget mkdir -p ~/scripts wget -O ~/scripts/setup.py https://git.olli.info/Oliver/GadgetbridgeMqtt/raw/branch/main/setup.py python ~/scripts/setup.py ``` ### 3. Configure Gadgetbridge 1. Open **Gadgetbridge** app 2. Go to **Settings → Auto Export** 3. Set **Location** to: `/storage/emulated/0/Documents/GB_Export` 4. Enable export (the script will trigger syncs automatically) ### 4. Enable Autostart 1. Open **Termux:Boot** app once (this enables the autostart feature) 2. **Reboot your phone** The script will now start automatically on boot! ## Manual Operation ```bash # Start manually python ~/scripts/gadgetbridge_mqtt.py # View logs tail -f ~/gb_mqtt.log # Edit config nano ~/.config/gadgetbridge_mqtt/config.json ``` ## Configuration Config is stored at `~/.config/gadgetbridge_mqtt/config.json`: ```json { "mqtt_broker": "192.168.1.100", "mqtt_port": 1883, "mqtt_username": "your_username", "mqtt_password": "your_password", "export_dir": "/storage/emulated/0/Documents/GB_Export", "publish_interval": 300 } ``` ## Published Sensors | Sensor | Topic | Unit | |--------|-------|------| | Daily Steps | `gadgetbridge/{device}/daily_steps` | steps | | Weekly Steps | `gadgetbridge/{device}/weekly_steps` | steps | | Heart Rate | `gadgetbridge/{device}/heart_rate` | bpm | | Resting HR | `gadgetbridge/{device}/hr_resting` | bpm | | Max HR | `gadgetbridge/{device}/hr_max` | bpm | | Average HR | `gadgetbridge/{device}/hr_avg` | bpm | | Battery | `gadgetbridge/{device}/battery_level` | % | | Calories | `gadgetbridge/{device}/calories` | kcal | | Sleep Duration | `gadgetbridge/{device}/sleep_duration` | hours | | Is Awake | `gadgetbridge/{device}/is_awake` | bool | | Weight | `gadgetbridge/{device}/weight` | kg | | Last Update | `gadgetbridge/{device}/server_time` | timestamp | ## Home Assistant Sensors are automatically discovered via MQTT. They will appear under: - **Devices**: Gadgetbridge {Your Device Name} ## Troubleshooting ### Script not starting on boot - Make sure you opened Termux:Boot at least once - Check if battery optimization is disabled for Termux - Check logs: `cat ~/gb_mqtt.log` ### No data being published - Verify Gadgetbridge export directory is correct - Check if `.db` files exist in export folder - Ensure MQTT broker is reachable: `ping {broker_ip}` ### Permission issues - Run `termux-setup-storage` and grant permissions - Ensure Gadgetbridge has storage permissions ## How It Works 1. Every 5 minutes (configurable), the script: - Sends `ACTIVITY_SYNC` intent to Gadgetbridge (syncs data from band) - Sends `TRIGGER_EXPORT` intent (exports database) - Reads the exported database - Publishes sensor data via MQTT 2. Home Assistant discovers sensors automatically via MQTT discovery ## Files - `main.py` - Main MQTT publisher script - `setup.py` - Interactive setup script - `~/.config/gadgetbridge_mqtt/config.json` - Configuration - `~/.termux/boot/start_gb_mqtt` - Autostart script - `~/gb_mqtt.log` - Log file ## License MIT