Using Templates to describe my EV charge status

Background
I’ve been using HA for about 2 years now, and always find templates to be a bit of an arcane art. I have an EV (silent_bob) and had been using a whole load of conditional entity cards to display its current charge percentage, range remaining, rate of charge (if its charging), etc.

As I’ve gone on, I’ve realised that this isn’t the actual information I want. We typically charge it once a week to somewhere between 70 and 85% (roughly 200 - 250 miles). All I ever really want to know is “what’s the current range?” or (if its plugged in) “how quickly am I charging and at what time can I expect to have a decent amount of range?”

I’ve just realised I can do this with a single card, using templates.

Goal
If the car is currently charging:

  • tell me the rate of charge, and…
    • let me know what time I can expect to have 200 miles of range.
    • if I already have over 200 miles, tell me what time I might expect to have 250 miles range.

If the car is not currently charging:

  • tell me the current range in the battery
  • tell me the current percentage charge level

Template for primary information

{% if(states('binary_sensor.silent_bob_charger_sensor')) == 'on' %}
  Charging at {{states('sensor.silent_bob_charging_rate_sensor')}} mph 
{% else %}
  Range Remaining {{states('sensor.silent_bob_range_sensor') | int}} mi
{% endif %}

That handles the primary data for the charging rate or range remaining

Template for secondary information

{% if(states('binary_sensor.silent_bob_charger_sensor')) == 'on' %} 
{%   set current_charge = states('sensor.silent_bob_range_sensor')|float %} 
{%   if (current_charge < 200) %} 
{%     set target = 200 %} 
{%   else %} 
{%     set target = 250 %} 
{%   endif %} 
{%   set ts = now().timestamp() | int %} 
{%   set hours_to_charge = (target - states('sensor.silent_bob_range_sensor')|float) / states('sensor.silent_bob_charging_rate_sensor')|float %} 
{%   set finish = ts + (hours_to_charge *60*60) %}
{%   if (hours_to_charge) > 0 %}
     Reaching
{%   else %}
     Reached
{%   endif %}
  {{target}} miles at {{ finish | timestamp_custom("%R", true) }} hrs.
{% else %}
  Battery has {{states('sensor.silent_bob_battery_sensor')}}% charge
{% endif %}    

Trip Hazards

  • The timestamp function takes two arguments. Being in the UK, I hadn’t realised the second one was UTC / local time, so 6 months of the year the charge duration was out by 1 hour.
  • If I had over 250 miles of range, the verbiage 'Reaching 250 miles at [some historical time] was a bit weird. Fixed that with a nested if.
  • My charge rate is pretty stable at 7mph or 26mph, depending on where it’s plugged in. If I were using a faster or smarter charger or charging to 100%, I’d probably need to factor in non linear estimates on charge duration.

Final Notes
I realise that mine is a pretty specific use case, but the templates seemed such an impenetrable syntax to me when I started, and I wish I’d found a post like this that spelt out how to create variables, parse / convert to ints and floats, add times, etc.

I’ve coupled this with the excellent mushroom-template-card so that I can use the same techniques above to conditionally set an icon and colour to my card. I even have colour coding for charge percentages, so that I can quickly see a red icon and know its time to plug things in.

Even if nobody else reads this, I can bet I come back to try to remember how these things are done. :slight_smile:

3 Likes