Nissan Leaf in Home Assistant

Robert Andresen Home Assistant, Internet of things, Tutorials 2 Comments

I found this:¬†https://github.com/glynhudson/leaf-python-mqtt, and thought it was a simple task… But nooooo. I have very little experience with MQTT, other than the basics, so this took some time ūüôā

This article has been re-written 2018.01.22. I have added more details (mostly copy-paste) from glynhudson/leaf-python-mqtt on Github. I will advise you to follow the readme on Github instead, to get the newest update.

My setup

Lenovo with Ubuntu 16.04 and Home Assistant, but this was also testet on my last setup with Rasbian on RaspberryPi.

I installed leaf-python-mqtt in my home-folder:
/home/<user>/leaf-python-mqtt

 

Install Requirements

Requires python 2.7.9 & python pip

Install carwings python lib

See pycarwings2 repo for more info

Install other python libs

pip may require sudo.

I had to install one at the time, like “pip install schedule” and so on. The “time” component is default (included in python), so that one gave me an error message when trying to install it.

Install Leaf-python-mqtt

Clone this repo:

Create config file using default file as a template, edit config to suit requirements. You will need to enter your CarWings / NissanConnect EV username (not e-mail) and password.

See below for more MQTT broker details.

The default config is setup for Nissan Leaf cars in Europe with region code NE, change the region code to one of the following if you are in a different region:

Test script by running

Here is an example of the feedback:

Here is the data output from the MQTT stream:

The json from leaf/status/raw is prettified to make it easier to see.

 

Run script as system service

Create Systemd service

Create systemd service, assuming repo was cloned to /home/pi folder on a RaspberryPi, adjust paths if needed

Set permissions:

Reload systemd then enable the service at startup:

Check service status and view log snippet with:

 

MQTT broker

There is an included MQTT broker in Home Assistant, but I struggled a lot to get this working. You can read the docs here if you want to try: https://home-assistant.io/docs/mqtt/broker/.

One of the problems with the MQTT-broker in HA was understanding what was going on. I tried installing multiple MQTT-clients to monitor the topics. All of them connected just fine, but there was no stream of data. It did worked somehow, as I got the Carwings-data through MQTT to HA. I also tried to push an update for the car, but nothing happened.

I ended up registering an account on CloudMQTT. From there I created a homeassistant user and added the broker URL, username, password and port in /home/<user>/leaf-python-mqtt/config.ini.

MQTT client

Sometimes it can be nice to see if the messages is actually being sent over the MQTT stream. For that you need a client. There is a lot of client, but somehow I have problem with most of them.

Here are some clients you can try: https://www.hivemq.com/blog/seven-best-mqtt-client-tools

HiveMQ is a online service, but this one gives me a socket error. MQTT.fx starts on my computer, but the window doesn’t show. mqtt-spy does not start. MQTT Lens show so big icons and have a GUI bug.

I ended up installing a client on my ubuntu:

 

Troubleshoot

Error: “KeyError: BatteryStatusRecords”

Solution: Start the Carwings app on your phone. Log out and in again, and update the car status. After I did this, I got this result in my console:
 

Error: Connection refused

Solution: Your HA or MQTT component/broker is probably not running correct.

 

Add sensors to HA

Docs: https://home-assistant.io/components/sensor.mqtt/

I have created a separate sensor.yaml file, where I added this:

(Remember to add/check correct indends if you copy/paste)

 

(Update: I dropped the sensor.leaf_battery_remaining and added round(2) for the battery_percent)

Automation

After getting MQTT to work as expected, I could do some automation. The script below will fetch new status from the car at 21:00, then check if the car is NOT_CHARGING and the battery is below 50% at 21:30. If this criteria is met, it will send a push-message to remind me.