This script will fetch the current tariffs using teslapy library.
#!/usr/bin/env python3
import teslapy
with teslapy.Tesla("[email protected]") as tesla:
tesla.fetch_token()
battery = tesla.battery_list()[0]
# tariff = battery.api('SITE_TARIFF')
# battery.set_backup_reserve_percent(100)
# battery.set_operation(backup)
# print(battery) # should print battery status once successfully authenticated
# print('----')
print(battery.api('SITE_TARIFF'))
This script will set the utility rate tariff:
import datetime
import teslapy
with teslapy.Tesla('[email protected]') as tesla:
batteries = tesla.battery_list()
battery = batteries[0]
# Define load cost and production price forecasts
load_cost_forecast = [0.36, 0.24, 0.2, 0.23, 0.19, 0.21, 0.2, 0.2, 0.18, 0.18, 0.18, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.18, 0.21, 0.26, 0.27, 0.39, 0.18, 0.12, 0.07, 0.06, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12, 0.33, 0.38, 0.54, 0.63, 0.5, 0.38, 0.38, 0.37, 0.39, 0.38, 0.22, 0.2, 0.24, 0.22, 0.21, 0.2, 0.21, 0.18, 0
.18, 0.17, 0.17, 0.15, 0.15, 0.15]
prod_price_forecast = [0.13, 0.14, 0.11, 0.13, 0.1, 0.12, 0.11, 0.1, 0.09, 0.09, 0.09, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.09, 0.12, 0.16, 0.17, 0.28, 0.09, 0.04, -0.01, -0.01, -0.02, -0.02, -0.02, -0.02, -0.02, -0.02, -0.02, 0.04, 0.04, 0.04, 0.04, 0.04, 0.04, 0.1, 0.15, 0.3, 0.38, 0.25, 0.15, 0.15, 0.14, 0.15, 0.15, 0.13, 0.11, 0.14, 0.12, 0.12, 0.11, 0
.11, 0.09, 0.09, 0.08, 0.08, 0.06, 0.06, 0.06]
# Define time periods for each tariff
periods = []
current_time = datetime.datetime.now().time()
# Find the index of the current time in 30-minute intervals
start_index = current_time.hour * 2 + current_time.minute // 30
start_time = datetime.time(current_time.hour, current_time.minute // 30)
current_time = datetime.datetime.now()
start_time = datetime.time(current_time.hour, 30 * (current_time.minute // 30))
print (start_time)
exit
skip_next = False # Flag to skip the next period
for i in range(48):
if skip_next:
skip_next = False
start_time = (datetime.datetime.combine(datetime.date.today(), start_time) + datetime.timedelta(minutes=30)).time()
continue
end_time = (datetime.datetime.combine(datetime.date.today(), start_time) + datetime.timedelta(minutes=30)).time()
if end_time == datetime.time(0, 0):
skip_next = True
start_time = end_time
continue
load_cost = load_cost_forecast[i]
prod_price = prod_price_forecast[i]
period = teslapy.BatteryTariffPeriodCost(buy=load_cost, sell=prod_price, name=f'p{i}')
periods.append(teslapy.BatteryTariffPeriod(period, start_time, end_time))
print(period, start_time, end_time)
start_time = end_time
# Create the tariff
new_tariff = teslapy.Battery.create_tariff(teslapy.BatteryTariffPeriodCost(buy=0.16, sell=0.05, name='DEFAULT'), periods, provider='Amber', plan='Custom via API')
# Debugging: Print the original and updated tariffs
# print("Original Tariff:", battery.get_tariff())
# print("Updated Tariff:", new_tariff)
# Set the new tariff
battery.set_tariff(new_tariff)