This is a Python project that allows you to pull your data from the Oura API using a personal access token, save it to a local PostgreSQL database, then use tools like Grafana to revisualise it. Currently the data being scraped is:
Although last change was made approx 3 months ago - script should be working as of this edit, October 2023.
This tool was created for PERSONAL USE and such is not intended as a production-quality community tool. Use at your own risk. In my configuration, all necessary programs are running under a Rapsberry Pi running . The tool should work on other platforms such as MacOS, Windows, other Linux distriubtions. Support for other platforms will not be provided at this stage.
Installing Postgres is recommended first. For proper documentation, follow instructions for your platform at: https://www.postgresql.org/download/ (or consider using something like brew on macOS) or General instructions:
Update your package list and install postgres
sudo apt update && sudo apt install postgresql
Switch to the postgres user using
sudo -i -u postgres
Access the PostgreSQL terminal by running:
psql
Create the database you will use to host the data (by default this tool calls it 'sleepdb')
CREATE DATABASE sleepdb;
Setting up a virtual environment is recommended to keep the project dependencies isolated. Follow these steps to set up a virtual environment. Note on MacOS you may need to follow some of the steps described in https://stackoverflow.com/questions/34304833/failed-building-wheel-for-psycopg2-macosx-using-virtualenv-and-pip
Change to the project directory:
cd your-repository
Create a virtual environment:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
Install the project dependencies by running the following command:
pip install -r requirements.txt
config-template.ini
file in a text editor.export OURA_SLEEP_CONFIG_PATH="e.g. PATH/TO/YOUR/CONFIG-FILE.ini"
As long as a database exists as defined in your config file, the program will create the necessary table for you.
python3 sleep-data.py
Grafana allows your to visualise data from a range of sources. Follow their guide at: https://grafana.com/docs/grafana/latest/setup-grafana/installation/
Follow this guide for connecting grafana to your postgres server https://grafana.com/docs/grafana/latest/datasources/postgres/
Create a new Visualization on your main Grafana dashboard. Set the data source to PostgreSQL. Under the 'code' query builder, you can now pull out the fields you need.
SELECT score as "Sleep Score", date FROM sleep_sessions WHERE $__timeFilter(date)
python3 sleep-data.py
You can use a tool like cron to run the script every day.