Skip to content
ramhee98Public

About

Personal bedtime and wake-up planner built from your Oura Ring data. Calendar export, sleep-debt tracking, and a Streamlit UI. Works out when you should sleep from your own Oura history — not a generic 8 hours — and puts it in your calendar.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

go_to_bed

Work out when you should go to bed and when to get up, from your own Oura Ring history — as an .ics calendar you can subscribe to, and a Streamlit app that shows the reasoning.

Oura's own sleep_time endpoint is not much help here: it returns optimal_bedtime: null with status only_recommended_found on most days. This derives the recommendation from the nights you actually slept well instead.

Tonight

Features

  • 🛏️ Personal sleep need — the median time you actually slept on nights that scored well, not a generic eight hours
  • ⏰ Wake times per day — Monday–Friday, Saturday and Sunday configured separately so a late Sunday start never drags your weekday bedtime with it, plus an optional override for any individual weekday
  • 🛌 Fixed bedtimes, optionally — prefer a set routine to a computed one? Configure bedtimes the same way, per day, and the app still tells you whether that time actually covers your sleep need
  • 📉 Sleep debt — shortfalls over a rolling window pull your bedtime earlier, spread across several nights and capped, so recovery never demands one brutal early night
  • 🎯 Efficiency-aware — the target is time in bed, not time asleep: your sleep need divided by your median efficiency, which folds in how long you take to fall asleep and how long you're awake mid-night
  • 🚧 Guard rails — the recommendation is held inside a configured window, so a bad fortnight can't suggest a 19:00 bedtime
  • 📅 Calendar output — bedtime.ics with "Go to bed" and "Wake up" events and alarms, ready for Google Calendar, Apple Calendar or Outlook
  • 🔔 Stacked reminders — set one lead time or several, so you get an hour's warning and then a final nudge at fifteen minutes
  • 📊 Streamlit app — tonight's plan, how your sleep need was derived, the nights ahead, and how each past night landed
  • ⚙️ Settings page — edit every setting from the browser and write it back to config.py, comments intact. The Oura token is never shown or written there
  • 🎯 Recalibration in the browser — type a few days' sleep debt as the Oura app shows them, see which baseline reproduces them and how closely, and save it in one click. The baseline drifts, so this is a recurring chore, not a one-off
  • 📆 Calendar-aware — point it at one or more .ics feeds and an early first commitment pulls your alarm, and with it that night's bedtime, earlier. A late first event never makes you sleep in
  • 🔌 Pluggable wake times — WAKE_SOURCE selects where wake times come from
  • 🔁 Self-updating config — a run adds any setting config.py.template has that your config.py lacks, with its comment and in the right section. Your values are never touched, so upgrading is just git pull
  • 📦 Cached API responses — the CLI and the app share one on-disk cache, so a cron run and a page load minutes apart cost a single Oura call
  • Degrades gracefully: API errors print a message and return empty rather than raising, and a thin history falls back to a configured default

Requirements

  • Python 3.9+
  • Oura API token with sleep and daily scopes
  • Dependencies:
    • requests, icalendar — for the CLI
    • streamlit, pandas, altair — for the UI

Setup

  1. Clone the repo:

    git clone https://github.com/ramhee98/go_to_bed.git
    cd go_to_bed
  2. Run the installer, which creates the virtual environment, installs the dependencies and copies the config template:

    bash install.sh

    Or by hand:

    python3 -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
    cp config.py.template config.py
  3. Edit config.py: set OURA_TOKEN and your wake times.

Reminders

BED_ALARM_MINUTES_BEFORE and WAKE_ALARM_MINUTES_BEFORE accept several forms:

BED_ALARM_MINUTES_BEFORE = (60, 15)    # two reminders: 1 hour, then 15 minutes
BED_ALARM_MINUTES_BEFORE = 15          # a single reminder
BED_ALARM_MINUTES_BEFORE = "60,15"     # comma-separated string
BED_ALARM_MINUTES_BEFORE = None        # no alarm

WAKE_ALARM_MINUTES_BEFORE = 0          # exactly at the wake time

