Custom Componenet: Oura Ring sleep data sensor

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:

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

  2. 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:

################################################################################
# Oura Ring: Sleep data.
################################################################################

- platform: oura
  name: sleep_quality
  client_id: !secret oura_client_id
  client_secret: !secret oura_client_secret
  scan_interval: 21600  # 6h = 6h * 60min * 60 seconds

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.

Example:

{
  "access_token": "6VZHGOJ27YOGGBOTZOFSAOK2H7FFWVAP",
  "refresh_token": "TMWJD7RL6W46WRTUZCQNOFWKQ2WX4OGNC"
}

I have updated the code on the gist.

The next steps before I am done will be:

  1. Support multiple days (e.g. yesterday, monday, tuesday, 7d_ago)
  2. Support multiple sensors.
  3. If I find a way, a way to do the first OAuth process within Hassio.

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.

1 Like

If someone else is interested in integrating Oura data, I have just published the full code and instructions on Github. I hope it is helpful.

1 Like

Thanks for sharing this!
Can it be used via HACS?

Not at the moment, but the repository is open to contributions and feature requests :wink:

My partner is a Oura Ring user so finding this custom component is exciting. Thanks for all the hard work. Do you have a ‘Buy Me Coffee’?

@adrkable Glad to hear it’s helpful. I have now, but no need :slight_smile:

I have started adding HACS support. You can follow progress in this issue.

Support for HACS seems to be ready :slight_smile: @Jamie_Pryer @adrkable

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?

Hi @villevirtanen

Can you paste your configuration? Did you follow the steps on GitHub to configure and install it?

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.

Anyway, great that this works now. Thanks!

Happy to hear :slight_smile: Enjoy the data!

1 Like

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.

HACS support now fully reverted in protest for the bad direction of the Home-Asisstant project. You can still install the code through manual install from GitHub.

1 Like

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)

1 Like