MG Motor Mg5 Electric Car Integration

Universal card for displaying vehicle information from ngocjohn:

1 Like

I have set this up but am having an issue. I have checked the logs and its telling me my accounts been locked and to wait a day or change the password. I have changed the password using the app, but still no joy. The app works fine with new password.

Preformatted text 2024-09-30 13:54:08,659 [ ERROR ] MqttGateway crashed due to an Exception during startup - main
Traceback (most recent call last):
File “/usr/src/app/./mqtt_gateway.py”, line 441, in run
login_response_message = await self.saic_api.login()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/site-packages/saic_ismart_client_ng/api/base.py”, line 72, in login
result = await self.deserialize(req, response, LoginResp)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/site-packages/saic_ismart_client_ng/api/base.py”, line 185, in deserialize
raise se
File “/usr/local/lib/python3.12/site-packages/saic_ismart_client_ng/api/base.py”, line 170, in deserialize
raise SaicApiException(error_message, return_code=return_code)
saic_ismart_client_ng.exceptions.SaicApiException: return code: 15033, message: Sorry, the account has been locked, please try again tomorrow or you may reset password by clicking “forgot password”if you forget it.
return code: 15033, message: Sorry, the account has been locked, please try again tomorrow or you may reset password by clicking “forgot password”if you forget it.
2024-09-30 13:54:08,704 [ ERROR ] Task was destroyed but it is pending!
task: <Task pending name=‘Task-1’ coro=<Client._resend_qos_messages() running at /usr/local/lib/python3.12/site-packages/gmqtt/client.py:176>> - asyncio
sys:1: RuntimeWarning: coroutine ‘Client._resend_qos_messages’ was never awaited Preformatted text

Any ideas?

this looks great, do you use it and have some config?

I had a play around with this on the long weekend. It’s quite elegant:

Still massaging the layout (the UI is quite good once you wrap your head around it). Code:

type: custom:vehicle-status-card
button_card:
  - button:
      primary: Charger Connected
      secondary:
        - entity: sensor.mg4_charge_connected
          attribute: ''
          state_template: ''
      notify: ''
    button_type: action
    hide_button: false
    card_type: default
    default_card: []
    custom_card: []
    button_action:
      entity: sensor.mg4_charge_connected
      tap_action:
        action: more-info
      hold_action:
        action: none
      double_tap_action:
        action: none
  - button:
      primary: Odometer
      secondary:
        - entity: sensor.mg4_distance_total
          attribute: ''
          state_template: ''
      notify: ''
    button_type: action
    hide_button: false
    card_type: default
    default_card: []
    custom_card: []
    button_action:
      entity: sensor.mg4_distance_total
      tap_action:
        action: more-info
      hold_action:
        action: none
      double_tap_action:
        action: none
  - button:
      primary: AUX Battery
      secondary:
        - entity: sensor.mg4_lvbattery
          attribute: ''
          state_template: ''
      notify: ''
    button_type: action
    hide_button: false
    card_type: default
    default_card: []
    custom_card: []
    button_action:
      entity: sensor.mg4_lvbattery
      tap_action:
        action: more-info
      hold_action:
        action: none
      double_tap_action:
        action: none
  - button:
      primary: EVSE
      secondary:
        - entity: sensor.evse_kwh
          attribute: ''
          state_template: ''
      notify: ''
    button_type: action
    hide_button: false
    card_type: default
    default_card: []
    custom_card: []
    button_action:
      entity: sensor.evse_kwh
      tap_action:
        action: more-info
      hold_action:
        action: none
      double_tap_action:
        action: none
images:
  - url: /api/image/serve/de7ce78bd4c53e589df5748364cb6d16/original
    title: MG4_T.JPG
indicators:
  group:
    - name: Charging Data
      icon: mdi:car
      visibility: ''
      items:
        - entity: sensor.mg4_t_charge_start_soc_kwh
          name: Start SOC
          icon: ''
        - entity: sensor.mg4_t_charge_start_range_used
          name: Start Range Used
          icon: ''
          attribute: ''
        - entity: sensor.mg4_t_charge_start_range_remaining
          name: Start Range Remaining
          icon: ''
          attribute: ''
        - entity: sensor.evse_kwh
          name: EVSE kWh
          icon: ''
          attribute: ''
        - entity: sensor.mg4_t_charge_end_soc_kwh
          name: Charge End SOC
          icon: ''
          attribute: ''
  single: []
