# Expiry and slices of time

### Expiry

Certificates, domains, subscriptions and keys expire. `expires` reads how many days are left (`days`) or when it expires (`date`), and `as` says what it gives:

- `state`: `ok`; `warning` from `warning` days left (14 by default); `critical` from `critical` days left (0 by default, so due today) and once it has expired. `unknown` when there's no input, unless the rule has its own `default`.
- `text`: "Expires today", "Expires tomorrow", "9 days left"; "Expired today", "Expired yesterday", "Expired 3 days ago".
- `days`: whole days left. 0 is today, whether it expires later today or expired earlier today; negative from the day after.

```json
{ "expires": { "days": "monitor_cert_days_remaining" }, "warning": 14, "as": "state" }
{ "expires": { "date": "renews_at" }, "as": "text" }
```

- **Days** are calendar days on this Mac, from today to the day it expires, as the `day` filter says "Tomorrow". A certificate expiring at 2 AM UTC on 9 November expires today on the 8th in New York.
- **`days`** is a number, or text that reads as one (`"12"`). A fraction counts toward zero: 13.9 is 13 days left, and -0.4 expired today.
- **`date`** is read as the [`date` rule](https://legatobar.com/docs/history-dates.md#dates) reads dates: Unix time, ISO 8601, a plain date, a date and time with no zone (as this Mac's time), carriers' words, and a feed's dates. A date with no time is good through that day; a moment has gone once it's now.
- **`warning` and `critical`** are whole days from 0 to 3650, and `critical` is no more than `warning`.
- **A missing input**, or one that isn't a number or a real date (`2026-02-30` isn't), gives nothing, so `default` applies.
- **When it's worked out:** a card's `map` works it out each time the page is drawn, and `derive` when the source answers. When the day changes, pages draw again and a source whose `derive` counts to a date is read again.
- It can't be used in a history source's [`row`](https://legatobar.com/docs/history-reading.md#only-what-the-page-needs-row), since a kept value can't follow the days as they pass. Name its input in `row` and use it in `derive`.

A token's own expiry date can come from Settings → Secrets rather than the service: give the source `"secret_expires": "token_expires"` and read `token_expires` like any date. Put the rule in the card's `map` and `when`, not `derive`, so the days keep counting while the service doesn't answer. A card that warns only in the token's last 14 days, and a note while no date is saved:

```json
{ "id": "token", "type": "status", "label": "API access token", "state": "unknown",
  "source": { "ref": "api",
              "when": { "value": { "expires": { "date": "token_expires" }, "as": "state" }, "in": ["warning", "critical"] },
              "map": { "state": { "expires": { "date": "token_expires" }, "as": "state" },
                       "detail": { "expires": { "date": "token_expires" }, "as": "text" } } } }
{ "id": "token-date", "type": "text", "body": "Add the token's expiry date in Settings → Secrets.",
  "source": { "ref": "api", "when": { "path": "token_expires", "exists": false } } }
```

Uptime Kuma sends `monitor_cert_days_remaining` for HTTPS monitors only, so a fleet's state gives a `default` of `ok`, or every other monitor would be `unknown`. A certificate with 0 days left is `critical` (red), as is an expired one. An invalid certificate isn't an expiring one, so that keeps its own test:

```json
"state": { "if": { "path": "monitor_cert_is_valid", "equals": 0 },
           "then": { "template": "critical" },
           "else": { "expires": { "days": "monitor_cert_days_remaining" }, "as": "state", "default": "ok" } }
```

A list of certificates, soonest first:

```json
"items": { "each": "monitors", "where": { "path": "monitor_cert_is_valid", "in": [0, 1] },
           "sort": "monitor_cert_days_remaining", "limit": 4,
           "fields": { "label": "monitor_name",
                       "value": { "expires": { "days": "monitor_cert_days_remaining" }, "as": "text" } } }
```

A card that shows only while a certificate needs attention tests `"not": "ok"`. A missing field is `unknown`, so it shows the card too; `"equals": "critical"` would hide it.

```json
"when": { "value": { "expires": { "days": "cert_days" }, "as": "state" }, "not": "ok" }
```

### Slices of time

`keep` beside `buckets` chooses what each run keeps. A value list takes `"max"`, `"min"`, `"sum"`, `"average"` or `"last"`. The default is average. A template or fields list takes `{"max":"path"}` or `{"min":"path"}` to keep the row with the highest or lowest number at that path. Ties keep the earlier row. A row with no number there loses to one that has one. The default is the last row. For rain, `{"each":"one.minutely","value":"precipitation","buckets":12,"keep":"max"}` keeps a five-minute shower that an average would flatten.

`buckets` gathers rows by count, which suits a steady series. For a strip of an item's [samples](https://legatobar.com/docs/history-collect.md#samples), where an hour with no sample has to show as one, add `by_time`: the span is cut into `buckets` equal slices of time, and each row goes in the slice its time falls in.

```json
"segments": { "each": "kept.rows", "by_time": { "at": "at", "span": "kept.span" }, "buckets": 96,
              "fields": { "state": { "worst": "row.state", "of": "rows" }, "label": { "template": "{from|time}" } } }
```

- **`by_time`** is `{ "at": path, "span": path }`: where each row's time is, and where the answer has the span, an object with `from` and `until`. A dataset source of samples answers with both: each row's `at` and `kept.span`. Times are ISO 8601 (as samples are), Unix seconds, or any instant the [date rules](https://legatobar.com/docs/history-dates.md#dates) read.
- **`span`** can also be written out, `{ "from": …, "until": … }`, in Unix seconds or instants: the page's own period, `{ "from": "{period.start_unix}", "until": "{period.end_unix}" }`, for an answer that doesn't say its span. With `"keep": "sum"` and `value`, an empty slice is 0 rather than nothing, so a count over time keeps every slice: one series per client from Pi-hole's clients over time.
- **`last_hours`** in place of `span` makes the span the last that many whole hours (1 to 168), ending with the one this is, one slice an hour unless `buckets` says. For a log the service answers as rows, such as an hour-by-hour strip of one domain's queries over a day: `"by_time": { "at": "time", "last_hours": 24 }`.
- **Every slice is made,** empty or not, oldest first, and each is `{ "from", "until", "count", "rows" }`: `fields` and `template` read the slice, so `count` is its rows and an aggregate `"of": "rows"` reads them. A row whose time is outside the span, or at its `until`, is left out.
- **`buckets`** is 2 to 240 slices; without it, `by_time` makes 144, ten minutes each of a day. A [strip](https://legatobar.com/docs/card-time.md#strip) takes up to 240, so a day of 5-minute samples (288) needs slices of at least two of them.
- **`value`** averages each slice's numbers, and an empty slice gives nothing, so a [sparkline](https://legatobar.com/docs/card-numbers.md#sparkline) of the same rows closes its gaps up as sparklines do. The strip draws them.
- `where` applies first. `by_time` doesn't take `group`, `sort`, `limit`, `last`, `collapse`, `flatten`, `entries` or `lookup`: its slices are in time order, each one there.

**`worst`** is the worst state among rows: `{ "worst": "row.state", "of": "rows" }`. States rank `ok`, `off`, `unknown`, `warning`, `critical`, so a slice with one `critical` sample is `critical`. A row whose value is missing, or isn't one of these, counts as `unknown`, and no rows at all gives `none`, the strip's empty slice. `off` ranks above `ok` here (the page's status counts it as `ok`), so a slice where a guest was off shows that it was.

### Calendar months

`by_month` makes one slice per calendar month instead: the last `months` months up to this one, oldest first, each one there whether it has rows or not. It's for a service that leaves out months with nothing in them, such as Immich's photos per month, so a columns card still shows a month with none.

```json
"items": { "each": "buckets", "by_month": { "at": "timeBucket", "months": 12 },
           "fields": { "label": { "template": "{from|month}" }, "values[0]": { "sum": "count", "of": "rows" } } }
```

- **`by_month`** is `{ "at": path, "months": 1 to 36 }`: where each row's time is, and how many months, the current one last.
- **Each month** is `{ "from", "until", "count", "rows" }`: `from` is its first day and `until` the next month's, as plain dates (`2026-11-01`), so `{from|month}` gives "Nov". `count` is its rows, and an aggregate with `"of": "rows"` reads them; a `sum` over a month with no rows is 0.
- **A plain date or month, or a time with no zone** (`2026-11-01`, `2026-11`, `2026-11-06T16:46:00`) counts in the month it's written in. A moment with a zone, or Unix seconds, is read in the Mac's time zone, so a photo at 00:30 UTC on 1 December is November's in Chicago.
- `where` applies first. `by_month` doesn't take `buckets`, `by_time`, `group`, `sort`, `limit`, `last`, `collapse`, `flatten`, `entries` or `lookup`.

### Calendar days

`by_day` makes one slice for each calendar day from `from` through `through`, oldest first, each one there whether it has rows or not. It's for charting each day from a service that lists events rather than days, such as Stripe's payments: columns for the last week, or a line through the days of the page's [period](https://legatobar.com/docs/periods.md#periods) (a columns card takes 48 at most).

```json
"items": { "each": "moves.data", "where": { "path": "type", "equals": "charge" },
           "by_day": { "at": "created", "from": "{date.days_ago_6}", "through": "{date.today}" },
           "fields": { "label": { "template": "{from|weekday}" }, "values[0]": { "sum": "amount", "of": "rows" } } }
```

- **`by_day`** is `{ "at": path, "from": day, "through": day }`. Each day is a plain date (`2026-11-01`) or one token: `{period.start}` and `{period.end}` on a page with a period, or a [date token](https://legatobar.com/docs/source-requests.md#dates-in-requests) such as `{date.days_ago_29}` and `{date.today}`. They're worked out as the rule runs, so the chart moves with the period and the calendar. Both days are included, up to 400 days.
- **Each day** is `{ "from", "until", "count", "rows" }`: `from` is the day and `until` the next, as plain dates, so `{from|date}` gives "Nov 3" and `{from|weekday}` "Tue". A `sum` over a day with no rows is 0.
- **A plain date, or a time with no zone,** counts on the day it's written. A moment with a zone, or Unix seconds, is read in the Mac's time zone, so a payment at 02:00 UTC on 4 November is the 3rd's in Chicago. A service's own dashboard may count days in another zone.
- A day with no rows looks the same as a quiet day. When the list behind it may be cut short (a [list across pages](https://legatobar.com/docs/source-requests.md#a-list-across-pages) that stopped at its `max`), say so on the card, since its earliest days would look empty.
- **A running total** draws a month so far, as Stripe's own dashboard does: derive the days with their totals, then list those with `"running": true`, which makes each number itself plus every one before it. A day with nothing adds nothing, so the line stays level.

  ```json
  "derive": [ { "name": "days", "value": { "each": "moves.data", "by_day": { "at": "created", "from": "{period.start}", "through": "{period.through}" },
                                           "fields": { "day": "from", "gross": { "sum": "amount", "of": "rows" } } } } ]
  ```
  ```json
  "points": { "each": "days", "value": "gross", "running": true }, "labels": { "each": "days", "template": "{day|date}" }
  ```

- `where` applies first. `by_day` doesn't take `buckets`, `by_time`, `by_month`, `group`, `sort`, `limit`, `last`, `collapse`, `flatten`, `entries` or `lookup`.

