tdorssers / TeslaPy

A Python module to use the Tesla Motors Owner API
MIT License
374 stars 83 forks source link

Add CHARGING_AMPS incl. yet unknown parameter name to README.md "Commands" table #36

Closed frsteinb closed 3 years ago

frsteinb commented 3 years ago

The new command CHARGING_AMPS obviously has to have a parameter to set the intended current. Does anybody know the name of this parameter and its value range (maybe, e.g. simply 5..16 for Model 3)? When it is known, it should be added to the according table in the Commands section of the README.md file.

godkane1 commented 3 years ago

I would also find this useful.

tdorssers commented 3 years ago

This endpoint should become available with 2021.36 software, and that's pretty much everything I know about this.

treynaer commented 3 years ago

Hi, I have 2021.36 on my model 3 since last Friday. I also can change the charge amps in the Telsa app. How can I assist?

frsteinb commented 3 years ago

Thank you very much for your assistance!

If you are able to setup TeslaPy, including the authentication steps and execute a command like the one from the README.md file for your vehicle:

python cli.py -e elon@tesla.com -w -a ACTUATE_TRUNK -k which_trunk=front

then you could start trying to adjust the amps by a similar command. When it does not return an error code, then you could check from the Tesla App (or in the car) whether it really succeeded and modified the charging amps. You could try (assuming amps are not currently set to exactly 10 A):

python cli.py -e elon@tesla.com -w -a CHARGING_AMPS -k amps=10 python cli.py -e elon@tesla.com -w -a CHARGING_AMPS -k charging_amps=10 python cli.py -e elon@tesla.com -w -a CHARGING_AMPS -k chargingamps=10 python cli.py -e elon@tesla.com -w -a CHARGING_AMPS -k current=10 ...

tdorssers commented 3 years ago

If you use the Android app, it might be possible to use a SSL proxy to capture the API calls made by the app.

dmaunder commented 3 years ago

According to https://tesla-api.timdorr.com/miscellaneous/endpoints the command is set_charging_amps

