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

Model S REST API

This site may earn commission on affiliate links.
If you have a computer that could run the Apache2 web server, and that includes a $5 Rapsberry Pi Zero, then this is pretty easy to set up and I will give you some code to do this in Python. You just create a Python script put it in your /usr/lib/cgi-bin folder and hit the URL http://myserver/cgi-bin/climeon.py . And you can learn a bit about how to easily write scripts that you can execute from pretty much any web browser.
I'm also quite interested in this, I have an Apache2 server running for other things already, but I'm no expert on this stuff. If you could send me the code I'd love to try modifying it to suit my needs.
 
I'm also quite interested in this, I have an Apache2 server running for other things already, but I'm no expert on this stuff. If you could send me the code I'd love to try modifying it to suit my needs.
The first thing you need to do is enable python scripts to execute in Apache - I followed these directions (they are for the Raspberry Pi but I think they should work in any install of Apache2) Raspberry Pi web server - Writing CGI scripts in Python.

Then any python code that is in /usr/lib/cgi-bin directory will execute when you hit that URL. There are a few minor details like making sure the file is executable and you should probably change the owner to www-data.

So let's say you write some code called climate_on.py to turn on the climate of your car. If you then go to the web page http://myserver/cgi-bin/climate_on.py it will execute that script. With port forwarding you could also access this from anywhere on the internet and it should even work on a smartphone.

You can turn on authentication in Apache2 but that is more of a general Apache2 security function rather than anything specific to this process.
 
The first thing you need to do is enable python scripts to execute in Apache - I followed these directions (they are for the Raspberry Pi but I think they should work in any install of Apache2) Raspberry Pi web server - Writing CGI scripts in Python.

Then any python code that is in /usr/lib/cgi-bin directory will execute when you hit that URL. There are a few minor details like making sure the file is executable and you should probably change the owner to www-data.

So let's say you write some code called climate_on.py to turn on the climate of your car. If you then go to the web page http://myserver/cgi-bin/climate_on.py it will execute that script. With port forwarding you could also access this from anywhere on the internet and it should even work on a smartphone.

You can turn on authentication in Apache2 but that is more of a general Apache2 security function rather than anything specific to this process.
I'm already comfortable doing all of that, and already have a web server running 24/7 with other things on it. What I'm missing is the python script you keep talking about...
 
I'm already comfortable doing all of that, and already have a web server running 24/7 with other things on it. What I'm missing is the python script you keep talking about...
OK - have you retried an auth token yet? That is the first step and for some reason I initially had some problems with that. What do you want to do in your code? Turn on the climate?

Here is a post where I posted some python code that I have running on a daily basis to adjust your start charging time so that it ends at a specific time - I have mine set to finish charging at 6:55am.
http://www.teslamotorsclub.com/show...-times/page3?p=1275471&viewfull=1#post1275471
 
On the Tesla app you get notification of certain items, like Charging Started or Charging Ended.

How are these notifications actually kicked off? Is there some sort of an event viewer that handles stuff like this or does the software just have to poll the API on a frequent basis, like every minute, and then send a notification when it sees that something has changed?
 
On the Tesla app you get notification of certain items, like Charging Started or Charging Ended.

How are these notifications actually kicked off? Is there some sort of an event viewer that handles stuff like this or does the software just have to poll the API on a frequent basis, like every minute, and then send a notification when it sees that something has changed?

I think these notification use the native Apple and Google Push Notifications and are not part of the REST API.
 
I think these notification use the native Apple and Google Push Notifications and are not part of the REST API.

This seems highly unlikely, I'm pretty sure it's the app displaying these, I suspect the app is subscribing itself to something where Tesla pushes to it, so the question becomes how to subscribe something else to the same service.

Charging started and charging ended must be available via the API since Visible Tesla can send email notifications of those actions, right?
 
Charging started and charging ended must be available via the API since Visible Tesla can send email notifications of those actions, right?

You can poll to get the current charge state and infer if a charge has started or ended by comparing with prior results. This can all be done using the existing API (which is likely what Visible Tesla does) via the /vehicles/{id}/data_request/charge_state endpoint. However I do not believe that is what the mobile apps do. I think the mobile apps get a push notification from Tesla and do not have to poll. They do something similar to notify of new firmware releases.

