# Scheduled charging based on completion time with a Tasker script



## JML (Jul 26, 2018)

A means of getting our cars to _finish_ charging at a certain time, instead of starting to charge at a certain time, has long been requested. As we move into the colder part of the year, this will be useful so that the battery is warm before leaving in the morning.

I've put together some Tasker profiles and tasks so that the car finishes charging at a specified time and charge level. It does this by predicting how long it will take to charge from the current state of charge, and automatically starting the charging at the necessary time. This prediction is based on a pretty simple equation:
`[(EndSoCPercent/100)*MaxRangeMiles - CurrentSoCMiles]/ChargeSpeedMPH`
which estimates how long it will take to charge from the current range to the desired percent SoC. In the task, I multiply this by `-3600` to get the number of seconds to start charging prior to the desired completion time.

This depends on a few things:

Tasker for Android ($3.49)
Tesla Plugin for Tasker ($0.99 + $0.99 in app purchase to read current charge state)
AutoTools ($2.99 or ad supported) This is used to do time arithmatic. It could probably be replaced by doing the math manually, or using the `date` shell command.
In its current form, this is probably only useful to people who already have tasker and are familiar with creating tasks and profiles. *This is not polished, and is not a ready to go completed app.* One of the main reasons I'm posting this is to get other people's input, suggestions, and modifications.

This is made up of several parts that need to be imported into Tasker:

The predictive estimator task
The charging start task
The stop charging task
The predictive charging profile
The charging interruption profile
The predictive estimator is the main piece. It requires setting several variables.









`%maxrange` should be set to your cars estimated maximum range in miles. If you've experienced some range loss, then put in the lower value.
`%chargespeedmph` should be set to the your charge speed in miles per hour.
`%fillsoc` should be set to the car's charge limit.
`%endtime` should be set to the time you want the charge to complete in 24 hour notation, so 19:30 to complete at 7:30pm, for example.
The Tasker Plugin for Tesla has to be set for your car to `Get Charge State`. You might need to go into the other two tasks and setup the plugin, too.

The Predictive Charging Interrupter profile should be set to run 1 minute or so after whatever time the car is normally set to start charging. For example, my car is set to start charging at 9:30, because of the lower electric rates at night. I've set the profile to run at 9:31 to stop charging.

This is the general flow of how these things all work:

Manually run the Predictive Charging Estimator task
That will read the car's SoC
Estimate how long it will take to charge from the current SoC to the desired completion SoC.
Subtract that time from the desired completion time.
Set the estimated time into a global variable, `%PredictedChargeStart`
Enable the Predictive Charging Interrupter

The Predictive Charging Interrupter will stop the normal scheduled charging.
Predictive Charging will start at `%PredictedChargeStart`
The Predictive Charging Interrupter profile will be disabled, so that normal charging can occur tomorrow.

So far this has worked for me at least once:









I set the charge to finish at 7:30 am, and it automatically started at 4:36 am. I could have just done the calculation by hand, and manually set the cars start time to 4:30, but it was more fun putting this together, and besides, I don't want to have to do that every night. It was off by 3 minutes, which isn't too bad.

So here are some problems I can think of, not ordered by seriousness, just how they came to me:

There is no error checking of any kind. The predicted start time might be before the normal scheduled charge time, so the charge gets interrupted, or it uses expensive electricity.
At the moment, this is not automated. That could be fixed with a profile to automatically run the predictive estimator task.
There is no checking to see if the car is connected to the charger.
If the predicted start time has already passed, then charging won't start until tomorrow.
$2.99 just to compute `7:30 - 5686 seconds`.
Doesn't do anything to take into account variable charge rates.
It uses the car's charge limit to stop the charging.
I'm sure lots of other things I haven't thought of yet.


----------



## Frully (Aug 30, 2018)

I think the technical term is: "Neat."

Well done!


----------

