core icon indicating copy to clipboard operation
core copied to clipboard

Average fuel consumption issue

Open onlize opened this issue 1 year ago • 13 comments

The problem

Subaru Average fuel consumption sensor does not show numbers between 26.7 and 27. Every time when I have 26.7, it shows correct information. When it goes to 26.8 or 26.9, it shows 27. When the Average fuel consumption drops to 26.7, it shows 26.7. After that, if I go higher, it shows 27.

What version of Home Assistant Core has the issue?

I set it up with 2023.9.x, updated every time, and now it is 2023.12.1 with the same issue.

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Subaru

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

onlize avatar Dec 12 '23 14:12 onlize

Hey there @g-two, mind taking a look at this issue as it has been labeled with an integration (subaru) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of subaru can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign subaru Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


subaru documentation subaru source (message by IssueLinks)

home-assistant[bot] avatar Dec 12 '23 15:12 home-assistant[bot]

It looks like it is probably a rounding error during the conversion from the native units (liters per 100km) to miles per gallon.

https://github.com/home-assistant/core/blob/5b3e1306f82b2d370154e57657250b3107ae768a/homeassistant/components/subaru/sensor.py#L239C17-L239C17

In the past, the subaru API did not provide MPG, so the calculation had to be done. Looking at the most recent data, it looks like they now provide MPG so we should be able to avoid the rounding error in a future release.

G-Two avatar Jan 15 '24 18:01 G-Two

I am not sure that is the problem. The average was going down. Started at 26.7. It ignored 26.6, it ignored 26.5 and today it finally dropped to 26.4 from 26.7.

onlize avatar Jan 18 '24 17:01 onlize

It's not ignoring it, that's just the result of the lossy conversion from l/100km to mi/gal. The Subaru API reports avgFuelConsumptionLitersPer100Kilometers in 0.1 liter increments, which results in mi/gal conversions that change in ~0.3 mile increments. I've reproduced the error below.

Python 3.11.6 (main, Oct 28 2023, 15:08:23) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from homeassistant.util.unit_conversion import DistanceConverter, VolumeConverter
>>> from homeassistant.const import UnitOfLength, UnitOfVolume
>>> 
>>> L_PER_GAL = VolumeConverter.convert(1, UnitOfVolume.GALLONS, UnitOfVolume.LITERS)
>>> KM_PER_MI = DistanceConverter.convert(1, UnitOfLength.MILES, UnitOfLength.KILOMETERS)
>>> 
>>> L_PER_GAL
3.785411784
>>> KM_PER_MI
1.6093439999999999
>>> 
>>> #https://github.com/home-assistant/core/blob/5b3e1306f82b2d370154e57657250b3107ae768a/homeassistant/components/subaru/sensor.py#L239C17-L239C17
>>> #round((100.0 * L_PER_GAL) / (KM_PER_MI * avgFuelConsumptionLitersPer100Kilometers), 1)
>>>
>>> round((100.0 * L_PER_GAL) / (KM_PER_MI * 8.7), 1)
27.0
>>> round((100.0 * L_PER_GAL) / (KM_PER_MI * 8.8), 1)
26.7
>>> round((100.0 * L_PER_GAL) / (KM_PER_MI * 8.9), 1)
26.4
>>> 

The good news is that the Subaru API now appears to directly report avgFuelConsumptionMpg in addition to avgFuelConsumptionLitersPer100Kilometers, so we should be able to use that value directly instead of the lossy calculation.

Before I can implement the change, I'll need to see your HA generated device diagnostics report during a time when the HA reported fuel consumption state doesn't exactly match what your actual vehicle is showing. Please paste or attach the diagnostics info to this issue.

G-Two avatar Jan 20 '24 18:01 G-Two

I'd be happy to do this, but please tell me how.

onlize avatar Jan 21 '24 10:01 onlize

During a time when the fuel consumption is incorrect, navigate to: Settings -> Devices & services -> Subaru -> 1 Device -> Download diagnostics

image

Check the file contents to ensure it doesn't contain any information that should be redacted, then attach to this issue.

Thanks!

G-Two avatar Jan 21 '24 16:01 G-Two

Here is the file. The car shows 26.3, but HA shows 26.4. subaru-c6b0503fe64918d3fb4d68dd4b5f22c6-2023 Forester Sport-3cf33d68e8c8aae1686426a46491e39c.json.txt

onlize avatar Jan 21 '24 20:01 onlize

@onlize unfortunately the diagnostics file wasn't helpful since it only shows the data after it has been processed by subarulink and home assistant (i.e. it's the same incorrect data). I just submitted a PR #114119 that will return all the raw API data from Subaru, which will help. Once that PR is added to the next HA release, you'll be able to grab a more detailed diagnostic that shows every bit of data. This will help me see where to get a more accurate fuel consumption value.

G-Two avatar Mar 24 '24 17:03 G-Two

Okay, thank you. Please let me know when you want me to send you new logs.

onlize avatar Mar 25 '24 17:03 onlize

@onlize the PR has been merged, please send new device diagnostics dump, and I can look into it with more detail now.

G-Two avatar Jun 11 '24 04:06 G-Two

Hi, sorry for delay. Here is the latest diagnostic. subaru-c6b0503fe64918d3fb4d68dd4b5f22c6-2023 Forester Sport-3cf33d68e8c8aae1686426a46491e39c.json

onlize avatar Jun 20 '24 20:06 onlize