Skip to content

Instantly share code, notes, and snippets.

@ngardiner
Last active May 21, 2023 21:46
Show Gist options
  • Save ngardiner/0cbb6b87142aba044596426c8a21581c to your computer and use it in GitHub Desktop.
Save ngardiner/0cbb6b87142aba044596426c8a21581c to your computer and use it in GitHub Desktop.
Using TeslaMate MQTT Topics in HomeAssistant

TeslaMate MQTT Integration - HomeAssistant

Introduction

This documents the process of replacing the official HomeAssistant Tesla component with MQTT interactions with TeslaMate.

Benefits

TeslaMate is better at polling the Tesla API, through the use of a better implemented polling mechanism with appropriate back-offs to allow Tesla vehicles to sleep.

It is also extremely useful to have running in parallel with HomeAssistant, offering a logical split between monitoring and visualisation (teslamate) and automation (HomeAssistant). Given HomeAssistant can talk via a common bus to other smart home components such as Garage Doors, Charger systems etc, it would not make sense to re-implement automation externally to HomeAssistant where it can be avoided.

Status

Things that aren't implemented yet:

  • Climate (display or interaction) - May be able to use the MQTT climate component, but will need more MQTT data
  • Locks
    • Door lock state is published via MQTT. No control available.
    • Charger lock state not published, no control available.
  • Maps
    • Was a sensor in the official Tesla component, not published by TeslaMate
  • Park Brake Status
    • Was a sensor in the official Tesla component, not published by TeslaMate

HASS Configuration

Current State

current_state_1

Lock Configuration (configuration.yaml)

lock:
   - platform: mqtt
     name: tesla_door_lock
     state_topic: "teslamate/cars/1/state"
     optimistic: false

ui-lovelace.yaml (Lovelace UI configuration)

  - path: car
    title: Car
    badges: []
    icon: 'mdi:car-connected'
    cards:
      - type: vertical-stack
        cards:
          - type: glance
            entities:
              - entity: sensor.tesla_battery_level
                name: Battery Level
              - entity: sensor.tesla_state
                name: Car State
              - entity: sensor.tesla_plugged_in
                name: Plugged In
          - type: glance
            entities:
              - entity: binary_sensor.tesla_model_3_parking_brake_sensor
                name: Park Brake
              - entity: sensor.tesla_sentry_mode
                name: Sentry Mode
              - entity: sensor.tesla_speed
                name: Speed
          - type: glance
            entities:
              - entity: sensor.tesla_healthy
                name: Car Health
              - entity: sensor.tesla_windows_open
                name: Window Status
          - type: horizontal-stack
            cards:
              - type: 'custom:button-card'
                entity: sensor.tesla_locked
                name: Charger Door
                show_state: true
                state:
                  - value: locked
                    icon: 'mdi:lock'
                    color: green
                    tap_action:
                      action: call-service
                      service: lock.unlock
                      service_data:
                        entity_id: lock.tesla_model_3_charger_door_lock
                  - value: unlocked
                    icon: 'mdi:lock-open'
                    color: red
                    tap_action:
                      action: call-service
                      service: lock.lock
                      service_data:
                        entity_id: lock.tesla_model_3_charger_door_lock
              - type: 'custom:button-card'
                entity: lock.tesla_door_lock
                name: Car Door
                show_state: true
                state:
                  - value: locked
                    icon: 'mdi:lock'
                    color: green
                    tap_action:
                      action: call-service
                      service: lock.unlock
                      service_data:
                        entity_id: lock.tesla_model_3_door_lock
                  - value: unlocked
                    icon: 'mdi:lock-open'
                    color: red
                    tap_action:
                      action: call-service
                      service: lock.lock
                      service_data:
                        entity_id: lock.tesla_model_3_door_lock
      - type: vertical-stack
        cards:
          - type: map
            entities:
              - device_tracker.tesla_model_3_[CAR VIN]_location_tracker
          - type: thermostat
            entity: climate.tesla_model_3_hvac_climate_system
      - type: entities
        entities:
          - entity: sensor.tesla_charge_limit
            name: SOC Charge Limit
          - entity: sensor.tesla_charge_energy_added
            name: Last Charge Energy Added
          - sensor.tesla_model_3_mileage_sensor
          - entity: sensor.tesla_estimated_range
            name: Estimated Range
          - entity: sensor.tesla_rated_range
            name: Rated Range
          - entity: sensor.tesla_inside_temp
            name: Tesla Temperature (inside)
          - entity: sensor.tesla_outside_temp
            name: Tesla Temperature (outside)
          - entity: proximity.home_tesla
            name: Distance to Home

sensor.yaml (sensor: section of configuration.yaml)

- platform: mqtt
  name: tesla_battery_level
  state_topic: "teslamate/cars/1/battery_level"
  unit_of_measurement: '%'
  icon: mdi:battery-80

- platform: mqtt
  name: tesla_charge_energy_added
  state_topic: "teslamate/cars/1/charge_energy_added"
  unit_of_measurement: 'kW'
  icon: mdi:battery-80

- platform: mqtt
  name: tesla_charge_limit
  state_topic: "teslamate/cars/1/charge_limit_soc"
  unit_of_measurement: '%'
  icon: mdi:battery-80

- platform: mqtt
  name: tesla_estimated_range
  state_topic: "teslamate/cars/1/est_battery_range_km"
  unit_of_measurement: 'km'
  icon: mdi:map-marker-path

- platform: mqtt
  name: tesla_healthy
  state_topic: "teslamate/cars/1/healthy"
  icon: mdi:car-connected

- platform: mqtt
  name: tesla_inside_temp
  state_topic: "teslamate/cars/1/inside_temp"
  unit_of_measurement: °C
  icon: mdi:thermometer-lines

- platform: mqtt
  name: tesla_locked
  state_topic: "teslamate/cars/1/locked"
  icon: mdi:lock

- platform: mqtt
  name: tesla_outside_temp
  state_topic: "teslamate/cars/1/outside_temp"
  unit_of_measurement: °C
  icon: mdi:thermometer-lines

- platform: mqtt
  name: tesla_plugged_in
  state_topic: "teslamate/cars/1/plugged_in"
  icon: mdi:power-plug

- platform: mqtt
  name: tesla_rated_range
  state_topic: "teslamate/cars/1/rated_battery_range_km"
  unit_of_measurement: 'km'
  icon: mdi:map-marker-path

- platform: mqtt
  name: tesla_sentry_mode
  state_topic: "teslamate/cars/1/sentry_mode"
  icon: mdi:cctv

- platform: mqtt
  name: tesla_speed
  state_topic: "teslamate/cars/1/speed"
  icon: mdi:speedometer

- platform: mqtt
  name: tesla_state
  state_topic: "teslamate/cars/1/state"
  icon: mdi:car-connected
  
  - platform: mqtt
  name: tesla_windows_open
  state_topic: "teslamate/cars/1/windows_open"
  icon: mdi:car-door

Writing Values

Not sure how writing values will be achieved. It is unlikely that the teslamate project will support pushing values back to the vehicles. One option would be to take the current tesla component, remove all of the polling for MQTT values, retain the code for the climate and locks (door and charger door), and have those read the values from MQTT sensors and write the values via the API. This could be used as a custom component for teslamate users.

Requires a second set of logins for the API, however this should result in full functionality.

@baylanger
Copy link

@ngardiner I was wondering if you've made any update since the above release?

Thanx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment