Home Assistant – Leaf-MQTT

Robert AndresenHome Assistant, Internet of things, Tutorials Leave a Comment

For some reason the Leaf-component in Home Assistant has not been working for some time. Since November 2020 according to the community forum.

UPDATE: Before you read any futher… Home Assistant bumped pycarwings2 to 2.10 in the last 2020.12.2 release. I actually found this out just when I was going to add the data from the solution below, into HA.

For documentation-purposes, I will still publish the article – as it can be used for other apps or automations – or maybe the next time it breaks in HA.

Today i decided to try a temporary fix to make HA get my Leaf information.

I downloaded some code from Github: https://github.com/freiwerk/leaf-mqtt

I copied the code to one of my Linux/Docker-servers and edited the config.ini file.

username and password are what you use to connect with on your Nissan EV app.

You will need an MQTT-broker to send the data over. If you already have this, you will know what to type in. If you do not, you can add a default MQTT-broker add-on under Supervisor in HA (or you find details there if you have one).

Then build the docker image from the Dockerfile.

# cd /var/www/leaf-mqtt-master/
# docker build -t leaf-mqtt .
Sending build context to Docker daemon  17.41kB
Step 1/10 : FROM python:3.7
3.7: Pulling from library/python
6c33745f49b4: Pull complete
...
a8c72204fafe: Pull complete
Digest: sha256:bdd950df83006ce9e7f7f9cb878a2ca72e945f3cbd37faef07509c8510d22b....
Successfully built aa39e349bde8
Successfully tagged leaf-mqtt:latest

…and run the docker image. Add a -d flag to run as detached (in background).

# docker run leaf-mqtt
INFO:root:Startup leaf-mqtt: 2021-01-04 16:10:22
INFO:root:Unable to find setting USERNAME in env
INFO:root:Unable to find setting PASSWORD in env
INFO:root:Unable to find setting MQTT_HOST in env
INFO:root:Unable to find setting MQTT_PORT in env
INFO:root:Unable to find setting MQTT_USERNAME in env
INFO:root:Unable to find setting MQTT_PASSWORD in env
INFO:root:Unable to find setting MQTT_CONTROL_TOPIC in env
INFO:root:Unable to find setting MQTT_STATUS_TOPIC in env
INFO:root:Unable to find setting NISSAN_REGION_CODE in env
INFO:root:Unable to find setting API_UPDATE_INTERVAL_MIN in env
leaf-mqtt.py:42: DeprecationWarning: The SafeConfigParser class has been renamed to ConfigParser in Python 3.2. This alias will be removed in future versions. Use ConfigParser directly instead.
  parser = SafeConfigParser()
INFO:root:Loaded config file /conf/config.ini
INFO:root:Prepare Session
INFO:root:Login...
INFO:root:Start update time: 2021-01-04 16:10:22
INFO:root:Connected to MQTT host 192.168.1.185 with result code 0
INFO:root:Suscribing to leaf control topic: leaf/control
INFO:root:Publishing to leaf status topic: leaf/status
INFO:root:get_latest_battery_status
INFO:root:date jan. 04,2021 12:16
INFO:root:date 2021/01/04 11:16
INFO:root:battery_capacity2 11
INFO:root:battery_capacity 11
INFO:root:charging_status NOT_CHARGING
INFO:root:battery_capacity 11
INFO:root:battery_remaining_amount 12
INFO:root:charging_status NOT_CHARGING
INFO:root:is_charging False
INFO:root:is_quick_charging False
INFO:root:plugin_state CONNECTED
INFO:root:is_connected True
INFO:root:is_connected_to_quick_charger False
INFO:root:time_to_full_trickle None
INFO:root:time_to_full_l2 None
INFO:root:time_to_full_l2_6kw None
INFO:root:leaf_info.battery_percent 100.0
INFO:root:End update time: 2021-01-04 16:10:31
INFO:root:publishing to MQTT base status topic: leaf/status
INFO:root:End update time: 2021-01-04 16:10:35
INFO:root:Schedule API update every 10min

Now the data will be available in the MQTT data-stream. I didn’t get so far to add the MQTT-topics in HA before i saw the update – but there is a lot of info out there in how to do this.

I used MQTT Explorer to monitor the data.

Troubleshoot

I only had one error along the way. The solution was to write the correct password for the MQTT-broker in the config.ini file 🙂

INFO:root:Login...
INFO:root:Start update time: 2021-01-04 16:08:13
INFO:root:Connected to MQTT host 192.168.1.185 with result code 5
INFO:root:Suscribing to leaf control topic: leaf/control

Here is the log from the MQTT-broker. Something tries to connect, but can’t.

1609775263: New connection from 192.168.1.246 on port 1883.
[INFO] found robert on local database
1609775264: New client connected from 192.168.1.246 as mqtt-explorer-606d527d (p2, c1, k60, u'robert').
1609775564: New connection from 192.168.1.210 on port 1883.
[WARN] Not found robert on local database
1609775564: Socket error on client <unknown>, disconnecting.
1609775565: New connection from 192.168.1.210 on port 1883.
1609775565: Socket error on client <unknown>, disconnecting.
1609775567: New connection from 192.168.1.210 on port 1883.
1609775567: Socket error on client <unknown>, disconnecting.
1609775571: New connection from 192.168.1.210 on port 1883.
1609775571: Socket error on client <unknown>, disconnecting.
1609775579: New connection from 192.168.1.210 on port 1883.
1609775579: Socket error on client <unknown>, disconnecting.
1609775595: New connection from 192.168.1.210 on port 1883.
1609775595: Socket error on client <unknown>, disconnecting.
1609775627: New connection from 192.168.1.210 on port 1883.
1609775627: Socket error on client <unknown>, disconnecting.