"CHARGING_AMPS": { "TYPE": "POST", "URI": "api/1/vehicles/{vehicle_id}/command/set_charging_amps", "AUTH": true

frsteinb commented 3 years ago

That's correct. The same can also be found in teslapy/endpoints.json, already. :-) What we are looking for is the name of the parameter that has be passed to the POST request to that given URI.

dmaunder commented 3 years ago

Ok, I can test it, from a python program, what do I call in Python code, as opposed to what you have above with using cli.py?

frsteinb commented 3 years ago

if you have a vehicle object, it's just this:

vehicle.command('CHARGING_AMPS', amps=10)

but again, the question is what the right parameter name should be instead of amps here.

dmaunder commented 3 years ago

vehicles[0].command('CHARGING_AMPS', amps=10)

Traceback (most recent call last): File "C:/Users/dmaunder/Programming/Python/powermeter/tesla-test.py", line 32, in vehicles[0].command('CHARGING_AMPS', amps=10) File "C:\Python27\lib\site-packages\teslapy__init.py", line 524, in command response = self.api(name, **kwargs)['response'] File "C:\Python27\lib\site-packages\teslapy\init.py", line 394, in api return self.tesla.api(name, {'vehicle_id': self['id_s']}, **kwargs) File "C:\Python27\lib\site-packages\teslapy\init__.py", line 262, in api raise ValueError('Unknown endpoint name ' + name) ValueError: Unknown endpoint name CHARGING_AMPS

godkane1 commented 3 years ago

Try set_charging_amps instead of CHARGING_AMPS.

dmaunder commented 3 years ago

Same.

vehicles[0].command('SET_CHARGING_AMPS', amps=10)

ValueError: Unknown endpoint name SET_CHARGING_AMPS

frsteinb commented 3 years ago

Same here, but my Model 3 does not yet have the firmware 2021.36. I thought that would be the reason for the unknown endpoint.

So, it might be more promising to follow Tim's advice above to use an Android App and an SSL proxy as a sniffer.

tdorssers commented 3 years ago

ValueError: Unknown endpoint name CHARGING_AMPS means that this endpoint is not found in endpoints.json. You are probably using the version is on pypi. If you checkout the latest commit (882c8ef) from git, then it should work.

frsteinb commented 3 years ago

Ack. My message above was based on an old TeslaPy version. With the current version and my pre 2021.36 vehicle, I get:

File "/usr/local/lib/python3.8/dist-packages/TeslaPy-2.0.0-py3.8.egg/teslapy/__init__.py", line 524, in command File "/usr/local/lib/python3.8/dist-packages/TeslaPy-2.0.0-py3.8.egg/teslapy/__init__.py", line 394, in api File "/usr/local/lib/python3.8/dist-packages/TeslaPy-2.0.0-py3.8.egg/teslapy/__init__.py", line 277, in api File "/usr/local/lib/python3.8/dist-packages/TeslaPy-2.0.0-py3.8.egg/teslapy/__init__.py", line 119, in request File "/usr/lib/python3/dist-packages/requests/models.py", line 940, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: `` for url: https://owner-api.teslamotors.com/api/1/vehicles/15183525502xxxxxx/command/set_charging_amps

treynaer commented 3 years ago

No luck yet with the SSL Proxy. In the app you see the current AMPS and have arrows up and down to change.

dmaunder commented 3 years ago

ValueError: Unknown endpoint name CHARGING_AMPS means that this endpoint is not found in endpoints.json. You are probably using the version is on pypi. If you checkout the latest commit (882c8ef) from git, then it should work.

My vehicle is on 2021.36. Sorry do be a dummy, but how do I get/install the latest version from github? I running on Windows for dev and then raspberry pi for production

dmaunder commented 3 years ago

Ignore last, got them installed, and got it all working.

print 'Current charge rate requested ' + str(vehicles[0].get_vehicle_data()['charge_state']['charge_current_request']) + 'A, actual rate is ' + str(vehicles[0].get_vehicle_data()['charge_state']['charger_actual_current']) + 'A'
print 'Setting rate to 24A'
vehicles[0].command('CHARGING_AMPS', charging_amps=24)
time.sleep(5)
print 'Current charge rate requested ' + str(vehicles[0].get_vehicle_data()['charge_state']['charge_current_request']) + 'A, actual rate is ' + str(vehicles[0].get_vehicle_data()['charge_state']['charger_actual_current']) + 'A'

gives output

Current charge rate requested 20A, actual rate is 20A Setting rate to 24A Current charge rate requested 24A, actual rate is 24A

And you see the charge rate change in the app.

tdorssers commented 3 years ago

Added to the table, see commit b217a4e. @dmaunder can you investigate SCHEDULED_CHARGING and SCHEDULED_DEPARTURE as well?

frsteinb commented 3 years ago

Thanks, dmaunder! Thanks, Tim!

dmaunder commented 3 years ago

@dmaunder can you investigate SCHEDULED_CHARGING and SCHEDULED_DEPARTURE as well?

Ill have a look on Thursday if I get time

themonomers commented 3 years ago

Through trial and error, I found the payload to pass in the POST for set_scheduled_charging is:

payload = { 'enable': 'True', 'time': <time> }

where <time> is the number of minutes so if you want 6:00AM local time, then <time> would be 360.

tdorssers commented 3 years ago

Thanks @themonomers, what about SCHEDULED_DEPARTURE?

themonomers commented 3 years ago

Thanks @themonomers, what about SCHEDULED_DEPARTURE?

No luck yet. Still working on it; I have it half-working.

themonomers commented 3 years ago

Thanks @themonomers, what about SCHEDULED_DEPARTURE?

No luck yet. Still working on it; I have it half-working.

Here's where I am on set_scheduled_departure. The payload currently looks like this:

payload = { 'enable': 'True', 'departure_time': <time>, 'preconditioning_enabled': 'False', 'preconditioning_times': 'weekdays' 'off_peak_charging_enabled': 'True', 'off_peak_charging_times': 'weekdays', 'off_peak_hours_end_time': (6 * 60) }

What works: enable, departure_time, preconditioning_enabled, and off_peak_charging_enabled. These 4 attributes correctly modify the settings and reflect the same in the mobile app. The other ones I can't seem to figure out. I've tried varied names for the attributes and even tried some nested structures.

Additionally, perhaps it's a defect or my lack of understanding but in the mobile app there's no "enable" toggle for departure mode like there is for scheduled charging mode. When you toggle the enable for the scheduled charging in the mobile app, when it's plugged in it will show a message like "Charging will start at 5:00 AM" in the charging section but not for departure mode UNLESS you also toggle on Off-peak Charge. Only if you toggle Off-peak Charge in the mobile app will it show a message in the charging section about the departure time. When using the API, I can't get it to do that because I'm not passing the correct attribute for off_peak_hours_end_time and it defaults to 12:00 AM, despite having toggled the value for off_peak_charging_enabled. If I manually update the off_peak_hours_end_time from the mobile app, it will correctly show the message in the charging section.

themonomers commented 3 years ago

Thanks @themonomers, what about SCHEDULED_DEPARTURE?

Ok I finally figured it out. Here's the payload to POST:

payload = { 'enable': 'True', 'departure_time': <time_1>, 'preconditioning_enabled': <'True' or 'False'>, 'preconditioning_weekdays_only': <'True' or 'False'>, 'off_peak_charging_enabled': <'True' or 'False'>, 'off_peak_charging_weekdays_only': <'True' or 'False'>, 'end_off_peak_time': <time_2> }

where <time_1> and <time_2> are in minutes. This was quite tricky as some of the attributes in the POST don't match their output names.

tdorssers commented 3 years ago

Excellent job, thanks!

purcell-lab commented 3 years ago

CHARGING_AMPS can be set to less than 5, by calling the API twice.

The first call sets it to 5, then the second call will set to the desired value less than 5.

This is useful for fine control matching solar production.

This is not possible from the Telsa App, only the API call.

I have submitted PR #42.

Screenshot_20211026-172359

treynaer commented 3 years ago

You can even go to 0 if you want to quickly stop charging when your house needs power.

dmaunder commented 3 years ago

I thought I had read elsewhere previously that the car didn’t like it at less than 5A

From: purcell-lab @.> Sent: Wednesday, 27 October 2021 10:11 AM To: tdorssers/TeslaPy @.> Cc: @.; Mention @.> Subject: Re: [tdorssers/TeslaPy] Add CHARGING_AMPS incl. yet unknown parameter name to README.md "Commands" table (#36)

CHARGING_AMPS can be set to less than 5, by calling the API twice.

The first call sets it to 5, then the second call will set to the desired value less than 5.

This is useful for fine control matching solar production.

This is not possible from the Telsa App, only the API call.

I have submitted PR #42https://github.com/tdorssers/TeslaPy/pull/42.

[Screenshot_20211026-172359]https://linkprotect.cudasvc.com/url?a=https%3a%2f%2fuser-images.githubusercontent.com%2f79175134%2f138973820-103a2c50-2740-4154-8579-64efd908a2c3.png&c=E,1,IB0_ua3UC49wSFeky6M670s7ZjhtMCX0T20rmYObh-aIHqub7rCD4JU1CNlu42J0tumoQsaV0HAWXMevLPuFxuAZwM03KKmBT-BapJeZ_E3MqiRDxw,,&typo=1

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/tdorssers/TeslaPy/issues/36#issuecomment-952397563, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AASYAA7QV7IWQAE5QBLPKZDUI4YRVANCNFSM5FREA25A. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

RichieB2B commented 3 years ago

Very interesting! My MS2018 was able to charge less than 5A at 3 phases using the "set twice" trick. After setting it to 0A the charging was interrupted (Tesla App sent me a push message) and I was unable to restart it by increase CHARGING_AMPS. Even START_CHARGE failed because the charging session was apparently still in progress. Only after a STOP_CHARGE/START_CHARGE + CHARGING_AMPS > 0 cycle the charging actually resumed. So use with care!