jnxxx / homeassistant-connectedcars_io

Home Assistant integration for connectedcars.io (Min Volkswagen)
17 stars 2 forks source link

`Economy` in prerelease v1.1.0 #9

Closed mm98 closed 1 year ago

mm98 commented 1 year ago

Hi,

I've been using v1.1.0 for a couple of weeks now and something is "wrong" with the economy calculations (I think). If you look at the screenshot below. My car has a tank size of 54L. Which would mean, that the total range traveled since last refuel is 595,1 km with the usage of 40L fuel. Economy would be 14,88 km/l (595,1 / 40) ? Are these values calculated in the code or are these existing data from connectedcars?

image

jnxxx commented 1 year ago

The Economy value is coming from the Connectedcars API. I cannot say how it is calculated, but I too think it seems a bit low.

I tried to calculate it myself, but reverted to the one from the API when I realized how slow totalTripStatistics updates. Furthermore I notice with my car the Tank level is not straight proportional. In the upper range it drops slowly, and in the lower range quickly...

Once I have "mileage since refuel" working I will do another attempt. Otherwise we will have to make do with the value from the API.

mm98 commented 1 year ago

I did some testing with the updated v1.1.0 and the audi_a5_mileage_since_refuel is fast and 100% on point now (compared to my own custom fuel usage sensor). This also means, that it is possible to calculate the economy much more exact now (and without knowing the fuel tank size. Since it can roughly be calculated by the fuel percentage and with a bit of rounding).

{%- set fuelLevel      = states("sensor.audi_a5_fuellevel") | float(0) -%}
{%- set fuelPercentage = states("sensor.audi_a5_fuelpercentage") | float(1) -%}
{%- set fuelTankSize   = (fuelLevel / fuelPercentage * 100) | round(0, 'floor') -%}
{%- set fuelUsed       = (fuelTankSize - fuelLevel) | float(1) -%}
{%- set mileage        = states("sensor.audi_a5_mileage_since_refuel") | float(0) -%}

{{ (mileage / fuelUsed) if mileage > 0 and fuelUsed > 0 else 0 | round(2) }}
- platform: template
  sensors:
    audi_a5_economy:
      friendly_name: "Audi A5 Economy (Fuel)"
      unique_id: audi_a5_economy
      unit_of_measurement: km/l
      availability_template: >
        {{ has_value("sensor.audi_a5_mileage_since_refuel") }}
      value_template: >
        {%- set fuelLevel      = states("sensor.audi_a5_fuellevel") | float(0) -%}
        {%- set fuelPercentage = states("sensor.audi_a5_fuelpercentage") | float(1) -%}
        {%- set fuelTankSize   = (fuelLevel / fuelPercentage * 100) | round(0, 'floor') -%}
        {%- set fuelUsed       = (fuelTankSize - fuelLevel) | float(1) -%}
        {%- set mileage        = states("sensor.audi_a5_mileage_since_refuel") | float(0) -%}

        {{ (mileage / fuelUsed) if mileage > 0 and fuelUsed > 0 else 0 | round(2) }}
      icon_template: mdi:gas-station-outline
Old  :  9.90 km/l (from connectedcars)
New  : 14.00 km/l

Thanks for the update!

jnxxx commented 1 year ago

Thanks for the feedback, I am glad it works. 👍

Nice you found an alternative method to calculate mileage per liter, that works better for you. I doubt it will work in any case though. Firstly it assumes the tank is filled up. Maybe it is often the case, but not guaranteed. Secondly, with my car it would not work well:

In the API we can query for the tank size of the car. However, mine just reports null. If you try to enable debug logging, and then look for "fuelTankSize": null or hopefully some size reported. If reported, I can easily add it as an attribute you can read, instead of doing the percentage calculation.

mm98 commented 1 year ago

In the API we can query for the tank size of the car. However, mine just reports null. If you try to enable debug logging, and then look for "fuelTankSize": null or hopefully some size reported.

It is also null by me. No problem at all, i'll just add a fixed state (54). Thanks for all the work you do!

{
   "data":{
      "viewer":{
         "vehicles":[
            {
               "primary":true,
               "vehicle":{
                  "odometer":{
                     "odometer":33250
                  },
                  "odometerOffset":null,
                  "id":"{REDACTED}",
                  "vin":"{REDACTED}",
                  "licensePlate":"{REDACTED}",
                  "name":"Audi A5 SB PRE Plus 40 TFSI 204 HK S tro",
                  "brand":"audi",
                  "make":"Audi",
                  "model":"A5",
                  "year":2021,
                  "engineSize":null,
                  "avgCO2EmissionKm":244.84023048716605,
                  "fuelEconomy":9.545,
                  "fuelType":"gasoline",
                  "fuelLevel":{
                     "time":"2023-07-11T15:13:10.000Z",
                     "liter":29
                  },
                  "refuelEvents":[
                     {
                        "litersAfter":54,
                        "time":"2023-06-30T14:10:52.000Z"
                     }
                  ],
                  "fuelTankSize":null,
                  "fuelPercentage":{
                     "percent":53,
                     "time":"2023-07-11T15:15:11.000Z"
                  },
                  "adblueRemainingKm":[

                  ],
                  "chargePercentage":null,
                  "highVoltageBatteryTemperature":null,
                  "ignition":{
                     "time":"2023-07-11T15:16:52.748Z",
                     "on":false
                  },
                  "lampStates":[
                     {
                        "type":"exhaust",
                        "time":"2023-07-11T13:44:57.000Z",
                        "enabled":false,
                        "lampDetails":{
                           "title":"Motorfejl",
                           "subtitle":"Advarsel"
                        }
                     },
                     {
                        "type":"brake_fluid",
                        "time":"2023-07-11T13:44:58.000Z",
                        "enabled":false,
                        "lampDetails":{
                           "title":null,
                           "subtitle":null
                        }
                     },
                     {
                        "type":"coolant",
                        "time":"2023-07-11T13:44:58.000Z",
                        "enabled":false,
                        "lampDetails":{
                           "title":"K\u00f8lev\u00e6skeniveau",
                           "subtitle":"Advarsel"
                        }
                     },
                     {
                        "type":"wipers",
                        "time":"2023-07-11T13:44:58.000Z",
                        "enabled":false,
                        "lampDetails":{
                           "title":"Viskere",
                           "subtitle":"Advarsel"
                        }
                     }
                  ],
                  "outdoorTemperatures":[
                     {
                        "celsius":24,
                        "time":"2023-07-11T15:16:14.000Z"
                     }
                  ],
                  "position":{
                     "latitude":0,
                     "longitude":0,
                     "speed":0,
                     "direction":0
                  },
                  "service":{
                     "predictedDate":"2025-03-02"
                  },
                  "latestBatteryVoltage":{
                     "voltage":12.376,
                     "time":"2023-07-12T05:57:31.000Z"
                  },
                  "health":{
                     "ok":true,
                     "recommendation":null
                  }
               }
            }
         ]
      }
   }
}