layout_config:
  button_grid:
    rows: 3
    swipe: true
  hide:
    button_notify: false
    buttons: false
    images: false
    indicators: false
    mini_map: true
    range_info: false
  theme_config:
    mode: auto
    theme: default
  images_swipe:
    max_height: 200
    max_width: 400
mini_map:
  device_tracker: device_tracker.mg4_location
  enable_popup: false
  theme_mode: auto
name: Bluey
range_info:
  - energy_level:
      - entity: sensor.mg4_soc_percent
        attribute: ''
        icon: ''
    range_level:
      - entity: sensor.mg4_range
        attribute: ''
    progress_color: ''
tire_preview: null
btn_preview: null
card_preview: null
default_card_preview: null

1 Like

Hi @PeatyPete and @wattmatters.
You may have already had some similar issues and resolved them but felt others reading this thread may benefit from my troubles. As mentioned my SOC was only updating at the rate of “Gateway charging refresh period” which is between 1000 and 1500 seconds while charging (between 17 - 25mins).
I have setup some automations that track the excess power being generated and changes the “current limit” in the MG integration so that I am not drawing from the grid to charge the MG. So the 17 - 25 minute updates were a little bit too long to accurately adjust the current based energy being fed into the grid.

My solution was to create a template sensor that tracks how long it has been since HA received a charge update from the MG, then use an automation to force an update if the car is charging and the time since last update is greater than 300 seconds (5 mins)

#Automation
- id: '17148028106762'
  alias: MG current force update when inactive
  description: check for update when inactive
  mode: single
  trigger:
  - platform: numeric_state
    entity_id:
      - sensor.mg4_seconds_since_update
    above: 300
  condition:
  - condition: numeric_state
    entity_id: sensor.mg4_energy_flow_from_grid
    above: 20
  - condition: state
    entity_id: binary_sensor.lsjwh4094pn199624_battery_charging
    state: 'on'
  action:
  - action: select.select_option
    metadata: {}
    data:
      option: force
    target:
      entity_id: select.lsjwh4094pn199624_gateway_refresh_mode

#Sensor
template:
  - sensor:
      - name: "mg4_seconds_since_update"
        unit_of_measurement: "Seconds"
        state: >
          {% set n = now() %}
          {% set t = states('sensor.lsjwh4094pn199624_last_charge_state') | as_datetime | as_local %}
          {{ (n-t).seconds }}

I am wary of the 12V battery with the drain issue when polling more frequently and when you stand next to the car you hear some of the systems turn on and the driver’s screen flashes on for a few seconds when the update is sent. Hopefully, the fact that the automation will only run when it is plugged in and charging will mean it will be ok. Do you know if the 12V battery charges during EV charging?

Interesting idea!
I wouldn’t be concerned about the LV battery - according to the manual the car charges it automatically as needed (refer the “Intelligent Charging” section).
My observation is “Intelligent Charging” always takes place when the car is charging. Eg, here’s my last charging session. You can see the LV battery jump during the session:

Our MG4 does. It also gets charged while driving.

Just check the auxiliary battery voltage during a charge session, that will tell you. It is an entity provided by the integration. e.g. here is my car’s charging power and auxiliary battery voltage during a charge session (note, the charging power data is from my own home circuit monitoring, not this integration):

You can see the auxiliary battery voltage rises to over 14 V while the car is being charged, then once the charging has ceased the auxiliary battery voltage drops back to normal resting voltage (a little less as it is being polled for a short while after a charge session).

Here’s the last 30 days of auxiliary battery voltage data:

Can see how it is regularly lifted >14 V when the car gets charged or is being driven, then drops down in between. In last couple of weeks we have not driven the car aside from this weekend just gone and so can see how the auxiliary battery voltage has slowly dropped in that two weeks.

Note that on the right side the voltage shows the auxiliary battery being charged again - the car in this instance was not being charged, but the auxiliary battery also gets recharged while driving.

nice thankyou!!

Just recently installed this and I’m blown away by how sophisticated it is.
(Having lots of fun already.)
One thing that puzzles me is these settings:

Gateway grace period after car shutdown 600 seconds
Gateway refresh period after car shutdown 120 seconds

What is the difference between these?

In the documentation it goes through each option for set up config and topic:

| /refresh/period/afterShutdown/set        | refresh interval (sec)   | After the vehicle has been shutdown, the gateway queries the status every 120 seconds (default value). The refresh interval can be modified with this topic.                                                                          |
| /refresh/period/inActiveGrace/set        | grace period (sec)       | After the vehicle has been shutdown, the gateway continues to query the state for 600 seconds (default value). The duration of this extended query period can be modified with this topic.                                            |

