home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.81k stars 28.94k forks source link

Average fuel consumption issue #105565

Open onlize opened 6 months ago

onlize commented 6 months ago

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

home-assistant[bot] commented 6 months ago

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)

G-Two commented 5 months ago

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.

onlize commented 5 months ago

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.

G-Two commented 5 months ago

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.

onlize commented 5 months ago

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

G-Two commented 5 months ago

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!

onlize commented 5 months ago

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

G-Two commented 3 months ago

@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.

onlize commented 3 months ago

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

G-Two commented 3 weeks ago

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

onlize commented 2 weeks ago

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