Skip to content

uratmangun/tuya-battery-monitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tuya Battery Monitor

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.

Features

  • 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

Prerequisites

  • Rust toolchain (rustc, cargo)
  • Linux system with systemd
  • Docker and Docker Compose (for Home Assistant)
  • Home Assistant setup with a smart plug

Setting up Home Assistant

  1. 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
  1. Create the configuration directory:
mkdir config
  1. Start Home Assistant:
docker-compose up -d
  1. Access Home Assistant web interface at http://localhost:8123

  2. Set up your smart plug following Home Assistant's documentation

  3. 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

Installation

  1. Clone the repository:
git clone <your-repo-url>
cd tuya
  1. 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

Building

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

Running

Manual Run

You can run the application directly:

./tuya-monitor

Running as a System Service

Setting up the Systemd Service

  1. 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
  1. Copy the service file to systemd directory:
sudo cp tuya-monitor.service /etc/systemd/system/
  1. Reload systemd daemon and enable the service:
sudo systemctl daemon-reload
sudo systemctl enable tuya-monitor
sudo systemctl start tuya-monitor
  1. Check service status:
systemctl status tuya-monitor

Service Management

  • 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

Configuration

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.

System Information CLI Tool

The project includes a CLI tool for checking system information directly:

Building the CLI Tool

cd sysinfo-cli
cargo build --release

Usage

The tool provides three main commands:

  1. Show all system information:
./target/release/sysinfo
# or
./target/release/sysinfo all
  1. Show only CPU temperature:
./target/release/sysinfo temp
  1. Show only battery information:
./target/release/sysinfo battery

Example Output

System Information:
  CPU Temperature: 51.9°C
  Battery Level: 72%
  Battery Status: Charging

Troubleshooting

  1. If the service fails to start, check the logs:
journalctl -u tuya-monitor -f
  1. Verify environment variables:
systemctl show tuya-monitor
  1. Check file permissions:
ls -l tuya-monitor
  1. If CPU temperature shows as N/A, ensure your user has the right permissions:
sudo usermod -aG sys your_username

License

MIT License. See LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published