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.
 
Guess I'll need to dedicate a computer near the car to control the charging as my current setup is at the extreme range of bluetooth. This is getting expensive. Might as well go with a 3P charge solution instead, would be cheaper to put together.
 
Oh well, this looks like I'll have to read about the new API and the Bluetooth option. Unfortunately I'm quite slammed with work and holidays planned until Dec, so this might end up being an Xmas project. Perhaps by then I can copy and paste a solution together from someone else's hard work, or ChatGPT will have come up with a solution ;).
 
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.

I can confirm this works. I compiled it on an RPi4 because it's faster, but am running these commands on an RPi3 I have in my garage, which is firewalled from the rest of the world so has no way to talk to the car except via Bluetooth.

With your suggested changes I can control the amps, and can see the changes reflected in the mobile phone app.
These commands all work:

Bash:
 $ tesla-control -h
Usage: tesla-control [OPTION...] COMMAND [ARG...]

Run tesla-control help COMMAND for more information. Valid COMMANDs are listed below.
 
 * Commands sent to a vehicle over the internet require a VIN and a token.
 * Commands sent to a vehicle over BLE require a VIN.
 * Account-management commands require a token.

Available OPTIONs:
  -ble 
        Force BLE connection even if OAuth environment variables are defined
  -debug
        Enable verbose debugging messages
  -domain value
        Domains to connect to (can be repeated; omit for all)
  -key-file file
        A file containing private key. Defaults to $TESLA_KEY_FILE.
  -key-name name
        System keyring name for private key. Defaults to $TESLA_KEY_NAME.
  -keyring-debug
        Enable keyring debug logging
  -keyring-file-dir directory
        keyring directory for file-backed keyring types (default "~/.tesla_keys")
  -keyring-type type
        Keyring type (file|keyctl|pass). Defaults to $TESLA_KEYRING_TYPE.
  -session-cache file
        Load session info cache from file. Defaults to $TESLA_CACHE_FILE.
  -token-file File
        File containing OAuth token. Defaults to $TESLA_TOKEN_FILE.
  -token-name name
        System keyring name for OAuth token. Defaults to $TESLA_TOKEN_NAME.
  -vin string
        Vehicle Identification Number. Defaults to $TESLA_VIN.

Available COMMANDs:
  add-key                Add PUBLIC_KEY to vehicle whitelist with ROLE and FORM_FACTOR
  add-key-request        Requset NFC-card approval for a enrolling PUBLIC_KEY with ROLE and FORM_FACTOR
  auto-seat-and-climate  Turn on automatic seat heating and HVAC
  autosecure-modelx      Close falcon-wing doors and lock vehicle. Model X only.
  charge-port-close      Close charge port
  charge-port-open       Open charge port
  charging-set-amps      Set charge rate to AMPS
  charging-set-limit     Set charge limit to PERCENT
  charging-start         Start charging
  charging-stop          Stop charging
  climate-off            Turn off climate control
  climate-on             Turn on climate control
  climate-set-temp       Set temperature (Celsius)
  drive                  Remote start vehicle
  flash-lights           Flash lights
  frunk-open             Open vehicle frunk. Note that there's no frunk-close command!
  get                    GET an owner API http ENDPOINT. Hostname will be taken from -config.
  honk                   Honk horn
  list-keys              List public keys enrolled on vehicle
  lock                   Lock vehicle
  media-set-volume       Set volume
  ping                   Ping vehicle
  post                   POST to ENDPOINT the contents of FILE. Hostname will be taken from -config.
  product-info           Print JSON product info
  remove-key             Remove PUBLIC_KEY from vehicle whitelist
  rename-key             Change the human-readable metadata of PUBLIC_KEY to NAME, MODEL, KIND
  seat-heater            Set seat heater at POSITION to LEVEL
  sentry-mode            Set sentry mode to STATE ('on' or 'off')
  session-info           Retrieve session info for PUBLIC_KEY from DOMAIN
  software-update-cancel Cancel a pending software update
  software-update-start  Start software update after DELAY
  steering-wheel-heater  Set steering wheel mode to STATE ('on' or 'off')
  trunk-close            Closes vehicle trunk. Only available on certain vehicle types.
  trunk-move             Toggle trunk open/closed. Closing is only available on certain vehicle types.
  trunk-open             Open vehicle trunk. Note that trunk-close only works on certain vehicle types.
  unlock                 Unlock vehicle
  wake                   Wake up vehicle

