A Node.js application that monitors your system's battery level and automatically controls a Tuya smart plug to manage charging. When the battery level drops below 30%, it turns on the smart plug to start charging, and when it reaches 80%, it turns off the plug to prevent overcharging.
- Real-time battery level monitoring
- Automatic smart plug control based on battery levels
- Desktop notifications for battery status
- Systemd service integration for automatic startup
- Built with Rust native addon for system information
- Node.js 18 or higher
- pnpm package manager
- Rust toolchain (for native addon compilation)
- Linux system with systemd
- Tuya smart plug and API credentials
- Clone the repository:
git clone <your-repo-url>
cd tuya
- Install dependencies:
pnpm install
- Create a
.env
file in the project root with your Tuya credentials:
TUYA_API_URL=your_api_url
TUYA_ACCESS_KEY=your_access_key
TUYA_ACCESS_SECRET=your_secret_key
TUYA_POWER_STRIP_DEVICE_ID=your_device_id
- Build the Rust addon:
pnpm build:rust
- Build the application:
pnpm build
This will create an executable named tuya-monitor
in your project directory.
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
WorkingDirectory=/path/to/tuya/directory
ExecStart=/usr/bin/node dist/index.js
Restart=always
RestartSec=10
Environment=NODE_ENV=production
[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:
sudo systemctl status tuya-monitor
- Check service status:
sudo systemctl status tuya-monitor
- Stop the service:
sudo systemctl stop tuya-monitor
- View logs:
sudo journalctl -u tuya-monitor
The application monitors battery levels with the following thresholds:
- Below 30%: Turns ON the smart plug to start charging
- Above 80%: Turns OFF the smart plug to prevent overcharging
- Checks every 10 minutes
To modify these thresholds, edit the values in index.mjs
.
The project uses:
- ES6+ JavaScript (transpiled to ES5)
- Rust for native system information
- Babel for transpilation
- pkg for binary creation
To modify the code:
- Make changes to
index.mjs
- Run
pnpm build
to create a new binary - Restart the service if running:
sudo systemctl restart tuya-monitor
- If the service fails to start, check the logs:
sudo journalctl -u tuya-monitor -f
- Verify environment variables:
sudo systemctl show tuya-monitor
- Check file permissions:
ls -l tuya-monitor
ISC License