Sleep in Datler is one thing you log and several things read from it. What you log is Sleep: one interval per sleep, from Bedtime to Waketime, each end logged when it happens. A nap is another Sleep entry, and so is the second half of a broken night. Everything else, how long the main sleep was, whether you napped, when you went to bed, is a derived trackable reading Sleep’s entries. Nothing is logged twice, and changing how it is read never changes what was logged. Sleep is one interval rather than two moments paired up afterwards: each end is logged when it happens anyway, an interval can have only its end, and one entry per sleep has a place for fields of its own, which a moment does not.
The module is sleepModule in packages/core/src/sleep-module.ts. Like every module it is a configuration of the general primitives, with nothing about sleep that a person’s own trackable could not have (Modules).
What you log
Sleep is a trackable with one interval field, Time (Intervals):
- its ends are called Bedtime and Waketime;
- it is dated by its end, so a night belongs to the morning it ended and a 00:30 bedtime stays with that night;
- an open sleep is a Still running issue from the moment it starts, which you ignore while asleep, and after 16 hours it is stale and the issue says it has no waketime (Issues);
- its own Reading is a Sum, every sleep of the day added up.
When it joins the Rundown it is asked twice (The Rundown): Waketime first thing, at 07:00, which asks for the end, and Bedtime last thing, at 22:30, which asks for the start. Logging Bedtime opens a sleep. Waketime stops the open one, through the same button a running entry shows, or, when none is open, offers a sleep ending at the moment, with its Bedtime to type or to leave empty. A forgotten bedtime has no path of its own: the sleep is logged with only its end, and an issue says it has no bedtime. A sleep missing an end has no length, so that night’s Main sleep is unknown, never 0, and a day’s sleep totals read as at least what is known (≥).
What is read from it
| Trackable | What it reads | Kind |
|---|---|---|
| Main sleep | the day’s main sleep, by the rule below | a duration |
| Other sleep | every sleep that day except the main one, summed | a duration |
| Napped | whether there was any sleep besides the main one | yes or no |
| Total sleep | every sleep that day, summed | a duration |
| Bedtime | Main sleep’s start, as a time of day | a time |
| Waketime | Main sleep’s end, as a time of day | a time |
Bedtime and Waketime follow Main sleep, so a nap moves neither. A bedtime the evening before reads as a negative time from the morning’s midnight and is charted noon to noon, so late nights sort after early ones (Time of Day). Durations read as hours and minutes (“7h 10m”). “Other sleep” and “Napped” differ on purpose: the amount keeps a neutral name, and the yes or no question uses the word people say.
The main-sleep rule
Main sleep is a Reading of Sleep with two list steps (Readings): first coalesce, which joins sleeps less than an hour apart into one, across midnight too, so a night broken by being up at 3 is one sleep whose length is the time asleep; then maxBy over the length, which picks the longest that meets the rule. The rule is one of:
- the longest sleep that ended that day (the default);
- the longest of at least some hours, so a long nap on a day with no night is not taken for the night;
- the one running at a clock time, on the day it ended or the day before: 03:00 finds the night and 14:30 the afternoon nap, which suits a sleep pattern no single hour of the day frames.
The rule is a choice in Main sleep’s own form, and so is how far apart two sleeps may be and still join, in minutes; 0 joins nothing. Other sleep and Napped read Main sleep’s rest, so they follow the same rule without repeating it.
Total sleep adds up the sleeps as logged and joins nothing, so a night broken across midnight (woken at 23:50, asleep again at 00:20) counts its first piece toward Total sleep on the earlier day, while Main sleep and Other sleep count the joined night on the later one. On those two days Total sleep and Main sleep plus Other sleep disagree.
Presets
A module can offer presets: choices of which derived trackables exist, never of what is logged (Modules). Sleep has two:
- Nights and naps: Main sleep, Other sleep, Napped, Total sleep, Bedtime and Waketime.
- Just the total: Total sleep alone.
Setup asks Sleep’s two questions under its card, the preset and what counts as the main sleep, both defaulted. Settings › Modules switches the preset later. It shows what the switch would add, remove and keep before anything changes, and changes nothing until confirmed. A switch never touches what was logged; a trackable it would drop is kept, and says why, if it was edited by hand (its name, formula or zones changed) or if something kept still reads it. The main-sleep rule is recorded with the install, so a Main sleep removed by one switch comes back with its rule on the next.
In the demo profiles
Chad and John log their sleeps as intervals. Chad now and then forgets one end, a waketime with no bedtime or a bedtime never closed, so the issues show; a few of his nights are broken at 03:10 and read as one main sleep; he naps after a short night; and his Snoozed is a zone on Waketime from 06:30, read back as a yes or no for each day (Time of Day).
Related
Intervals · Readings · Time of Day · Modules · The Rundown · Derived Trackables · Issues