Fixing The VW Tiguan Home Assistant MQTT Km/h Error
Hey guys! Ever run into a pesky error message in Home Assistant when trying to integrate your VW Tiguan? Specifically, a "unit of measurement (Km/h) is not a valid unit of measurement" error? Yeah, it's a bit of a head-scratcher, but don't worry, we're going to break down what's happening and how to fix it. This guide focuses on the CarConnectivity-plugin-mqtt and the issues it has with Home Assistant. Let's dive in!
Understanding the Problem: The Km/h Unit Error
So, the main issue here is a mismatch in how Home Assistant expects to receive speed data and how the CarConnectivity-plugin-mqtt is sending it. Home Assistant, when dealing with speed, is expecting the unit of measurement to be in lowercase "km/h". However, the plugin is sending it as "Km/h". This seemingly small difference in capitalization is enough to trigger an error, preventing Home Assistant from correctly interpreting the speed data from your Tiguan. When the plugin sends this, it is not able to match, because HA expects the state, which is the speed, to be formatted as "km/h". The error occurs specifically within the MQTT integration of Home Assistant when it tries to process the discovery message from the plugin. This message contains details about the device and its sensors, including the unit of measurement for speed. Because of the unit not matching, the error prevents HA from integrating properly.
The error message you see in your Home Assistant logs highlights this perfectly: "The unit of measurement Km/h is not valid together with device class speed." This error message shows that the unit of measurement for the speed of your car does not match what HA is expecting, and it is failing to initialize that entity. It then prevents HA from recognizing the speed data correctly. This can lead to a variety of issues, such as incorrect speed readings, failure to display speed data in the HA interface, or even errors in automations and scripts that rely on speed information. This is something that has to be fixed in the source files, but it's not a difficult fix, and can easily be implemented.
The Root Cause: MQTT Discovery and Units
At the heart of this problem lies the MQTT (Message Queuing Telemetry Transport) protocol and the way Home Assistant uses it for device discovery. The MQTT integration in HA automatically detects and configures devices that publish data via MQTT. The plugin sends a discovery message to Home Assistant, which includes a configuration payload describing the device and its sensors. It's the unit_of_measurement key where the capitalization error resides, causing HA to reject the sensor configuration. If you're not familiar with MQTT, it's a lightweight messaging protocol that is perfect for constrained devices. It's designed to be efficient for devices with limited resources, making it perfect for connecting devices in your smart home to your HA system. The plugin uses this protocol to send the speed data, among other things. The error that we're talking about stems from how the plugin is formatting the data it sends via MQTT. To resolve this error, we need to modify the plugin to ensure that it correctly formats the unit of measurement for speed to match what Home Assistant expects. This involves changing the capitalization of "Km/h" to "km/h" in the configuration payload sent via MQTT.
Troubleshooting Steps
Before diving into the fix, let's make sure you've covered the basics. These steps can help pinpoint the problem and ensure you're on the right track:
- Check Your Home Assistant Logs: As you've already done, the logs are the best place to start. They provide detailed information about the error, including the specific message and the source of the problem. Make sure to review the logs to confirm that the error is indeed related to the unit of measurement for speed and that it's originating from the MQTT integration. You can do this by going to Developer Tools -> Logs in your Home Assistant instance.
 - Verify Plugin Configuration: Double-check your MQTT configuration within Home Assistant and the CarConnectivity-plugin-mqtt. Ensure that the MQTT broker is correctly set up and that the plugin is configured to publish data to the correct topics. If your MQTT broker is not correctly configured, this can also cause errors, so verify that your broker is running, and that your devices are correctly set up to use it.
 - Restart Home Assistant: Sometimes, a simple restart can resolve temporary glitches. Restart your Home Assistant instance and see if the error persists. This ensures that any cached data is cleared and that Home Assistant reloads the configurations correctly. A full restart of your HA instance will refresh all integrations, including MQTT, and allow Home Assistant to reprocess the discovery messages from the plugin. This is a very simple step, but is always a good start when troubleshooting.
 - Update Your Software: Make sure your Home Assistant core, frontend, and the CarConnectivity-plugin-mqtt are up to date. Updates often include bug fixes and improvements that may address the issue. You can update your HA core from the Settings -> System -> Updates page. Check for plugin updates within the plugin's configuration, or the plugin documentation. In some cases, updates to the plugin might have already fixed the issue, so ensure you have the latest version.
 
