ngardiner / TWCManager

Control power delivered by a Tesla Wall Charger using two wires screwed into its RS-485 terminals.
The Unlicense
132 stars 55 forks source link

API Support for Charge Rate #374

Open MikeBishop opened 3 years ago

MikeBishop commented 3 years ago

According to a recent leak, car firmware 2021.36+ supports remotely adjusting the charge rate via the API. This means TWCManager could support cars connected by UMC, Gen3 TWC, etc. once the specific API endpoint is known. Also means RS-232 can become optional.

mikey4321 commented 3 years ago

Finally :)

image

MikeBishop commented 3 years ago

So I was thinking about architecture. I think the way to manifest this is probably to support a collection of EVSEs which can be of different types:

Just like we poll all the EMS modules for generation and consumption data, we'd need to poll all the EVSE modules for connected and charging cars. It also becomes interesting because the TeslaAPI module might be of multiple types. (Or simply a wrapper that sits on top of the existing CarAPI module and assumes it's loaded.)

There also needs to be a dedupe step where a vehicle found in the API isn't treated as a separate EVSE instance if it's connected to a TWC we already control. If the TWC reports VINs, that's easy. If not, there's a path that offloads it to the user (they should disable vehicles-as-EVSEs in config) or a path that attempts to be smart (if we change the charge current, does the car see that change?). We'll need to pick one.

We probably need to support a maximum charge rate for the home overall and perhaps for the connected TWCs specifically. I'd wait to see if there's demand for load-sharing within API vehicles.

Of course, we still need someone to extract the API endpoint from the APK before any of this matters. That's not something I know how to do offhand, but I bet it won't take long for someone to identify it.

MikeBishop commented 3 years ago

Oh, and the big bugaboo here -- the fakeMaster / process message loop will probably now live in a module. There is so much logic that triggers on TWC status messages coming in which now still needs to run in the absence of an RS-232 interface. That'll be... fun.

ngardiner commented 3 years ago

So the endpoint is identified already, it is:

api/1/vehicles/{vehicle_id}/command/set_charging_amps

I don't know any more about it at this point, but am feeling brave enough to do some testing over the weekend.

MikeBishop commented 3 years ago

I haven't seen any vehicles get 2021.36 yet, though more power to you if you have.

ngardiner commented 3 years ago

Some do, not I unfortunately, however this was about testing the API endpoint more than the vehicle functionality, and the outcome was a 404, so likely Tesla returns 404 for vehicle firmwares pre 2021.36 (or the endpoint URL is wrong or the request malformed) as others do have it and can see the charge rate control in their app.

Here's a screenshot:

image

Anyway, we'll find out soon enough. I have a prototype function added already and there's a new debug option to send API commands to vehicles which allows the charge rate to be specified. We're not far off being able to use it.

DotNetDann commented 3 years ago

Any news on this one?

I attempted to work out the API today however I cannot get the tesla app to accept a non-tesla certificate (proxying it) I did manage to confirm that the API does something

POST https://owner-api.teslamotors.com/api/1/vehicles/1111/command/set_charging_amps { "amps": 15 } I get a 200 response from the call and It changed my car from 11A to 5A (the minimum)

I also attempted { "value": 20 } but it changed my car to 0 amps

Is there any way to work out what parameter it is expecting?

DotNetDann commented 3 years ago

Update. I have this endpoint working with { "charging_amps": 20 }

RichieB2B commented 3 years ago

TeslaPy has this completely working already, see https://github.com/tdorssers/TeslaPy

ngardiner commented 3 years ago

As of a few days ago you should be able to manually control the vehicle's amps from the Debug tab in the web interface, if you have the latest developer (v1.2.4) commit:

image

Note that our payload does differ from the Tesla API one - it's just because we used different terminology internally within TWCManager.

The next step is to tie this in with the charge rate adjustment. It is coming over the next few days, with some caveats:

So keep an eye out, it is coming and it is a high priority to implement.

ngardiner commented 3 years ago

I've had my car updated yesterday to 2021.36 and have spent today working on some very early code which I'll be pushing tonight to support API charge rate adjustment for charging.

For the moment it will ship disabled by default, with a drop-down to allow you to select currently from two options:

image

In the new non-default mode, almost all requests to change charge rate will be sent to the Tesla API. I say almost as there's a routine to deal with a situation where a TWC may exceed the total power budget, and rather than having that implemented by API (which is slower to respond than the TWC and which is prone to being lost/ignored by the API) we instead drop the offered amps on the TWC in this case.

The current implementation that I have been successfully testing today doesn't deal with multiple vehicles however a follow up will. Just something to keep in mind if you have more than one vehicle, it does not associate the vehicle with the TWC currently and will only adjust the first vehicle's charge rate, but it's not going to be a limitation for long.

I do plan a second option called API Assisted Charge Rate Adjustment, in which we use the TWC (which frankly is more responsive and doesn't have certain drawbacks such as leaving your charge rate at the last set value and a habit I have observed from the API of acknowledging the command but not changing the charge rate at times, plus less dependency on internet connectivity) performs the majority of the adjustments, with the API used to do things such as reduce charge rate from 6A to 5A which the european TWCs cannot do.

Expect more updates as work on the interface continues.

ngardiner commented 3 years ago

An update to this, I've further updated the API based charge rate logic to identify vehicles connected to the TWC based on VIN. This should ensure that the appropriate vehicle's charge rate is changed.

surfarn commented 3 years ago

Can the I use the Tesla API to controll the Charge rate without an RS-485 connected? I would like to use load balancing so that fuses do not blow when the spa is heating when the car is charging.

RichieB2B commented 3 years ago

In general, yes you can. I believe TWCManager now also supports this.

surfarn commented 3 years ago

I think something is missing to run it without the RS-485. Keep getting this error even when after removing the USB-port in the compose file.

Traceback (most recent call last): File "TWCManager.py", line 34, in <module> import TWCManager.TWCManager File "/usr/src/TWCManager/lib/TWCManager/TWCManager.py", line 553, in <module> modinstance = modclassref(master) File "/usr/src/TWCManager/lib/TWCManager/Interface/RS485.py", line 52, in __init__ self.ser = self.serial.serial_for_url(self.port, self.baud, timeout=0) File "/usr/local/lib/python3.7/dist-packages/pyserial-3.5-py3.7.egg/serial/__init__.py", line 90, in serial_for_url instance.open() File "/usr/local/lib/python3.7/dist-packages/pyserial-3.5-py3.7.egg/serial/serialposix.py", line 325, in open raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg)) serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'

RichieB2B commented 3 years ago

In /etc/twcmanager/config.json disable the RS485 interface and probably enable the Dummy interface instead:

    "interface": {
      "Dummy": {
        "enabled": true,
      },
      "RS485": {
        "enabled": false,
surfarn commented 3 years ago

Thanks managed to start without the error message now

rickhuizinga commented 2 years ago

If using the API to change the charge rate, is the full functionality supported without plugging into the RS485 port? Specifically:

1) Will it automatically stop charging when the car is plugged in at night. In this case, assume non-scheduled power is track green energy and that the solar panels are producing 0W

2) Will it let the car go to sleep when it's not at home? I wouldn't want it to be kept awake to poll it's charging status to determine whether it should be instructed to stop charging.

jherby2k commented 2 years ago

Anyone else seeing crazy behavior with this API?

Not TWCManager related, just the API itself. Even using the mobile app - if i set the current from like 32 to 7, it'll flip back to 32 a few seconds later. If i set it to 16, then 7, it'll flip back to 16, then back to 32. Like it rolls back the history or something. So strange.

RichieB2B commented 2 years ago

No, I am using the API daily for loadbalancing (not using TWCManager) and it is very stable for me.

jherby2k commented 2 years ago

Thanks - i'm gonna go pull the TWCManager out of there and see if that's causing issues. You using a V2 charger as well?

RichieB2B commented 2 years ago

Yes, I have a TWC V2 using it with 3 phases at 230V.

jherby2k commented 2 years ago

Removed the TWCManager hardware and its stopped doing it. Got a moderately complex Home Assistant automation now handling it all via API. It was a good hard hack while it lasted... sniff.

bikeymouse commented 2 years ago

Removed the TWCManager hardware and its stopped doing it. Got a moderately complex Home Assistant automation now handling it all via API. It was a good hard hack while it lasted... sniff.

Hi, I’m very interested in that. I’m also wrestling with a combination of Home Assistant / Node-Red and TWC-manager to do loadbalancing en solar tracking. Also seeing TWC having strange behavior (in that combination). Would you be willing to share your set-up/code? (Perhaps a bit off-topic here, but it could also be in the HASS forum.)

surfarn commented 2 years ago

I would also be very interested to see how you did this in HASS.

alexeiw123 commented 2 years ago

I do plan a second option called API Assisted Charge Rate Adjustment

Oh this sounds an ideal use of the API. Being able to use 5A without API priority would be great.