TMC is an independent, primarily volunteer organization that relies on ad revenue to cover its operating costs. Please consider whitelisting TMC on your ad blocker and becoming a Supporting Member. For more info: Support TMC

Model S REST API

Discussion in 'Model S: User Interface' started by timdorr, Feb 2, 2013.

  1. 4sevens.com

    4sevens.com Member

    Joined:
    Aug 20, 2012
    Messages:
    418
    awesome thanks!

    I'm going to try it in osx... i wonder what flavor sh will work... will bash work?
     
  2. aviators99

    aviators99 Model S - R140

    Joined:
    Jan 1, 2010
    Messages:
    1,458
    Location:
    Weston, Florida, United States
    bash is backwards compatible with sh, so it should work.
     
  3. SigS905

    SigS905 Sig S 905 + X 5

    Joined:
    May 9, 2012
    Messages:
    200
    Location:
    FV, CA
    #83 SigS905, Feb 12, 2013
    Last edited: Feb 12, 2013
    It should work fine. Just made another modification so I can charge at Fashion Island which is close to home without interuption. Download the new version Model S REST API - Page 8. Tested just fine in OSX.
     
  4. 4sevens.com

    4sevens.com Member

    Joined:
    Aug 20, 2012
    Messages:
    418
    This is too easy! I'm thinking of setting a cron job that runs every 10 minutes - and creating dummy file to indicate whether the last time the script was run if the car was charging or not and if for some reason charging stopped since the last time the script was run, it'll email or better yet text me (google voice). This would be a great tool to notify you if either a fuse tripped overnight or someone unplugged you or the charge station shut you down or something... that will save you from a nasty surprised of an uncharged or partially charged car while on a trip :)
     
  5. 4sevens.com

    4sevens.com Member

    Joined:
    Aug 20, 2012
    Messages:
    418
    what are all the variables that can be accessed? I'd like to poll my car every 10 minutes and inject it into a mysql database. Then another script can take action based on the data analyzed.
     
  6. SigS905

    SigS905 Sig S 905 + X 5

    Joined:
    May 9, 2012
    Messages:
    200
    Location:
    FV, CA
    See post #1 documentation web site for current variables and their uses. You could change the cron entry shown in post #72 to be "0-59/10" to achieve ten minute reports.
     
  7. Banahogg

    Banahogg Member

    Joined:
    Jan 22, 2013
    Messages:
    293
    Location:
    San Jose, CA
    I just picked up my S from the factory, so now I can actually play with the API :)

    I noticed something interesting in my logging app. A snippet of data:

    inside_temp
    outside_temp
    driver_temp_setting
    is_auto_conditioning_on
    battery_current
    25.5
    19.5
    22.5
    false
    -0.6
    25.5 19.5
    22.5 false
    -0.6
    25.5
    19.5
    22.5
    false
    -0.6
    25.5
    19.5
    22.5
    false
    -0.6
    25.5

    22.5

    0


    22.5

    -0.1
    It looks like even without the global "sleep" setting, there are still subsystems that go to sleep over time, and are reflected in the battery current. These are at 10 minute intervals (the timestamp columns broke the bbcode table somehow)

    I also noticed that my car has had 2 max range charges already. Guess I should have picked it up sooner...
     
  8. aviators99

    aviators99 Model S - R140

    Joined:
    Jan 1, 2010
    Messages:
    1,458
    Location:
    Weston, Florida, United States
    Strange, my max_range_charge_counter is at 0 and I have done a couple of range charges.
     
  9. Banahogg

    Banahogg Member

    Joined:
    Jan 22, 2013
    Messages:
    293
    Location:
    San Jose, CA
    Interesting. I wonder if it's just broken then?
     
  10. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    My max_range_charge_counter is at 0 as well. Now I have to do another max range charge just to see if it's working correctly.
     
  11. kendallpb

    kendallpb Model S: P 8061

    Joined:
    Oct 29, 2010
    Messages:
    1,225
    Location:
    MD, USA
    Is there any way to check that information in the UI in the car, or is it just available through the API?
     
  12. hans

    hans P631

    Joined:
    Sep 27, 2012
    Messages:
    1,132
    Location:
    Menlo Park
    Just through the API.
     
  13. PureAmps

    PureAmps Model S P85 (#2817)

    Joined:
    Oct 22, 2012
    Messages:
    359
    Location:
    SF Bay Area
    Calculating kWh

    Anybody have any ideas how to calculate kWh remaining via the API given the car's current charge state? I'm trying to calculate Wh per mile, similar to what one finds in the trip meters in the car. I can get the odometer readings for the mileage, but not obvious how to calculate the watt hours used. Here's some relevant sample data from my car while charging:

    Code:
     "battery_range"=>183.28,
     "ideal_battery_range"=>210.94,
     "est_battery_range"=>187.98,
     "battery_level"=>74
    
    The main problem is that the "battery_level" which seems to be the same as State of Charge (SoC), has a pretty low precision (1% of total battery), which makes calculations very imprecise. So I'm hoping that the range numbers can be used to calculate remaining kWh, as they have greater precision. What is unknown is the math Tesla uses to calculate the ranges from the battery state (and other factors like temperature, etc.).

    Any thoughts?

    My current plan is just to log some data, and see if I can find some kind of obvious correlation between the numbers.
     
  14. aviators99

    aviators99 Model S - R140

    Joined:
    Jan 1, 2010
    Messages:
    1,458
    Location:
    Weston, Florida, United States
    First, I'm not positive about the battery_level. Right now mine says "93" at rated 241.64. If it's really percentage of full, this would put 100% at less than 260. I will check next time I do a range charge, which puts the rated at 270+
    Second, how do you get the odometer?
     
  15. ahimberg

    ahimberg Member

    Joined:
    Aug 8, 2010
    Messages:
    349
    Location:
    Woodinville, WA
    I was expecting that max_range_counter to be related to the 'you've charged in max range mode 3 times in a row' and it starts warning you to switch to standard? Not the count of lifetime max range charges
     
  16. PureAmps

    PureAmps Model S P85 (#2817)

    Joined:
    Oct 22, 2012
    Messages:
    359
    Location:
    SF Bay Area
    Via the streaming API. :)

    Try this:

    Code:
    curl --user USERNAME:TOKEN https://streaming.vn.teslamotors.com/stream/VEHICLE_ID/?values=speed,odometer,soc,elevation,est_heading,est_lat,est_lng,power,shift_state
    where USERNAME is your MyTesla username, TOKEN is the first token returned in your vehicle's JSON returned from the REST API, VEHICLE_ID is the "vehicle_id" returned in vehicle's JSON. The TOKEN value rotates every few minutes, so you will need to query your vehicle's JSON description before opening a new stream.

    Fields are returned as a comma separated list of values matching the field references in the URL query string. The first field is a timestamp.

    Now run that curl command, then hop in your vehicle and drive around for a while, and watch it stream. :)

    For those that use Ruby, I've already developed a ruby gem that supports both the rest and streaming APIs as well as a high-level Vehicle Object API. I'll release a public version by the end of the month, just need some time to clean it up a little (and I'm traveling quite a bit this month). Here's some sample code (from my twitter client) using the API:

    Code:
    portal = Teslamatic::API::Portal.new(config[:username], config[:password])
    
    vehicles = Teslamatic::Vehicle.vehicles(portal)
    vehicle = vehicles.first
    
    current_gear = vehicle.gear_selector
    current_odometer = vehicle.odometer
    
     
    • Informative x 1
  17. Banahogg

    Banahogg Member

    Joined:
    Jan 22, 2013
    Messages:
    293
    Location:
    San Jose, CA
    I just checked my log from today and it really looks like there's just a simple linear relationship between battery_range and battery_level.

    Interestingly, there seems to be quantization of the battery_range at what's the equivalent of about 1/7 of a percent, at least on my 60kwh, from looking at the numbers during "vampire load". That may be indicative of either an artifact of their measurement or of the reality of the 12V drawing down and recharging to a specific level.
     
  18. aviators99

    aviators99 Model S - R140

    Joined:
    Jan 1, 2010
    Messages:
    1,458
    Location:
    Weston, Florida, United States
    This is great stuff. But for some reason the token field is now coming up empty for me. I used to get it?

     
  19. PureAmps

    PureAmps Model S P85 (#2817)

    Joined:
    Oct 22, 2012
    Messages:
    359
    Location:
    SF Bay Area
    That has happened to me a few times. I would just wait a few seconds and try to query again. I think there is some coordination going on between the REST and streaming servers that may cause that to occur.
     
  20. timdorr

    timdorr Model S P1698 / VIN 1657

    Joined:
    Aug 2, 2012
    Messages:
    461
    Location:
    Atlanta, Georgia, United States

Share This Page

  • About Us

    Formed in 2006, Tesla Motors Club (TMC) was the first independent online Tesla community. Today it remains the largest and most dynamic community of Tesla enthusiasts. Learn more.
  • Do you value your experience at TMC? Consider becoming a Supporting Member of Tesla Motors Club. As a thank you for your contribution, you'll get nearly no ads in the Community and Groups sections. Additional perks are available depending on the level of contribution. Please visit the Account Upgrades page for more details.


    SUPPORT TMC