$ tesla-control -ble lock
$ tesla-control -ble unlock
$ tesla-control -ble climate-on
$ tesla-control -ble sentry-mode on
$ tesla-control -ble sentry-mode off
$ tesla-control -ble steering-wheel-heater on
$ tesla-control -ble charging-set-limit 80
$ tesla-control -ble charging-set-limit 90
$ tesla-control -ble charging-start
$ tesla-control -ble charging-set-amps 10
$ tesla-control -ble charging-set-amps 11
$ tesla-control -ble charging-set-amps 5
$ tesla-control -ble charging-set-amps 6
$ tesla-control -ble charging-stop
 
I can confirm this works. I compiled it on an RPi4 because it's faster, but am running these commands on an RPi3 I have in my garage, which is firewalled from the rest of the world so has no way to talk to the car except via Bluetooth.

With your suggested changes I can control the amps, and can see the changes reflected in the mobile phone app.
These commands all work:

Bash:
 $ tesla-control -h
Usage: tesla-control [OPTION...] COMMAND [ARG...]

Run tesla-control help COMMAND for more information. Valid COMMANDs are listed below.
 
 * Commands sent to a vehicle over the internet require a VIN and a token.
 * Commands sent to a vehicle over BLE require a VIN.
 * Account-management commands require a token.

Available OPTIONs:
  -ble
        Force BLE connection even if OAuth environment variables are defined
  -debug
        Enable verbose debugging messages
  -domain value
        Domains to connect to (can be repeated; omit for all)
  -key-file file
        A file containing private key. Defaults to $TESLA_KEY_FILE.
  -key-name name
        System keyring name for private key. Defaults to $TESLA_KEY_NAME.
  -keyring-debug
        Enable keyring debug logging
  -keyring-file-dir directory
        keyring directory for file-backed keyring types (default "~/.tesla_keys")
  -keyring-type type
        Keyring type (file|keyctl|pass). Defaults to $TESLA_KEYRING_TYPE.
  -session-cache file
        Load session info cache from file. Defaults to $TESLA_CACHE_FILE.
  -token-file File
        File containing OAuth token. Defaults to $TESLA_TOKEN_FILE.
  -token-name name
        System keyring name for OAuth token. Defaults to $TESLA_TOKEN_NAME.
  -vin string
        Vehicle Identification Number. Defaults to $TESLA_VIN.

Available COMMANDs:
  add-key                Add PUBLIC_KEY to vehicle whitelist with ROLE and FORM_FACTOR
  add-key-request        Requset NFC-card approval for a enrolling PUBLIC_KEY with ROLE and FORM_FACTOR
  auto-seat-and-climate  Turn on automatic seat heating and HVAC
  autosecure-modelx      Close falcon-wing doors and lock vehicle. Model X only.
  charge-port-close      Close charge port
  charge-port-open       Open charge port
  charging-set-amps      Set charge rate to AMPS
  charging-set-limit     Set charge limit to PERCENT
  charging-start         Start charging
  charging-stop          Stop charging
  climate-off            Turn off climate control
  climate-on             Turn on climate control
  climate-set-temp       Set temperature (Celsius)
  drive                  Remote start vehicle
  flash-lights           Flash lights
  frunk-open             Open vehicle frunk. Note that there's no frunk-close command!
  get                    GET an owner API http ENDPOINT. Hostname will be taken from -config.
  honk                   Honk horn
  list-keys              List public keys enrolled on vehicle
  lock                   Lock vehicle
  media-set-volume       Set volume
  ping                   Ping vehicle
  post                   POST to ENDPOINT the contents of FILE. Hostname will be taken from -config.
  product-info           Print JSON product info
  remove-key             Remove PUBLIC_KEY from vehicle whitelist
  rename-key             Change the human-readable metadata of PUBLIC_KEY to NAME, MODEL, KIND
  seat-heater            Set seat heater at POSITION to LEVEL
  sentry-mode            Set sentry mode to STATE ('on' or 'off')
  session-info           Retrieve session info for PUBLIC_KEY from DOMAIN
  software-update-cancel Cancel a pending software update
  software-update-start  Start software update after DELAY
  steering-wheel-heater  Set steering wheel mode to STATE ('on' or 'off')
  trunk-close            Closes vehicle trunk. Only available on certain vehicle types.
  trunk-move             Toggle trunk open/closed. Closing is only available on certain vehicle types.
  trunk-open             Open vehicle trunk. Note that trunk-close only works on certain vehicle types.
  unlock                 Unlock vehicle
  wake                   Wake up vehicle

