diff --git a/README.md b/README.md index d24662e..dd4570a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -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. - diff --git a/teslamate_mqtt2abrp.py b/teslamate_mqtt2abrp.py index 7501e24..c154bd1 100644 --- a/teslamate_mqtt2abrp.py +++ b/teslamate_mqtt2abrp.py @@ -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. @@ -17,6 +16,7 @@ -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. @@ -24,7 +24,6 @@ ## [ IMPORTS ] import sys -import json #TODO/TODELETE: Needed? import datetime import calendar import os @@ -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'] @@ -85,6 +84,8 @@ "battery_range": "", "ideal_battery_range": "", "ext_temp": "", + "model": "", + "trim_badging": "", "car_model":f"{CARMODEL}", "tlm_type": "api", "voltage": 0, @@ -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": @@ -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(): @@ -217,7 +251,8 @@ def updateABRP(): print(message.topic) print(message.payload) -## [ Main ] +## [ MAIN ] + # Starts the forever loop updating ABRP i = -1 while True: