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:

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

  - id: car_charging_target
    alias: Car battery target
    description: Update the maxSocLowerLimit
      - alias: Trigger from car
        platform: state
        entity_id: sensor.licenseplate_state_of_charge
        attribute: maxSocLowerLimit
        id: car
      - alias: Trigger from slider
        platform: state
        entity_id: input_number.car_charging_target
        id: slider
      - choose:
        - conditions:
            - alias: Changed in car or app
              condition: trigger
              id: car
            - alias: Update slider
              service: input_number.set_value
                entity_id: input_number.car_charging_target
                value: "{{ state_attr('sensor.licenseplate_state_of_charge', 'maxSocLowerLimit') | int }}"
        - conditions:
            - alias: Changed slider
              condition: trigger
              id: slider
            - alias: Update maxSocLowerLimit
              service: mbapi2020.battery_max_soc_configure
                vin: !secret car_vin
                max_soc: "{{ trigger.to_state.state | int }}"

# Optional
  - 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 -%}
        {% endif %}        

The lovelace cards are also very easy to set up.

The slider is just an entities cards:

type: 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
  - type: custom:dual-gauge-card
      entity: sensor.licenseplate_state_of_charge
      label: Charge
      unit: '%'
      min: 0
      max: 100
        - value: 0
          color: var(--error-color)
        - value: 26
          color: var(--warning-color)
        - value: 51
          color: var(--success-color)
        - value: 81
          color: var(--info-color)
      entity: sensor.licenseplate_range_electric
      label: Range
      unit: km
      min: 0
      max: 400
        - 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
      - 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

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!