Tesla Integration to Query Status and Send Commands to Your Car

Genius! Saving my network ID now, incase this happens again

I am interested in learning more about how to do this. I just installed the Tesla drivers and was able to see my car within HE, but I need to know the location of the car in order to do what I want.

My ultimate goal is if the car is at home, charging, and it is between 4 PM and 9 PM, stop charging.

My utility company charges an arm and a leg between 4 PM and 9 PM. I have the car se to start charging at 9, but sometimes during the mid-day I need a little top off and don't want the top off to run into the high peak $$ time. If you have anything on how you fed the data from TeslaMate's MQTT into HE, I'd love to see it. I too am a TM user. Just need this last integration information.

All that can.be done with only this. Go back and look at my rule examples.

Are you referring to post 108 for your example? I don't have the Home Connect and am wondering how you set your Mod 3 arrives. Is this a presence detection based on pinging an IP address?

it has nothing to do home connect as far as i know that is just to open the garage door. one of the attributes of the driver is presence it will show like a normal presence sensor as present or not depeing on whether the car is local.. tesla always knows the location of your car.

it should just be transparent as long as you set your home address in the car

ie

if (data.vehicleState) {
if (debug) log.debug "vehicle state = $data.vehicleState"

    	sendEvent(name: "presence", value: data.vehicleState.presence)
        sendEvent(name: "lock", value: data.vehicleState.lock)
        sendEvent(name: "odometer", value: data.vehicleState.odometer.toInteger())
        sendEvent(name: "sentry_mode", value: data.vehicleState.sentry_mode)
        sendEvent(name: "front_drivers_window" , value: data.vehicleState.front_drivers_window)
        sendEvent(name: "front_pass_window" , value: data.vehicleState.front_pass_window)
        sendEvent(name: "rear_drivers_window" , value: data.vehicleState.rear_drivers_window)
        sendEvent(name: "rear_pass_window" , value: data.vehicleState.rear_pass_window)

ie

Screenshot 2022-03-28 204105

1 Like

As noted in some of the other replies, you may be able to do this with the other code, I needed real time presence updates to effectively open my garage door when my car arrived, and I wasn't going to be able to do that via polling alone. Also keep in mind that this code is for reading data about the car only, it won't be able to control things like Kahn's code can.

So here's the very rough code - keep in mind that I'm not a dev. I made the bare minimum updates in places to get this to work, some of the stuff is unnecessary, and the code may not be entirely clean. Basically this code could be cleaned up some, but it works well enough for me, so I don't have a strong desire to do so.

Some things would need to be updated in the code:

  • There is a lat and long value in the code at line 389 and 390, I was originally going to use this to calculate a distance from a location (home or otherwise). Ended up using teslamate's geofence capability instead. The code that calls this function should be commented out at the moment.
  • There's some assumptions about geofence locations made. I only have a single geofenced location, so the code assumes if it gets any geofence data, that it is at home. Would need to tweak this code if there were multiple teslamate geofence locations configured.
  • Some of the attributes are kind of noisy, especially when driving - long, lat, and odometer. These can be commented out. I don't have perf issues with the hub as far as I can tell, but I also have this integration configured on a fairly empty hub that only does cloud integrations.
  • I had one instance where teslamate reported the car leaving and then returning, I imagine some sort of junk data from the TeslaAPI. Result was the incorrect presence change triggered a rule to open my garage door unnecessarily. There is a 2 minute delay in the code to try and prevent phantom updates like this, however as a warning, as with any sort of automation that controls doors or locks, use at your own risk and think about the implications/protections that can be put in place via rules. It has only happened once in a year of ownership and hasn't been an issue for me, but it doesn't mean it couldn't be an issue for someone else.

Basically use at your own risk, I got it working for me, it may not work for anyone else without tweaks.

1 Like

version 1.2 on git hub with these changes

lgk 3/29/22 add attributes for longitude and latitude
so maybe people without home connect can add some rule machine rules with math to determine if you are home or approaching home to open garage door.

  • speed is already there as was heading but not as attributes that can be queried by rules so added that as well.
  • also some more info if debugging is on

i am assume a rule like..

if longitude between x and y and latitude is between x and y
speed is < xxx and > zzz
heading is xxx or > y and < x (depeding on precision and trial and error)

and motion is active.. etc. open garage door.

you also may need to increase polling time.. not sure how the load on the hub or if the tesla api will get too pissed if you poll too often.. May be cheaper to buy homelink ... lol

And probably more reliable. (I just use HomeLink in our two Tesla cars and let them automatically open/close the garage doors)

1 Like

or the samsung arrival/presence sensor which i still use for our two cars in mich and my wifes car and it works very well to open/close the garage door.

However, they are no longer available and tend to break easily replacing the battery, which is why i have spares in the basement.. i also on my wifes soldiered it into a 2 aa battery case which so far lasts for over 2 yrs.

What I’m doing with Teslamate has been very reliable, the door opens as I’m pulling into my driveway every time. I have an extremely old garage door, that doesn’t even have remote capabilities (I use a zen16 to trigger it). So even if I wanted to spend the couple hundred to add home link to the car, it wouldn’t help me.

how often are you polling the api

I don't know how telsamate collects its data, but I don't believe it uses polling at all. I think there is some sort of streaming API/websocket API it uses to interact and collect data about the car without polling and constantly waking the car up from its sleep.

For example, it's collecting very precise data throughout a drive, that it wouldn't be able to do via polling at all.

no the only way to get data is via the api.. from the looks of the graph it is polling or querying the api every 30 sec.
it could be an open mqtt socket somehow to the api.. seems neat.. but there is no other way to get data.

You’re speaking in absolutes, but I don’t believe you are correct. Just looking at the code, there is a streaming API endpoint it uses, looks like a web socket.

Don’t think I suggested it wasn’t using an API.

Also incorrect, there are in some cases multiple datapoints per second. Might want to use it before trying to interpret how it works.

It’s in a language I don’t know, but it’s pretty clear that it using a Tesla streaming api of some sort provided via a web socket. Which is distinctly different from polling.

The use of MQTT is on my side, Teslamate publishes data to a MQTT topic.

1 Like

Maybe this is where I'm getting hung up. I read this as if the driver is present in the car. And not as the car is present at home. My M3 has been parked in my driveway for about an hour now and the presence is showing Not Present.
image

Am I correct in this will show present when there is a driver in the seat? If so, am I better off with something similar to your suggestion? If Lat/Lon is between X/Y (+/- a little), then the car is at home? Or is there an alternate way to know that the car is at home.

no it should show present when the car is present. turn on debuggin and try a wake and refresh and post the output.. most likely your token has expired or something.

have you set the home location in the car if under navigation.

A couple of hours ago I updated the driver to your latest and noticed that my car wasn't showing up in the app drop down. I generated a new token and reconnected the car to the app. My child device was removed and recreated. The Not Present screenshot I shared was from after the token. I'll enable debug and post the logs.

Yes - This is set correctly and to my address

Output from wake then refresh. Showing presence Not Present

[dev:1125](http://192.168.0.14/logs#dev1125)2022-04-01 03:15:47.673 pm [debug](http://192.168.0.14/device/edit/1125)climateState = [temperature:100.22, thermostatSetpoint:71.06, seat_heater_left:0, seat_heater_right:0, seat_heater_rear_left:0, seat_heater_rear_right:0, seat_heater_rear_center:0]
[dev:1125](http://192.168.0.14/logs#dev1125)2022-04-01 03:15:47.668 pm [debug](http://192.168.0.14/device/edit/1125)vehicle state = [presence:not present, lock:locked, odometer:39163.034748, sentry_mode:On, front_drivers_window:Closed, front_pass_window:Closed, rear_drivers_window:Closed, rear_pass_window:Closed]
[dev:1125](http://192.168.0.14/logs#dev1125)2022-04-01 03:15:47.665 pm [debug](http://192.168.0.14/device/edit/1125)DriveState = [latitude:33.*****, longitude:-117.*****, method:wgs, heading:18, lastUpdateTime:Fri Apr 01 15:15:47 PDT 2022]
[dev:1125](http://192.168.0.14/logs#dev1125)2022-04-01 03:15:47.662 pm [debug](http://192.168.0.14/device/edit/1125)chargeState = [battery:49, batteryRange:139.81, chargingState:Disconnected, chargeLimit:80, minutes_to_full_charge:0]
[dev:1125](http://192.168.0.14/logs#dev1125)2022-04-01 03:15:47.659 pm [debug](http://192.168.0.14/device/edit/1125)processData: [state:online, motion:inactive, speed:0, vin:5YJ************, thermostatMode:off, chargeState:[battery:49, batteryRange:139.81, chargingState:Disconnected, chargeLimit:80, minutes_to_full_charge:0], driveState:[latitude:33.*****, longitude:-117.*****, method:wgs, heading:18, lastUpdateTime:Fri Apr 01 15:15:47 PDT 2022], vehicleState:[presence:not present, lock:locked, odometer:39163.034748, sentry_mode:On, front_drivers_window:Closed, front_pass_window:Closed, rear_drivers_window:Closed, rear_pass_window:Closed], climateState:[temperature:100.22, thermostatSetpoint:71.06, seat_heater_left:0, seat_heater_right:0, seat_heater_rear_left:0, seat_heater_rear_right:0, seat_heater_rear_center:0]]
[dev:1125](http://192.168.0.14/logs#dev1125)2022-04-01 03:15:46.858 pm [debug](http://192.168.0.14/device/edit/1125)Executing 'refresh'

weird it should show like this.. it should know you are home.. try setting it to your actual address and saee if that fixes it..

does anyone elses show present without having homelink active in their car.. maybe as summizaed it is tied to that I cannot find much info on the attribute.. Tesla does not really document their api.. no suprise there.

It is set to my actual address.
As a work around, I could set my "home" based on the Lat/Lon, with a few degrees of difference. I don't need the immediate update. I only want to know if I'm charging between peak times or forgot to plug in the car.