Each lead time becomes its own VALARM, worded so stacked reminders aren't identical — "Bedtime in 1 hour", then "Bedtime in 15 min". Values are de-duplicated and ordered furthest-out first; anything unreadable is dropped with a warning rather than stopping the run.

Google Calendar ignores these. It discards VALARM entries on calendars subscribed by URL: the events appear, the alarms never fire, and a subscribed calendar has no notification settings to turn them back on. Clients that do honour them include Apple Calendar — leave Remove Alerts unchecked when adding the subscription — and ICSx⁵ on Android. Importing the file into a Google calendar you own works too, but Google then applies that calendar's default reminder rather than the lead times set here.

Calendar-driven wake times

Set WAKE_SOURCE = "calendar" to read your actual commitments:

WAKE_SOURCE = "calendar"

CALENDAR_URLS = [
    "https://example.com/work.ics",
    "https://example.com/lectures.ics",
    "/var/www/html/personal.ics",      # local paths work too
]

CALENDAR_LEAD_MINUTES  = 90        # getting ready + travel
CALENDAR_ONLY_EARLIER  = True      # a late meeting is no reason to sleep in
CALENDAR_EARLIEST_WAKE = "05:00"   # floor, whatever the calendar says
CALENDAR_SKIP_ALL_DAY  = True
CALENDAR_SKIP_FREE     = True

Each day, the earliest qualifying event sets wake = first event − lead time. That flows through the sleep model, so an 08:15 lecture doesn't just move your alarm — it moves the previous night's bedtime too.

It only ever moves the alarm earlier. With CALENDAR_ONLY_EARLIER on, a day whose first event is at 14:00 keeps your configured wake time; only a morning starting earlier than usual pulls it forward. Turn it off to follow the calendar in both directions.

What it deliberately ignores:

Skipped Why
All-day events A birthday or holiday doesn't start your morning
TRANSP:TRANSPARENT Marked free, so not a commitment
STATUS:CANCELLED It isn't happening
Anything before CALENDAR_EARLIEST_WAKE A stray 03:00 entry shouldn't demand a 01:30 start
Zero-length events (CALENDAR_SKIP_ZERO_LENGTH) Point-in-time reminders like "pay rent"
Events shorter than CALENDAR_MIN_EVENT_MINUTES A 15-minute standup rarely justifies an early night
Titles matching CALENDAR_IGNORE_SUMMARIES The recurring entries you don't actually attend

Filtering by title:

CALENDAR_MIN_EVENT_MINUTES = 30      # 0 disables

CALENDAR_IGNORE_SUMMARIES = [
    "lunch",        # matches anywhere: catches "Team Lunch"
    "daily*",       # contains * -> matches the whole title
    "OOO*",
]

Matching is case-insensitive. A plain word matches anywhere in the title; a pattern containing * or ? is matched against the whole title, so "standup*" catches "Standup — backend" but not "Daily standup" (use "*standup*" for that). An event whose length the feed doesn't state is always kept — dropping what can't be measured risks oversleeping, while keeping it only risks a needlessly early night.

A timed event with neither DTEND nor DURATION is zero-length, not unknown: RFC 5545 says it ends at its start. Those are the point-in-time reminders calendars accumulate, and CALENDAR_SKIP_ZERO_LENGTH (on by default) drops them. Genuinely undeterminable lengths — a malformed event pairing a timestamp with a date — are the ones that get kept.

Recurring events are expanded (RRULE), including EXDATE cancellations, so a weekly lecture sets the alarm every week rather than only on its first date.

Feeds are read once per run. A feed that is unreachable, malformed or empty logs a warning and that day falls back to your configured wake time — a calendar being down never stops the run.

Bedtimes

By default the bedtime is computed. To keep a fixed routine instead, set BED_SOURCE = "fixed" and configure it exactly like the wake times:

BED_SOURCE = "fixed"

BED_TIME_WEEKDAY  = "23:00"   # any Mon-Fri night not overridden below
BED_TIME_SATURDAY = "23:30"
BED_TIME_SUNDAY   = "22:30"

