Skip to main content

A Python module to charge/discharge Tesla Powerwall based on solar forecast and peak/off peak tariffs.

Project description

PwForecast

A Python module to charge/discharge Tesla Powerwall based on solar forecast and peak/off-peak tariffs.

Utilises the excellent TeslaPy by Tim Dorssers.

PwForecast is primarily designed for those with dual rate tariffs. If there is interest in expanding this to provide more flexible control for those with more complex tariffs, please let me know.

Version

Getting Started

PwForecast requires a Solcast API key and Site ID. You can sign up for a free Hobbyist account which allows up to two sites and 50 API calls per day.

TeslaPy has great documentation on getting started. It even works with two-factor authentication enabled.

Rather than inherit, PwForecast requires an instance of a TeslaPy Tesla class passed to it. This allows you to configure the object based on your credentials before passing to PwForecast.

When using PwForecast, Self Powered mode is recommended. Time Based Control could be used if you need to charge faster than 1.7kW per Powerwall, but results may be unpredictable.

Overview

PwForecast has two main methods, set_peak_mode and set_off_peak_mode. Both can be configured using a PwForecast instance.

Here is an example showing how to configure teslapy and PwForecast:

import teslapy
from pwforecast import PwForecast

tesla_email = 'name@example.com'
solcast_api_key = '1234-1234'
solcast_site_ids = {'Site_A': '1234-1234',
                    'Site_B': '1234-1234'}

with teslapy.Tesla(email=tesla_email) as tesla:

    pw_forecast = PwForecast(teslapy_session=tesla,
                             solcast_api_key=solcast_api_key,
                             solcast_site_ids=solcast_site_ids)

Setting Peak Mode

Use this method when your peak rate starts. You can configure what backup reserve the Powerwall can discharge down to by setting the min_reserve_peak_rate value.

pw_forecast.min_reserve_peak_rate = 10  # Default 20
pw_forecast.set_peak_mode()

PwForecast will try to ensure power flow behaves as expected. Please see Retry Logic for more info.

A summary report will then be printed:

-----------------------------------
Powerwall state of charge: 19.7%
Powerwall backup reserve: 100%
Powerwall capacity: 26.59kWh
Powerwall state of health: 95.0%
-----------------------------------

Setting Off-Peak Mode

Use this method when your off-peak rate starts. This requires a little more setup.

  • Configure what backup reserve the Powerwall can discharge down to by setting the min_reserve_off_peak_rate attribute.
  • Configure the maximum backup reserve allowed by setting the max_reserve attribute.
  • Configure the amount of energy you require during the peak-rate by setting the required_energy_peak_rate attribute.

When setting peak mode, PwForecast will determine how much solar will be generated tomorrow. It will then calculate how much to fill the batteries based on the remaining energy requirement that solar will not satisfy. If solar generation completely satisfies required_energy_peak_rate, the backup reserve will be set to min_reserve_off_peak_rate.

get_solar_forecast_tomorrow is called internally as part of set_off_peak_mode. This will consume a Solcast API call per site ID you have provided.

pw_forecast.min_reserve_off_peak_rate = 25  # Default 30
pw_forecast.max_reserve = 95  # Default 100
pw_forecast.required_energy_peak_rate = 20000  # Default 30000
pw_forecast.set_off_peak_mode()

PwForecast will try to ensure power flow behaves as expected. Please see Retry Logic for more info.

A summary report will then be printed:

-----------------------------------
Solar forecast tomorrow: 10.2kWh
Powerwall state of charge: 19.7%
Powerwall backup reserve: 100%
Powerwall capacity: 26.59kWh
Powerwall state of health: 95.0%
-----------------------------------

Getting Solar Forecast

If you're only interested in getting the solar forecast, you can call get_solar_forecast_tomorrow. This will consume a Solcast API call per site ID you have provided. The value returned will be an int of the estimated solar forecast tomorrow in Wh.

pw_forecast.get_solar_forecast_tomorrow()

Retry Logic

When setting backup reserve, it is common to take two or three attempts before power flow changes to the expected figures. As the Tesla API is unofficial, it is difficult to determine why. Waiting longer does not seem to fix this issue, although waiting upwards of 45 minutes does sometimes result in power flow eventually changing correctly. Re-applying the setting does appear to fix this issue. Perhaps the unofficial Tesla API is missing a commit command. Please do let me know if you have any ideas.

Calling set_peak_mode and set_off_peak_mode will set the backup reserve, wait 20 seconds, and then check site power flow to confirm the setting has been applied. If an incorrect power flow is detected, the method will retry up to the set_backup_reserve_retry_limit limit. If the set_backup_reserve_retry_limit is reached, an exception will be raised and caught by the global retry logic. PwForecast will then attempt to re-apply the setting up to the global_retry_limit limit, eventually raising an exception.

Both set_backup_reserve_retry_limit and global_retry_limit can be configured on the PwForecast instance. The set_backup_reserve_retry_limit has been split from the global_retry_limit to try and avoid exhausting Solcast API calls.

The amount of time between each global_retry can be configured via global_retry_sleep. The amount of time between each set_backup_reserve_retry_limit can be configured via set_backup_reserve_response_sleep.

Advanced Configuration

PwForecast has a few advanced configuration options. Please check the class docstring for more info.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pwforecast-1.1.3.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pwforecast-1.1.3-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file pwforecast-1.1.3.tar.gz.

File metadata

  • Download URL: pwforecast-1.1.3.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pwforecast-1.1.3.tar.gz
Algorithm Hash digest
SHA256 44fd3eac8768b57814830237543efd3ef33d02df5bbaa455d58305d9518eccf8
MD5 445289acbd27115c58e6b72c08df4d46
BLAKE2b-256 941133fafc34e3134ec2350a9fee84ea4f60a97e0d1f6aacaf821229f9bfed35

See more details on using hashes here.

File details

Details for the file pwforecast-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: pwforecast-1.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for pwforecast-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 80ca4c023b4f43b81c621026b822e1cee3d3c6a781132ca9a2375ced2aade97b
MD5 8bf9ffd85292dab79b6c8c5ee63e5980
BLAKE2b-256 ba025c9b9051fd62b7c820bbda8f8ec19f294bf77976dd56dd2c684ef23c70f0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page