Welcome to Tesla Motors Club
Discuss Tesla's Model S, Model 3, Model X, Model Y, Cybertruck, Roadster and More.
Register

Charging from Excess Solar with Teslapy

This site may earn commission on affiliate links.
I finally got around to playing with some Python code and the Teslapy module to use the excess solar to charge my car. My daughter (11 yo) helped out over the school holidays to get her more involved in programming. We started off by using ChatGPT to generate sample code for extracting the data from my Fronius inverter and then we borrowed bits of code from the Teslapy documentation, TesSense by @israndy and Suncatcher by @Haselsmasher.

Today was a good test of the code as there was intermittent cloud cover. I check for the solar, house power draw and export data every 60 seconds, changing the amps accordingly. If I have to stop charging then I wait 5 minutes before evaluating whether to charge again or not. There is also some buffer to allow for 1,500 W, but I'm going to increase the to 2,000 W to see if that helps with some grid usage creeping in. Ultimately the clouds are annoying! I saw 13 kW drop to 3kW as clouds pass over.

Anyway, here is the output from today. The grey is solar production, blue line is house consumption and yellow is solar consumption.

1681889080355.png


The bump from 6 am is the heat pump coming on for the hot water. Then I started charging the car after 9 am and finished before 1 pm. The block of usage that drops off after 3 pm is the pool pump. The solar system is 15 kW and I have 3 phase.

I'll clean up the code (my first Python program) and post it to Github for others to look at over the weekend.
 
Interesting, but why not use ChargeHQ?
Is it because this will work with any brand invertor?
  1. I needed to get my programming brain working again and I've not used Python before.
  2. My daughter was interested in doing the project.
  3. As [Zombie] said, security. I don't use any third-party Tesla apps for that reason too.
  4. However, I signed up for ChargeHQ to check it out, but due to living overseas previously my Google account wasn't set to Australia, making installation impossible.

The current version of the code is 178 lines, including comments. A decent programmer, with the help of ChatGPT, could get it done in a day.

As for working with other inverters... if an inverter has an IP address with an API endpoint that returns JSON then it will work. You'll need to know what data you need from the JSON response, but that's not too hard to work out.
 
I needed to get my programming brain working again and I've not used Python before

Python is a great language. I’m not a big coder but I used to use perl, which is rather dated now, not as easy to use, and doesn’t have simple libraries for https requests, only http. Which makes it useless for 99% of web interactions these days.

I learned python as a lockdown project in 2020, and have coded up a “smart charging” cron job similar to what you have done, for similar reasons - I am not handing my Tesla credentials or Auth tokens over to anyone, even when third party App developers swear on the Bible they don’t sniff them.

I check for the solar, house power draw and export data every 60 seconds, changing the amps accordingly. If I have to stop charging then I wait 5 minutes before evaluating whether to charge again or not.

You need to be careful you don’t hammer the Tesla API with too many calls. I assume you are doing this from a server or Raspberry Pi or something that will have a fixed IP address. Tesla logs IP addresses that make calls and will block addresses that annoy it. This is not an issue with using the Tesla mobile App, because mobile phones change their IP address every time they handover to a new cell.

I have had periodic issues with getting “503 Forbidden” responses to my API calls. I check only every 15 minutes during 9am - 2pm, and reauthenticate only once a day. Tread carefully.
 
You need to be careful you don’t hammer the Tesla API with too many calls. I assume you are doing this from a server or Raspberry Pi or something that will have a fixed IP address. Tesla logs IP addresses that make calls and will block addresses that annoy it. This is not an issue with using the Tesla mobile App, because mobile phones change their IP address every time they handover to a new cell.

I have had periodic issues with getting “503 Forbidden” responses to my API calls. I check only every 15 minutes during 9am - 2pm, and reauthenticate only once a day. Tread carefully.
Interesting. It's running now and I'm still checking every minute, but calls to the API will be lower than that if the charge rate doesn't change or it stops charging for a while. I've not had any errors yet and do have a fixed IP (needed for some house automation).
 
