EV battery dynamic icon

Hi!

I’m using OVMS along with my Kia Soul EV. Was using Server 2 (HTTP API) and transitioned to Server v3 (MQTT).

Have all of the data working properly, but one of the frontend elements is not working as expected. I wanted to show the battery icon based on the state of the sensors, but no icon is represented when using the below code.

Any hints?

sensor:

  - name: ovms_kia_charge_status
    state_topic: ovms/user/MYCAR/metric/v/c/charging
    availability: 
      topic: "ovms/user/MYCAR/metric/s/v3/connected"
      payload_available: "yes"
      payload_not_available: "no"
      
  - name: ovms_kia_soc
    state_topic: ovms/USER/MYCAR/metric/v/b/soc
    unit_of_measurement: "%"
    device_class: battery
    availability: 
      topic: "ovms/USER/MYCAR/metric/s/v3/connected"
      payload_available: "yes"
      payload_not_available: "no"
entity: sensor.ovms_kia_soc
layout: icon_state
icon: |-
  (((
   if (states['sensor.ovms_kia_soc'].state == 100)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery"; else return "mdi:battery-charging";
   if (states['sensor.ovms_kia_soc'].state >= 90)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-90"; else return "mdi:battery-charging-90";
   if (states['sensor.ovms_kia_soc'].state >= 80)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-80"; else return "mdi:battery-charging-80";
   if (states['sensor.ovms_kia_soc'].state >= 70)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-70"; else return "mdi:battery-charging-70";
   if (states['sensor.ovms_kia_soc'].state >= 60)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-60"; else return "mdi:battery-charging-60";
   if (states['sensor.ovms_kia_soc'].state >= 50)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-50"; else return "mdi:battery-charging-50";
   if (states['sensor.ovms_kia_soc'].state >= 40)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-40"; else return "mdi:battery-charging-40";
   if (states['sensor.ovms_kia_soc'].state >= 30)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-30"; else return "mdi:battery-charging-30";
   if (states['sensor.ovms_kia_soc'].state >= 20)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-20"; else return "mdi:battery-charging-20";
   if (states['sensor.ovms_kia_soc'].state >= 10)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-60"; else return "mdi:battery-charging-10";
   if (states['sensor.ovms_kia_soc'].state < 10)
   if (states['sensor.ovms_kia_charge_status'].state == "no") return "mdi:battery-alert-variant-outlinee"; else return "mdi:battery-charging-outline";
  )))
show_label: false
show_name: false
show_state: true
show_units: true
type: custom:button-card
styles:
  icon:
    - color: |-
        (((
          if (states['sensor.ovms_kia_soc'].state >= 70.1) return '#40bf40';
          if (states['sensor.ovms_kia_soc'].state >= 30.1) return '#bf9540';
          if (states['sensor.ovms_kia_soc'].state >= 0) return '#bf4040';
        )))
  card:
    - font-size: 20px
    - font-weight: bold

1 Like

Hey - what is OVMS and what are you using MQTT for with your car? I like the idea of having my car data available on my MQTT server - but I don’t think I’ve seen any integration that offers that. So, I’m sorry I can’t help you with your problem - but I’m very curious about what you’re doing.

The reason I ended up here was because I was searching for the reason my Tucson Plug-In Hybrid seemed to not be refreshing the battery state of charge. The battery sensor currently is displaying 76% when I know it’s just finished charging and is actually 100%.

I ended up looking through all of the “state” data to try and find where it was picking up the 76% from - and I found it! And I also found the 100% I was looking for.

I see in the json:
/vehicle_data/status/battery/batSoc: 76
/vehicle_data/status/evStatus/batteryStatus: 100. So, my best guess at this point is that the 76% I’m seeing might actually be referring to the 12V battery? And the EV Battery charge level is the 100.

This has had me scratching my head for 2 weeks - and I thought it just wasn’t updating properly - but this would explain it - so I’m recording it here so that someone can confirm (or re-educate me) and so hopefully the next person isn’t stuck scratching their head for nearly as long as I did.

1 Like

Could you use device_class: battery instead of the icon code ? I think the icon is generated from a state value in percent.

I think the code above is not working as the state will probably be a string and you’re testing for a number.

1 Like

Here is the link for the OVMS project: https://www.openvehicles.com/. Please be warned that I do not recommend this unless you are a major tech savvy with electronics and programming (which I am not, hence why I forgo the whole project).

I am only looking at 2 sensors. 1 for whether the car is charging or not and the other is the soc level. 12v reports to another sensor.

I actually see that the initial sensors that I put in the post where not the correct ones (fixed :P).

Thanks!

It was already defined as a battery class. But the problem is that the sensor by itself does not report charge state.

There’s 2 different sensors that make up what I want to achieve (battery level and charge state [Yes or No]).

You’re point about being a string might be the right direction. How could I do the comparison in that code for a string value?

