I had a play with TeslaPy today and came up with a script to monitor the charger's power state and adjust the car's desired SoC accordingly to allow it to go to sleep when the charger is off:
from datetime import datetime
import teslapy
import time
email = '<your email>'
def set_charge_limit(car, percent):
print('Setting charge limit to', percent)
try:
car.command('CHANGE_CHARGE_LIMIT', percent=percent)
except BaseException as exc:
print(exc)
with teslapy.Tesla(email) as tesla:
cars = tesla.vehicle_list()
car = cars[0]
charging_state = ''
while True:
CHARGER_ON_STATES = ['Complete']
CHARGER_OFF_STATES = ['Disconnected', 'NoPower']
new_charging_state = car.get_vehicle_data()['charge_state']['charging_state']
print('{}: charge state {}'.format(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), new_charging_state))
if new_charging_state in CHARGER_ON_STATES and charging_state not in CHARGER_ON_STATES:
set_charge_limit(car, 100)
elif new_charging_state in CHARGER_OFF_STATES and charging_state not in CHARGER_OFF_STATES:
set_charge_limit(car, 50)
charging_state = new_charging_state
time.sleep(15)
Example output (slightly outdated):
$ python minimise_wakeup_time.py
06/04/2022 01:12:43: charge state Disconnected
Charger is off, setting min charge limit
06/04/2022 01:12:59: charge state NoPower
06/04/2022 01:13:14: charge state NoPower
06/04/2022 01:13:29: charge state NoPower
06/04/2022 01:13:45: charge state Complete
Charger is on, seting max charge limit
06/04/2022 01:14:00: charge state Charging
06/04/2022 01:14:15: charge state Charging
06/04/2022 01:14:31: charge state Disconnected
Charger is off, setting min charge limit
06/04/2022 01:14:46: charge state Disconnected
06/04/2022 01:15:01: charge state Complete
Charger is on, seting max charge limit
06/04/2022 01:15:17: charge state Charging
06/04/2022 01:15:32: charge state Charging
06/04/2022 01:15:48: charge state Charging
06/04/2022 01:16:03: charge state Disconnected
Charger is off, setting min charge limit
I need to test it for a few days under different conditions. One feature that is likely needed is geofencing: when connected to a charger outside from home (which is not schedule-able), we'd likely want to adjust the maximum SoC to a different value other than 100% so the script must not interfere in those cases.