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.
 
Really? Even on a Tesla Gen 2 Waller charger?

Hope you're right....been waiting and waiting for that feature...


2023.26 Release Notes

Charge your Tesla with solar energy if you have a Powerwall. You can choose how much charging comes from solar versus other sources.

To set it up, open the Tesla app for your vehicle, tap the “Charge on Solar” message, and follow the instructions. Requires Powerwall v23.12.10 and Tesla app v4.22.5


Admittedly you have to get your Powerwall onto the new firmware, and Tesla is normally pretty slow with those updates, we're still on 23.12.2 for the gateway.
Somewhat related.. i'm exporting to the grid our Powerwall stored power right now at 76c per kw/h in Vic cause there is a price spike on right now...

1688369520946.png


I'll recharge the car from the grid tomorrow... it's generally able to buy at around 4c to 15c per kw/h.

If anyone wants a Amber Electric referral ping me..

If you've got a Powerwall and EV, the wholesale grid is pretty good... Prices went down from July 1 :)
 
  • Informative
Reactions: Vostok
well - that was easy - haven't actually scripted it yet - but should be a bit easier :D
Yes! I've implemented this in my HA with the following pieces:
  1. Tesla integration :)
  2. Solar inverter integration for HA (had to write my own) - syncs data for necessary sensors (grid import/export/etc.)
  3. Template sensor to calculate desired charge rate (we have a house battery so it is: grid_export - grid_import - battery_discharge + battery_charge)
  4. Statistics sensor to calculate a rolling 10 min average for desired charge rate (smooth out spikes due to clouds etc.)
  5. Automation triggered every X minutes to call a script (only when car is at home and plugged in)
  6. Script works like this: if 10-min-avg charge rate above/below a certain limit turn Tesla charging on/off and set charging amps accordingly
The one thing to keep in mind is you want to avoid flip-flopping the charging on/off too frequently as this will engage/disengage a contactor (the "clunk" you hear when starting charging). The wisdom online is that excessive toggling of the contactor could prematurely wear out that mechanical component.
 
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.

View attachment 929829

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.
Nice!

I went through a similar process to get my SMA inverter with speedwire to work with ChargeHQ using their push API. The extra complication I had was that my solar system doesn't have any consumption monitoring so I had to find a way to install a power monitoring solution to also connect to get my consumption and generation to get ChargeHQ to work accurately on excess generation. Ended up getting a DiUS Powersensor kit to do consumption.

I also ended up using ChatGPT to help come up with some basic python to extract the data from my SMA inverter and another python to access the raw sensor data from the Powersensor kit and then used a shell script running on Raspberry Pi to fusion the data together to push to ChargeHQ. I also modified the ChargeHQ scripts to also upload the consumption data to PVoutput to fill the gap that I've always had in that data since day 1 of my solar system.

I hadn't used ChatGPT before and I'm sure I could have spent a lot more time giving it very detailed and specific requirements to write the code to get it to produce almost the entire solution however I really only wanted it to give me some basic building blocks for the connections so I could write the rest myself as a hobby project. Some of the SMA protocol is a bit obtuse and I really didn't have the interest having to research the protocol and mess around getting the code to work when ChatGPT was able to generate some basic results in 5 minutes.
 
Last edited:
The one thing to keep in mind is you want to avoid flip-flopping the charging on/off too frequently as this will engage/disengage a contactor (the "clunk" you hear when starting charging). The wisdom online is that excessive toggling of the contactor could prematurely wear out that mechanical component.
Yes, but...

The clunk you hear is the relay in the EVSE, yes that can wear out, the BIG relay that I paid $900 before I wrote this app wore out from just letting the car get woken up by me opening the door to get something outta the car or Tezlab thinking the car was already awake and polling it, heck I think my car used to wake up just by me walking by it with my phone in my pocket.

I do try to limit my turning the charging off and on, mostly by allowing it to charge down to two amps (Send the command to set amps to '1' twice) and then not turning it back on after it goes off until there are 5 amps of power available. Clouds do mess with that, but I haven't done the 10 min average yet. I think that would ruin my ability to turn off at the first notice of the Hot Tub or Oven running.
 
I do try to limit my turning the charging off and on, mostly by allowing it to charge down to two amps (Send the command to set amps to '1' twice) and then not turning it back on after it goes off until there are 5 amps of power available.

Does the car even do any charging at 2A on USA voltage? There’s a baseline of consumption in the charging electronics and cooling pump. Is there anything left to go into the battery?

The script I wrote to charge from excess solar is a bit different because I have a PW2 to act as a buffer. First, I wake the car only once, when all the conditions I have set to start charging the car are met. I charge at a fixed rate (usually 5A x 3 phases = 15A). Then, I don’t care if there‘s short periods of cloud or whatever, the car keeps charging at the same rate, using whatever solar is available and the rest from the PW2.

Charging stops when either the car has reached my charge limit (I set that to 90%) or the PW2 has depleted to a level where it would not fill up again before sunset, usually between 85-90%. I then set the charge limit on the car back to 50%, before it goes back to sleep, so that it does not grid charge overnight.

So the car wakes only once, charge rate is set only once, and at some point charging stops, minimising Tesla API calls and risk of being blocked.
 
Does the car even do any charging at 2A on USA voltage? There’s a baseline of consumption in the charging electronics and cooling pump. Is there anything left to go into the battery?
It's 2a x 240v, so probably yes, but the greater point is to NOT be toggling the power relay just because there isn't 5a to charge when a cloud runs overhead. It would be nice to have a battery to buffer this with but doesn't that have a relay to power it off too?
 
