Calculating EV battery charge pricing with Perl

Presently I have great interest in “EVs” Electric Vehicles but I haven’t seen any data on how much it would cost to charge an electric vehicle from 0 % to 100 % battery charge at home in NYC ( So I wrote a Perl script to do just that ) but before we dig in into it I explain a few things about Electric Vehicles.

Electric Vehicles will have a battery capacity that is represented by kilowatt-hour units or kWh for short.

An EV’s driving range is represented in miles units ( In the US ) and the average mileage is determined by the EPA battery range rating ( the bigger the battery capacity usually means the more driving range you will have in a car ) after conducting a few tests ( so in reality your mileage will vary ).

Electric vehicles have an onboard charger which determines its charging rate in Kilowatt per Hour and it varies by car makers. Most EV car owners will install a Level 2 charger that is usually capable of charging cars up to 7.2 kWH rate using 220 volt electric circuit with 32 amps of power ( but there are chargers that can go at a higher rate ).

Ok now that I explained a few things lets dig into the data used to make my script.

I checked my electric bill and found that my electricity rate in NYC is $0.13 cents per kWh.

For comparison I phoned a friend in Florida to get electricity rates where he lives which is $ 0.07 cents per kWh. ( Almost half of NY rate )

I took as an example this car a 2021 Tesla Model 3 Long Range with a battery capacity of 82kWh. It has an EPA mileage of 353 miles.


#### Tesla Model 3 
#
say "Tesla Model 3 2021 Long Range: 82kwh battery pack";
say "All Electric range: 353 miles";
my $tesla_total_price = 82 * .13 ;
my $tesla_total_price_fl = 82 * .07 ;
my $tesla_charge_time = 82 / 7.2;  # 7.2 is the onboard charger rate ( it can also be 3.3 kwh  rate )  
my $tesla_miles_per_kWh = 353 / 82;

say "NYC Full charge price: \$ " .  sprintf("%.2f" , $tesla_total_price);
say "FL Full charge price: \$ " .  sprintf("%.2f" , $tesla_total_price_fl);
say "Time to charge: " . sprintf("%.2f" , $tesla_charge_time) . " hours";
say "Miles per kWh : " . sprintf("%.2f" , $tesla_miles_per_kWh) . " miles";

print "\n";
which will output the following:
Tesla Model 3 2021 Long Range: 82kwh battery pack
All Electric range: 353 miles
NYC Full charge price: $ 10.66
FL Full charge price: $ 5.74
Time to charge: 11.39 hours
Miles per kWh : 4.30 miles

$10.66 dollars for a 353 miles in New York City driving is pretty sweet deal!

I looked into a few other cars and started to copy and paste code which resulted in this script: https://github.com/itcharlie/ev-calc-pricing/blob/main/evchargeprice_calculator.pl

and of course there is more than one way to do it so I rewrote the script with a Hash of EV cars: https://github.com/itcharlie/ev-calc-pricing/blob/main/rewriteevchargepricecalculator.pl



#!/usr/bin/perl
use strict;
use warnings;

use Data::Dumper;
use feature 'say';


my %cars;

$cars{'EV'}->{'KIA'}->{'NIRO'} = { 'year' => 2020 , 'battery_size' => 64, 'onboard_charger_rate' => 7.2 , 'aer' => 239 };
$cars{'EV'}->{'TESLA'}->{'MODEL 3'} = { 'year' => 2021 , 'battery_size' => 82, 'onboard_charger_rate' => 7.2 , 'aer' => 353 };
$cars{'EV'}->{'CHEVY'}->{'BOLT'} = { 'year' => 2021 , 'battery_size' => 60, 'onboard_charger_rate' => 7.2 , 'aer' => 259 };

my %electric_rate;

$electric_rate{'NY'} = '0.13';
$electric_rate{'FL'} = '0.07';

foreach my $ev_brand ( keys %{$cars{'EV'}} ) {

    print "-" x 50;

    foreach my $model ( keys %{$cars{'EV'}->{$ev_brand}} )  {
        print "\n";
        say "$ev_brand  $model $cars{'EV'}->{$ev_brand}->{$model}->{'year'}";
        say "All electric range: $cars{'EV'}->{$ev_brand}->{$model}->{'aer'} miles";
        my $charge_time = $cars{'EV'}->{$ev_brand}->{$model}->{'battery_size'}  / $cars{'EV'}->{$ev_brand}->{$model}->{'onboard_charger_rate'} ; 
        my $miles_per_kWh =  $cars{'EV'}->{$ev_brand}->{$model}->{'aer'}   /  $cars{'EV'}->{$ev_brand}->{$model}->{'battery_size'}  ;
        say "Time to charge: " . sprintf("%.2f" , $charge_time ) . " hours";
        say "Miles per kWh : " . sprintf("%.2f" , $miles_per_kWh) . " miles";
        print "\n";

        foreach my $state ( keys %electric_rate )  {
           my $total_price = $cars{'EV'}->{$ev_brand}->{$model}->{'battery_size'}  * $electric_rate{$state} ;
           say "$state state 0-100% battery charge price: \$ " .  sprintf("%.2f" , $total_price);
        }
    }
}
 

Thank you for your time and hope you enjoyed this post!

Leave a comment

About Charlie Gonzalez

user-pic I blog about Perl.