Intro

I recently got an Electric Vehicle (EV) and of course I want to get it integrated in Home Assistant :)

Luckily, I found a HACS integration for MercedesME that provides me with a lot of data about my car and allows me to send some commands to it as well. The command I mainly want to touch upon in this short blogpost is the mbapi2020.battery_max_soc_configure services that allows you to set the battery charge target.

This target defines up to which percentage the car will charge its battery when connected to a EV charger. So you could call this a max. carge limit as well.

Generally, I set this limit on 50% which is plenty sufficient to cover my daily drives. And it scores well in the Mercedes Eco Coach app :D However, about once a week I need to do some longer trips and I don’t have reliable access to a charger while I’m out, so I prefer to have a bigger buffer and charge to 80-100%. To do this, I either need to change the limit via the EQ app in my car’s mediacenter, or I need to use the Mercedes ME app on my phone and go through 2 or 3 menus.

The car also allows you to set 3 Charge Profiles: Home, Work and Standard. Home and Work can be set automatically based on the cars location, so you can configure the car to charge e.g. up to 100% at work and up to 50% at home. However, this doesn’t help me with the situation where I want to top it of before leaving for work.

So my idea is to allow me to control the charge target from within Home Assistant and also set an automation to increase the charge at night before I leave for work.

Installing the MercedesME integration

Installation of this integration is easy as it comes as a HACS integration.

You open HACS, go to the Integrations section and install the “MercedesME 2020” integration. After a reboot of HA, the integration can be added as a Home Assistant integration.

You will need to have used the Mercedes ME mobile app at least once before as you’ll need to login using that account in the integration. For certain services, like unlocking or remotely starting your car, you will also need to have setup a security PIN within the app and then pass it to the integration.

Controlling battery charge target/limit

Below I’ll simply put some screenshots and YAML for the stuff I did to control the charge target of my car. Most of it should be self-explanatory.

Screenshot of Lovelace dashboard controlling car charge target
Lovelace setup

I may dedicate another blog post on how I set up the preconditioning control for my car.

The YAML itself in the end is fairly compact:

input_number:
  car_charging_target:
    name: Car charging target
    min: 50
    max: 100
    step: 10
    unit_of_measurement: "%"
    icon: mdi:battery-charging

automation:
  - id: car_charging_target
    alias: Car battery target
    description: Update the maxSocLowerLimit
    trigger:
      - alias: Trigger from car
        platform: state
        entity_id: sensor.licenseplate_max_state_of_charge
        id: car
      - alias: Trigger from slider
        platform: state
        entity_id: input_number.car_charging_target
        id: slider
    action:
      - choose:
        - conditions:
            - alias: Changed in car or app
              condition: trigger
              id: car
          sequence:
            - alias: Update slider
              service: input_number.set_value
              target:
                entity_id: input_number.car_charging_target
              data:
                value: >-
                  {% set max_soc = states('sensor.licenseplate_max_state_of_charge') %}
                  {{ max_soc | int if is_number(max_soc) }}                  
        - conditions:
            - alias: Changed slider
              condition: trigger
              id: slider
          sequence:
            - alias: Update maxSocLowerLimit
              service: mbapi2020.battery_max_soc_configure
              data:
                vin: !secret car_vin
                max_soc: "{{ trigger.to_state.state | int }}"
    mode: single

# Optional
template:
  - binary_sensor:
    - name: Car Charging Active
      unique_id: car_charging_active
      device_class: battery_charging
      state: "{{ state_attr('sensor.licenseplate_range_electric', 'chargingactive') }}"
  - sensor:
    - name: Car Charging Power
      unique_id: car_charging_power
      unit_of_measurement: kW
      device_class: power
      icon: mdi:ev-plug-type2
      # The if/else below is to fix a rare case of chargingPower being unavailable 
      #  after a HA restart. It reappears after a first charge.
      state: >
        {%- if is_state('binary_sensor.car_charging_active', 'on') -%}
          {{ state_attr("sensor.licenseplate_range_electric", "chargingPower") }}
        {%- else -%}
          0
        {% endif %}        

The lovelace cards are also very easy to set up.

The slider is just an entities cards:

type: entities
entities:
  - input_number.car_charging_target

If I now move the slider, Home Assistant will call the service to update the maxSocLowerLimit of my car. When I change this target from within the car’s EQ app or the MercedesME app on my phone, this is reflected in Home Assistant as well.

The gauges make use of a custom card and the charging indicator for me is just informational. You can define your own values for the gauge scales, I’m still tweaking them to best match real-life experiences.

type: vertical-stack
cards:
  - type: custom:dual-gauge-card
    outer:
      entity: sensor.licenseplate_state_of_charge
      label: Charge
      unit: '%'
      min: 0
      max: 100
      colors:
        - value: 0
          color: var(--error-color)
        - value: 26
          color: var(--warning-color)
        - value: 51
          color: var(--success-color)
        - value: 81
          color: var(--info-color)
    inner:
      entity: sensor.licenseplate_range_electric
      label: Range
      unit: km
      min: 0
      max: 400
      colors:
        - value: 0
          color: var(--error-color)
        - value: 51
          color: var(--warning-color)
        - value: 151
          color: var(--success-color)
        - value: 301
          color: var(--info-color)
  - type: horizontal-stack
    cards:
      - type: tile
        entity: binary_sensor.car_charging_active
        name: Charging
        state_color: true
      - type: tile
        entity: sensor.car_charging_power
        unit: kW
        name: Power
        state_color: false
  - type: entities
    entities:
      - input_number.car_charging_target

Identify charge program

We can also check which charging program is selected for the car (Standard, Home, Work). The selectedChargeProgram attribute of sensor.licenseplate_range_electric reports this to us, but using a numeric value. So we use a template to make this more readable.

template:
  - sensor:
    - name: Car Charging Program
      unique_id: car_charging_program
      device_class: enum
      state: >-
        {% set program = state_attr('sensor.licenseplate_range_electric','selectedChargeProgram') | int %}
        {%- if program == 0 -%}
          Standard
        {%- elif program == 2 -%}
          Home
        {%- elif program == 3 -%}
          Work
        {%- else -%}
          Unknown ({{ program }})
        {% endif %}        
      icon: >-
        {% set state = this.state %}
        {%- if state == "Standard" -%}
          mdi:lightning-bolt-outline
        {%- elif state == "Home" -%}
          mdi:home-lightning-bolt-outline
        {%- elif state == "Work" -%}
          mdi:office-building-outline
        {%- else -%}
          mdi:battery-unknown
        {% endif %}        
Screenshot of charge program card
Charge program

To add this card to the Lovelace card setup above, you’ll need to add 1 tile card to the horizontal stack:

# ...
  - type: horizontal-stack
    cards:
      - type: tile
        entity: binary_sensor.car_charging_active
        name: Charging
        color: green
        show_entity_picture: false
        vertical: false
      - type: tile
        entity: sensor.car_charging_program
        show_entity_picture: false
        vertical: false
      - type: tile
        entity: sensor.car_charging_power
        color: green
        vertical: false
# ...

Finishing thoughts

Ideally, the target battery percentage would also be visualised on the gauge card. But I still need to find a custom card that would allow me to do so. I’m already using a custom dual-gauge-card to display my range in both battery % and theoretical kilometers.

Also, I’m looking for a nice card to visualise all info from my car and allow some control over it. If you have a nice car dashboard and are willing to share the info with me, hit me up on Twitter!

Changelog

  • 2023-04-13
    • make use of the new max_state_of_charge sensor as trigger for the automation.
    • add car charge program