BED_TIME_FRIDAY   = "00:30"   # optional per-day override
BED_TIME_MONDAY   = None      # None -> uses BED_TIME_WEEKDAY

The day names the evening you turn in, not the morning you get up: BED_TIME_MONDAY is Monday night, pairing with Tuesday's wake time. A time before noon is read as the small hours of the next morning, so "00:30" on Friday means half past midnight on Saturday.

A fixed bedtime is a decision rather than a suggestion, so neither the sleep debt adjustment nor the EARLIEST_BEDTIME / LATEST_BEDTIME guard rails move it. Your sleep need is still computed, and the calendar event says how the fixed time compares:

⚠️ Fixed bedtime gives 7:20 in bed, 0:38 short of your 7:58 target.

Wake times

Wake times resolve in three layers, most specific first:

WAKE_TIME_WEEKDAY  = "06:30"   # any Mon-Fri not overridden below
WAKE_TIME_SATURDAY = "08:00"
WAKE_TIME_SUNDAY   = "08:00"

WAKE_TIME_WEDNESDAY = "09:45"  # optional per-day override
WAKE_TIME_FRIDAY    = "05:30"
WAKE_TIME_MONDAY    = None     # None -> uses WAKE_TIME_WEEKDAY

A per-day override wins over the weekday default; Saturday and Sunday are set by their own lines. A day left as None, or a time that can't be parsed, falls through to the layer below with a warning rather than stopping the run.

Usage

Generate the calendar:

python3 main.py
Fetching sleep history for the past 90 days...
📦 Using cached sleep (6m ago, 114 record(s)).
📦 Using cached daily_sleep (6m ago, 87 record(s)).
📦 Using cached daily_readiness (6m ago, 31 record(s)).
Learning your personal sleep need...
  Sleep need: 7:13 (from Oura, not derived)
  Efficiency: 93% → 7:45 in bed
  Oura sleep debt 0:30 spread over 7 nights → 0:04 earlier.
  Decay-weighted over 14 days against a 7:13 baseline; nights only.
  Oura sleep balance: 93/100.
Planning the next 14 nights...

  🛏️  Tonight: bed at 22:30, up at 06:20 (7:50 in bed)

Loading existing calendar...
Loaded existing calendar with 30 events.
Kept 2 existing event(s) outside the planning window.
Added 28 planned event(s) for 14 night(s).
Saving calendar to ./bedtime.ics...
Done.

Then subscribe to bedtime.ics from your calendar client so the reminder reaches your phone. Re-running is safe: plans for days in the planning window are replaced, and plans for days already past are kept as a record of what was recommended at the time.

Re-running also leaves unchanged nights alone. Every planned night is rebuilt from scratch, so the new events are compared against the ones already in the file — same times, title, description and alarms — and an unchanged event is written back exactly as it was found. CREATED therefore keeps saying when the event first appeared, LAST-MODIFIED only moves when something really changed, and two runs over an unchanged plan produce a byte-identical file rather than a fortnight of events your calendar thinks were all just edited.

That record is bounded by KEEP_PAST_DAYS (90 by default). Without a limit the file only grows — a daily run adds a night and removes nothing, so a year of cron leaves some 730 events for your phone to download every refresh. Set it to 0 to keep no history at all, or None to keep everything. Only events this tool created are ever removed: anything else sharing the file is passed through untouched, however old.

Explore the reasoning:

streamlit run app.py

Work with the cache:

python3 main.py --no-cache      # ignore cached responses, call the API
python3 main.py --clear-cache   # delete every cached response, then exit

Run the tests:

python3 tests/test_model.py     # or: pytest

