A Python script that automatically updates Tesla Powerwall electricity rates based on ENTSOE day-ahead market prices.
- Fetches real-time electricity prices from ENTSOE transparency platform
- Automatically configures Tesla Powerwall Time-of-Use (TOU) rates
- Supports dynamic rate bands based on market price percentiles
- Includes fallback to stub implementation for testing
- Secure API key management with local JSON files
- Python 3.8+
- Tesla Powerwall with Time-of-Use enabled
- ENTSOE API key (register at https://transparency.entsoe.eu/)
- Tesla account with API access
-
Clone or download the project files
-
Install dependencies:
pip install -r requirements.txt
-
Configure API keys:
- Edit
entsoe_api_key.jsonwith your ENTSOE API key - The Tesla refresh token will be automatically managed
- Edit
Edit entsoe_api_key.json:
{"api_key": "your_entsoe_api_key_here"}TESLA_USE_SIMPLIFIED_TOU=true- Use simplified 2-period TOU formatTESLA_APPLY_TODAY_RATES=true- Apply today's rates (for early execution)DEBUG=true- Enable debug output
python powerrates.pyUse the pre-configured launch configurations:
Run PowerRates Main Script- Execute the main scriptDebug PowerRates Main Script- Debug with breakpointsRun PowerRates Tests- Run the test suiteDebug PowerRates Tests- Debug specific tests
python -m unittest test_powerrates -vThe test suite includes an integration test that validates actual connectivity to the ENTSOE API:
# Run only the connectivity test
python -m unittest test_powerrates.TestEntsoeApiKey.test_entsoe_api_connectivity_with_real_key -vThis test will:
- ✅ Verify your API key is properly formatted
- ✅ Test actual network connectivity to ENTSOE
- ✅ Validate API response structure
- ✅ Skip automatically if using placeholder credentials
/Users/ries/oteny/power/
├── powerrates.py # Main script
├── test_powerrates.py # Test suite
├── entsoe_api_key.json # ENTSOE API key (configure this)
├── tesla_refresh_token.json# Tesla refresh token (auto-managed)
├── requirements.txt # Python dependencies
├── .vscode/ # VS Code configuration
│ ├── launch.json # Debug configurations
│ ├── tasks.json # Build tasks
│ └── settings.json # Editor settings
├── .gitignore # Git ignore patterns
└── README.md # This file
The project includes comprehensive unit tests following Python best practices:
test_powerrates.py- Complete test suite using unittest- Tests for API key loading, price fetching, and utility functions
- Mocked external API calls for reliable testing
- Edge case and error handling coverage
Run tests:
# From VS Code: Use "Run PowerRates Tests" launch config
# Or from terminal:
python3 -m unittest test_powerrates -vThe project includes optimized VS Code configuration:
- Pre-configured debug launchers for main script and tests
- Python linting with flake8
- Code formatting with black
- Integrated terminal tasks
- Follows PEP 8 style guidelines
- Line length limit: 120 characters
- Comprehensive test coverage
- Type hints where appropriate
- API keys are stored in local JSON files (excluded from git)
- Tesla refresh tokens are automatically managed
- Never commit sensitive credentials to version control
-
API Key Issues: Ensure
entsoe_api_key.jsoncontains a valid ENTSOE API key -
Tesla Authentication: First run may require browser authentication
-
Network Issues: Check internet connectivity for API calls
-
SSL Certificate Issues (macOS): If you encounter SSL certificate errors:
# Run the certificate installer (recommended) /Applications/Python\ 3.12/Install\ Certificates.command # Or update certifi package pip install --upgrade certifi
Enable debug output:
DEBUG=true python3 powerrates.pyUse VS Code's "Debug PowerRates Tests" configuration to step through test execution.
- Follow the existing code style
- Add tests for new functionality
- Update documentation as needed
- Ensure all tests pass before submitting
This project is provided as-is for educational and personal use.