Interesting. It's running now and I'm still checking every minute, but calls to the API will be lower than that if the charge rate doesn't change or it stops charging for a while. I've not had any errors yet and do have a fixed IP (needed for some house automation).

I hope it keeps working for you. In 2021/22 my scripts would regularly die and it seemed to take about 2 weeks of not pinging the API at all before they would start working again, which implied temporary IP address blocking. I then had a good run for about 6 months where my scripts worked daily but then they suddenly stopped working again in early March. I re-cut my code to directly talk to the API rather than use TeslaPy so that I could see the server responses directly and try to work out what was going on.

It seemed they had changed the policy regarding the Refresh Tokens and they could only be used once to generate a new Auth Token, whereas previously they could be used repeatedly and apparently had no expiry date (which is pretty insecure, right).

Anyway my scripts are working again and we’ll see how long it lasts 😄

Also note because the Tesla API is not officially “public” nor officially documented, Tesla will change stuff periodically and break your code. Some commands now require a specific User-Agent or X-User-Agent to be passed in order to work and return 404 Not Found if you don’t. TeslaPy hides most of this. How people reverse engineer it I have no idea.
 
This is fascinating - But I suspect my programming days are behind me now.
Is anyone aware of a method of achieving this goal with Enphase microinverters? I have a feeling I will need to look for a proprietary approach as I think Enphase lock down access to their inverter management system...
 
This is fascinating - But I suspect my programming days are behind me now.
Is anyone aware of a method of achieving this goal with Enphase microinverters? I have a feeling I will need to look for a proprietary approach as I think Enphase lock down access to their inverter management system...
I don't have experience (yet with the latest Enphase gateways, but the issue with the gateways is the frequency of data updates. 5 minute updates isn't going to cut it - ideally you need data on a per-second basis (maybe 5 seconds would be good enough) and would then be able to feed that into your charger at that speed if necessary, too, to limit charging speed. 1-minute data is probably "good enough" for most cases, though, but house loads and clouds can change things quickly enough that you'd like to have more resolution.

With the latest Enphase systems which can support whole house whole energy consumption monitoring, the data must be in there somewhere to support integration with Enphase batteries without having to go to the cloud, but I'm not sure if there's a way to get the data out at the frequency needed to support charging your car with excess solar production.
 
Off-topic, but perl absolutely supports https with standard perl modules and has done so for decades.

Hmm… 🤔 I started web scraping with http in the naughties, but by the early 2010s fewer and fewer websites allowed http access and I could never get https to work in perl… I never found any libraries which worked out of the box… but I’m glad I gave up and looked for alternatives and discovered python because it’s so much nicer!
 
  • Like
Reactions: Dave EV
I don't have experience (yet with the latest Enphase gateways, but the issue with the gateways is the frequency of data updates. 5 minute updates isn't going to cut it - ideally you need data on a per-second basis (maybe 5 seconds would be good enough) and would then be able to feed that into your charger at that speed if necessary, too, to limit charging speed. 1-minute data is probably "good enough" for most cases, though, but house loads and clouds can change things quickly enough that you'd like to have more resolution.

I have a PW2 so I don’t care about variations in solar production. My “smart charging” solution simply waits until the PW2 is full, and provided there is > 2.5 kW of excess solar at the time, and it’s before 2pm, the car will start charging at a fixed 15A (3-phase x 5A). My solar array is small and so at full pelt is only just above this.

The car only stops charging when either the target SoC is reached, or the PW2 is depleted to a level below which it is unlikely it will fill up again before the end of the day (I want it to be full so that I don’t pay for any peak ToU grid electricity in the evening).

So I check the PW2 SoC every 10 minutes, and never adjust the car’s charging rate, so every now and again the car will be charging using a contribution from the PW2 but it doesn’t matter as long as I stop charging before PW2 is depleted too much.
 
