No description
- Python 100%
| .gitignore | ||
| main.py | ||
| Readme.md | ||
| setup.py | ||
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 - Terminal emulator
- Termux:Boot - Auto-start scripts on boot
- Gadgetbridge - Fitness tracker app
2. Run Setup in Termux
Open Termux and run:
# Grant storage access
termux-setup-storage
# Install Python and download setup
pkg update -y && pkg install -y python
mkdir -p ~/scripts && curl -sL https://git.olli.info/Oliver/GadgetbridgeMqtt/raw/branch/main/setup.py -o ~/scripts/setup.py
# Run setup (interactive)
python ~/scripts/setup.py
3. Configure Gadgetbridge
- Open Gadgetbridge app
- Go to Settings → Auto Export
- Set Location to:
/storage/emulated/0/Documents/GB_Export - Enable export (the script will trigger syncs automatically)
4. Enable Autostart
- Open Termux:Boot app once (this enables the autostart feature)
- Reboot your phone
The script will now start automatically on boot!
Manual Operation
# Start manually
python ~/scripts/gadgetbridge_mqtt.py
# View logs
tail -f ~/gb_mqtt.log
# Edit config
nano ~/.config/gadgetbridge_mqtt/config.json
# Update to latest version
curl -sL https://git.olli.info/Oliver/GadgetbridgeMqtt/raw/branch/main/main.py -o ~/scripts/gadgetbridge_mqtt.py
Configuration
Config is stored at ~/.config/gadgetbridge_mqtt/config.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
}
Security Note: MQTT credentials are stored in plaintext. The setup script sets file permissions to 600 (owner read/write only), but be aware of this if sharing your device or backups.
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 |
| Sleep Stage | gadgetbridge/{device}/sleep_stage |
text |
| Sleep Stage Code | gadgetbridge/{device}/sleep_stage_code |
int |
| 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
.dbfiles exist in export folder - Ensure MQTT broker is reachable:
ping {broker_ip}
Permission issues
- Run
termux-setup-storageand grant permissions - Ensure Gadgetbridge has storage permissions
How It Works
-
Every 5 minutes (configurable), the script:
- Sends
ACTIVITY_SYNCintent to Gadgetbridge (syncs data from band) - Sends
TRIGGER_EXPORTintent (exports database) - Reads the exported database
- Publishes sensor data via MQTT
- Sends
-
Home Assistant discovers sensors automatically via MQTT discovery