A Rust application that monitors your system's battery level and automatically controls a smart plug through Home Assistant to manage charging. When the battery level drops below 20%, it turns on the smart plug to start charging, and when it reaches 79%, it turns off the plug to prevent overcharging.
This project uses Bardi Smart Power Strip as the smart plug, but you can use any smart plug that's compatible with Home Assistant and tuya.
- Real-time battery level monitoring
- Automatic smart plug control based on battery levels
- Desktop notifications for battery status
- Systemd service integration for automatic startup
- CPU temperature monitoring
- Built with pure Rust for optimal performance
- Rust toolchain (rustc, cargo)
- Linux system with systemd
- Docker and Docker Compose (for Home Assistant)
- Home Assistant setup with a smart plug
- Create a
docker-compose.yml
file:
version: '3'
services:
homeassistant:
container_name: homeassistant
image: "ghcr.io/home-assistant/home-assistant:stable"
volumes:
- ./config:/config
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
privileged: true
network_mode: host
- Create the configuration directory:
mkdir config
- Start Home Assistant:
docker-compose up -d
-
Access Home Assistant web interface at
http://localhost:8123
-
Set up your smart plug following Home Assistant's documentation
-
Generate a Long-Lived Access Token:
- Go to your profile in Home Assistant
- Scroll to the bottom
- Under "Long-Lived Access Tokens" click "Create Token"
- Save this token for use in the
.env
file
- Clone the repository:
git clone <your-repo-url>
cd tuya
- Create a
.env
file in the project root with your Home Assistant credentials:
HOMEASSISTANT_URL=your_homeassistant_url
HOMEASSISTANT_TOKEN=your_long_lived_access_token
Build the release version:
cargo build --release
This will create an executable at target/release/tuya
. Copy it to your desired location:
cp target/release/tuya tuya-monitor
You can run the application directly:
./tuya-monitor
- Create a
tuya-monitor.service
file with the following content:
[Unit]
Description=Tuya Battery Monitor Service
After=network.target
[Service]
Type=simple
User=your_username
AmbientCapabilities=CAP_SYS_RAWIO
WorkingDirectory=/path/to/tuya/directory
ExecStart=/path/to/tuya/directory/tuya-monitor
Restart=always
RestartSec=10
Environment=DISPLAY=:0
EnvironmentFile=/path/to/tuya/directory/.env
[Install]
WantedBy=multi-user.target
- Copy the service file to systemd directory:
sudo cp tuya-monitor.service /etc/systemd/system/
- Reload systemd daemon and enable the service:
sudo systemctl daemon-reload
sudo systemctl enable tuya-monitor
sudo systemctl start tuya-monitor
- Check service status:
systemctl status tuya-monitor
- Start the service:
sudo systemctl start tuya-monitor
- Stop the service:
sudo systemctl stop tuya-monitor
- Restart the service:
sudo systemctl restart tuya-monitor
- View logs:
journalctl -u tuya-monitor -f
The application monitors battery levels with the following thresholds:
- Below 20%: Turns ON the smart plug to start charging
- Above 79%: Turns OFF the smart plug to prevent overcharging
- Checks every 5 minutes
To modify these thresholds, edit the values in src/main.rs
.
The project includes a CLI tool for checking system information directly:
cd sysinfo-cli
cargo build --release
The tool provides three main commands:
- Show all system information:
./target/release/sysinfo
# or
./target/release/sysinfo all
- Show only CPU temperature:
./target/release/sysinfo temp
- Show only battery information:
./target/release/sysinfo battery
System Information:
CPU Temperature: 51.9°C
Battery Level: 72%
Battery Status: Charging
- If the service fails to start, check the logs:
journalctl -u tuya-monitor -f
- Verify environment variables:
systemctl show tuya-monitor
- Check file permissions:
ls -l tuya-monitor
- If CPU temperature shows as N/A, ensure your user has the right permissions:
sudo usermod -aG sys your_username
MIT License. See LICENSE file for details.