arpanghosh8453 / public-fitbit-projects

A script to fetch data from Fitbit servers using their API and store the data in a local influxdb database.
BSD 4-Clause "Original" or "Old" License
323 stars 29 forks source link

Error in script due to 'sleep level = unknown' #14

Closed mwelkers closed 6 months ago

mwelkers commented 6 months ago

When using the Web API and recent docker-compose.yml to setup the containers the script errors: Traceback (most recent call last): File "/app/Fitbit_Fetch.py", line 580, in get_daily_data_limit_100d(start_date_str, end_date_str) # 1 query File "/app/Fitbit_Fetch.py", line 411, in get_daily_data_limit_100d 'level': sleep_level_mapping[sleep_stage["level"]], KeyError: 'unknown'

Apparantly the data returned by Fitbit contains a sleep level unknown. {"dateTime":"2024-03-31T08:02:00.000","level":"unknown","seconds":3600}],"

Because the script breaks in the container, it also doesn't save anything to the influxdb. Is there a way to catch this error in the code or still save the rest of the data to the influxdb?

arpanghosh8453 commented 6 months ago

Thank you for reporting the issue. It's been fixed in eba14ce70f064460cc08c85117b222ce6762ca3c

The current mapping stands as sleep_level_mapping = {'wake': 3, 'rem': 2, 'light': 1, 'deep': 0, 'asleep': 1, 'restless': 2, 'awake': 3, 'unknown': 4}

So, the 'unknown' level will be mapped to level 4 in the influxdb database. You might need to adjust the Grafana dashboard accordingly. You might set the cap at level 3 to disregard the unknown values.

Please run docker compose pull and docker compose down followed by docker compose up -d to restart the container with the patched container I just released. Your refresh tokens are stored locally with local mount so you won't have to go through the setup again. I hope this patch fixes the issue. Please reopen this thread if not.

Thank you again @mwelkers

mwelkers commented 6 months ago

Can confirm it all works now. Thanks for the quick fix!

arpanghosh8453 commented 6 months ago

No problem at all! Happy to help.