Nissan Leaf Component(s)/Platform

Hi, I am trying to setup the leaf2mqtt docker container by @mitsumaui and am running into some issues. Immediately upon starting the container, with all the correct environment variables (see Docker compose code below), I get a message saying an error occurred while logging in, and to verify my leaf type, username, and password.
I’m located in Canada and using NissanConnect EV with my Leaf 2022. I can access my Leaf no problem directly on Nissan’s site (https://canada.nissanconnect.com/).

Any ideas on what may be going on? Thanks in advance!

Docker compose file (some info redacted for privacy):

leaf2mqtt:
    container_name: leaf2mqtt
    image: ghcr.io/k8s-at-home/leaf2mqtt:latest
    restart: always
    environment:
        - [email protected]
        - LEAF_PASSWORD=PASSWORD
        - LEAF_TYPE=newerThanMay2019
        - MQTT_HOST=IP
        - MQTT_PORT=PORT
        - MQTT_USERNAME=USERNAME
        - MQTT_PASSWORD=PASSWORD
        - MQTT_BASE_TOPIC=leaf
        - UPDATE_INTERVAL_MINUTES=360
        - CHARGING_UPDATE_INTERVAL_MINUTES=360
        - LOG_LEVEL=INFO

Docker container log:

INFO: 2021-10-31 17:59:23.188737: main: Logging in.
WARNING: 2021-10-31 17:59:28.543852: main: An error occured while logging in. Please make sure you have selected the right LEAF_TYPE, LEAF_USERNAME and LEAF_PASSWORD. Retrying in 5 seconds.
1 Like

hi,
i am new on HomeAsistant and i use the nissanleaf integration

i have only
-charge
-range ( with and without AC )
-pluged
-charge
-climate switch

Before HA, i use jeedom since many years and the nissan leaf plugin get additionnal informations :slight_smile:


(it’s in french, but easily understandable :stuck_out_tongue: )

Question 1 : it is possible to add some missed informations ?
BatteryRemainingAmountWH is important for me , i use it to calculate SOH and graph it over months

Question 2 : i ave tried Homeassistant begining’21 , and i have my car on the map , i did n have it now, this function was diseapear ?

Question 3 : did my car compatible with leaf2mqtt ?
european Leaf40 model 2018 with 7inch GPS
in France, leaf before 2019 ( 7inch GPS SD card ) use Nissan connect EV ( like carwings for 24/30 ) , and 2019+ models ( with 8inch GPS OTA no SD ) use new app Nissan connect services

last question (4) is is possible to install kubernetes on hassio OS ? (mine run n odroid XU4) ( i already have helm and mqtt ) , it s a requirement for leaf2mqtt …

thank s

What you’re saying seems a bit muddled and confused…

I have a 2016 30kWh Leaf in the UK, this model has the later Nissan Connect EV branded head unit not the older Carwings branded head unit (which was discontinued in 2015) and uses the Nissan Connect EV iOS app.

However I’m using this integration in Home Assistant (the same one you linked to) and it works fine with my car. (I’m wondering if you’re confusing the 3rd head unit generation which was released in 2018 about 6 months into the Leaf 2.0 generation which uses a different iOS app)

I’ve been working on a dashboard widget and automations that implement a customisable charge limiter - something the car lacks ever since the 80% option was removed in 2015 onward models… it works in conjunction with an EVSE with a smart switch in it.

I wanted to share what I have so far with others who might be interested as I have it working pretty nicely now, but I’m unsure whether this is the best thread to post to due to the confusion about different versions of the integration and which belongs in this thread.

Is there another thread that covers the “official” Nissan Leaf integration ? Or should I just start my own thread ?

Welcome!
Nice to find you joining HA. Your posts on SpeakEV are very in-depth and insightful.

This thread IS for the official HA Nissan Leaf integration (the one you linked), this thread is in the official Development category.

But perhaps your automation idea/implementation can be adopted for all EV’s charged by a controllable EVSE, so a dedicated thread in Share your Projects! may help more people.

If you do start a new thread, please do post a link here, because some people like myself only subscribe to certain threads.

Thanks for the welcome. :slight_smile:

I’m still finding my way around the forums here but I think you’re right - I’ll share what I’ve come up with in the Share my Projects section, that does seem like a more appropriate location for it.

Aside from that I do have a bug report, a suggestion for a minor tweak to the integration (specifically the value of PYCARWINGS2_SLEEP which is suboptimal) and also a query about whether last update times (from Nissan) are properly exposed by the integration - but I’m not sure who is currently maintaining the integration, whether it’s a group effort, and what the best way to pose these points is as I’m not sure opening git issues etc are appropriate at this point.

Here you go. It’s pretty long, but you knew it would be I’m sure. :slight_smile:

1 Like

Ok so a few things I’ve noticed about the Leaf integration, maybe someone can comment or maybe one of the integration devs can cast an eye over these…

  1. As I alluded to earlier, PYCARWINGS2_SLEEP which controls how long to wait after sending a request to poll again to see if the thing asked for has been retrieved from the car is set to 30 seconds and there are also three retries set.

Before using Home Assistant I’ve used pycarwings2 directly a reasonable amount in my own python scripting and one thing I learnt is that the minimum time that it takes the car to respond when everything is going perfectly (good connection to the car, Nissan servers behaving etc) is pretty much bang on 30 seconds.

So if the initial delay before checking data has returned is 30 seconds it’s on a knife edge whether the data will be ready or not, and in the vast majority of cases the data will not be ready after a 30 second wait which will result in another 30 second wait before a 2nd attempt. So in the majority of cases it will actually end up polling for the data twice and taking 60 seconds to retrieve it.

Using pycarwings2 directly in the past I found empirically that 40 seconds was the ideal initial wait period - long enough that the vast majority of the time the data will be retrieved on the first try without having to do a retry, but not so long that you’re waiting unnecessarily long. In fact if only one retry is needed it only takes 40 seconds to get the data instead of 2 x 30 waits for a total of 60 seconds, so it is actually faster most of the time.

When the servers are heavily loaded (depending on time of day) I’ve found it can occasionally take up to about 1 minute 10 seconds for valid data to be available, with 3 retries and 30 second delays this will use up all three retries and take 1 minute 30 seconds to get the data, with 40 second delays it would use only two of the three retries and retrieve the data in 1 minute 20 seconds and still have one retry up its sleeve, so also a bit faster in that scenario.

Could I humbly suggest that the default sleep value be increased from 30 seconds to 40 seconds ? :slight_smile: (or if someone can suggest how I can adjust that value on my local installation in a way that won’t get overwritten by future updates that would be great)

  1. Switching climate control on/off does work but returns an error. For example if I use the toggle switch through the UI (switch.leaf1sbyr_climate_control in my case) I see a message about 3 seconds later saying “Failed to call service switch/turn_on”, or “Failed to call service switch/turn_off” respectively, regardless of the fact that it does in fact turn the climate control on and off. This error always occurs after about 3-4 seconds, which is far too soon for it to know whether the API call was successful or not let alone get any feedback from the car.

When using a toggle in the UI it’s not a major problem but I realised when trying to call this from a script using call service switch.turn_on that the error also causes the calling script (or automation) to fail, hence further steps in the script are not executed even though the climate control did actually start in the car.

Yes I know there are ways around this by making the call run asynchronously but this seems like a kludge. Should the success/fail status of the change of switch status be reporting whether the climate request was successfully enqueued at the Nissan servers (but not necessarily actioned) or is it actually intended to poll the car for data and confirm whether climate control did actually start ? Either way it doesn’t do either of these at the moment. Possibly related to this error it also doesn’t poll the car for updated status after sending a command to start or stop climate control.

Strangely while there is a pop up error to say that it failed to call the service there is no error in the log even with debugging for the integration enabled, just “2021-12-15 16:55:00 DEBUG (MainThread) [homeassistant.components.nissan_leaf] Requesting climate turn on for MY_VIN”

  1. This is really just a question - in the code it looks like after 3 retries with no updated data being retrieved from the car the last known values from the server are used instead. As the comments in the script say this is “better than nothing” as that data may have been sent voluntarily by the car at some point such as turn on or shutdown, however is there any way for me to know whether the data retrieved is cached data from the Nissan servers or freshly polled data from the car ?

I initially assumed that the updated_on attribute would be the datetime that the data was pulled from the car (which is available in the API responses) however now I’m not so sure and it looks like this is just the last time that the Leaf integration queried the Nissan servers successfully, regardless of whether the data from the car is fresh or not. In other words its the time of the server query but not necessarily when that data came from the car. Is this the case ?

If so something that would be very useful would be to have another attribute which is the exact date and time that the data came from the car - and this data is available in the API calls but doesn’t appear to be exposed at the moment by the integration.

One reason this might be needed for example is trying to extrapolate a charge end time based on previously time stamped SoC values during charging - something I talk about wanting to do in the thread I linked.

However if there is no guarantee that the updated_on attribute represents the time the data was retrieved from the car and could sometimes be stale cached data from the Nissan servers, such extrapolation of the charging curve cannot be safely implemented…

Hello!

I recently added support for 2018 and older Leafs in GitHub - yp87/leaf2mqtt: Pull in data from the Nissan Connect platform for the LEAF and publish over MQTT
You can also get the docker image here: Docker Hub
suggested tag: yp87/leaf2mqtt:latest

You can create issues in my Github if you encounter an issue.

Regards

2 Likes

I’ve just put in a couple of PRs for those issues:

I’m not sure about the latest update times from the servers. Needs more investigation.

2 Likes

This looks great. dartcarwings is well maintained and works on more modern leafs. For the moment I’ll also keep pycarwings2 plodding along while I have a 30kWh leaf.

Thanks for doing this! I was able to get it to work with my 2022 USA Leaf. I had to use LEAF_TYPE="olderUSA" (rather than "newerThanMay2019") which is a bit funny for a new car. Hopefully this is useful info to someone.

I was a bit thrown by the fact that the official Nissan Leaf integration does not work with cars made in the last few years, as some have noted in this thread. The documentation should definitely reflect this. Hopefully someone who understands the compatibility well can update the documentation. And then some version of leaf2mqtt should eventually be rolled into it!

1 Like

hello @wtadler, you can try with the “My Leaf” application and see if you get the same result, leaf2mqtt is using the same libraries as this app and are made by the same author.

I have been using the Nissan Leaf plugin for over a year, since I bought my 2018 Leaf (40kwh, UK model). It works well a lot of the time - updating the status as in the configuration, starting the AC from a Lovelace button, etc.

However every few days it stops getting updates. Calling the script to get an update doesn’t get a response. I use a couple of different apps on my phone and they continue to connect to the Nissan servers even when Home Assistant doesn’t seem to.

The one thing that fixes it is rebooting the HA server. I noticed this first when installing a core update and since then it’s worked every time. However this isn’t a very satisfactory way of keeping it alive.

I don’t know HA well enough to understand what might be happening with a reboot that would unjam the integration and get it responding again. I cannot see any obvious errors in the logs, but when I open the entity properties through Lovelace I see that the attributes for the variables, such as Charge level, show Last update as many hours ago and the Last attempt about half an hour after that. Perhaps most interesting is the fact that ‘Update in progress’ was TRUE. Once I have rebooted then ‘Update in progress’ is FALSE as expected.

Could this give a clue as to what is going wrong? Could I have configured something in Home Assistant so that it gets stuck trying to get an update from the Nissan Servers?

Any help and pointers would be greatly appreciated. I’m not a noob, but don’t know the ins and outs of HA and debugging very well.

Thanks.

It I recall, the plugin can’t recover from a session dropping out, which is then sorted when you restart HASS. The Nissan app has a habit of doing this too.

If someone has the time, it may take a bit if fiddling but it should be fixable. I just don’t really have time to do it myself

1 Like

The leaf2mqtt container posted a few comments above is resilient to the different issues the Nissan API has. If you are willing to setup an mqtt server and spin the container, you may have better results than the home assistant native integration.

1 Like

Hi Ben. That sounds like a good place to start. I appreciate you taking the time to give me this insight. I am ok at coding, but don’t do it every day, so would you mind giving me some pointers as to how a fix might be approached? If you don’t have time, that’s fine but it would be great if you could!

There’s a fix in the latest version of pycarwings2 that helps with handling timeouts properly. That will be in the next release of homeassistant.

1 Like

That’s fantastic, thanks.

Anyone have any problem with Leaf integration over last few days?

It started just before 2022.2 dropped, I was still on latest 2021.12. So I suspect there had been some issue with their servers?

Here is an issue someone raised:

------------EDIT: ----------

Problem is at my end entirely. I had been self-isolating because of COVID, the car parked for 2 weeks. I guess it had gone into some sort of deep sleep after 10 days.

Drove it today and then restarted HA, everything is back now.

I think was a Nissan server problem.

By(t)e