I don't have experience (yet with the latest Enphase gateways, but the issue with the gateways is the frequency of data updates. 5 minute updates isn't going to cut it - ideally you need data on a per-second basis (maybe 5 seconds would be good enough) and would then be able to feed that into your charger at that speed if necessary, too, to limit charging speed. 1-minute data is probably "good enough" for most cases, though, but house loads and clouds can change things quickly enough that you'd like to have more resolution.

At 1 min intervals the clouds can still wreck havoc on trying to remove grid draw. Today was another cloudy day, as you can see from the production graph:

1682330210340.png


Here is the consumption graph with only grid draw showing:

1682330316863.png


It's going to be difficult to remove those blips during the day without moving to <10 second adjustments. I don't want to do that in case I hit the Tesla API too often and get blocked. What we really want is a way to authenticate with a Tesla wall charger and adjust it directly on the local network rather than through the Tesla API. Unfortunately I don't even have the WiFi capable charger, so if they enable the feature I couldn't use it anyway.

BTW, this is what the output looks like:

Mon 12:46:42 PM Time to charge.

Mon 12:46:42 PM Current amps 14 - charging at 10080 Watts
Mon 12:46:42 PM Current Solar Power: 7456 W
Mon 12:46:42 PM Current House Power: 11047.29 W
Mon 12:46:42 PM Current Grid Power: 3591.29 W

Mon 12:46:42 PM Enough solar
Mon 12:46:42 PM Enough excess solar
Mon 12:46:43 PM Changed charge rate from 14 amps to 8 amps

Mon 12:47:43 PM Time to charge.

Mon 12:47:43 PM Current amps 8 - charging at 5760 Watts
Mon 12:47:43 PM Current Solar Power: 14148 W
Mon 12:47:43 PM Current House Power: 8295.630000000001 W
Mon 12:47:43 PM Current Grid Power: -5852.37 W

Mon 12:47:43 PM Enough solar
Mon 12:47:43 PM Enough excess solar
Mon 12:47:43 PM Changed charge rate from 8 amps to 15 amps

Mon 12:48:44 PM Time to charge.

Mon 12:48:44 PM Current amps 15 - charging at 10800 Watts
Mon 12:48:44 PM Current Solar Power: 5166 W
Mon 12:48:44 PM Current House Power: 13282.7 W
Mon 12:48:44 PM Current Grid Power: 8116.7 W

Mon 12:48:44 PM Charging stopped, not enough solar
Mon 12:48:44 PM Waiting for 300 s before checking again.

Mon 12:54:44 PM Time to charge.

Mon 12:54:44 PM Current amps 0 - charging at 0 Watts
Mon 12:54:44 PM Current Solar Power: 12309 W
Mon 12:54:44 PM Current House Power: 1505.6200000000008 W
Mon 12:54:44 PM Current Grid Power: -10803.38 W

Mon 12:54:44 PM Enough solar
Mon 12:54:44 PM Enough excess solar
Mon 12:54:44 PM Starting charging at 14 amps
Mon 12:55:47 PM Time to charge.

Mon 12:55:47 PM Current amps 14 - charging at 10080 Watts
Mon 12:55:47 PM Current Solar Power: 9034 W
Mon 12:55:47 PM Current House Power: 10191.92 W
Mon 12:55:47 PM Current Grid Power: 1157.92 W

Mon 12:55:47 PM Enough solar
Mon 12:55:47 PM Enough excess solar
Mon 12:55:47 PM Changed charge rate from 14 amps to 11 amps


As you can see above you can go from charging at 15 amps to 0 and then back to 14 amps just from clouds.
 
  • Like
Reactions: Dave EV
What we really want is a way to authenticate with a Tesla wall charger and adjust it directly on the local network rather than through the Tesla API. Unfortunately I don't even have the WiFi capable charger
If you have a Gen2 then you can control it over RS-485. It works great, and adjusts charge rates every ~15 seconds for me.

But I'm looking forward to someone cracking the master/slave protocol on the gen3s so they can be controlled the same way via WiFi