How the bedtime is calculated

  1. Sleep need — SLEEP_NEED_SOURCE picks where it comes from:

    • "computed" (default) — the median time asleep on nights scoring GOOD_SLEEP_SCORE or better. Naps are excluded; only Oura's long_sleep counts. If fewer than MIN_GOOD_NIGHTS clear that bar, your own top quartile is used instead, and only if that still isn't enough does FALLBACK_SLEEP_NEED_HOURS apply.
    • "oura" — the need Oura itself reports, set in OURA_SLEEP_NEED_HOURS (7h16 is 7.267). The v2 API does not publish it, so read it off the app. Efficiency and latency still come from your own history; only the need is substituted.

    The two can differ enough to matter: on the account this was built against, computed gives 7:29 where Oura says 7:16, a 13-minute gap that moves every bedtime.

  2. Time in bed — sleep need ÷ median efficiency. Skipped entirely when BED_SOURCE = "fixed".

  3. Sleep debt — shortfalls (never surpluses) over DEBT_WINDOW_DAYS, divided by DEBT_RECOVERY_NIGHTS and capped at MAX_DEBT_ADJUSTMENT_MINUTES. See Sleep debt below.

  4. Bedtime — wake time − time in bed − debt adjustment, held between EARLIEST_BEDTIME and LATEST_BEDTIME.

Sleep debt

DEBT_SOURCE picks where the adjustment comes from:

Source Basis Tonight, on the account this was built against
"oura" (default) Oura's own decay-weighted formula debt 3:20 → 28m earlier
"computed" Only your shortfalls, undecayed debt 7:24 → capped at 60m earlier
"oura_balance" Oura's sleep_balance readiness contributor balance 64/100 → 21m earlier
"none" No adjustment at all —

The Oura formula

L_n = sleep_need - time_slept        (for the night n days ago, signed)
D   = L_0 + L_1(0.93) + L_2(0.93)² + … + L_13(0.93)¹³
D   = max(D, 0), rounded to the nearest 10 minutes

Nights with no data are skipped rather than counted as zero sleep.

Surpluses offset deficits. L is signed and only the total is clamped, so a long night genuinely repays an earlier short one. This was checked against eight figures read from the Oura app, spanning debts from 10 minutes to 7 hours: clamping each night at zero instead overshot by 130-250 minutes on every single one. It is the behaviour that makes the formula work.

Validated against those eight observations with OURA_BASELINE_NEED_HOURS = 7.217: total error 70 minutes across all eight, four of them exact, most of the rest within one rounding step.

Day Oura app This app
1 Sept 420 410
3 Sept 320 320
4 Sept 300 300
12 Sept 30 30
14 Sept 10 10

A caveat on the Oura source. Oura does not publish a sleep debt duration — sleep_time returns optimal_bedtime: null on most days, and nothing in the v2 API gives hours owed. sleep_balance is a 0-100 score covering roughly the last two weeks, so the mapping to minutes is this app's, not Oura's:

adjustment = (100 - sleep_balance) / 100 × MAX_DEBT_ADJUSTMENT_MINUTES

A balance of 100 asks for nothing; 50 asks for half the configured maximum. It is markedly gentler than the computed figure, because it reflects Oura's view of balance rather than a literal tally of hours missed.

Matching Oura's own sleep need

The "oura" source measures against a baseline sleep need, and Oura publishes neither its debt figure nor the need behind it. The result is extremely sensitive to that baseline — the 14-day weights sum to about 9.1, so every minute of baseline error moves the debt by roughly nine minutes.

Read a few days' debt off the Oura app and invert the formula for them. In the browser, the Settings page has a panel for it; from the CLI:

python3 main.py --calibrate-debt 210             # today's figure, in minutes
python3 main.py --calibrate-debt 210,250,240     # today, yesterday, the day before
  Baseline that reproduces it: 7:15 (7.266 hours, naps excluded)
  Fit against each observation:
    2026-09-21  model 200m   app 210m   off -10
    2026-09-20  model 250m   app 250m   match
    2026-09-19  model 250m   app 240m   off +10

Put the result in OURA_BASELINE_NEED_HOURS. OURA_DEBT_INCLUDE_NAPS controls whether naps count toward each day's total; nights-only has fitted better in testing, so it defaults off.

The baseline drifts, so expect to refit it. On this account it was 7:13 in early September and 7:16 by the 21st, and no single value fits both stretches:

Observations 7:13 7:16 Best fit
1–14 Sept (5 readings) error 10 error 90 7:13
19–21 Sept (3 readings) error 70 error 20 7:16
All eight error 80 error 110 7:15, error 70

