16 Jun

Create a Smart Solar-Powered EV-Charger from a Granny Cable

I have solar panels on my house

And an electric car

If I have 2kW power coming in from the panels, but am using utensils that total 3kW, then the house needs to pull in 1kW from the grid to make up the difference. That costs money, making it a bad time to charge the car.

Alternatively, if I have 3kW coming from the panels but am only using 2kW in the house, then it’s not currently costing me anything. I actually get paid for the excess 1kW which gets sent out to the grid. But it’s still a bad time to charge the car, because it uses up to 2.5kW, so I’d be pulling in 1.5kW from the grid and paying for that.

But if I’m pulling in 4kW and only using 1.5kW in the house, then plugging the car in will be free.

So, sometimes it’s free to run the car charger, and sometimes it would cost money.

Doing these calculations is an annoyance. Especially as the total input changes throughout the day as the clouds come and go, and we turn various things on and off.

I don’t want to recalculate and turn the charger on/off every few minutes. That’s what computers are for!

The commercial solution for this is that you buy something designed for that purpose, such as the MyEnergi Zappi. And I really do recommend you do that, if you have the money and prefer something that just works. It basically looks at what the house is doing, and decides for itself whether the car should be charged, based on calculations and other decisions.

But, if you are like me and like to do things cheaply and hackily, then let’s build a cheap device that does (almost) the same thing, but at a fraction of the cost.

Materials needed

You will need a travel charger, also known as a granny cable. I use the Zencar charger which I managed to buy for €150. Any travel charger will do as long as it can be plugged into a socket in your house.

You will also need a wifi-enabled plug adaptor. I got a BG Home adaptor for about €20. Make sure that it is IFTTT-compatible. You’ll need that.

You will also need a computer that’s permanently on. I have a Linux machine I use as a file server. I used that for this purpose. You could also use a cloud server. A Linux server from Digital Ocean can be as cheap as $4 per month

And then you need a way to measure what power is coming into the house through the panels and the grid. I use a MyEnergi Eddi power diverter which was installed when the solar panels were installed. If you’re using something different, then adjust the code as you need to.

Setup

First thing you need to do is plug your travel charger in through the wifi-enabled plug adaptor. Follow its instructions to make sure it’s working as normal. After that’s set up, you should be able to enable/disable the adaptor (and therefore the charger that’s plugged into it) using an app on your mobile device.

Next, create and log into an IFTTT account.

Once thats done, you can start creating a webhook for turning the charger on/off using a URL.

Click the CREATE button on the top right of the IFTTT page. You will be shown a page with an “If This … Then That” message.

Click the “If This”. Search for and select “Webhooks”, then select “Receive a Web Request”. Give it the event the name “evcharger_on” then click Create Trigger

Click the “Then That”. Search for and select “BG Home” (or whatever is your wifi adaptor’s product). You may need to do some set up for that before it lets you go further. When done, choose “Turn Device On/Off”, and fill in the small form.

You should now see something like this:

We’re almost there. Click to Continue, and do whatever is needed to complete the applet and return to your main account page

To build the URL, you need to know your user API. It’s not obvious how to get that, but as of this minute, you should find it here.

The bit I’ve scrubbed out in red is your API key.

Now, construct a URL like this:

https://maker.ifttt.com/trigger/evcharger_on/with/key/{{KEY}}

Obviously, replace {{KEY}} with your own.

That URL should turn your charger on. Check it in a browser!

Now that that’s done, repeat the above but this time, name the trigger “evcharger_off” and have it turn off the adaptor.

Now that you can now turn your charger on/off with a browser, let’s write a program to do it!

Programming

First thing we do is set up a config file. Save this as config.php

<?php
define('MYENERGI_KEY', '{{MYENERGI_KEY}}');
define('MYENERGI_SERIAL', '{{MYENERGI_SERIAL}}');
define('IFTTT_ON', 'https://maker.ifttt.com/trigger/evcharger_on/with/key/{{KEY}}');
define('IFTTT_OFF', 'https://maker.ifttt.com/trigger/evcharger_off/with/key/{{KEY}}');
define('MYENERGI_URL', 'https://s18.myenergi.net/cgi-jstatus-E');
define('CHARGER_W', 2500);
define('W_MAX', 100);
define('CHEAP_TIME_START', 2);
define('CHEAP_TIME_END', 5);

You should adjust the first four lines above, using your own keys. You can get your own MyEnergi details by following this article.

CHARGER_W is the maximum wattage the charger will output. In my case, I’ve set 2500W, which is slightly above 240v * 10A.

W_MAX is how many watts you’re okay with paying for, if there’s not quite enough power to charge the car completely for free.

