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
70.53k stars 29.45k forks source link

Fitbit unit configuration in GB and body_value_fn format leads to incorrect values #116010

Open rpaines opened 4 months ago

rpaines commented 4 months ago

The problem

As a user in the UK, the Fitbit integration forces the use of stones as the weight unit, rather than the more useful and standard metric system here. The integration then rounds this value to 1 decimal place, even though the API returns a value accurate to 3 decimal places here. In turn, this means that setting the units to Kg in the Home Assistant UI provides the wrong value because it is converting from this value with reduced precision. Due to the difference in scale between stone and Kg, this leads to a significant discrepancy between the value provided in the Fitbit UI / on the scale (in Kg) and the value provided in Home Assistant. Ideally, I believe that the integration should always request a metric result from the Fitbit API, keeping all provided precision available for conversion to the user's preferred units within the Home Assistant entity.

What version of Home Assistant Core has the issue?

core-2024.4.3

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

Fitbit

Link to integration documentation on our website

https://www.home-assistant.io/integrations/fitbit/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

2024-04-22 10:57:14.674 DEBUG (MainThread) [homeassistant.components.fitbit.api] time_series(body/weight)={'body-weight': [{'dateTime': '2024-04-16', 'value': '13.885'}, {'dateTime': '2024-04-17', 'value': '13.933'}, {'dateTime': '2024-04-18', 'value': '13.98'}, {'dateTime': '2024-04-19', 'value': '13.822'}, {'dateTime': '2024-04-20', 'value': '13.832'}, {'dateTime': '2024-04-21', 'value': '13.842'}, {'dateTime': '2024-04-22', 'value': '13.852'}]}

Additional information

No response

home-assistant[bot] commented 4 months ago

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

Code owner commands Code owners of `fitbit` 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 fitbit` 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)


fitbit documentation fitbit source (message by IssueLinks)

allenporter commented 4 months ago

I came across this code recently when rewriting the code but left the behavior untouched, finding it very strange. I was mostly worried that changing it would break something else accidentally, or there were some norms about UK weights I was unaware of.

Can we just request from the API in metric and then let the home assistant UI display however the user wants it?

Any concerns about cases where this may be working corr correctly today for some users that may change/break?

rpaines commented 3 months ago

So, traditionally people in the UK measure bodyweight in stone (whilst weighing basically anything else in Kg), but this is changing over time and it's just not a unit that converts very well. You also need to convert it to calculate things like BMI. I would suggest that the best thing would be to just get it in metric from the API and let the home assistant UI get it to whatever unit people want to see. It's going to lead to better (or no) conversion for people who want Kg or lbs, whilst still allowing users to have it in stone if preferred. Looking at the Fitbit API documentation, the only measure which is different between the UK and metric options is weight, everything else is just metric in both cases. https://dev.fitbit.com/build/reference/web-api/developer-guide/application-design/#Units I guess this could be a breaking change for users who use stone, although I don't know how the home assistant UI would handle it, would it just transparently introduce the conversion when the integration changes the unit it provides? Worst case scenario, the fix would be for those users to change the unit in the UI.

allenporter commented 3 months ago

It seems to make sense to just change everything as you've suggested and the UI should handle things. (e.g. it should know the "native unit of measurement" and produce the correct UI values). One thing i'm unsure about is how statistics and history will be handled if at all, so I may need to test that out and see what happens.

allenporter commented 3 months ago

@emontnemery I spent a little time looking at what will happen, but wasn't sure so thought you might know: Is it reasonable to just fix the native unit of measurement going forward or do any internal cleanups/repairs need to happen for something like this where the prior native unit of measurement is different?

issue-triage-workflows[bot] commented 3 weeks ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.