Car mileage meter per month

Hi guys. I was hoping to get your help in solving a thing in hoping to set up

Ive got an MG ev on lease. This means i have 7500km over the span of three years to drive. We live in the countryside so it will be up to us to not overdrive. To solve this, i currently have a Google sheets in which we manually enter the current car mileage into a cell each month. It then checks each months range budget and displays a meter that is projected as an image for HA to show.

Now i recently found an addon that can read the car api into a sensor on HA, so now im hoping to automate the process. It has a mileage sensor to use.

I need to keep track each month of that months range budget (so 2500km per year divided by 12) and track and compensate for over/under budget. Then, present this as a gauge with the remaining range budget for that month.

What can i use? Ive found a Google sheets integration that can write data into cells, is that the best solution for the calculations?

Help is greatly appreciated

You know the start and end date, and the current odometer reading? Don’t bother with Google Sheets, or with monthly budgets.

Create a template sensor that shows the ±km versus budget at any time. Something like this:

template:
  - sensor:
      - name: MG odo vs budget
        unit_of_measurement: 'km'
        state: >
          {% set start = as_timestamp("2023-04-21") %}
          {% set end = as_timestamp("2026-04-21") %}
          {% set tnow = as_timestamp(now()) %}
          {{ (7500 * ((tnow-start)/(end-start))) -  
              states('sensor.mg_odo')|int(0) }}

Obviously, put in your own dates and sensor name. This shows negative numbers if you’re over budget; and assumes you started from 0km on the odometer.

1 Like

So this would track each day as a increasing number(7500 divided over three years) and compare to what the odometer is? So day 345 should be X number and its shows if youre over or under compared to what the car odometer sensor is?

If so thats brilliant and solves all my problems. Im going to try it tonight. Just pop that code part into my configuration.yaml ?

The sensor is mg5_mileage and it returns this format : 20,472.0 (unit is km) so i would have to input 75000 as the starting number right?

works very well when i put the {{}} part on the same row! Can i cut down on the number of numbers? its -118.678821772897 km at the moment :slight_smile: also, can it refresh it daily? seems to update by seconds now.

thank you so much for your help

Glad it’s working for you.

If you’re not interested in knowing your budget to the nearest nanometre, surround the template with brackets and force it to an int. To make it update daily, turn it into a trigger-based sensor:

template:
  - trigger:
      - platform: time
        at: "00:00:00"
    sensor:
      - name: MG odo vs budget
        unit_of_measurement: 'km'
        state: >
          {% set start = as_timestamp("2023-04-21") %}
          {% set end = as_timestamp("2026-04-21") %}
          {% set tnow = as_timestamp(now()) %}
          {{ ((7500 * ((tnow-start)/(end-start))) - states('sensor.mg_odo')|int(0))|int }}

Nice, is it also possible to get the mileage per month? So I can see in a graph how much we drive on a monthly basis?

I’ve no direct experience of this, but I would have thought that a Utility Meter would work in this application.

HA isn’t really designed with long-term recording as a priority — there are some options as above, but I wouldn’t build anything critical on it.

If you define the values you could graph them? So if you have one sensor that is the “daily odometer” and one that is “daily calculated budget” you could input them both into a monthly spanning graph couldnt you? So right now the above code gives the ± number as the entity but if you make two, one is the actual value and the other the difference, they could be applied to have a linear graph for each month, and then the real odometer that will hopefully follow the other line. And then, if you want, the difference.

I guess creating another sensor with the above code but removing the part with states and the odo sensor would do the trick. And a new name ofc

@troon

The value goes up between when i manually refresh the odometer sensor. Why is that?

The longest line today starts at -233km and ends at -188km before jumping down to --226km. Feels like something in the calculations are increasing between odometer updates? I checked the odometer sensor and its stable between updates

Paste in your final sensor configuration that is being graphed there, correctly formatted please.