Each stretch fits its own baseline nearly exactly and the compromise is worse than either, so this is real movement rather than a noisy fit. At ninefold amplification those three minutes are ~27 minutes of debt — enough to notice. When the figure starts looking wrong, refit it.

Recency beats quantity. Feeding the fit everything you have is worse than feeding it two recent days — predicting the 19–21 Sept readings from each set:

Fitted on Error on 19–21
21 Sept alone 30 min
20–21 Sept 20 min
14 + 21 Sept 20 min
All eight readings, spanning 3 weeks 30 min
The five oldest readings 60 min

Two errors pull against each other. The app rounds to 10 minutes, so a reading is ±5 minutes out — worth about 0.6 minutes of baseline, which averaging shrinks. Against that, the need drifts about 1.6 minutes of baseline per week, and an old reading drags the answer toward a window that has already passed. Drift is the bigger term, so use two to four readings from the last week rather than accumulating a history. Consecutive days share 13 of their 14 nights and are nearly the same measurement, which is why the third and fourth readings add so little.

An exact match every day should not be expected: the app rounds to 10 minutes, its sleep need drifts by a minute or two day to day, and the amplification turns either into a visible difference.

The Oura app shows a sleep debt in minutes, but no v2 endpoint exposes that number — it is computed in the app. So when DEBT_SOURCE = "oura_balance" the shortfall tally is still computed here and shown beside the score, giving you a duration to read even though the adjustment comes from the balance:

Sleep debt  7:24
Oura balance 64/100 → -0:21

The computed source

For every night inside DEBT_WINDOW_DAYS:

shortfall = sleep_need - time_asleep
if shortfall > 0:  debt += shortfall

Then, per planned night:

adjustment = min(debt / DEBT_RECOVERY_NIGHTS, MAX_DEBT_ADJUSTMENT_MINUTES)

Only deficits count. A night longer than your sleep need adds nothing — the surplus is discarded rather than credited, so a long Saturday does not repay a short Tuesday. Naps are excluded here too; only Oura's long_sleep counts.

Debt is repaid gradually. Dividing by DEBT_RECOVERY_NIGHTS and capping the result keeps a bad fortnight from demanding one punishing early night.

Two behaviours worth knowing:

  • The figure is computed once from history and applied to every night in the plan. It does not simulate paying itself down as you follow the schedule, so the whole window gets the same adjustment.
  • Because sleep_need comes from your good nights, the bar sits above a typical night. If most of your nights fall short, the cap does the work rather than the arithmetic — raise MAX_DEBT_ADJUSTMENT_MINUTES if you want the full correction.

Keeping config.py up to date

config.py is created once from config.py.template and then drifts as the app gains settings. Each run tops it up:

Added 1 new setting(s) from config.py.template: AUTO_ADD_MISSING_SETTINGS
   Previous version kept at /path/to/config.py.bak

Only missing keys are added — your values, your comments and any settings you added yourself are left alone. Each addition arrives with the comment that documents it in the template, placed in the same section rather than appended to the bottom, so the file stays readable rather than growing a junk drawer.

The .bak is written only when something is actually added, so ordinary runs don't churn. Set AUTO_ADD_MISSING_SETTINGS = False to manage config.py entirely by hand; a missing or unreadable template is a no-op either way, so the sync can never block a run.

Caching

Oura responses are cached on disk so the calendar run and the app don't each pull the same 90 days. Yesterday's nights don't change, and the ring only syncs when it's near your phone, so re-fetching every few minutes buys nothing.

Setting Default What it does
CACHE_ENABLED True Turns the cache off entirely when False
CACHE_TTL_MINUTES 60 How long a response counts as fresh. 0 re-fetches every time without deleting anything
CACHE_DIR ./.cache/oura Where the entries live
CACHE_SERVE_STALE_ON_ERROR True On a failed call, use the cached response even past its lifetime

One .json per endpoint, written 0600. Your token is never stored — only a short hash of it, so pointing the app at a second account can't serve the first account's nights back. The history window is part of the key too, so changing HISTORY_DAYS re-fetches rather than reusing the wrong range.

When a call fails, a complete cached response is preferred over the partial one the failure produced, and the run says so in its output. A failed call never overwrites a good entry.

Refresh data in the app's sidebar clears both layers — the disk cache and Streamlit's in-process one — so the button always means what it says. CACHE_TTL_MINUTES applies to the app's in-process cache too, but Streamlit fixes that when the process starts, so a changed lifetime takes effect on the next app restart.

Pages

The screenshots below are the dark theme. The app follows whichever theme Streamlit is set to — the chart palettes are defined for both, so nothing washes out either way. Force one with streamlit run app.py --theme.base dark, or set it once in .streamlit/config.toml.

Sleep need

How much sleep you need, and the nights that show it. The dose-response curve between time asleep and sleep score is usually clearer than expected.

Sleep need

Schedule

Every planned night from lights-out to alarm, with the weekend shift visible at a glance, plus a download for the .ics.

Schedule

History

How each night landed against your sleep need, and where the debt came from. The window selector covers the last 14 nights, the last 30, or everything you have; options longer than your history are hidden rather than shown empty. The Sleep debt metric is the exception — it stays on DEBT_WINDOW_DAYS, because that is the figure actually driving tonight's bedtime.

History

Settings

Every setting except the token, written straight back to config.py.

Settings

Edits are surgical: only the lines you actually change are rewritten, so comments, ordering and any settings you added by hand survive. A config.py.bak copy is taken before each save, and the file is replaced atomically so an interrupted write can't leave a broken config.

OURA_TOKEN is excluded by design — it is never read into the page and the writer refuses it outright. Values are validated and re-serialised as typed literals rather than pasted as text, so nothing typed into a form can execute when config.py is next imported.

Recalibrating the Oura baseline

Below the settings form is the panel that refits OURA_BASELINE_NEED_HOURS against figures read off the Oura app — the same solver as --calibrate-debt, without the round trip to a terminal.

Recalibration

Fill in two to four recent days — more is not better if they are old, for the reasons above. Debts are accepted in any of the forms the app prints them — 3:30, 3h30m or a bare 210 for minutes — and anything unreadable is reported rather than quietly counted as zero. The days need not be consecutive, which the CLI's positional list cannot express: the app only keeps a fortnight on screen, so the readings you can actually get tend to have gaps.

The fit reports the baseline, the total error and each day side by side, so a bad reading is visible as one row that misses rather than a number you have to trust. Days showing a debt of 0 are dropped and named: any sufficiently low baseline reproduces zero, so they pin nothing and would otherwise pad the error.

Sleep need shown in the app is optional and separate — it writes OURA_SLEEP_NEED_HOURS, the bedtime target used when SLEEP_NEED_SOURCE is "oura", rather than anything the debt fit uses. The two often agree, but they are different numbers and worth keeping distinct.

Adding another wake-time source

WAKE_SOURCE picks where wake times come from — "fixed" and "calendar" ship with the app. To add another (a shift roster, a travel itinerary):

  1. Write a WakeSchedule subclass with wake_time_for(day) and describe(day). Return None for days it has no opinion about, or delegate to FixedWakeSchedule to fall back to the configured time.
  2. Register it in SOURCES in wake/__init__.py.
  3. Add its settings to config.py.template.

Nothing else changes — main.py, the model and the Streamlit pages all talk to the WakeSchedule interface, not to any particular source. wake/calendar.py is the worked example.

Example calendar output

Title:

🛏️ Go to bed (8:44 in bed)

Description:

Bedtime: 21:46
Wake up: 06:30
Time in bed: 8:44
Sleep need: 7:29
Efficiency: 94%
Typical latency: 0:08
Sleep debt adjustment: -0:45

Fixed weekday wake time (06:30).
Sleep need 7:29 at 94% efficiency → 7:58 in bed.
Sleep debt 7:46 → capped at 45m earlier.
Learned from 34 good nights of 80 analysed.

Related

License

MIT — see LICENSE.

About

Personal bedtime and wake-up planner built from your Oura Ring data. Calendar export, sleep-debt tracking, and a Streamlit UI. Works out when you should sleep from your own Oura history — not a generic 8 hours — and puts it in your calendar.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages