# Periods

<a id="periods"></a>

A page can have a period under its title for choosing the span of time its cards show: Week, Month and Year, or Last 7 days and Last 30 days. It's one capsule, **This month  Oct 1 – 21**, that names the choice and its days; click it for the choices as pills and a calendar. The cards that read that span follow the choice together. [Spending by period](https://legatobar.com/features#spending) on Features is such a page, with figures an agent wrote; its row works there.

```json
"period": {
  "choices": [
    { "id": "month", "label": "Month", "span": "month" },
    { "id": "week",  "label": "Week",  "span": "week" },
    { "id": "30d",   "label": "Last 30 days", "last_days": 30 }
  ]
}
```

- **A choice says what it means.** `span` is this calendar `day`, `week`, `month` or `year`; weeks start on the Mac's first day of the week. `last_days` is today and the days before it, so `30` is today and the 29 days before. `last_hours` is the last whole hours, this one included: `24` ends with the current hour, so a chart of its hours is always full and its last column is Now. Legato works out the dates each time a request goes out.
- **The first choice is the default.** Each Mac remembers the one picked on each page. Two to six choices, shown as pills at the top of the capsule's calendar.
- **A card follows the period when its source asks for it**, with `{period.start}` and `{period.end}` in a request where it would have written `{date.month_start}` and `{date.month_end}` (see [Dates in requests](https://legatobar.com/docs/source-requests.md#dates-in-requests)). Cards whose sources don't, such as an account balance, stay as they are; put them under a [heading](https://legatobar.com/docs/card-text.md#heading) that says so, like **Current balances**.
- **Choose a view by the selected choice.** In a source, `{period.id}` becomes its choice id, such as `today` or `week`. A derived value `{ "name": "shown", "value": { "template": "{period.id}" } }` lets a card use `when: { "path": "shown", "equals": "today" }`. The id stays the same while stepping. It is available in sources, not card text, and there is no `{compare.id}`. Do not infer the choice from the number of readings: a week may have none or one.
- **A choice can change more than the dates.** Some services keep the same readings at different steps: UniFi's ISP metrics come every 5 minutes for the last day and hourly for 30 days, at different paths. Give each choice `values`, short words its requests take as `{period.<name>}`:

  ```json
  "period": { "choices": [
    { "id": "today", "label": "Today", "last_days": 1, "values": { "interval": "5m" } },
    { "id": "month", "label": "30 days", "last_days": 30, "values": { "interval": "1h" } } ] },
  "sources": { "metrics": { "url": "https://api.ui.com", "secret": "unifi", "auth": { "type": "header", "name": "X-API-Key" },
    "requests": { "metrics": "/v1/isp-metrics/{period.interval}?beginTimestamp={period.start_at}" } } }
  ```

  Every choice names the same ones, up to 4, and each value is 1 to 64 letters, digits, `.`, `_`, `~` and `-`. A name can't be one of the period's own (`start`, `id` and the rest). Like `{period.id}`, they're for sources, not card text, and have no `{compare.…}`.
- **Looking at another period changes nothing else.** Status, gauge and fleet cards always use the default choice, so the page's status and [notifications](https://legatobar.com/docs/notifications.md) don't move when you look at last year, and a sparkline's [`history` rule](https://legatobar.com/docs/mapping-sparklines.md#sparkline-readings) can't follow a period.
- **Step back through the calendar.** With `"step": true`, arrows at the start of the capsule step a `span` choice back a day, week, month or year at a time: **‹ September ›**, or **‹ Tue, Oct 20 ›** for a day. The calendar inside goes straight to any day (or week, month or year): up to a year of days back, or 59 weeks, months or years. **This month** (or week, or year, or **Today**) appears in grey beside the capsule to come back, and ⌘← and ⌘→ step and ⌘T comes back from the keyboard. A page that steps can have a single choice, and `"future": true` lets the arrows go ahead as far, for plans and forecasts. A `last_hours` choice steps back that many hours at a time, **‹ 11 AM – 10 AM ›** with **Now** to come back, and one of 24 takes the calendar too: a day picked there is the 24 hours ending at this hour on that day. Rolling days don't step: there's no "one back" for the last 30 days. A window of hours is for sources that take moments (`{period.start_unix}`, `{period.end_unix}`): History datasets and figures an agent writes for the period still go by whole days. A stepped page comes back to now the next day, and when Legato opens.

  ```json
  "period": { "step": true, "choices": [ { "id": "month", "label": "Month", "span": "month" } ] }
  ```

- **Mark the days that have something.** `marks` puts a dot under each day of the calendar that has rows, a bigger one for a bigger total, so a busy day stands out before it's picked. `ref` names one of the page's sources and `value` is a rule giving rows of `at` (Unix seconds, an ISO 8601 moment or a `YYYY-MM-DD` day) and an optional number `value`. The calendar shows months outside the period, so that source reads a window of its own and can't use `{period.…}`. Drives marks each day by its miles:

  ```json
  "period": { "step": true, "choices": [ … ],
    "marks": { "ref": "car-days", "value": { "each": "results", "fields": { "at": "started_at", "value": "odometer_distance" } } } }
  ```

- **One item of the period.** A page that shows one thing from the period's list, such as the drive on a map, can name it with a param whose `default` is the period's end, `"{period.end_unix}"`, on an integer param. A row's link sets it (`legato://page/tesla-trips?at=1795500000`); while the value falls inside the period shown it stays, and once the period moves past it the default comes back, so the page shows the newest item of the new span. Such a param is for sources only.

- **Compare with the period before.** `"compare": "previous"` adds `{compare.start}` and `{compare.end}` (and their `_at` moments) for a second request: the same stretch of the period before, so October so far, Oct 1 – 21, is compared with Sep 1 – 21, not all of September. A whole span compares with the whole one before, and the last 30 days with the 30 days before them. The capsule is followed by **vs Sep 1 – 21** when there's room. A card's own text can name the days too, with `{period.dates}` and `{compare.dates}`:

  ```json
  "requests": {
    "now":    "/v2/summary?start_date={period.start}&end_date={period.through}",
    "before": "/v2/summary?start_date={compare.start}&end_date={compare.end}"
  }
  ```

  ```json
  { "id": "spent", "type": "stat", "label": "Spent", "unit": "USD",
    "source": { "ref": "money", "map": { "value": "now.total", "delta": "change" } } }
  ```

  Work out `change` with a [derived value](https://legatobar.com/docs/mapping.md#derived-values) that subtracts `before.total` from `now.total`. While the period is still going, `{period.end}` is still its last day (Nov 30); a service that returns rows dated ahead, such as scheduled transactions, counts those against a comparison that stops at today's date last month. Use `{period.through}`, today while the period is going, for a like-for-like pair.

- **Written by an agent.** A card without a source can follow the period too, since the page has to carry its figures: whoever wrote them isn't there when someone picks another choice. One value a day, with `daily`, suits anything that adds up, such as spending or steps. Legato works out the period's figure from it, a sparkline's points, and columns by day, week or month. Longer rolling ranges group adjacent months to keep the whole range in the chart; each bucket is labelled with its first month and year. A current chart counts readings only through today. `aggregate` is `sum` (the default), `mean`, `last` (a balance), `min` or `max`, and with the period's `compare`, `"compare": true` makes a stat's delta the change from the stretch before.

  ```json
  { "id": "spent", "type": "stat", "label": "Spent", "unit": "USD",
    "daily": { "values": [["2026-10-01", 62], ["2026-10-02", 148]], "aggregate": "sum", "compare": true } }
  ```

  Figures that don't add up, such as an average or a count of distinct things, go in `periods`: the card's values for each choice, each entry saying which days it covers. The entry whose `from` is the first day showing is used, so stepping back finds September if it was written. With nothing for the days showing, the card says so rather than show another period's numbers; on the 1st of a month not yet written, last month's entry shows with **Covers Oct 1 – 31** under it until it's replaced. A rolling choice's days move every day, so its entry matches only on the day it's written, and shows with **Covers …** after that until it's written again. Every entry is checked when the page is read, whichever choice shows.

  ```json
  { "id": "avg", "type": "stat", "label": "Average order", "unit": "USD", "periods": [
    { "choice": "month", "from": "2026-10-01", "to": "2026-10-21", "updated": "2026-10-21T09:40:00-05:00", "value": 38.2 },
    { "choice": "week",  "from": "2026-10-18", "to": "2026-10-21", "value": 41.5 } ] }
  ```

- **A link carries the period.** Following a card's [link](https://legatobar.com/docs/card-fields.md#links-to-other-pages) to another page with a period opens it on the same kind of choice, stepped as far back: Week on Oct 18 – 24 on Health opens Steps on Week, Oct 18 – 24. Choices match by what they mean (`"span": "week"`, `"last_days": 7`), not by their ids or labels. If the page can't step that far it goes as far as it can, and a page with no choice of that kind keeps its own. It only goes forward: Back returns to the first page as you left it, and a page opened from the sidebar, Spotlight or Shortcuts shows its own choice. Give [detail pages](https://legatobar.com/docs/detail-pages.md#detail-pages) the choices their overview has, so every link keeps the period.
- **Each choice is its own fetch.** Only the period showing is fetched, along with the default when a status, gauge or fleet card reads it. Switching back to a period you looked at shows its last answer at once while it's fetched again. A new day or time zone clears period answers before they can appear under new dates. Shortcuts' Get Card Value and page summaries always read the default choice at its current dates, without changing the period showing in the panel.