@troon
since i have more template sensors i had to add it to the bottom, i included the first template row of my config and then the … represents all the other sensors in between, then the last solax sensor and this sensor. i had to reformat a tad for it to work. i couldnt find a way to include the once a day trigger so i left that out.

...
...
#Local sensors
template:
- sensor:
  - name: solax
...
...
...
  - name: "Solax Inverter Temperature ?"
    state: "{{ state_attr('sensor.solax', 'Data')[54] | int(default=0) }}"
    unit_of_measurement: "°C"
    device_class: "temperature"

- sensor:
    - name: MG5 körbudget
      unit_of_measurement: 'km'
      state: >
        {% set start = as_timestamp("2022-06-30") %}
        {% set end = as_timestamp("2025-06-30") %}
        {% set tnow = as_timestamp(now()) %}
        {{ ((75000 * ((tnow-start)/(end-start))) - states('sensor.mg5_mileage')|int(0))|int }}

Without the trigger it is evaluating every minute, so your “odometer budget” is increasing all the time. I think you probably have a unit problem — if your sensor is reporting 10× the actual km value, then your graph is not in km. Perhaps use:

        {{ ((7500 * ((tnow-start)/(end-start))) - (states('sensor.mg5_mileage')|int(0)/10))|int }}

Tried that code and its reporting the correct amount. But its still decreasing between updates. The odometer sensor mg5_mileage is stable with a value of 20,795.0 km. Its decreased from -20km to -17km over night.

Im thinking that something in the conversion from nanometers to kilometer is the problem? That with the constantly changing value of the decimal, its decreasing the value.

I am trying to remove the conversion to see if the value decreases. It should increase in my mind. And to be clear, i mean the negative value should increase right? Since the daily budget target should increase very slowly towards 75000km and it should increase the gap until i update the odometer.

Is there an easy way to store the daily budget number into a separate sensor? And not like now, use it in the calculation. So i can display that to,and then the difference.

A negative number means you’re over budget — you’ve driven too far. Over time, if you don’t use the car, that number will indeed increase: -20km to -17km is an increase.

Because you removed the trigger I suggested, it is continuously evaluated. Put the trigger back in and it’ll be evaluated daily.

changing back to nanometers, its increasing (and decreasing the negative value) ever so slightly so i guess its as it should then. I missunderstood you, and thought -20km was UNDER budget so that explains why i was thinking i saw it go down when it should have gone up.

How do i insert the trigger with the other sensors? since the - sensor is already as left as it can be and i have heard yaml needs those spaces to function. Can i just insert it above?

thank you for helping me and sorry for missunderstanding you.

@troon

so i made a distance sensor using your code removing the calculation

- name: MG5 körbudget total
  unit_of_measurement: 'km'
  state: >
        {% set start = as_timestamp("2022-06-30") %}
        {% set end = as_timestamp("2025-06-30") %}
        {% set tnow = as_timestamp(now()) %}
        {{ (7500 * ((tnow-start)/(end-start)))|int(0) }}

And it returns the value 2064, which is obviously miles. Adding a 0 to 7500 correctly returns 20649 which for me is kilometers.

I checked this against my odometer and thats at 20819 , so also kilometers. The gap then is -170 kilometers.

Using your code with 7500 times the odometer, its returning at -16km, which isnt correct according to the calculation. but, adding a zero, 75000 does return 168 which is more accurate.

  - name: MG5 körbudget
    unit_of_measurement: 'km'
    state: >
      {% set start = as_timestamp("2022-06-30") %}
      {% set end = as_timestamp("2025-06-30") %}
      {% set tnow = as_timestamp(now()) %}
      {{ ((75000 * ((tnow-start)/(end-start))) - (states('sensor.mg5_mileage')|int(0)))|int }}

This does return a closer value to what im expecting to see. Or am i wrong somewhere? this keeps both numbers at kilometers.

again, im so extremly grateful for your help. i just want a number that i can count on being right before i abandon my old excel file.