Custom Componenet: Oura Ring sleep data sensor

Content Edited in protest for the bad direction of the Home-Asisstant project.

If you are interested in this component, check the full documentation and code on Github repository.

3 Likes

For a $300 product, they should have an API that gives you whatever data you want.

3 Likes

100% agreed.

This is also why I’ve shared the project in this state, as I most likely will be returning the product and stop updating the logic. The API is a pain to work with, and they have very limited integration with third parties (which could allow pulling this data through them).

1 Like

Sleep data is interesting. My fitness tracker provides this, but only publishes to Google Fit which is difficult to access. For Home Automation purposes, really only the “in bed”/“not in bed” state is all I need. I’ve toyed with a few ways of getting this data, but I’d love a bedside device or an under the sheets device that would handle this at a reasonable cost that didn’t amount to massive jumble of wires under my bed (dogs and robot vacuum cleaners tend to not like these things).

Something like this, perhaps:

Do you use any devices like this?

Thanks a lot for sharing.

For the Google Fit data, have a look at this custom component. It should integrate well.

I do want more data than just sleep vs not. I want to understand sleep cycles, specially.
Withings looks quite interesting. I also want an acceptably priced bed solution.
My concern with Withings is how to extract the data. I need to research on that.
For your use case, it integrates with IFTTT so you can probably use hooks to automate stuff or update sensors. (Big +1 to the cables issue, though).

On my side, I have been using Mi Fit Band 3 and it was fine. Oura has been my second attempt as the band is not super accurate and I only care about sleep, not activity tracker.

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?