Thanks for that link. The project looks really interesting - but seems like it requires custom hardware and is targeted at full EV owners. Mine is just a PHEV.

But if you’re only looking for 2 data points, have you tried getting them from the KIA_UVO integration?

I’ve been using it for a couple of weeks and it seems pretty reliable. It’s not going to provide everything that the OVMS project does, but it might have what you need. Here’s a photo of my (work-in-progress) dashboard for the car which highlights many of the sensors the integration creates:

1 Like

Something like this ?

icon: |-
  {% set soc = states('sensor.ovms_kia_soc') | float(0) %}
  {% set charge = states('sensor.ovms_kia_charge_status') %}
  {% if soc == 100 %}
    {% if charge == 'no' %} mdi:battery {% else %} mdi:battery-charging {% endif %}
  {% if soc >= 90 %}
    {% if charge == 'no' %} mdi:battery-90 {% else %} mdi:battery-charging-90 {% endif %}
  {% if soc >= 80 %}
    {% if charge == 'no' %} mdi:battery-80 {% else %} mdi:battery-charging-80 {% endif %}
  {% if soc >= 70 %}
    {% if charge == 'no' %} mdi:battery-70 {% else %} mdi:battery-charging-70 {% endif %}
  {% if soc >= 60 %}
    {% if charge == 'no' %} mdi:battery-60 {% else %} mdi:battery-charging-60 {% endif %}
  {% if soc >= 50 %}
    {% if charge == 'no' %} mdi:battery-50 {% else %} mdi:battery-charging-50 {% endif %}
  {% if soc >= 40 %}
    {% if charge == 'no' %} mdi:battery-40 {% else %} mdi:battery-charging-40 {% endif %}
  {% if soc >= 30 %}
    {% if charge == 'no' %} mdi:battery-30 {% else %} mdi:battery-charging-30 {% endif %}
  {% if soc >= 20 %}
    {% if charge == 'no' %} mdi:battery-20 {% else %} mdi:battery-charging-20 {% endif %}
  {% if soc >= 10 %}
    {% if charge == 'no' %} mdi:battery-10 {% else %} mdi:battery-charging-10 {% endif %}
  {% if soc < 10  %}
    {% if charge == 'no' %} mdi:battery-alert-variant-outline {% else %} mdi:battery-charging-outline {% endif %}
1 Like

I appreciate the reply and info, but my car does not support KIA_UVO (aka Kia Connect). Hence why I use OVMS.
OVMS is the “custom hardware”. It comes pre-assembled and with the needed firmware. It is “almost” plug & play.

I will try and share feedback ASAP! Thank you very much!!

@anthonws That makes perfect sense. So this OVMS project is a solution for people that can’t use the BlueLink or KIA_Connect service? It might be worth looking into once my free BlueLink subscription expires! That’s a year away - and by then you’ll be an expert on the system and be able to answer all my questions!

Good luck. I hope you get your problem solved!

1 Like

I got it to work :slight_smile:

entity: sensor.ovms_kia_soc
layout: icon_state
icon: |-
 [[[
   if (states['sensor.ovms_kia_soc'].state == '100')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery"; else return "mdi:battery-charging";
   if (states['sensor.ovms_kia_soc'].state >= '90')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-90"; else return"mdi:battery-charging-90";
   if (states['sensor.ovms_kia_soc'].state >= '80')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-80"; else return"mdi:battery-charging-80";
   if (states['sensor.ovms_kia_soc'].state >= '70')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-70"; else return"mdi:battery-charging-70";
   if (states['sensor.ovms_kia_soc'].state >= '60')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-60"; else return"mdi:battery-charging-60";
   if (states['sensor.ovms_kia_soc'].state >= '50')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-50"; else return"mdi:battery-charging-50";
   if (states['sensor.ovms_kia_soc'].state >= '40')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-40"; else return"mdi:battery-charging-40";
   if (states['sensor.ovms_kia_soc'].state >= '30')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-30"; else return "mdi:battery-charging-30";
   if (states['sensor.ovms_kia_soc'].state >= '20')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-20"; else return"mdi:battery-charging-20";
   if (states['sensor.ovms_kia_soc'].state >= '10')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-10"; else return"mdi:battery-charging-10";
   if (states['sensor.ovms_kia_soc'].state < '10')
   if (states['sensor.ovms_kia_charge_status'].state == 'no') return "mdi:battery-alert-variant-outline"; else return "mdi:battery-charging-outline";
  ]]]
show_label: false
show_name: false
show_state: true
show_units: true
type: custom:button-card
style:
  font-weight: bold
styles:
  icon:
    - color: |-
        [[[
          if (states['sensor.ovms_kia_soc'].state >= '70.1') return '#40bf40';
          if (states['sensor.ovms_kia_soc'].state >= '30.1') return '#bf9540';
          if (states['sensor.ovms_kia_soc'].state >= '0') return '#bf4040';
        ]]]
  card:
    - font-size: 20px