Here's a small Python code I wrote tonight (don't be hard, it's my first Python program, learned it tonight as I wrote this). It queries my weather station (Acurite 5in1) through its WeeWX URL and if it detects it rains and the car's windows are open, it closes them
I'm planning on running it every five minutes either from a cron job or on loop itself in the Python code on a Raspberry Pi (the same one running the WeeWX software).
Having access to the car's API is very cool (and fun)
import teslapy
from urllib.request import urlopen
import json
print("Reading car")
with teslapy.Tesla('[EMAIL][email protected][/EMAIL]') as tesla:
if not tesla.authorized:
tesla.refresh_token(refresh_token=input('Enter SSO refresh token: '))
vehicles = tesla.vehicle_list()
vehicleData = vehicles[0].get_vehicle_data()
fd_window = int(vehicleData['vehicle_state']['fd_window'])
fp_window = int(vehicleData['vehicle_state']['fp_window'])
rd_window = int(vehicleData['vehicle_state']['rd_window'])
rp_window = int(vehicleData['vehicle_state']['rp_window'])
windows = fd_window + fp_window + rd_window + rp_window
print(windows)
print("Reading WeeWX")
response = urlopen("[URL]http://192.168.2.103/weewx/daily.json[/URL]")
data = response.read().decode("utf-8")
rain = json.loads(data)['current']['rainRate']
rain = float(rain)
print(rain)
moving = vehicleData['drive_state']['shift_state']
print(moving)
if moving is None and windows > 0 and rain > 0.0:
print("Closing the windows!")
latitude=vehicleData['drive_state']['latitude']
longitude=vehicleData['drive_state']['longitude']
vehicles[0].sync_wake_up()
vehicles[0].command('WINDOW_CONTROL', command='close', lat=latitude, lon=longitude)
else:
print("All is fine")
I'm planning on running it every five minutes either from a cron job or on loop itself in the Python code on a Raspberry Pi (the same one running the WeeWX software).
Having access to the car's API is very cool (and fun)
Last edited by a moderator: