View on GitHub

teslamate-abrp

A slightly convoluted way of getting your vehicle data from TeslaMate to A Better Route Planner.

TeslaMate: MQTT to ABRP

amd64+arm64 build GitHub license

A slightly convoluted way of getting your vehicle data from TeslaMate to ABRP.

Setup and Usage

1. Use as Docker container

1.1 Requirements

For this to work, you need a working instance of TeslaMate with MQTT enabled. See the official TeslaMate doc as a reference on how this might look like.

1.2 Getting a USER TOKEN from ABRP

Inside the ABRP (web)app, navigate to your car settings and use the “generic” card (last one at the very bottom) to generate your user token. Make a note of that token and keep it to yourself.

1.3 Adding the service to docker-compose.yml

In your TeslaMate docker-compose.yml, add the teslamate-abrp service by adding the following lines in the “services:” section:

ABRP:
  container_name: TeslaMate_ABRP
  image: fetzu/teslamate-abrp:latest #NOTE: you can replace ":latest" with ":beta" to use the bleeding edge version, without any guarantees.
  restart: always
  # privileged: true
  # NOTE: un-comment the previous line to run the container in privilege mode (necessary on RaspberryPi)
  environment:
    - MQTT_SERVER=mosquitto
    - USER_TOKEN=y0ur-4p1-k3y
    - CAR_NUMBER=1
    - CAR_MODEL=s100d #NOTE: This is optional, see below

Make sure to adapt the following environment variables:

Then from the command line, navigate to the folder where your docker-compose.yml is located and run:

docker-compose pull ABRP
docker-compose up -d ABRP

If all goes well, your car should be shown as online in ABRP after a minute. Logging should show “YYYY-MM-DD HH:MM:SS: [INFO] Connected with result code 0. Connection with MQTT server established.”.

1.4 Security

If you want to follow dockers recommendations regarding secrets, you should not provide them as ENVIRONMENT VARIABLE or command line parameters and instead use the build in secrets function. This will expose the secrets in the container to the file system at /run/secrets/. Read the documentation carefully if you wish to use docker’s secrets feature.

This is an example of a part of a docker-compose.yml file using:

version: '3'
services:
  # [...Other services such as TeslaMate, postgres, Grafana and the MQTT broker go here...]
  MQTT2ABRP:
    container_name: TeslaMate_ABRP
    image: fetzu/teslamate-abrp:latest #NOTE: you can replace ":latest" with ":beta" to use the bleeding edge version, without any guarantees.
    restart: always
    environment:
      CAR_NUMBER: 1
      MQTT_SERVER: your.server.tld # Replace with your server's service name or IP address
      MQTT_PORT: 8883 # This is a TLS enabled server, and usually that is enabled on a different port than the default 1883
      MQTT_USERNAME: myMQTTusername # Replace with your actually mqtt username
      MQTT_TLS: True # Connect to the MQTT server encrypted
      STATUS_TOPIC: teslamate-abrp # This will send status messages and a copy of the ABRP data to the topic "teslamate-abrp/xxx"
      TM2ABRP_DEBUG: True # This will enable debug level logging
      SKIP_LOCATION: True # Don't send location info to ABRP
      TZ: "Europe/Stockholm"
    secrets:
      - USER_TOKEN # Instead of having your token in clear text, it's found in the file below
      - MQTT_PASSWORD # Instead of having your password in clear text, it's found in the file below

secrets:
# These text files contains the token/passwords, and nothing else. 
# They can be placed "anywhere" on the host system and protected by appropriate file permissions.
  USER_TOKEN:
    file: ./path/to/abrp-token.txt 
  MQTT_PASSWORD:
    file: ./path/to/abrp-mqtt-pass.txt

To run it:

docker compose up -d

2. Use as python script

The script can also be run directly on a machine with Python 3.x. Please note that the machine needs to have access to your MQTT server on port 1883.

2.1 Installing requirements

To install the requirements, run

pip install -r requirements.txt

2.2 Running

To run, you can either use the CLI. Please note that USER_TOKEN, CAR_NUMBER, CAR_MODEL and MQTT_SERVER are required arguments.

If you are using a MQTT server with username or authentication, pass the -l (to use MQTT_USERNAME only) or -p (for authentication with MQTT_USERNAME and MQTT_PASSWORD) options. Be aware that passing a username and password on an MQTT server not set for it will cause the connection to fail.

Usage: 
    teslamate_mqtt2abrp.py [-hdlpsx] [USER_TOKEN] [CAR_NUMBER] [MQTT_SERVER] [MQTT_USERNAME] [MQTT_PASSWORD] [MQTT_PORT] [--model CAR_MODEL] [--status_topic TOPIC]

Arguments:
    USER_TOKEN            User token generated by ABRP.
    CAR_NUMBER            Car number from TeslaMate (usually 1).
    MQTT_SERVER           MQTT server address (e.g. "192.168.1.1").
    MQTT_PORT             MQTT port (e.g. 1883 or 8883 for TLS).
    MQTT_USERNAME         MQTT username, use with -l or -p.
    MQTT_PASSWORD         MQTT password, use with -p.

Options:
    -h                    Show this screen.
    -d                    Debug mode (set logging level to DEBUG)
    -l                    Use username to connect to MQTT server.
    -p                    Use authentication (user and password) to connect to MQTT server.
    -s                    Use TLS to connect to MQTT server, environment variable: MQTT_TLS
    -x                    Don't send LAT and LON to ABRP, environment variable: SKIP_LOCATION
    --model CAR_MODEL     Car model according to https://api.iternio.com/1/tlm/get_CARMODELs_list
    --status_topic TOPIC  MQTT topic to publish status messages to, if not set, no publish will be done.

Note:
    All arguments can also be passed as corresponding OS environment variables.

Note: All arguments can also be passed as corresponding OS environment variables. Arguments passed through the CLI will always supersede OS environment variables and docker secrets (in that order).

Credits

Based on/forked from letienne’s original code, with improvement by various contributors (see commit history).

License

Licensed under the MIT license.