I looked at the google_fit component. Had it running for a while. It was really buggy, though. Lots of errors. And data wouldn’t update when I expected it to. Maybe I’ll have a look again.
I also use a Mi Band 3 (I have a Mi Band 4 coming in a few weeks). But couldn’t find a reliable way to get fairly recent sleep data in to Home Assistant. The other downside to a wearable tracker is… if I fall asleep on the couch, at least for my use case, that’s different than going to sleep in bed. The Mi Band can’t tell the difference.
IFTTT is a decent solution for something like “in bed” tracking since a 5-30 seconds delay isn’t going to matter much. The problem with the Withings device, at least for me, is I don’t see enough positive reviews of people integrating with it well to make it worth the $99 (x2… both sides of the bed) price tag.
I had similar issues when I created it. But I think it was because how Mi Fit was pushing the data to Google Fit. Then it sthbilized and worked fine once I got the Mi Band. Although I was only checking weight, not the rest of the data.
It seems Withings has a Sleep API, so it’s an interesting option for me. Where did you see the bad reviews? Would love to read more, before I make another stellar-quality</ irony-off> purchase like the ring.
I didn’t see bad reviews from Home Assistant users. But I didn’t really see any good ones either. Just people struggling to get over the IFTTT hump to make it work.
I have updated the code to solve the delay and fix the issue with the token.
The code is updated on the gist, but it also require sensor config changes and a token file.
I was talking to Oura API team:
They’ve ensured me that the last data is available and, somehow, I can now retrieve previous day data without changes. Hence, delay is no longer required.
As for the token, they return an access token, but also a refresh token. With this one, the server can permanently renew the access token. I have changed the code to automatically refresh the token and it seems to be working. To achieve it, client_id and client_secret is also required. So the new YAML config looks like this:
Unfortunately, I am still not sure how to get the first token on set up, since it requires a redirect back which returns a code and then an extra call. Hence, this would require getting the first JSON manually doing manual API calls and creating a file called oura-token-cache on the config folder.
I am still experiencing intermittent issues where I cannot find the previous day data. As a result, I have added an option to enable the sensor to backfill data.
This way, if we’re looking for “yesterday’s” data, but it didn’t find any; it will look to the previous day.
If we’re looking for Monday data and last Monday is not available, it would look for last week data. At the moment, the backfill will happen for 3 periods (3 days before, or 3 weeks before; depending on the date type).
I will add the full documentation when everything is ready. However, the initial set up is currently the main challenge. I am not sure how to capture the “?code=” to continue the OAuth flow. Any guidance would be nice.
It seems the reason for which the previous day data was not available is because I need to keep the app for a few seconds for it to sync to the Cloud Oura data. Once there, API retrieves it. In other word, it was not an issue with the sensor or API, but with the data not being synced from the app to their servers.
As of right now, the only thing missing for this to be completed would be fixing the initial set up. Then, I will change the initial message to a full explanation on how to use.
I finally managed to install this component via HACS. However, HA log tells that “Oura API was unable to retrieve new API token.” and points to custom_components/oura/api.py:69. I can see data inside hass-config/oura-token-cache-sleep_quality file. Something like this.
{"code": "my_secret_token"}
I tried to HA restart couple of times and updated entity using HA services menu but did solve my problem. Something more I could try?
Well, one more time I deleted the token file, reinstalled custom component, deleted old app from Oura web page, re-created new app etc. And boom, it started to work. I have no idea what was wrong earlier.
Just a quick FYI, I am strongly considering removing HACS support for this custom_component as a protest to this. If it stops working, feel free to integrate manually.
Thanks for this, got it working.
First I didn’t have base_url setting because documentation says its not used any more, but then realized it was just removed and I have older version.
Then I had problem that token file couldn’t be written to disk. I tried to debug that, but eventually hardcoded the file path to /home/pi/.homeassistant and it worked.
In Oura developer page you also have to give permission Allow server-side authentication (grant-type code)