GadgetbridgeMqtt/Readme.md
Oliver Großkloß 592530aac2 fix sleep stage
2025-12-08 14:38:27 +01:00

133 lines
4.1 KiB
Markdown

# 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 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
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
# 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`:
```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 `.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