Telegram bot for power monitoring via Tapo Smart Plug.
- Features
- Requirements
- Demo
- Quick Start
- Configuration
- Bot Commands
- Project Structure
- Development
- Database Migrations
- Makefile Commands
- Troubleshooting
- Technologies
- License
- π Power status monitoring via Tapo Smart Plug
- πΎ Event history storage in PostgreSQL
- π± User notifications via Telegram about status changes
- β±οΈ Duration display for outages/power availability
- π Outage history view for recent period
- π₯ Subscribe/unsubscribe from notifications
- π§ CLI tools for administration
- Docker 20.10+
- Docker Compose 2.0+
- Git
- Telegram Bot Token (from @BotFather)
- Tapo Smart Plug with local network access
- Python 3.11+
- PostgreSQL 15+
- Git
- Telegram Bot Token (from @BotFather)
- Tapo Smart Plug with local network access
git clone https://github.com/dixydo/powerbot.git
cd powerbotCreate .env file based on .env.example:
cp .env.example .envEdit .env and fill in your values:
# Telegram Bot Configuration
BOT_TOKEN=your_telegram_bot_token_here
ADMIN_USER_ID=your_telegram_user_id
# Tapo Device Configuration
TAPO_EMAIL=your_tapo_email@example.com
TAPO_PASSWORD=your_tapo_password
DEVICE_IP=192.168.1.100
# Monitoring Settings
CHECK_INTERVAL=30
CONFIRMATION_CHECKS=2
TIMEZONE=Europe/KyivHow to get Bot Token:
- Find @BotFather in Telegram
- Send
/newbotcommand - Follow instructions and save the token
How to find your User ID:
- Find @userinfobot in Telegram
- Send
/startand get your ID
docker-compose up -dThat's it! Docker will:
- ποΈ Start PostgreSQL database
- π€ Build and start the bot
- π Apply migrations automatically
- π Create logs directory
# View bot logs
docker-compose logs -f bot
# View all logs
docker-compose logs -f
# View logs from file
tail -f logs/bot_$(date +%Y-%m-%d).log# Stop bot
docker-compose down
# Stop and remove volumes (database data)
docker-compose down -v
# Restart bot
docker-compose restart bot
# Rebuild and restart after changes
docker-compose up -d --build
# View running containers
docker-compose psNote: Changes in requirements.txt or Dockerfile require rebuild with docker-compose up -d --build.
git clone https://github.com/dixydo/powerbot.git
cd powerbotCreate .env file based on .env.example:
cp .env.example .envEdit .env and fill in your values:
# Telegram Bot Configuration
BOT_TOKEN=your_telegram_bot_token_here
ADMIN_USER_ID=your_telegram_user_id
# Tapo Device Configuration
TAPO_EMAIL=your_tapo_email@example.com
TAPO_PASSWORD=your_tapo_password
DEVICE_IP=192.168.1.100
# Monitoring Settings
CHECK_INTERVAL=30
CONFIRMATION_CHECKS=2
TIMEZONE=Europe/Kyiv
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=powerbot
DB_USER=powerbot
DB_PASSWORD=powerbotmake installMake sure PostgreSQL is running and accessible on localhost:5432.
Create database and user:
CREATE DATABASE powerbot;
CREATE USER powerbot WITH PASSWORD 'powerbot';
GRANT ALL PRIVILEGES ON DATABASE powerbot TO powerbot;
\c powerbot
GRANT ALL ON SCHEMA public TO powerbot;Note: The \c powerbot command connects to the database, and GRANT ALL ON SCHEMA public grants rights to the public schema, which is required for creating tables.
make migrateBackground mode (recommended):
make startBot will start in background. Logs will be written to logs folder.
Other commands:
make stop # Stop bot
make status # Check bot status
make logs # View current logsView logs:
# Current logs
tail -f logs/bot_$(date +%Y-%m-%d).log
# Or all logs for today
cat logs/bot_$(date +%Y-%m-%d).log
# Logs for specific date
cat logs/bot_2026-01-25.logLogs automatically rotate daily at midnight. Logs are kept for the last 30 days.
- Connect your Tapo Smart Plug to Wi-Fi via Tapo mobile app
- Find device IP address:
- Via Tapo app (Device Info)
- Via router web interface
- Using
arp -acommand or other network utilities
- Prepare email and password from Tapo account
By default, the bot uses Europe/Kyiv timezone. You can change it in .env:
TIMEZONE=Europe/Kyiv # or another timezone, e.g. America/New_YorkTimezone is used for:
- Displaying time in outage history
- Event logging
- Log file rotation
For production use, it's recommended to:
- Use separate PostgreSQL server
- Change passwords in
.envto secure ones - Configure backups
Bot supports both slash commands and menu buttons:
/start- Subscribe to notifications/status- Check current power status/history- View outage history/stop- Unsubscribe from notifications/broadcast <text>- Send message to all users (admin only)
- β Current Status - Check power status
- π View Outage History - View last 10 outages
- π Unsubscribe - Unsubscribe from notifications
power-bot/
βββ app/
β βββ main.py # Main application entry point
β βββ bot.py # Telegram bot handlers
β βββ monitor.py # Power monitoring loop
β βββ database.py # Database operations
β βββ config.py # Configuration and logging
β βββ migrate.py # Migration runner
β βββ migrations/ # SQL migrations
β βββ 001_create_users.sql
β βββ 002_create_power_events.sql
β βββ 003_create_notifications.sql
βββ logs/ # Log files (auto-created)
βββ .env # Environment variables (create from .env.example)
βββ .env.example # Environment variables template
βββ .dockerignore # Docker ignore rules
βββ .gitignore # Git ignore rules
βββ docker-compose.yml # Docker Compose configuration
βββ Dockerfile # Docker image definition
βββ requirements.txt # Python dependencies
βββ Makefile # Development commands
βββ LICENSE # MIT License
βββ README.md # This file
-
Create virtual environment:
make venv
-
Install dependencies:
make install
-
Apply migrations:
make migrate
-
Start bot:
make start
- Follow PEP 8
- Use type hints
- Write docstrings for functions
- Use async/await for I/O operations
Project uses custom migration system based on SQL files. Migrations are located in app/migrations/ directory.
NNN_description.sql
Where:
NNN- sequential number (001, 002, etc.)description- brief description
-
Create new file in
app/migrations/:touch app/migrations/005_add_new_table.sql
-
Write SQL:
CREATE TABLE new_table ( id BIGSERIAL PRIMARY KEY, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() );
-
Apply migration:
make migrate
Project automatically creates schema_migrations table to track applied migrations:
CREATE TABLE schema_migrations (
id BIGSERIAL PRIMARY KEY,
filename TEXT NOT NULL UNIQUE,
applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);make venv # Create virtual environment
make install # Install dependenciesmake start # Start bot in background
make stop # Stop bot
make status # Check bot status
make logs # View current logsmake migrate # Apply migrations-
Check if database is running:
pg_isready -h localhost -p 5432
-
Verify credentials in
.env -
Check PostgreSQL logs
-
Try applying migrations again
- Check device IP address in
.env - Make sure device is on same network
- Verify email/password from Tapo account
- Try rebooting Smart Plug
make stop- Python 3.11+ - main programming language
- aiogram 3.24.0 - Telegram Bot API framework
- psycopg 3 - PostgreSQL adapter for Python
- tapo 0.8.8 - API for Tapo Smart Plug control
- pytz 2025.2 - timezone handling
- PostgreSQL 15 - relational database
- Docker & Docker Compose - containerization (optional)
- Make - development command automation
- Asynchronous architecture based on
asyncio - Two parallel processes: bot + monitoring
- SQL migrations for database schema versioning
- Environment-based configuration
MIT License - see LICENSE for details.