Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added semi-automatic car model recognition #6

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# TeslaMate: MQTT to ABRP
[![](https://img.shields.io/docker/image-size/fetzu/teslamate-abrp/latest)](https://hub.docker.com/r/fetzu/teslamate-abrp)
[![](https://img.shields.io/docker/pulls/fetzu/teslamate-abrp?color=%23099cec)](https://hub.docker.com/r/fetzu/teslamate-abrp)

A slightly convoluted way of getting your vehicule data from [TeslaMate](https://github.com/adriankumpf/teslamate) to [ABRP](https://abetterrouteplanner.com/).

## Use as Docker container
### Requirements
Expand All @@ -17,14 +21,14 @@ In your TeslaMate docker-compose.yml, add the teslamate-abrp service by adding t
- MQTT_SERVER=mosquitto
- USER_TOKEN=y0ur-4p1-k3y
- CAR_NUMBER=1
- CAR_MODEL=s100d
- CAR_MODEL=s100d [NOTE: This is optional, see below]
```

Make sure to adapt the following environment varibles:
- The first value MQTT_SERVER corresponds to the name of your MQTT service name ("mosquitto" in the doc).
- The second values (USER_TOKEN) correspond to the value provided by ABRP.
- The third value corresponds to your car number (1 if you only have a single car).
- The last value corresponds to your car model; you need to find your car model on https://api.iternio.com/1/tlm/get_carmodels_list. Use the corresponding key as a value for CAR_MODEL (e.g. "s100d" for a 2012-2018 S100D).
- The last value corresponds to your car model. When this value is not set, the script will try to determine your car model automatically (this should work for Models S, X and 3 with standard configs). __If your car is a Model Y or has some exotic trim/config, you need to find your car model on https://api.iternio.com/1/tlm/get_carmodels_list and use the corresponding key as a value for CAR_MODEL (e.g. "tesla:m3:20:bt37:heatpump" for a 2021 Model 3 LR).__
- Additionally, MQTT_PASSWORD and/or MQTT_USERNAME can be set to use authentication on the MQTT server.

Then from the command line, navigate to the folder where your docker-compose.yml is located and run:
Expand Down Expand Up @@ -53,19 +57,22 @@ If you are using a MQTT server with username or authentication, pass the -l (to

```
Usage:
teslamate_mqtt2abrp.py [-hlap] [USER_TOKEN] [CAR_NUMBER] [CAR_MODEL] [MQTT_SERVER] [MQTT_USERNAME] [MQTT_PASSWORD]
teslamate_mqtt2abrp.py [-hlap] USER_TOKEN CAR_NUMBER MQTT_SERVER [MQTT_USERNAME] [MQTT_PASSWORD] [-m CAR_MODEL]

Arguments:
USER_TOKEN User token generated by ABRP.
CAR_NUMBER Car number from TeslaMate (usually 1).
CAR_MODEL Car model (from https://api.iternio.com/1/tlm/get_carmodels_list, e.g. "s100d" for a 2012-2018 S100D).
MQTT_SERVER MQTT server address (e.g. "192.168.1.1").
MQTT_USERNAME MQTT username (e.g. "teslamate") - use with -l or -a.
MQTT_PASSWORD MQTT password (e.g. "etamalset") - use with -a.

Options:
-h Show this screen.
-l Use username to connect to MQTT server.
-a Use authentification (user and password) to connect to MQTT server.
-m Car model according to https://api.iternio.com/1/tlm/get_CARMODELs_list

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.

49 changes: 42 additions & 7 deletions teslamate_mqtt2abrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
TeslaMate MQTT to ABRP

Usage:
teslamate_mqtt2abrp.py [-hlap] [USER_TOKEN] [CAR_NUMBER] [CAR_MODEL] [MQTT_SERVER] [MQTT_USERNAME] [MQTT_PASSWORD]
teslamate_mqtt2abrp.py [-hlap] [USER_TOKEN] [CAR_NUMBER] [MQTT_SERVER] [MQTT_USERNAME] [MQTT_PASSWORD] [-m CAR_MODEL]

Arguments:
USER_TOKEN User token generated by ABRP.
CAR_NUMBER Car number from TeslaMate (usually 1).
CAR_MODEL Car model (from https://api.iternio.com/1/tlm/get_carmodels_list, e.g. "s100d" for a 2012-2018 S100D).
MQTT_SERVER MQTT server address (e.g. "192.168.1.1").
MQTT_USERNAME MQTT username (e.g. "teslamate") - use with -l or -a.
MQTT_PASSWORD MQTT password (e.g. "etamalset") - use with -a.
Expand All @@ -17,14 +16,14 @@
-h Show this screen.
-l Use username to connect to MQTT server.
-a Use authentification (user and password) to connect to MQTT server.
-m Car model according to https://api.iternio.com/1/tlm/get_CARMODELs_list

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

## [ IMPORTS ]
import sys
import json #TODO/TODELETE: Needed?
import datetime
import calendar
import os
Expand Down Expand Up @@ -62,9 +61,9 @@
if arguments['CAR_NUMBER'] is None: CARNUMBER = os.environ['CAR_NUMBER']
else: CARNUMBER = arguments['CAR_NUMBER']

#car model list here curl --location --request GET 'https://api.iternio.com/1/tlm/get_CARMODELs_list'
#TODO: is there a way to find this automatically from TeslaMate?
if arguments['CAR_MODEL'] is None: CARMODEL = os.environ['CAR_MODEL']
if arguments['CAR_MODEL'] is None:
if "CAR_MODEL" in os.environ: CARMODEL = os.environ["CAR_MODEL"]
else: CARMODEL = None
else: CARMODEL = arguments['CAR_MODEL']


Expand All @@ -85,6 +84,8 @@
"battery_range": "",
"ideal_battery_range": "",
"ext_temp": "",
"model": "",
"trim_badging": "",
"car_model":f"{CARMODEL}",
"tlm_type": "api",
"voltage": 0,
Expand Down Expand Up @@ -121,6 +122,10 @@ def on_message(client, userdata, message):

if topic_postfix == "plugged_in":
a=1#noop
elif topic_postfix == "model":
data["model"] = payload
elif topic_postfix == "trim_badging":
data["trim_badging"] = payload
elif topic_postfix == "latitude":
data["lat"] = payload
elif topic_postfix == "longitude":
Expand Down Expand Up @@ -202,6 +207,35 @@ def on_message(client, userdata, message):
client.on_connect = on_connect # Define callback function for successful connection
client.loop_start()

## [ CAR MODEL ]
# Function to find out car model from TeslaMate data
def findCarModel():
sleep(10) #sleep long enough to receive the first message

# Handle model 3 cases
if data["model"] == "3":
if data["trim_badging"] == "50":
data["car_model"] = "3standard"
elif data["trim_badging"] == "74":
data["car_model"] = "3long"
elif data["trim_badging"] == "74D":
data["car_model"] = "3long_awd"
elif data["trim_badging"] == "P74D":
data["car_model"] = "3p20"

# TODO: Handle model Y cases
if data["model"] == "Y":
print("Unfortunately, Model Y is not supported yet and should be set through the CLI or environment var.")

# Handle simple cases (aka Model S and Model X)
else: data["car_model"] = data["model"].lower()+""+data["trim_badging"].lower()

# Log the determined car model to the console
print("Car model automatically determined as: "+data["car_model"])

# If the car model is not yet known, find it
if CARMODEL is None: findCarModel()

## [ ABRP ]
# Function to send data to ABRP
def updateABRP():
Expand All @@ -217,7 +251,8 @@ def updateABRP():
print(message.topic)
print(message.payload)

## [ Main ]
## [ MAIN ]

# Starts the forever loop updating ABRP
i = -1
while True:
Expand Down