The Solution: Fixing the units.py File
Okay, so the issue is identified and now we need to fix it. The fix involves modifying the units.py file within the CarConnectivity-plugin-mqtt. The specific location of this file might vary depending on your setup, but it's typically located within the plugin's directory. Here's a general guide to fix the issue. Keep in mind that you may have to adjust the paths based on your installation.
- Locate the 
units.pyfile: Find the file. You'll need to access the file system of the device where the CarConnectivity-plugin-mqtt is installed. If you have the plugin installed on a separate device, you'll need to connect to that device via SSH or another remote access method. Once you're connected, navigate to the directory where the plugin is installed and locate theunits.pyfile. The exact location depends on your setup, but it is typically in the plugin's source directory. - Edit the file: Once you've located the 
units.pyfile, open it in a text editor. This is where you'll make the necessary changes to correct the unit of measurement for speed. Be careful when editing these files, since any incorrect syntax changes can break the plugin and cause more errors. - Find and correct the unit: Search within the 
units.pyfile for the line(s) that define the unit of measurement for speed. You should find an instance where the speed unit is defined as "Km/h". Change this to "km/h". In most cases, it is a simple find and replace operation. - Save the file: Save the changes to the 
units.pyfile. Make sure that you save the file with the same name and in the same location. After saving, you need to restart the plugin for the changes to take effect. If you're using a containerized setup, make sure you restart the container so that the changes are applied. - Restart the plugin and Home Assistant: Restart the CarConnectivity-plugin-mqtt and Home Assistant to ensure the changes take effect. After restarting the plugin and Home Assistant, check your Home Assistant logs again to see if the error is resolved. The logs should no longer show the unit of measurement error.
 
This simple edit ensures that the plugin sends the correct unit of measurement (lowercase "km/h") that Home Assistant expects. The file units.py is the file that needs to be modified, and it's most likely where the issue is. This approach is the most common solution. The next time the plugin sends the data, Home Assistant should recognize the speed data correctly, and the error should disappear.
Additional Considerations and Troubleshooting Tips
Beyond the core fix, here are some extra tips to ensure smooth sailing:
- Version Compatibility: Ensure that the CarConnectivity-plugin-mqtt version is compatible with your Home Assistant version. Check the plugin's documentation or repository for any compatibility notes.
 - MQTT Broker Configuration: Make sure your MQTT broker is correctly configured and accessible by both Home Assistant and the plugin. Check the broker logs for any connection or authentication errors.
 - Backup Your Files: Always back up the 
units.pyfile before making any changes. This way, if something goes wrong, you can easily revert to the original version. - Test Thoroughly: After implementing the fix, test the speed sensor in Home Assistant to ensure that it's working correctly and displaying accurate readings. Test it by driving your car around, and verify the speed displayed by the sensor matches your car's speedometer.
 - Check Plugin Updates: Keep an eye out for updates to the CarConnectivity-plugin-mqtt. The developer might release a fix for this issue in a future update.
 
By following these steps, you should be able to resolve the "unit of measurement (Km/h) is not a valid unit of measurement" error in Home Assistant and successfully integrate your VW Tiguan. If you're still having trouble, double-check all the steps and consider reaching out to the Home Assistant community for additional assistance. The community is an invaluable resource for solving complex problems!
I hope this guide helps you get everything working smoothly! If you have any questions or run into any other issues, feel free to ask. Happy automating! And there you have it, folks! Now go and get those cars connected to Home Assistant and start monitoring them!