Last edited:
Since this thread came alive again, I'll give a bit of an update.

I've improved the code to help mitigate the impact of clouds and the resulting stop/starting charging issues. This was mitigated by adding the ability to go down to 2A and then only ramp up in 2A increments. While I might miss a bit of solar, it was working well. I want to change it so that rather than stop charging it sets it to 2A for 2 mins and then only stops if there is still not enough solar after 3 mins. I will use some grid power, but it will still prevent lots of stop/start behaviour.

Other changes include:
  1. Checks the charge status every 5 mins and prints out the charging information.
  2. If the car is within 2% of the charge limit then I check every minute and stop charging once charged.
  3. Cleaned up some logic that resulted in weird amp settings.
  4. Checks if the charger is connected every 5 mins.
I sold my house, so I no longer have access to the environment that the code was built for. I do have a different type of inverter at the rental I'm in now, but I haven't had a chance to look into whether that will work. Anyway, I'm only charging from a normal wall socket, so I just plug it in and leave it now.

I'll go through the code and update the Github repo over the weekend.

Results from a perfect winter's day on the Gold Coast.

1688563296825.png
 
Results from a perfect winter's day on the Gold Coast.
Nice! I'm Gold Coast, too. Here are my stats for the same day (14th June 2023).

1688623099760.png


Cyan = consumed battery (incl. Tesla charging)
Yellow = consumed solar (incl. Tesla charging)
Blue = grid import
Purple = grid export
Pink = battery charge
 

Attachments

  • 1688623075752.png
    1688623075752.png
    15.6 KB · Views: 30
  • 1688623020743.png
    1688623020743.png
    15.6 KB · Views: 18
  • 1688622741177.png
    1688622741177.png
    13.4 KB · Views: 33
We have a Gold Coast here in Alameda, too, but it's not on the water, though some of the houses are on the lagoon, so perhaps that's the coast they mean, and it's not about the brightness of the sun, it's often foggy here, it's about the price of the homes.
 
  • Informative
Reactions: cafz
Presumably this will need to be updated for access to the official API when the current one is killed off.

Oh wow this is news… so home-brewed API code, like what I’ve written to do excess solar charging of my Tesla, or using my PW2 to arbitrage grid rates automatically, will no longer work?

I hope there is a free level of SDK access to the new API - like SolCast has - so that hobbyists who don’t send many API commands per day can still do what they want to do. My code authenticates with Tesla at most twice per day and sends fewer than 10 API commands per day.
 
Oh wow this is news… so home-brewed API code, like what I’ve written to do excess solar charging of my Tesla, or using my PW2 to arbitrage grid rates automatically, will no longer work?
It sounds like current code will probably break, and have to be rewritten or proxied with the new APIs and authorisation in place.

I hope there is a free level of SDK access to the new API - like SolCast has - so that hobbyists who don’t send many API commands per day can still do what they want to do. My code authenticates with Tesla at most twice per day and sends fewer than 10 API commands per day.
Hopefully, those limits already fit comfortably under the temporary 'Discovery' tier.

(surely Tesla won't remove a free tier altogether; heck even Volvo gives 10,000 calls a day, though to be fair you can't do any commands via their API like you can with Tesla)
 
  • Informative
Reactions: Vostok
It sounds like current code will probably break, and have to be rewritten or proxied with the new APIs and authorisation in place.


Hopefully, those limits already fit comfortably under the temporary 'Discovery' tier.

(surely Tesla won't remove a free tier altogether; heck even Volvo gives 10,000 calls a day, though to be fair you can't do any commands via their API like you can with Tesla)
Ugh. Tesla's built in Charge with Solar doesn't work the way I want (updating the charging speed with excess solar after subtracting out the watts needed to get my Powerwall to full by 4pm in consumption mode), or let me charge with excess solar - house use after expensive electricity kicks in at 4pm allowing the battery to stay at 100% for longer.

I wonder if I can use the Tesla wall connector and some local API calls instead to control the charging speed instead of the Tesla cloud API... I guess if it's too anti-consumer we could install a different charger that allows local API control of charging speed, right?
 
  • Like
Reactions: israndy
Looking at the vehicle command SDK,


and explicitly


it seems that charging should largely now be able to be controlled via BLE, (bluetooth low energy). Once you pair and authenticate your raspberry pi or similar with the car via your keycard, charging can be controlled with
tesla-control -ble wake tesla-control -ble charging-start tesla-control -ble charging-stop

Although the commands.go file is missing an explicit charging-set-amps command line parsing it seems like that might just be an oversight and need only

C-like:
"charging-set-amps": &Command{
        help:             "Set charge rate to AMPS",
        requiresAuth:     true,
        requiresFleetAPI: false,
        args: []Argument{
            Argument{name: "AMPS", help: "Charging amps"},
        },
        handler: func(ctx context.Context, acct *account.Account, car *vehicle.Vehicle, args map[string]string) error {
            amps, err := strconv.Atoi(args["AMPS"])
            if err != nil {
                return fmt.Errorf("error parsing AMPS")
            }
            return car.SetChargingAmps(ctx, int32(amps))
        },
    },

added to that file as all the rest of the plumbing for SetChargingAmps is there. Although it is possible, for some reason, that command would be excluded from BLE control but given the vast range of other things now controllable via BLE I would lean to this being an oversight in the command line parsing within the 'tesla-control' program.

So for local solutions, this would not even need to be on the internet assuming modbus connection to a solar inverter, this looks like a big a step up in features that an owner can control.
 
Last edited: