I normally trickle charge our electric car overnight when electricity is cheap between 00:30 to 04:30.
But sometimes I need to charge it at a more expensive rate AND make the most of the 4 hours of cheap rate charging.
I cobbled together this sensor template to tell me when to start charging the car so that it’ll reach 100% charge by the end of the cheap rate (more or less).
{% set datetomorrow =(now().date() + timedelta(days=1)) | string %}
{% set endtime ='04:30:00' | string %}
{% set chargeminutes = (((268 - states.sensor.tesla_range_rounded.state | int)/9)*60) %}
{{ strptime(((datetomorrow +' '+ endtime)), '%Y-%m-%d %H:%M:%S') - timedelta(minutes=chargeminutes) }}
- Line 1 sets tomorrow’s date
- Line 2 sets the end time of my cheap rate electricity
- Line 3 calculates the number of minutes in total needed to charge the car to 100%
- Line 4 subtracts the charge time from the end time to output the datetime I need to start charging
Line 3 works like this:
- 268 is the range estimate at 100% charge
- Subtract the current range estimate (states.sensor.tesla_range_rounded.state)
- Divide by the 9 miles per hour of range added when charging
- Multiply by 60 to render the total charge time as minutes
The result is sensor.datetime which I use in an automation to update datetime.helper.
I think this step is needed because I couldn’t figure out how to trigger an automation from a datetime sensor - I could only get it to accept the helper as a datetime trigger.
alias: "Set time helper"
trigger:
- platform: state
entity_id:
- sensor.datetime
condition: []
action:
- service: input_datetime.set_datetime
target:
entity_id: input_datetime.helper_datetime
data:
datetime: "{{ states('sensor.datetime') }}"
mode: single
Finally, the automation to start charging using the datetime.helper as a trigger:
alias: Start charge
trigger:
- platform: time
at: input_datetime.helper_datetime
action:
- service: switch.turn_on
target:
entity_id: switch.charger_power
mode: single
I’m sure there are many ways to make this less complicated and more elegant - but I thought I’d throw this in here as a starter.