Calculation of daily/monthly fuel consumption

Using the BMW ConnectedDrive add-on, I’m able to use a sensor that knows the fuel level in my car (sensor.320i_remaining_fuel). This value will, of-course, decrease from time 2 time, and once in a while it will shoot up again when the tank is filled again.

Since the sensor always reads the current value, would it possible to calculate the average consumption (on a timed interval, daily/weekly etc) on a template sensor? i.e. xx amount of Liters p/day ?

Yes, i did succeed to do so last week with a margin of error of 1 Liter (when refuelling).

for that you will need:

1. with Utility Meter (if not install it) create a utility sensor:

  • Name: " X3 Liters Daily " (in my case or use any other name and amend the formulas)
  • Meter reset cycle = Daily
  • Offset = 0
  • Supported tariff: " usage " and " refuel "
  • Net Consumption = ACTIVE

Once created two new sensors shall also be created:

  • sensor.x3_liters_daily_refuel
  • sensor.x3_liters_daily_used

2. Add an automation to switch the utility meter tariff created (step 1) based on the connecteddrive lids boolean

alias: X3 Using or Refuelling
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.x3_xdrive30e_door_lock_state
condition: []
action:
  - service: select.select_option
    target:
      entity_id:
        - select.x3_liters_daily
    data:
      option: >-
        {{ 'used' if 'off' in
        states('binary_sensor.x3_xdrive30e_door_lock_state') else 'refuel' }}
  - device_id: 250d13ae635775162bc0f5b00982c340
    domain: button
    entity_id: button.x3_xdrive30e_refresh_from_cloud
    type: press
mode: single

3. create an additional sensor for swapping the liters positive and another for calculating the L/100Km in your configuration.yaml or on your templates file

sensor:
   - name: "X3 Liters used"
     unit_of_measurement: "L"
     icon: mdi:fuel
     state: "{{ (-1 * states('sensor.x3_liters_daily_used')| float(0) ) }}"  
     
   - name: "X3 Daily Consumption L on 100KM"
     unit_of_measurement: "%"
     icon: mdi:car-info
     state: >
          {% set X3_L_today = ( states('sensor.x3_liters_used') | float(0.000000000001) )%}
          {% set X3_KM_today = states('sensor.x3_daily_mileage') | float(0) %}
          {{ (( X3_L_today / X3_KM_today ) * 100) | round(2) }} 

4. Add it in a graphical design. I use apex-charts

type: custom:apexcharts-card
graph_span: 31d
span:
  start: month
header:
  show: true
  title: Daily Mileage
  show_states: true
  colorize_states: true
yaxis:
  - id: left
    show: true
    opposite: false
    min: 0
    apex_config:
      decimalsInFloat: 0
      tickAmount: 6
      forceNiceScale: true
  - id: right
    show: true
    opposite: true
    max: 10
    min: 0
    apex_config:
      decimalsInFloat: 0
      tickAmount: 6
      forceNiceScale: true
all_series_config:
  stroke_width: 2
series:
  - entity: sensor.x3_daily_mileage
    type: column
    name: KM
    yaxis_id: left
    group_by:
      duration: 1d
      func: max
    show:
      datalabels: false
      extremas: true
  - entity: sensor.x3_daily_mileage
    type: line
    name: KM Avg.
    yaxis_id: left
    group_by:
      duration: 30d
      func: avg
    show:
      datalabels: false
      extremas: false
  - entity: sensor.x3_daily_consumption_l_on_100km
    type: line
    name: L/100 KM
    yaxis_id: right
    group_by:
      duration: 1d
      func: last
    show:
      datalabels: false
      extremas: false
  - entity: sensor.x3_daily_consumption_l_on_100km
    type: line
    name: L/100 KM Avg.
    yaxis_id: right
    group_by:
      duration: 30d
      func: avg
    show:
      datalabels: false
      extremas: false

If you want to check how many liters used during the day you can use the “sensor.x3_liters_used” or "sensor.x3_liters_refuel"

2023-04-11 15_29_05-BMW X3 – Home Assistant

1 Like