TeslaFi.com API integration for HA?

I wonder if anyone with any JSON-based web API integration experience for HA might consider creating an integration for the TeslaFi.com website API documented here: https://teslafi.com/api.php

The API itself seems very simple - it returns a json blob with a bunch of data related to your vehicle, and can issue a few basic commands to the car.

Why is this interesting if there is already a Tesla car API integration? Two reasons: (1) the API returns not only car data, but some additional data derived by TeslaFi.com that is interesting, and more importantly (2) TeslaFi.com has some very good polling algorithms that when setup correctly donā€™t get in the way of the car sleeping or excessively drain the battery. Conversely, the existing Tesla integration seems to poll the car indiscriminately every 300 seconds (or what you set the poll interval to be) which can have a big impact on sleep states/battery.

Thanks!

3 Likes

Doesnā€™t TeslaFi require you to hand over your Tesla account credentials?

Many people prefer the HA approach as the credentials are stored locally. This is like handing over your car keys; hope you trust TeslaFi and also their ability to keep user data safe.

I agree that the current integration leaves a bit to be desired for polling, but it now has a switch entity exposed to disable polling. I have my refresh interval set at 10 minutes and a couple automations in place:

  1. disable polling if battery is below 20 and the car is stationary and unplugged
  2. enable polling if my juicebox EVSE is plugged in (canā€™t rely on the Tesla sensor since this wonā€™t be polled)
  3. enable polling if any of my other device trackers show me moving > 30mph within 1/2mi of the last known position of the vehicle (assumes Iā€™m in the car and moving)

this helps a little bit at least

I would love to see this done with an integration but I do have some basic data from Teslafi by using the REST sensor platform.

To counter @snicker TeslaFi does not store your password, you donā€™t even have to give it to them if you are capable of requesting your own API key, which isnā€™t all that hard especially for those who already play with HA under the hood. But even if you do put your password into their website, they donā€™t store it, they use it to request an API key and a refresh token (if you allow them the latter) and API key based access doesnā€™t allow you to remote start the car.

But anyway, the question was not ā€œshould I use TeslaFiā€ but ā€œIs there an integration for this APIā€

1 Like

TeslaFi does not require your credentials, you can provide a Token instead.

1 Like

UPDATE: Iā€™ve just discovered a new data logging project called TeslaMate that may end up being a great solve to this challenge. Check it out here:

2 Likes

I wrote an integration for TeslaFi in SmartThings, with a service connect SmartApp and a custom device handler (actually split into 5 separate components for better visibility in the new app): https://github.com/jhansche/st-teslafi-v2

I was also looking for a way to do the same integration in Home Assistant. It looks like creating a new component is significantly more involved than I was anticipating, but I plan on giving it a shot. If/when I have something to share, Iā€™ll update it here.

@millercentral Have you tried this TeslaMate service, and if so were you able to integrate it in HA? Iā€™m wondering if going the self-host+MQTT route might make for an easier integration :thinking:

I struggled a bit with the MQTT approach, but I do pipe my SmartThings items into HA, so Iā€™ll try your SmarttApp approach. Thanks!

So, I didnā€™t move forward with my custom integration. But I did come across the rest and template sensor types, which kind of work great together for exactly this purposeā€¦

Added this to sensors.yaml:

- platform: rest
  name: TeslaFi Report
  resource: https://www.teslafi.com/feed.php
  scan_interval: 300
  params:
    token: !secret teslafi_api_token
    command: lastGood
  value_template: "{{ value_json.display_name }}"
  json_attributes:
    - vehicle_id
    - vin
    - vehicle_name
    - odometer
    - carState
    - car_version
    - battery_level
    - battery_range
    - locked
    - inside_tempF
    - outside_tempF
    - is_user_present
    - homelink_nearby
    - latitude
    - longitude
    - speed
    - charging_state
    - charger_actual_current
    # charger_power is rounded to whole kW
    - charger_voltage
    - charge_rate
    - charge_energy_added
    - time_to_full_charge

Define teslafi_api_token inside secrets.yaml, which you can get from https://www.teslafi.com/api.php. That creates a single device with all those attributes extracted from the JSON data.

Next, you can create individual sensor entities using the template sensor type:

- platform: template
  sensors:
    # General states
    teslafi_odometer:
      friendly_name_template: "{{ state_attr('sensor.teslafi_report', 'vehicle_name') + ' Odometer' }}"
      value_template: "{{ state_attr('sensor.teslafi_report', 'odometer') | round(1) }}"
      unit_of_measurement: "mi"
      icon_template: mdi:counter
    teslafi_battery:
      friendly_name_template: "{{ state_attr('sensor.teslafi_report', 'vehicle_name') + ' Battery' }}"
      value_template: "{{ state_attr('sensor.teslafi_report', 'battery_level') | int }}"
      unit_of_measurement: "%"
      icon_template: mdi:battery-80
    teslafi_range:
      friendly_name_template: "{{ state_attr('sensor.teslafi_report', 'vehicle_name') + ' Battery Range' }}"
      value_template: "{{ state_attr('sensor.teslafi_report', 'battery_range') | float }}"
      unit_of_measurement: "mi"
      icon_template: mdi:gauge
...

The template sensors can almost be copied from TeslaMate docs (link below).

Itā€™s not quite as concise as Iā€™d like (single ā€œDeviceā€ with several platform entities representing all the states), but it gets the job done.

References:

3 Likes

Thanks, this is a useful workaround, whilst the main Tesla integration is broken.

Teslamate has been great for integrating MQTT read-only sensors. I never actually use the official Tesla integration on HA (even before it was removed) just because I was worried about polling intervals and vampire drain, but I was considering looking into the custom integration and just setting the polling interval super high while retaining use of the MQTT sensors.