Hessel's Tech Blog

An overview of my random tech projects

Nissan Leaf integration in Home Assistant

I did not expect Nissan to have an API or something else to communicate with the car from an external source, but they do! It is hella slow (an API call takes about 60 seconds) but, it works. Also Home-Assistant already has an component built in that supports the Nissan Leaf so high gains, for low profit. Just as I like it 😉 The component has support for getting the available range, remaining battery capacity and turning on the AC. The interface looks as follows:

Setting up was easy, just as expected. To setup the connection to your Leaf just add the following things to your configuration.yaml:

nissan_leaf:
  username: yournissanleaf@email.address
  password: averygoodpassword
  region: NE

I also changed the logger, to see if communication was working properly. I can highly recommend this, it makes debugging much easier.

logger:
  default: critical
  logs:
    homeassistant.components.nissan_leaf: debug
    homeassistant.components.sensor.nissan_leaf: debug
    homeassistant.components.switch.nissan_leaf: debug

Since the Nissan Connect is unreliable, I wanted to be able to see if status updates worked properly. Therefore I created three template sensors: one to see the last update attempt, another one the see the last successful update and at least one to see when the next status update will be executed.

  - platform: template
    sensors:
      leaf_last_update:
        friendly_name: 'Leaf Last Update'
        value_template: >
          {%- macro suffix(d) %}
          {%- set sfx = {1:'st',2:'nd',3:'rd'} %}
          {{- 'th' if 11 <= d <= 13 else sfx.get(d%10, 'th') }}
          {%- endmacro %}
          {% set day = as_timestamp(states.sensor.xxxxx_charge.attributes.updated_on) | timestamp_custom('%a') %}
          {% set date = as_timestamp(states.sensor.xxxxx_charge.attributes.updated_on) | timestamp_custom('%d') |int %}
          {% set time = as_timestamp(states.sensor.xxxxx_charge.attributes.updated_on) | timestamp_custom('%I:%M %p') %}
          {{ day }} {{ date }}{{ suffix(date) }} {{ time }}
  - platform: template
    sensors:
      leaf_next_update:
        friendly_name: 'Leaf Next Update'
        value_template: >
          {%- macro suffix(d) %}
          {%- set sfx = {1:'st',2:'nd',3:'rd'} %}
          {{- 'th' if 11 <= d <= 13 else sfx.get(d%10, 'th') }}
          {%- endmacro %}
          {% set day = as_timestamp(states.sensor.xxxxx_charge.attributes.next_update) | timestamp_custom('%a') %}
          {% set date = as_timestamp(states.sensor.xxxxx_charge.attributes.next_update) | timestamp_custom('%d') |int %}
          {% set time = as_timestamp(states.sensor.xxxxx_charge.attributes.next_update) | timestamp_custom('%I:%M %p') %}
          {{ day }} {{ date }}{{ suffix(date) }} {{ time }}
  
  - platform: template
    sensors:
      leaf_last_attempt:
        friendly_name: 'Leaf Previous Attempt'
        value_template: >
          {%- macro suffix(d) %}
          {%- set sfx = {1:'st',2:'nd',3:'rd'} %}
          {{- 'th' if 11 <= d <= 13 else sfx.get(d%10, 'th') }}
          {%- endmacro %}
          {% set day = as_timestamp(states.sensor.xxxxx_charge.attributes.last_attempt) | timestamp_custom('%a') %}
          {% set date = as_timestamp(states.sensor.xxxxx_charge.attributes.last_attempt) | timestamp_custom('%d') |int %}
          {% set time = as_timestamp(states.sensor.xxxxx_charge.attributes.last_attempt) | timestamp_custom('%I:%M %p') %}
          {{ day }} {{ date }}{{ suffix(date) }} {{ time }}

Now the connection should be working, but I am not able to do anything with the connection. Therefore I created an dashboard in the Home-Assistant LoveLace UI. The Dashboard needed four functions in my opinion: starting the climate control remotely, showing current battery percentage,starting a charge remotely, and I wanted a button to manually update all information of the Leaf. The first two were relatively easy, since those are just entities which can easily be added with the entity card. The other two required creating a entity button which calls an service on tapping it. This was works great for the start charging button, since there is a charging status entity.

Unfortunately, there is no way to simply call an service with an button press without adding a entity to it in Home Assistant. That is why I created an entity for the charge level and just added an update icon to it. Well, all leads to the following dashboard configuration:

  - cards:
      - entities:
          - entity: binary_sensor.xxxxx_charging_status
          - entity: binary_sensor.xxxxx_plug_status
          - entity: sensor.xxxxx_charge
          - entity: switch.xxxxx_climate_control
          - entity: sensor.xxxxx_range
          - entity: sensor.xxxxx_range_ac
          - entity: sensor.leaf_last_update
          - entity: sensor.leaf_next_update
          - entity: sensor.leaf_last_attempt
        show_header_toggle: false
        title: Nissan Leaf
        type: entities
      - entity: sensor.xxxxx_charge
        hold_action:
          action: none
        icon: 'mdi:update'
        name: Update Leaf Status
        tap_action:
          action: call-service
          service: nissan_leaf.update
          service_data:
            vin: 
        type: entity-button
      - type: entity-button
        tap_action:
          action: call-service
          service: nissan_leaf.start_charge
          service_data:
            vin: 
        hold_action:
          action: none
        show_icon: true
        name: Laden starten
        entity: binary_sensor._charging_status
      - entities:
          - sensor.xxxxx_charge
          - sensor.xxxxx_range
        title: Laad-status
        type: history-graph
    badges:
      - binary_sensor.xxxxx_charging_status
      - sensor.xxxxx_charge
    path: nissan-leaf
    title: Nissan Leaf

Okay! Now I have a working Leaf dashboard, and if I am honest I like it more than the app that the Nissan made ;p. It only needs one extra feature, and that is to being able to see remaining charging time. This data is available in the status updates that Home-Assistant receives from the Nissan API but it only needs to be serialized. Should be quite easy, but lets make that a project for a later time!

Nissan Leaf integration in Home Assistant
Scroll to top