$ tesla-control -ble lock
$ tesla-control -ble unlock
$ tesla-control -ble climate-on
$ tesla-control -ble sentry-mode on
$ tesla-control -ble sentry-mode off
$ tesla-control -ble steering-wheel-heater on
$ tesla-control -ble charging-set-limit 80
$ tesla-control -ble charging-set-limit 90
$ tesla-control -ble charging-start
$ tesla-control -ble charging-set-amps 10
$ tesla-control -ble charging-set-amps 11
$ tesla-control -ble charging-set-amps 5
$ tesla-control -ble charging-set-amps 6
$ tesla-control -ble charging-stop
Feel free, in fact I insist, to submit this as a PR on github. Testing is 90% the work anyway.
 
We charge 2x EVs from the sun, run the house and charge up the PW2.

4-5mths a year we have zero issue doing this with no grid draw (build up credit in excess), 4-5mths a little planning and break even (including off setting supply charge), 2-3mths we draw from the grid however the excess from other months offsets the usage in these months….

Is it worth it? I would suggest if you have a home battery absolutely, at least that’s my experience…
 
Last edited:
  • Like
Reactions: Hairyman
Is charging from solar really worthwhile? I have a 10kw solar system but Ovo Energy pays 10c for fit and 8.39 c to charge from 12-6am, so financially it doesn’t add up for me?
You're right - but on the "bigger picture" scale, the energy you're drawing overnight at 8.39c/kW is not green - there is an additional cost on most plans that ensure it is green, but the rate goes up as a result.

This isn't meant to judge you or anyone else - please don't misunderstand.

For me, a major motivator in buying an EV was to minimise our household greenhouse gas emissions, so over the past 2 years, we embarked on a project to convert to 3 phase power, install a 24kW solar system with 22kW battery, and swap over all household devices to electric (even the lawnmower). Full disclosure, we have one device to go - a gas cooktop that's used about 2 times a week.

All of that is integrated into a Fronius system that ensures everything is run as far as possible "on the sun". October saw us export 2MW to the grid, so I guess it's working!
 
You're right - but on the "bigger picture" scale, the energy you're drawing overnight at 8.39c/kW is not green - there is an additional cost on most plans that ensure it is green, but the rate goes up as a result.

This isn't meant to judge you or anyone else - please don't misunderstand.

For me, a major motivator in buying an EV was to minimise our household greenhouse gas emissions, so over the past 2 years, we embarked on a project to convert to 3 phase power, install a 24kW solar system with 22kW battery, and swap over all household devices to electric (even the lawnmower). Full disclosure, we have one device to go - a gas cooktop that's used about 2 times a week.

All of that is integrated into a Fronius system that ensures everything is run as far as possible "on the sun". October saw us export 2MW to the grid, so I guess it's working!
Same idea for us, we went electric mower many many years ago now 😉

We have gone low to no meat consumption and embarked on minimalism on our purchases (waste bin goes out once a month and generally 75% empty) etc.

Once the changes are made and habit kicks in it’s really not hard “to do”.

Next up will be 3 times water re-use (if the Stonnington Council will let me!)
 
From what others have said it sounds like it's based on the SW that your car is running, some seem to already have lost certain features and are using the new API while those running the FSD beta and perhaps others have not gotten that update yet.
 
I like the part about simple BLE commands to the car for charging-start & charging-stop. I'm somewhat handy with Arduino, and I might give that a try, if I can get an Arduino Giga or Arduino Nano 33 BLE to pair with the car. Any tips on that step?

The other part of "excess solar", though, is finding out when my PowerWalls are nearly full. Is there a branch of this solution that can request SoC status info from the PowerWalls to help me decide when to charge the car? The car and PowerWalls are all in my garage within BLE range of each other, and perhaps even the Gateway if that's where the info is hosted.
 
So what are 3P solutions that can do this.

I just had an Enphase PV installed.

Currently I just set the Offpeak End at 3PM and hits a good portion of Solar.

I really don’t have time to fiddle. Already have huge coding projects on the side.

Curious what ROI is if I bought 3P solution? 20 years ;)

I am concerned with stopping charging to much. Throttle current if great.

I have a Gen 2 Wall Connector.