Integrating TelsaMate into Home Assistant

Reading Time: 4 minutes

In a previous article, I explained how to install TeslaMate on a Vultr VPS. TeslaMate gives you a lot of information and graphs, but you can’t really “act” on the data. That’s where Home Assistant comes into play. Once you have integrated TeslaMate with Home assistant, you’ll be able to create automations to simplify your life (or at least make things geeky and cool).

Prerequisites

The first and most obvious one is that you’ll need Home Assistant installed and working. Secondly, you’ll need a MQTT broker installed. In a nutshell, MQTT is a standard for sending messages between applications. One application (TeslaMate in this case) creates and sends the message. Other applications (Home Assistant in this case) subscribe to (listen for) these messages. Home Assistant allows you to easily install the Mosquitto MQTT broker via the add-on’s. You can easily install Mosquitto by following the Home Assistant guide. You’ll also need is a user, so TeslaMate can securely access your Home Assistant. This is very easy to add:

  • Log into your Home Assistant
  • Click on Configuration in the left menu
  • Choose Users
  • Click the “ADD USER” button in the bottom right

You then just need to fill in the Display Name, username and the password. The user should not be an administrator. The username can be anything but “homeassistant” or “addon”, but I’d suggest something that is easy to remember, like teslamate.

The last thing that you’ll need to do is to make sure that your TeslaMate VPS can communicate with your Home Assistant on TCP port 1883. Setting this up is outside the scope of this article, as it depends on the network/firewall that your Home Assistant is running on.

Configuring TeslaMate to integrate with Home Assistant

The first file that you will need to modify is the “.env” file. You will be updating this file to add 4 more variables. To edit the file (using nano) use the following command:

nano .env

You will be adding the following 4 lines to the end of the file:

MQTT_HOST=##.##.##.##
MQTT_PORT=1883
MQTT_USERNAME=#####
MQTT_PASSWORD=#####
  • MQTT_HOST is the IP address of your Home Assistant server.
  • MQTT_USERNAME is the username that you created.
  • MQTT_PASSWORD is the password for the user that you created.

The second file that you will need to modify is “docker-compose.yml”. To edit that file, use the following command:

nano docker-compose.yml

You will need to add 4 lines to the end of the environment section for the teslamate application. Here is what the file should look like before you start:

services:
   teslamate:
     image: teslamate/teslamate:latest
     restart: always
     depends_on:
       - database
     environment:
       - DATABASE_USER=${TM_DB_USER}
       - DATABASE_PASS=${TM_DB_PASS}
       - DATABASE_NAME=${TM_DB_NAME}
       - DATABASE_HOST=database
       - MQTT_HOST=mosquitto
       - VIRTUAL_HOST=${FQDN_TM}
       - CHECK_ORIGIN=true
       - TZ=${TM_TZ}
     volumes:
       - ./import:/opt/app/import

And this is what it looks like after you add the 4 new variables:

services:
   teslamate:
     image: teslamate/teslamate:latest
     restart: always
     depends_on:
       - database
     environment:
       - DATABASE_USER=${TM_DB_USER}
       - DATABASE_PASS=${TM_DB_PASS}
       - DATABASE_NAME=${TM_DB_NAME}
       - DATABASE_HOST=database
       - MQTT_HOST=mosquitto
       - VIRTUAL_HOST=${FQDN_TM}
       - CHECK_ORIGIN=true
       - TZ=${TM_TZ}
       - MQTT_HOST=${MQTT_HOST}
       - MQTT_PORT=${MQTT_PORT}
       - MQTT_USERNAME=${MQTT_USERNAME}
       - MQTT_PASSWORD=${MQTT_PASSWORD}
     volumes:
       - ./import:/opt/app/import

Now you simply need to restart TeslaMate with the following command:

docker-compose up -d

Adding sensors to Home Assistant

The final step is to add the sensors and binary sensors to Home Assistant. This is documented on the TeslaMate Home Assistant Ingegration page. Exactly how you do this will depend on how you have implemented Home Assistant.

In my case, I use the web interface to create my automations. So rather than copy/paste the settings into the “automations.yaml” file, I used the web interface. I have two MQTT triggers setup. One is for the latitude and the other for longitude. I don’t have any conditions setup. I use the following yaml for actions:

service: device_tracker.see
 data_template:
   dev_id: tesla_location
   gps:
     - '{{ states.sensor.tesla_latitude.state }}'
     - '{{ states.sensor.tesla_longitude.state }}'

I simply copied the code for the know_devices.yaml file into my configuration files (I had to create this file, as it did not exist).

In my configuration I have multiple files for sensors and binary sensors. I simply copied the YAML for sensors and put that into “sensors/tesla.yaml”. I then copied the YAML for binary sensors and put that into “binary_sensors/tesla.yaml” Your configuration may be different depending on if you have everything in one large configuration.yaml or if you have things broken into folders and then include those folders into the configuration.yaml.

The last step is to check your configuration and then to restart Home Assistant so it picks up all the configuration changes.

Automation examples

This is where the magic of the TeslaMate and Home Assistant automation begins. There are numerous things that you can automate. One of the first things that I implemented was a reminder if I forget to plug my car in for the evening. My code looks like this:

- id: '1620581997032'
  alias: Tesla not plugged in
  description: ''
  trigger:
  - platform: time
     at: '20:00'
  condition:
  - condition: state
     entity_id: binary_sensor.tesla_plugged_in
     state: 'off'
  action:
  -  service: notify.frank_pushover
     data:
       title: Tesla not plugged in
       message: Tesla not plugged in
  mode: single 

This automation checks at 8:00 PM if my Tesla is plugged in. If it’s not (state is off) then it uses a notify service to ping my phone. In this example, you’d have to have the Home Assistant application running on your phone to receive the notification.

There are numerous other examples that you can implement on the TeslaMate website. Examples include:

  • Opening or closing your garage door based on geofence location.
  • Notifying if you leave your doors or windows open

Possible cleanup

There are a lot of sensors and binary sensors included with the TeslaMate Home Assistant integration. While you will wan to implement them all at first, you may find that you really don’t need them all. From example, speed and range are given in kilometers and well as miles. If you use kilometers, then it’s not necessary to have the sensors in miles. If your in a country that uses miles, unfortunately you can’t get rid of the kilometers sensors, as the miles sensors are based off those sensors. The same thing holds true for temperature sensors.

For any sensors that you find you don’t need, you can simply comment them out in the configuration file (Add a “#” as the first character of the line for all parts of the sensor) and then restart Home Assistant.

While there is no real harm in leaving the extra sensors, it’s going to generate a lot of extra history in Home Assistant, was well as make the database larger (and therefore potentially slower).

Conclusion

While it may be a bit confusing at first to get setup, once you have TeslaMate integrated with Home Assistant, there are an infinite number of things that you can do. You can add new cards to your Host assistant web interface or you can add various automations. The possibilities are only limited by your imagination and the other devices that you have integrated with Home Assistant.

Leave a Comment