Skip to content

EV Battery SoC in Home Assistant ‐ Example

MeatPi Electronics edited this page Oct 27, 2023 · 3 revisions

Introduction

In this example, we will learn how to set up Home Assistant to request the battery State of Charge (SoC) without using Node-RED. This example has been tested on an ORA Funky Cat vehicle and can also serve as a reference for requesting other Parameter IDs (PIDs) in addition to SoC. (credit to guhlandreas)

Setup WiCAN

  1. Enable Ap+Station mode, fill in your Home network ssid and password.
  2. Set protocol to elm327
  3. Enable MQTT
  4. Enable MQTT elm327 log
  5. Fill in your MQTT parameters as setup in Home Assistant.
  6. Click the submit button.

Logging OBD2 Request To MQTT

In some cases where we are uncertain about the specific PID required to request the battery State of Charge (SoC), we can log the PID request and response data. In this scenario, we assume that an application like "Car Scanner" is capable of reading the battery SoC when it connects to WiCAN in ELM327 mode. WiCAN will then publish all the OBD2 request and response data to a designated topic. To log these PID request and response data, we can use any MQTT client such as MQTT Explorer. This will allows us to easily log the communication between the vehicle's OBD2 system and "Car Scanner".

  1. Begin by installing MQTT Explorer or any other MQTT client of your choice.
  2. Open MQTT Explorer and create a new connection. Fill in the required MQTT broker parameters and then click on the "Connect" button.
image
  1. Using "Car Scanner" connect to WiCAN
  2. Click on "All Sensors", then Click on Battery SoC.(Car scanner will continuously request Battery SoC)
  3. In MQTT explorer, you will find a topic wican/xxxxxxxxxxxx/elm327 (xxxxxxxxxxxx is the device id), the data will look like this:

{"bus":"0","type":"tx","ts":21767,"frame":[{"id":1931,"dlc":8,"rtr":false,"extd":false,"data":[3,34,3,8,170,170,170,170]}]}

{"bus":"0","type":"rx","ts":21782,"frame":[{"id":1995,"dlc":8,"rtr":false,"extd":false,"data":[5,98,3,8,2,69,204,204]}]}

Setup CAN to JSON interrupter

WiCAN offers a very useful feature that allows you to read CAN frames and use mathematical expressions to calculate values, then publish them to MQTT in JSON format. Here's how to set it up:

  1. In the WiCAN configuration page, disable MQTT ELM327 logging.
  2. Consider the received CAN frame from the previous section, which looks like this:

{"bus":"0","type":"rx","ts":21782,"frame":[{"id":1995,"dlc":8,"rtr":false,"extd":false,"data":[5,98,3,8,2,69,204,204]}]}

  1. Add a filter like in the screenshot below, then click Add ID then Store button. The battery SoC is calculated using Byte 4 and Byte 5, ((2*256)+69)/10

ID: 1995 Name: BatterySoC PID: 3 Start bit: 0 (Not used) Bit Length: 1 (Not used) Expression: ((B4*256)+B5)/10 Cycle: 1000

image

  1. Click submit.

Home Assistant setup

To create a new automation in Home Assistant (HA) for sending OBD requests at a specified interval using the received tx frame as the payload, follow these steps:

  1. Open Home Assistant and navigate to the Automation Editor.
  2. Click on the "+ Add Automation" button to create a new automation.
  3. Configure the automation as follows:
  • Alias: Provide a descriptive name for your automation, e.g., "OBD Request Sender."

  • Trigger Type: Choose a trigger that suits your desired interval. If you want to send OBD requests every 5 seconds, select "Time" as the trigger type. Set the interval to 5 seconds.

  • Action: Add an action by clicking the "+ Add Action" button.

  • Action Type: Choose "Publish a Message to an MQTT Topic."

  • Service Data:

  • Topic: Enter the MQTT topic wican/xxxxxxxxxxxx/can/tx, replace xxxxxxxxxxxx with your device ID.

  • Payload: Set the payload to the received tx frame you mentioned. This should look something like:

{"bus":"0","type":"tx","ts":21767,"frame":[{"id":1931,"dlc":8,"rtr":false,"extd":false,"data":[3,34,3,8,170,170,170,170]}]}

image

  1. The result will be something like:

image