CHEAP_TIME_START/CHEAP_TIME_END are the hours when your local cheapest tariff begins/ends (that you’re okay with paying). In my case, I’m with Bord Gáis, who have an EV Tariff which runs between 2am and 5am. They’re currently advertising an EV rate of 10.40 per kWh vs a standard 40.82 during normal hours

Once you have that sorted, create a file in the same directory that contains the actual program we’re building. I called mine update-status.php because it’s updating the status of the charger. yeah. not good at names.

<?php
require_once 'config.php';

function getData() {
	// { get current info
	$ch=curl_init();
	curl_setopt_array($ch, [
		CURLOPT_URL=>MYENERGI_URL,
		CURLOPT_FOLLOWLOCATION=>1,
		CURLOPT_RETURNTRANSFER=>1,
		CURLOPT_SSL_VERIFYPEER=>false,
		CURLOPT_HTTPHEADER=>[
			'Content-Type: application/json',
			'Accept: application/json'
		],
		CURLOPT_HTTPAUTH=>CURLAUTH_DIGEST,
		CURLOPT_USERPWD=>MYENERGI_SERIAL.':'.MYENERGI_KEY,
	]);
	$res=curl_exec($ch);
	$obj=json_decode($res, true);
	// }
	return $obj;
}

$prev=intval(@file_get_contents('/tmp/ev-charger'));
$on=$prev;
echo "start\n";
$obj=getData();
$msg='failed to retrieve data from MyEnergi Eddi diverter';
if (isset($obj['eddi'][0])) {
	$eddi=$obj['eddi'][0];
	$grid=intval($eddi['grd']);
	$boiler=intval($eddi['div']);
	$on=0;
	$msg='charging car would use too much grid|grid='.$grid.'|charger_w='.CHARGER_W.'|prev='.$prev.'|w_max='.W_MAX;
	if ($grid+CHARGER_W-($prev*CHARGER_W)<=W_MAX) { // if charging would not cause us to use the grid
		$on=1;
		$msg='can charge without using much grid';
	}
	else if ($grid-$boiler+CHARGER_W-($prev*CHARGER_W)<=W_MAX) { // the boiler is on. turning the charger on will automatically turn the boiler off
		$on=1;
		$msg='disabling boiler to charge car';
	}
	else { // check the time to see if we're in a low-tariff time
		date_default_timezone_set('Europe/Dublin');
		if (CHEAP_TIME_START<CHEAP_TIME_END) {
			if (intval(date('G'))>=CHEAP_TIME_START && intval(date('G'))<CHEAP_TIME_END) {
				$on=1;
				$msg='ev tariff';
			}
		}
		else {
			if (intval(date('G'))>=CHEAP_TIME_START || intval(date('G'))<CHEAP_TIME_END) {
				$on=1;
				$msg='ev tariff';
			}
		}
	}
	if ($prev!=$on) {
		file_put_contents('/tmp/ev-charger', $on);
		if ($on) {
			echo file_get_contents(IFTTT_ON)."\n";
		}
		else {
			echo file_get_contents(IFTTT_OFF)."\n";
		}
	}
}
echo $prev.'|'.$on.'|'.$msg."\n";

echo "finish\n";

That’s it!
When that’s run, it will check the MyEnergi cloud service to see what’s going on with the house at the moment, and will turn on the charger if it’s not already on.

The last think to do is to just set up a cron job to run that once per minute. I’ll leave that to you.

Example

Here’s today’s grid usage so far. the orange on the top is power coming into the house from the grid, and the yellow is excess solar-generated power being sent out to the grid.

the solid block on the left is between 2am and 5am, when power is cheapest (10c per kW), so the program turns the charger on.

next we have a growing yellow curve that increases until it hits about -2.5kW at 9am. then the car starts charging and the excess drops .

about 10am, I unplug the car as we need to go do something in town. you can see the excess spiking downwards as there’s hardly anything in the house running. there are clouds, so it’s unsteady.

at 11am we get back to the house, and it’s time for pizza! so we turn the oven on, which spikes the power to a pull of 3kW from the grid for a few minutes. My wife has also turned on a washing machine …let’s not comment about her timing…

from that point onwards, the car is basically charging. Turning on/off as cloud-cover fluctuates and the draw from the grid goes above zero.

Conclusion

The thing to pull out of this is that by automating the charging of the car, we essentially charge it for free, letting us concentrate on other things and spend less time worrying about the right time to do things. Just get into the habit of plugging in when we get home, and the car just stays charged.

Obviously there are still cases where this is not enough – if you’ve done a very long drive one day then need to do another very long drive the next day, then there is obviously not enough time in between for the car to recharge for free/cheap. In those cases, just go to your local rapid-charger and top it up.

Leave a Reply