Code:
[FONT=Menlo][~] $ teslacmd -c[/FONT]
[FONT=Menlo]{ charging_state: 'Disconnected',[/FONT]
[FONT=Menlo]  charge_limit_soc: 90,[/FONT]
[FONT=Menlo]  charge_limit_soc_std: 90,[/FONT]
[FONT=Menlo]  charge_limit_soc_min: 50,[/FONT]
[FONT=Menlo]  charge_limit_soc_max: 100,[/FONT]
[FONT=Menlo]  charge_to_max_range: false,[/FONT]
[FONT=Menlo]  battery_heater_on: false,[/FONT]
[FONT=Menlo]  not_enough_power_to_heat: false,[/FONT]
[FONT=Menlo]  max_range_charge_counter: 0,[/FONT]
[FONT=Menlo]  fast_charger_present: false,[/FONT]
[FONT=Menlo]  fast_charger_type: '<invalid>',[/FONT]
[FONT=Menlo]  battery_range: 157.11,[/FONT]
[FONT=Menlo]  est_battery_range: 108.17,[/FONT]
[FONT=Menlo]  ideal_battery_range: 178.67,[/FONT]
[FONT=Menlo]  battery_level: 79,[/FONT]
[FONT=Menlo]  usable_battery_level: 79,[/FONT]
[FONT=Menlo]  battery_current: -0.7,[/FONT]
[FONT=Menlo]  charge_energy_added: 0,[/FONT]
[FONT=Menlo]  charge_miles_added_rated: 0,[/FONT]
[FONT=Menlo]  charge_miles_added_ideal: 0,[/FONT]
[FONT=Menlo]  charger_voltage: 0,[/FONT]
[FONT=Menlo]  charger_pilot_current: 0,[/FONT]
[FONT=Menlo]  charger_actual_current: 0,[/FONT]
[FONT=Menlo]  charger_power: 0,[/FONT]
[FONT=Menlo]  time_to_full_charge: 0,[/FONT]
[FONT=Menlo]  trip_charging: false,[/FONT]
[FONT=Menlo]  charge_rate: 0,[/FONT]
[FONT=Menlo]  charge_port_door_open: false,[/FONT]
[FONT=Menlo]  motorized_charge_port: false,[/FONT]
[FONT=Menlo]  scheduled_charging_start_time: null,[/FONT]
[FONT=Menlo]  scheduled_charging_pending: false,[/FONT]
[FONT=Menlo]  user_charge_enable_request: null,[/FONT]
[FONT=Menlo]  charge_enable_request: false,[/FONT]
[FONT=Menlo]  eu_vehicle: false,[/FONT]
[FONT=Menlo]  charger_phases: null,[/FONT]
[FONT=Menlo]  charge_port_latch: 'Engaged',[/FONT]
[FONT=Menlo]  charge_current_request: 40,[/FONT]
[FONT=Menlo]  charge_current_request_max: 40,[/FONT]
[FONT=Menlo]  managed_charging_active: false,[/FONT]
[FONT=Menlo]  managed_charging_user_canceled: false,[/FONT]
[FONT=Menlo]  managed_charging_start_time: null }[/FONT]
 
You can poll to get the current charge state and infer if a charge has started or ended by comparing with prior results. This can all be done using the existing API (which is likely what Visible Tesla does) via the /vehicles/{id}/data_request/charge_state endpoint. However I do not believe that is what the mobile apps do. I think the mobile apps get a push notification from Tesla and do not have to poll. They do something similar to notify of new firmware releases.

Ahhh, I understand. Thanks for the explanation.
 
You can poll to get the current charge state and infer if a charge has started or ended by comparing with prior results. This can all be done using the existing API (which is likely what Visible Tesla does) via the /vehicles/{id}/data_request/charge_state endpoint. However I do not believe that is what the mobile apps do. I think the mobile apps get a push notification from Tesla and do not have to poll.
Thanks - that's what I was wondering as I wouldn't mind being able to run some of my own code when certain events occur. I didn't want to be polling too frequently as then you could be locked out by Tesla for hammering their servers.

For example - I would like to log when my car starts charging and the SOC and then log the time the charge is complete and the SOC. This allows me to start gathering data about my charge rate that I can then use for other purposed - like adjusting my charge time to end at a specific time.

Do any of the 3rd party tools like VT log this sort of charging data?
 
Thanks - that's what I was wondering as I wouldn't mind being able to run some of my own code when certain events occur. I didn't want to be polling too frequently as then you could be locked out by Tesla for hammering their servers.

For example - I would like to log when my car starts charging and the SOC and then log the time the charge is complete and the SOC. This allows me to start gathering data about my charge rate that I can then use for other purposed - like adjusting my charge time to end at a specific time.

Do any of the 3rd party tools like VT log this sort of charging data?

If you're familiar with MongoDB and/or Node, TeslaMS does this.

Here's a query I did that looks for any voltages >= 250 but <= 270 and prints the highest one found:

Capture.PNG


(Sorry for the photo, the forum software horribly butchers the text when it displays it... The text is okay, but something in vBulletin or the extensions that are used on TMC butchers it on display - Tapatalk is fine.)

PS: 256V is excessive. This was seen during a failure of a regulation component at the substation.
 
(Sorry for the photo, the forum software horribly butchers the text when it displays it... The text is okay, but something in vBulletin or the extensions that are used on TMC butchers it on display - Tapatalk is fine.)
Did you try using [noparse]
Code:
 and
[/noparse] around your text? usually that prevents it from destroying things, if that fails, also try [noparse][noparse][/noparse] to tell it to ignore special characters in the text.
 
Did you try using [noparse]
Code:
 and
[/noparse] around your text? usually that prevents it from destroying things, if that fails, also try [noparse][noparse][/noparse] to tell it to ignore special characters in the text.
Yes... I tried noparse, code, etc... It's not in the bbcode handling, it's in the display code that makes the page look pretty, and occurs after everything loads. You can see it display correctly for a few hundred msec, then it 'jumps'. Happens on all browsers I tried on multiple systems (including iPad).