Basic Tesla Integration

I have written a basic device handler for Tesla Model S.

It provides a SmartThings Presence capable device based on the GPS location of the car and also provides basic control of the car HVAC system. There is also a SmartApp that turns on HVAC in certain circumstances.

The device handler uses a python proxy that needs to be running on a server on the same LAN segment as the SmartThings hub which talks to the Tesla API (yes, sub optimal given Tesla’s cloud API is directly available, but it fits my existing architecture).

Code available on GitHub:

Thanks for sharing! exactly what I’m looking for. But how to install the lan proxy? I have QNAP that runs homebridge docker image so hardware is there, but no python experience here whatsoever. Any chance for docker image of the proxy?

Sweet. I just got my Model S last week. I have been waiting for some ST integration. Can this run on any host running Python? I have a mac osx desktop. All of my other devices are Windows tablets/laptops.

Yes, you can run on OSX - I developed it on OSX before moving it to a Raspberry PI.

If you install pip (sudo easy_install pip) you can then install Flask and geopy and then download and install teslajson (links all on GitHub)

Flask provides a built in web server that you can run from command line:

$ export FLASK_APP=teslacontrol.py
$ flask run --host=0.0.0.0

Flask project specifically warns against using the in built webserver ‘in production’ - the alternative is to wrap it behind a grown up web server - nginx or similar

Hope that helps

Hi,

You need to get to a point where you have working Python and pip to install dependencies. I am afraid I don’t know much about the QNAP, but this tutorial appears to show you the way:

Afraid I don’t have the time to roll a Docker image at the min - may get a chance in a couple of weeks

Cheers

I installed Pip, Flask and Geopy on my OSX, when I go to run flask run command, I get the below error:

Traceback (most recent call last):
File “/usr/local/bin/flask”, line 11, in
sys.exit(main())
File “/Library/Python/2.7/site-packages/flask/cli.py”, line 507, in main
cli.main(args=args, prog_name=name)
File “/Library/Python/2.7/site-packages/flask/cli.py”, line 374, in main
return AppGroup.main(self, *args, **kwargs)
File “/Library/Python/2.7/site-packages/click/core.py”, line 697, in main
rv = self.invoke(ctx)
File “/Library/Python/2.7/site-packages/click/core.py”, line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File “/Library/Python/2.7/site-packages/click/core.py”, line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File “/Library/Python/2.7/site-packages/click/core.py”, line 535, in invoke
return callback(*args, **kwargs)
File “/Library/Python/2.7/site-packages/click/decorators.py”, line 64, in new_func
return ctx.invoke(f, obj, *args[1:], **kwargs)
File “/Library/Python/2.7/site-packages/click/core.py”, line 535, in invoke
return callback(*args, **kwargs)
File “/Library/Python/2.7/site-packages/flask/cli.py”, line 417, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File “/Library/Python/2.7/site-packages/flask/cli.py”, line 146, in init
self._load_unlocked()
File “/Library/Python/2.7/site-packages/flask/cli.py”, line 170, in _load_unlocked
self._app = rv = self.loader()
File “/Library/Python/2.7/site-packages/flask/cli.py”, line 231, in load_app
rv = locate_app(self.app_import_path)
File “/Library/Python/2.7/site-packages/flask/cli.py”, line 90, in locate_app
import(module)
File “/Users/marcbh/Tesla/teslacontrol.py”, line 5

^
SyntaxError: invalid syntax

Can you confirm that you have downloaded from Github as-is and that line 5 is ‘import json’?

Have you edited the variables in the following 4 lines to match your situation? (email, password, vin, lat and long of home)

Cheers

Oops, I didn’t download the actual .py file, but rather the html file.

Now I downloaded the right one, put all the info in and I still get the same error.

Well that’s very odd. There is no reason that ‘import json’ (line 5) should throw a syntax error. Can you please post your version of the whole file somewhere and share link- without your username and password etc.!

Also, please post output of ‘python -V’ command

Hi folks,

I have been working with the author, @jonbur on why this code isn’t working on my V1 hub. We have confirmed that the local proxy/web server on my Mac OSX is working fine and communicating back to the Tesla API. What is NOT working is the communication from the hub -> MacOSX using the hubaction command. We have tried several things such as using a MAC address instead of IP, additional debugging, deleting and re-adding device handler, etc. I ran a packet capture on my MacOSX and there is no communication between the ST hub and my Mac.

Since the hubaction is undocumented and I see there is a lot of other folks with difference device types having issues, I was hoping someone can chime in here? Here is the logs from the last try:

7:38:47 AM: debug GET /api/isvehiclehome HTTP/1.1
Accept: application/json
User-Agent: Linux UPnP/1.0 SmartThings
HOST: 192.168.1.9:5000

7:38:47 AM: debug Request if vehicle is home sent
7:38:47 AM: debug Executing 'refresh’
7:38:47 AM: debug GET /api/isvehiclehome HTTP/1.1
Accept: application/json
User-Agent: Linux UPnP/1.0 SmartThings
HOST: 192.168.1.9:5000

7:38:47 AM: debug Request if vehicle is home sent
7:38:47 AM: debug Executing 'refresh’
7:38:47 AM: debug Executing 'poll’
7:38:47 AM: info Tesla Version 0.1 Copyright © 2016 JB
7:38:47 AM: debug Updated with settings: [ip:192.168.1.9, port:5000]

To add to that, the device handler code in question is: https://github.com/jonbur/smartthings/blob/master/tesladevice.groovy

This works for me on a v2 hub,

As @Mbhforum says, on his V1 hub we have tried DNI set to HEX(IP):HEX(port) (which I have checked is correct) and DNI set to MAC of the server (in the correct format - no punctuation) and in both cases nothing is sent from the hub

The function that hubAction is embedded in returns the hubAction object per the docs

What else could we be missing?

Many thanks

1 Like

To add…@jonbur is on EU1 shard, and I am on NA1. I have seen situations where different shards have behaved differently.

I figured out the issue. When creating the device type, you MUST select your Hub. Normally with devices in Smartthings, this doesn’t matter I don’t believe, but for anything that uses hubaction, the device type needs to know where to communicate to.

Sneak preview of the progress @jonbur and myself have made on this device handler. I know half of you will be getting the Model 3 one day!!!

2 Likes

Possible to make similar to VW E-Golf Car Net?

This device type uses very specific Tesla API calls and would be a major rewrite to support any other vehicles. Here is the progress weve made:

Ahh, looks promising for my Model 3 when it arrives… in late 2019 :slight_smile: Great work! Is it possible to you use this Smart App as presence sensor based Tesla’s location?

Stuck with E-Golf. Hopefully some developer will make same kind of app for VW :slight_smile:

Yes, this device handler has presence capability to determine if you are home or not. In the future @jonbur suggested we also can get GPS coordinates and show the actual location as well.

1 Like

How often does this ping the Tesla API? I know when third party services are hitting the API, it can prevent the car from sleeping resulting in vampire drain. Did you build a method to check whether the car is asleep and, if so, not run the full query? If not, that could be a helpful addition.

Somewhat related plug, but if anyone is interested in data logging for your car’s drives, charges, idle and sleep sessions, check out TeslaFi.com. It’s $5/month or $50/year after the trial period. It also pings the API to collect and store this data then display it in several different ways (hence my note about sleep mode above). I’ve worked with the site owner QAing and putting in feature requests since it’s inception and he’s done an amazing job with it. Tell him I sent you. :stuck_out_tongue:

I am concerned about waking up the car due to vampire drain. Should I place a presence sensor in my Model X or is there another way I could provide location info with smartthings?