Saturday, May 15, 2021

Automating Your Tesla's HVAC

It’s time to do the real fun stuff and control a Tesla vehicle from a smartphone or tablet! The examples I’m providing are to turn on the heating, ventilation, and air conditioning (HVAC) system. The first flow turns the system on with the click of a button. The second one turns it on at a specific time of day depending on the location of the vehicle.

All Tesla vehicles have WiFi and 4G capability, but they don’t sit there consuming full power waiting for commands. They go to “sleep” like most power hungry devices to conserve energy. We have to wake the computer first and it may take multiple attempts. The command to turn on the HVAC also may need to be repeated. Status messages are relayed to the user through toast and the notification bar.

Tesla Start HVAC NOW [flo][pdf][png]


I display a confirmation dialog to make sure the user wants to turn on the HVAC. I do this because I’ve accidentally launched apps from the home screen far too often to not include this kind of protection.

The tesla.txt file with the authentication and vehicle information is read. If that file is missing or if the token has expired, the authentication flow is run and the resulting file is (re-)read.

I make five attempts to wake the vehicle with each retry ten seconds apart. After a success (or five attempts) the flow attempts to start the HVAC. Again, I make five attempts, but the 4th and 5th occur after five seconds. The success or failure is displayed to the user as a toast and notification.

I leave notifications last with the “Proceed” option set to “When dismissed.” The reason for this is that notifications will be cleared when a fiber is terminated. Therefore, the fiber will remain running until the user clears the notification. The flow beginning could have “Allow parallel launches from this block” checked to allow this to be re-run without forcing the user to close the notifications from the previous run.

Tesla Start HVAC Work [flo][pdf][png]


Once started, this flow runs indefinitely. The “Time await” block should be set with the desired schedule. The default is Monday through Friday at 4:45 PM. This flow is more complex, but similar to the previous one. The first “Variable set” block includes the work_lat and work_long variables. Find an online service to translate the latitude and longitude of the address you want and change it in this block. After waking the vehicle, we get the vehicle’s location and store it in the variables latitude and longitude. If the vehicle is within 250 meters, the flow will attempt to turn on the HVAC. All of these variables are written to the tesla.txt file for debugging.

The toast and notification block orders are swapped. The notification blocks now proceed immediately since the fiber doesn’t terminate. Notifications have two exit paths, so it makes more sense to put them first.












No comments:

Post a Comment