Car Telemetry in Home assistant

Wouldn’t it be cool if you could get live data from your car and feed that into Home Assistant?

Well it seemed cool to me and now that I am writing about it, I am forced to come up with some good reasons as to why you dear reader, might do this. So I will jot down a few reasons here. But for me (and probably you), the real reason we do these things is for the joy of investigating how to do it and then making it happen.

Reasons for integrating car telemetry into home assistant:

  • Always have an up to date map position of where your car (or the family car) is and get notifications when it arrives home.
  • Store data on the performance of your car journeys, such as fuel used per trip, CO2 used per trip, engine performance etc.
  • Chart the performance of your car over time using ODB2 parameters such as LTFT,STFT an MAF and other metrics and warn of possible engine faults before they get fail.
  • Keep a log of car journeys.
Screen shot from Grafana showing a recent car journey

Where to start

Firstly, you are going to need the right hardware. This is essential to get a reliable data feed into Home Assistant which doesn’t fail (very often) and means you can just forget about things and they will just happen. This is not necessarily a cheap project, although I will offer some alternatives as to how you can lower the price or use your own smartphone rather than purchasing expensive hardware for your car.

Hardware needed:

  • An ODB2 Bluetooth or WIFI car transmitter. Nearly all modern cars in the EU and USA have a service port somewhere on or under the car dashboard which provides live data from many sensors within your car engine and body.  I use a cheap ODB2 receiver to grab the sensor information and transmit via bluetooth to my android device. Its also important for this project to pick the right ODB2 receiver.  You need one which goes into sleep mode automatically and even more importantly, wakes up when it receive a Bluetooth connection attempt. Most cheap ODB2 receivers do not do this, so I am recommending the one I have working which is the Vgate Icar Pro.  Click the link for an amazon reference.
  • An android device (v8 or later). I have two options here, which have different advantages:
    • Expensive option: swap out your car stereo
      • Use an in-car entertainment system based on android or use your android smartphone. I have a Nissan Qashqai and swapped out my factory installed care stereo for an android car stereo from a company called Joying. (https://www.joyingauto.com). Joying are well known for their good value after-market care stereos based on android.  This is another subject in its own right, so I cannot advise you here on what the best after market car stereo for your own vehicle might be. However, I do think Joying probably have a solution for your particular car.  There are many advantages of replacing your basic car stereo for an android stereo, but for the purposes of this project it provides dedicated hardware in which to host the ODB2 software (Torque).
      • An in car mobile data usb dongle.  I use this model in the UK, but you can use whichever suits your country. Obviously, what we are doing here, is providing a mobile data connection so that information can be sent back to your home. As a side benefit, you get in car wifi.  In the UK, it is also simple to purchase a no contract data sim with a set amount of data.  Here is an example. The key thing to look for is a data sim which allows your data to be spread over 12/24 months. By the way, this project will use very little data. The bulk of my data use on the usb dongle, is to stream internet radio, you tube and run google maps in the car.

Tip: Some of the newer Android car stereo models from Joying now include a built in 4g mobile module.

  • Cheaper option:
    • An android smartphone. Whilst this option will work, it only works when your smartphone is in the car. If you share your car, then this is not ideal to capture every journey.

Software needed:

  • Llamalab Automate (available on google play). If you have never heard of this app before, it is a visual scripting tool which allows you to do a lot of clever things on your android device. It is similar to a product called Tasker, but I really like the visual scripting language. Also if you choose this product you will be able to download my pre-prepared script to grab the ODB2 data and post it to Home Assistant.
  • Torque lite (or pro). This is a very popular application which runs on android devices and connects to ODB2 receivers. It can be used to show the data in the application, but for this project we will be using its data logging capabilities.
  • MQTT Broker (like mosquitto). It’s likely that your Home Assistant server is already running an MQTT server for other devices, but just in case it is not, you need to run an MQTT server and configure in Home Assistant.

Step 1 : Installing the ODB2 adapter and Torque software:

So assuming you have the components I have described above, the first thing to do is install the ODB2 dongle into you car ODB2 Port. Your car manual should tell you where the ODB2 port is if you cannot find it. Once found, it is simply a matter to plug it in. I should warn you that you do this at your own risk. I can’t be held responsible for any damage resulting from this. I and many people have installed these devices without any problems. If you purchased the model I recommended, then it should be safe to leave this device in the USB port permanently. I have had it in place for over a year. When disconnected, it will power down and will not drain your car battery.

Installing Torque Application:

Turn on your car ignition so there is power to the ODB2 receiver and then on your android device, go to the bluetooth setting and search and pair to the ODB2 receiver.

Now install Torque lite (or pro) onto your android device. The app will ask you to complete some information to profile your particular car.  If you are lucky the app will automatically connect via Bluetooth to your ODB2 adapter. If it does not connect, go to the Torque settings and check the ODB2 adapter settings. The front screen always shows a tachometer, and when there is a successful Bluetooth connection, the current revs of your engine will be shown.

Now that we have a successful connection, we need to set up the logging. Go to ‘settings’ and select ‘Data logging & upload’.   Complete the following settings below:

Select what to log:

The first thing you are going to do is select the data set that you want to log. These datasets are called PID’s in the ODB2 protocol.  The specific dataset (PID’s) available from your car will be dependent on what the cars manufacturer is willing to provide over the ODB2 interface. The Torgue app makes this simple for you by highlighting the PID’s available in light green. I recommend you choose 1 or 2 PId’s for basic testing at first.

Synchronous logging file log interval: off

File logging interval: Choose 5 seconds.

Log when Torgue is started: Yes

Only when ODB connected: Yes

Automatically log GPS: Yes

Rotate log files: No

Ignore any other options and go back to the main settings and choose ‘Dash installation settings’ and complete the following settings below:

Start Torque on reboot: Yes

Ok, to recap. We have installed the ODB2 adapter and paired it to the android device. We have installed the Torque app and configured it to connect to the ODB2 adapter and log some data continuously to always run in the background (even after reboots).

Step 2 : Configure Home assistant access and security:

For the next step we need to prepare Home Assistant to receive data from an external device and to do this we are going to use the ‘services api call’ available in Home Assistant. But first, a word on accessing Home Assistant through your home network router.

Clearly Home Assistant needs to be accessible from the internet so that your car can communicate to it. But we don’t want any bad actor hacking your Home Asistant server, so we need to implement some security. I’m going to assume that as a user of Home Assistant, you are already aware of how and why you should implement SSL security to access your server. If you have not yet implemented security, this project will still work but will be, well , less secure.

We are going to communicate to Home Assistant using the services api. For security, we are going to set a password which must be provided for any external device connecting to the API. Choose a nice long one. This is defined under the auth_providers section. See example below.

  auth_providers:

    – type: legacy_api_password

      api_password: [your_api_password]

Update on Security. Home Assistant have made several advances in implementing security in recent versions and you may choose an alternative secure way of posting data to Home Assistant using ‘long lived tokens’. The Llamalab Automate script I have created now supports long lived access tokens. Simply create an access token in Home assistant and copy the string provided for later use.

Step 3: Installing Llamalab Automate:

On your android device, install Llamalab Automate from the Google play store. Then go to the Llamalab community section in the app and search for a script called ‘Car Telemetry to Home Assistant’.  Download the script posted by me (Robert Cunningham) and open it.

We are going to instruct Llamalab Automate to send data to your particular Home Asssitant instance. To do this we use the HTTP request block in Llamalab Automate. An HTTP request to home assistant would look like this:

https://[Your Home assistant server domain address]/api/service/mqtt/publish

Note: my Home assistant server connection is SSL Encrypted. Yours may or may not be. I’m not judging.

Llamalab Automate presents scripts in a visual friendly block diagram. Each block represents a task and then is linked to the next task by a line. The code is all visible for you to review. Here is what my script will do:

  • At startup, get some information from you to communicate with your own instance of home assistant and store it on your device.
  • Wait until a new log file called Torque.log appears on your android device. (when a file is created we know that the Torque app has connected to your ODB2 receiver and is recording data)
  • Scan the header of the file to obtain the names of each PID you selected to log and store it.
  • Scan the last line of the log file for the latest PID dataset (which is appended to the log every few seconds by Torque).
  • Format the data into a JSON format message ready for acceptance by Home Assistant. (Home Assistant will pass this data to the MQTT server).
  • Construct a HTTP Post request to the Home assistant services API, containing your user authentication and the payload.
  • Confirm the data was received by Home Assistant.
  • Repeat sending the data until the car engine is switched off (journey end).

There are a few additional feature which you can also benefit from in my script:

  • Scan the immediate area for bluetooth devices. (I use this to report who is in the car, but you don’t strictly need this) Scans the immediate area for bluetooth devices. (I use this to report who is in the car, but you don’t strictly need this)
  • Sends information in the correct format to be used by the ‘owntracks’ integration to Home Assistant. This allows your car to show up on the map in home assistant.

Step 4a: Inserting API password into Llamalab Automate script:

NOTE: The llamalab automate script is now configured to use long lived access tokens in Home Assistant. Proceed to step 4b. Step 4a is only if you really need to use an api password in Home Assistant and you are capable of modifying the llamalab script.

There are a few parameters which need altering in the Automate script for your unique installation. Some of the data is captured when you first run the script but the API password you created earlier needs to be placed directly into the correct blocks as this password is only stored on your own device (and not the script iteslf) for your own security.

Edit the automate script and you will see all the blocks. In the menu, at top right click the 3 dots and select the option called ‘Goto’. This will take you to each block we need to edit. You need to edit all blocks which make a ‘http request’. in the automate script you downloaded these will be blocks 1359,1382,1379 and 1443. Go to each block and open it.

You will see it is pre-populated with the data and formatting needed to send a message. Scrolling down, you will see an option called ‘Basic authorisation account’. Select this option and select ‘Add account’ Give it a name and then enter the API password you created earlier in the Home assistant configuration file.

Repeat this for the other blocks, but this time choosing the account name you just created.

Note:Your api_password is secured in your android device, so if you move this script to a different device, it will not work until you create the password in the new device. This protects your security as the script can be copied, but the password cannot.

Step 4b: Inserting long lived access token into Llamalab Automate script:

If you are using a long lived access token, simply run the Automate script and when asked, paste in the access token you noted down from Home Assistant earlier. It should read as follows … Bearer xxxxxxx….(long list of characters). If this is not the first time you have run the script, goto the /torqLogs directory on your android device and delete the file called car_config.json

Step 5: First test:

OK, this completes the basic setup stage. We have configured Home Assistant to accept secure API messages and publish them to your internal MQTT server. We have set up you car device to connect to the Cars ODB2 port, receive the data, log the data and then using the Llamalab Automate script to transmit the data in a suitable format to Home Assistant. With these basics in place, you should be able to test it out.

Run the Torque App and the Automate Script. When you first run the Llamalab Automate script, it requests the name you want to call your car and the domain name of your Home assistant server. It stores this data in a file called ‘car_config.json’ in a directory called ‘torqueLogs’. if you need to change this infomation, delete this file and re-run the automate script. If everything has gone well you should be able to turn on you car engine and data should start flowing. Of course, I’m sure you will have to work out any little bugs in your setup. With the Automate script running you will begin to see messages in the Automate script log. If you are seeing messages like, ” Rest Post success:Torque data:200″, then this means that the messages are getting through to your Home Assistant server correctly. If you a seeing exception messages or “catch Failure” messages, you will need to debug the problem. Some tips from me are below:

  • Remember that if you are not using SSL security on your Home Assistant server then the http blocks we looked at earlier need to be ‘http’ requests, rather than ‘https’ requests.
  • Check Torque is creating a log file called Torque.log and that the LlamaLab Automate script is picking up that log file from the correct location on your android device.
  • Check in the Llamlab Automate Log that Home Assistant is returning a HTTP post response of value 200. This essential means that Home assistant received the data and the format of the data was acceptable to it.
  • Check your MQTT server and search for the topic called ‘devices/[car name]/torque’. This should now contain a JSON packet of Torque data from your car.

I’m sure there might be a few stumbles along the way, but essentially it should now automatically start reporting telemetry from your car into Home assistant.

Step 6: Home Asisstant Configuration

The last step should be familiar to an Home assistant User. The Home Assistant configuration needs a sensor to make the data available. This is easily done now by adding the following lines to the configuration.yaml file.

  • platform: mqtt
    name: “Quashqai Torque”
    state_topic: “devices/quashqai/torque”
    value_template: “{{ state_attr(‘sensor.quashqai_torque’, ‘GPS Time’) }}”
    json_attributes_topic: “devices/quashqai/torque”

Note: Since I drive a Quashqai, this is what I have named my sensor. You should replace all with the name you gave your car in the Automate script.

Screenshot of the Car telemetry sensor in Home Assistant

Finally

So now we have the data in Home Assistant. I will leave you to decide how you present the data and what ingenious automations you come up with. I share my setup below to give you ideas. In a later post, I’ll show how I record the data to a database and present it in a data visualisatrion tool.

Some longer term issues to note:

It’s been fun learning how to do this project, but I should make you aware of some feedback over the longer term.

  • The android car stereo and ODB2 Bluetooth adapter have performed well without any intervention from me. It took a while to craft the scripts to make it reliable so please benefit from my learning.
  • If the car engine is turned off and on in quick succession, you may get some broken trip reports as Torque App has cleared the log and started a new journey. So this can happen when you make a fuel stop. The data will still be captured but will be reported in Grafana as two trips.
  • Llamalab Automate may fail on occasion to send data to Home assistant because your mobile network is unreliable. I have made the Automate script resilient so the script should not crash, but you may get some gaps in the data.

12 thoughts on “Car Telemetry in Home assistant”

  1. Does not work. Do with the help 4a option. But the message “Login attempt or request with invalid authentication” comes from the Home Assistant”. Need help!

    1. If you are using ‘api passsword’ in home assistant, then you are getting this message most likely because your
      config file doesn’t match with the password you have put in the llamalab script. Try constructing a simple http request in your web browser first. Also, note that home assistant prefers you to use long lived access tokens now, which is option 4b. The latest version of the llamalab script is using the long lived access token method.

      1. sorry, my mistake, I use 4b method with long lived access token and have this message “Login attempt or request with invalid authentication”

          1. Tried. Not help. All the same, HA writes the error that I wrote above. In the HA settings, I don’t have any restrictions on networks. I use DuckDNS and SSL. If it helps, here is a link to the logs. I changed the real address there, of course. https://yadi.sk/d/_esUNlcqoLBEBA

  2. So your flow log shows a 401 error, which means you are connecting successfully, but HA does not like your authorisation code. I looked in your car_config.json file and see that your token is not correct. It needs to have “Bearer xxxxxx…” at the beginning. That’s bearer and then a space and then your token key. I think it should work for you then.

  3. Could you please send me to my email (gp_astron@mail.ru )how should correct car_config.json file looks like. I don’t know word “Bearer” and Google translate didn’t help me in understanding. Sorry 🙂

  4. Congratulations, incredible project. It works perfectly for me.
    Now I have to see how to automatically start Torque and Automate when I turn on Android Car Radio.

    1. i have a new automate script which I will publish soon which should do this for you. I found the latest version of Torque app was not auto starting anymore. ‘Llamalab Automate’ scripts should restart after rebbot if you choose that option in settings.

Leave a Reply

Your email address will not be published. Required fields are marked *