IOW it will (using default settings) poll the car a couple more times after it has been shut down but then cease polling after that.

Thanks wattmatters.
I’d figured that out about 2 hours ago!
The integration keeps polling the car after shutdown for 10 minutes at 2 minute intervals.
It’s pretty obvious really - a bit of a dumb question from me, but thanks for replying :slightly_smiling_face:

FYI if you set those values they will be reset to their defaults the next time the add-on restarts.
There’s a fix for this in the newer release, however it wasn’t backported to the AU/NZ version. I briefly messed about with my local install to try and backport the fix however I wasn’t successful and haven’t had the time to dedicate to it furher.

1 Like

Is it possible to get the code for all your manual sensors you’ve setup here?

Apologies, I missed your post.
Sure - the first few sensors are the normal ones provided by the integration (I have them defined manually as MQTT discovery is turned off in my deployment):

mqtt:
  sensor:
    - name: mg4_charge_connected
      icon: mdi:power-plug-outline
      state_topic: "saic/my@email/vehicles/LSJ_VIN/drivetrain/chargerConnected"
      value_template: "{{ value }}"

    - name: mg4_distance_total
      icon: mdi:counter
      state_topic: "saic/my@email/vehicles/LSJ_VIN/drivetrain/mileage"
      value_template: "{{ value |round(0) }}"
      unit_of_measurement: "km"

    - name: mg4_lvbattery
      icon: mdi:lightning-bolt-outline
      state_topic: "saic/my@email/vehicles/LSJ_VIN/drivetrain/auxiliaryBatteryVoltage"
      value_template: "{{ value }}"
      unit_of_measurement: "v"

sensor.evse_kwh is specific to my EVSE (well actually the supply side) and has nothing to do with the car.

The rest are template sensors to capture “point in time” measurements. They’re updated whenever a charger is connected/disconnected:

template:
  - trigger: #This trigger template sensor updates when the MG4 charger is unplugged, to capture the end of charging SOC
      - platform: state
        entity_id:
          - sensor.mg4_charge_connected
        from: "False"
        to: "True"
    sensor:
      - name: "mg4_t_charge_start_soc_kwh"
        icon: mdi:lightning-bolt-outline
        unit_of_measurement: "kWh"
        state: "{{ states('sensor.mg4_soc_kwh') }}"
      - name: "mg4_t_charge_start_range_used"
        icon: mdi:arrow-left-right
        unit_of_measurement: "km"
        state: "{{ states('sensor.mg4_u_distance_last_charge') }}"
      - name: "mg4_t_charge_start_range_remaining"
        icon: mdi:arrow-left-right
        unit_of_measurement: "km"
        state: "{{ states('sensor.mg4_range') }}"

  - trigger: #This trigger template sensor updates when the MG4 charger is unplugged, to capture the end of charging SOC
      - platform: state
        entity_id:
          - sensor.mg4_charge_connected
        from: "True"
        to: "False"
    sensor:
      - name: "mg4_t_charge_end_soc_kwh"
        icon: mdi:lightning-bolt-outline
        unit_of_measurement: "kWh"
        state: "{{ states('sensor.mg4_soc_kwh') }}"

And I have a utility meter sensor that’s referenced by one of the template sensors. There’s an automation which resets its value when the charger is unplugged

utility_meter:
  mg4_u_distance_last_charge: #Note, this counter is reset by an automation when charger is disconnected
    source: sensor.mg4_distance_total

How are you calculating sensor.mg4_energy_flow_from_grid

The mg flow from grid is from a current sensor I had in my power meter box. I have the MG charger but wasn’t able to integrate it. This was my work around solution.

Ah I have a MyEnergi Zappi which does expose some via its CT Clamp but this is data for the whole house.

I suppose I could make a Derivative sensor if it exceeds a certain % for a period of time. As typical use seems to be low and turning on EV charger nets it shooting to over 7kwH

Actually edit; found sensor.myenergi_home_power_charging which shows the W use of charging in real time.

The iSmart 2.0 app has finally started rolling out in Australia this afternoon.

I just swtiched from the legacy version and it works!

saic_rest_uri:
https://gateway-mg-au.soimt.com/api.app/v1/

saic_region:
au

everything else can be copied straight from the legacy version.

Fun fact though, If you keep HA using the legacy version then you can use the 2.0 app and have both going together.
As soon as you setup the newer HA addin it logs you out of the app again.

Good to know.

What does the new version enable compared with the old?

That might be preferable!

Main thing is just being on a maintained version again. The current version has a ton of fixes and updates that weren’t in the legacy branch.