# Conditions

<a id="conditions"></a>

Conditions appear in `where`, `if` and a button's `when`. Each has a `path` (or a `value`, which is any rule) and one test: `equals`, `not`, `in` (a list), `not_in`, `gt`, `gte`, `lt`, `lte`, `contains`, `in_list` (a [list](https://legatobar.com/docs/lists.md#lists) in Settings → Lists) or `exists`. `"1"` and `1` count as equal, and a missing value meets `not`.

`exists: true` holds when there's a value, and `exists: false` when there's none. Null counts as none, as GitHub's `"latestRelease": null` says there's no release; `""`, `0`, `false` and an empty list are values. Use it rather than `not: null`, which a missing value also meets, so it holds for every answer. Unlike the other tests it always has an answer, so a card's `when` never shows the can't-say note for it. This names the latest release, or None:

```json
{ "if": { "path": "latestRelease.tagName", "exists": true }, "then": "latestRelease.tagName", "else": { "template": "None" } }
```

`contains` holds for text that has the given text in it (case matters), or a list with an item equal to it: a guest tagged `docker;homelab` contains `homelab`.

`in_list` names a [list](https://legatobar.com/docs/lists.md#lists) kept in Settings → Lists, and holds when the value contains any of its entries. Case, accents and punctuation don't matter, and an entry matches only whole words: "Hill" matches "Fernhollow Hill, ST" but not "Hillside". Add `"if_missing": true` to make the test hold when the list is missing or empty, and `"optional": true` when the page works fine without the list, so its cards don't ask you to add it. This counts the deliveries whose delivered place is in the list `delivery-area`:

```json
{ "count": "deliveries", "where": { "path": "location", "in_list": "delivery-area" } }
```

- Like other tests, it can take a `value` (any rule) in place of `path`.
- A list that doesn't exist or has no entries makes the condition false, and the page says "Add the list delivery-area in Settings → Lists" (or "Add entries to the list delivery-area in Settings → Lists").
- 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 list can change in Settings and a kept value can't know. Use it in `derive`.

`equals`, `not`, `gt`, `gte`, `lt`, `lte` and `contains` can compare with another value in the same answer instead of one written in the page: give `{ "path": "…" }` in its place. In a `where`, that path is in the same row; `{ "answer": "…" }` reaches outside it, into the whole answer, such as the backups made since a guest was created: `"where": { "path": "ctime", "gte": { "answer": "created" } }`. An `if` in a row's `fields` reaches the whole answer the same way, to mark the row that matches something else in it. This counts the packages whose candidate version isn't the installed one, from Proxmox's `apt/versions`:

```json
{ "count": "versions.data", "where": { "all": [ { "path": "CurrentState", "equals": "Installed" },
                                               { "path": "Version", "not": { "path": "OldVersion" } } ] } }
```

When the other path has nothing there, the condition can't say, as when the tested path has nothing: a card's `when` shows the card with a note, and `where`, `if` and buttons count it as false (a missing value still meets `not`).

To test more than one thing, put 2 to 8 conditions in `all` (every one must hold) or `any` (at least one must). They nest. This lists credit cards that don't round to $0:

```json
"where": { "all": [ { "path": "type", "equals": "credit" }, { "value": { "path": "to_base", "round": 0 }, "not": 0 } ] }
```

Conditions also decide whether a whole card shows: see [Cards that show on a condition](https://legatobar.com/docs/mapping-conditions.md#cards-that-show-on-a-condition).

### Cards that show on a condition

For help choosing the structure, see [Page design](https://legatobar.com/docs/design.md#choose-the-right-structure), including a [linked overview and detail example](https://legatobar.com/docs/design.md#a-linked-overview-and-detail-example).

A card's `when` shows it only while a condition holds in its source's answer, the derived values before `map`. A page can then stay quiet and show only the exceptions: a door left open, water running, blocking turned off. Linked to a page with the details, an exception is one click from them.

```json
{ "id": "garage", "type": "status", "label": "Garage door open", "state": "warning", "url": "legato://page/security",
  "source": { "ref": "house", "when": { "path": "garage", "equals": "open" } } }
```

- **True:** the card shows. **False:** it's hidden, with its buttons. It doesn't count toward the page's status or notify. Notifications quietly count it as OK again (no "Back to OK", since a condition turning false isn't a recovery), so a warning card, or a fleet's item, that comes back notifies each time.
- **The answer can't say:** nothing or null at a path the condition tests, or a comparison (`gt`, `lt`, …) with something that isn't a number, like `"unavailable"` (`exists` always has an answer). The card shows, with a note saying so, so a field the service stopped sending can't quietly hide an alert. This is stricter than `where`, `if` and a button's `when`, where a missing value meets `not` and a comparison that can't be made is false.
- **Before the first answer**, the card waits as any sourced card does. **After a failed fetch**, the last answer decides, so the card keeps the visibility it had, and the source goes on polling while the card is hidden, so it can come back. If cards are hidden behind a source that's failing, a notice appears above the page’s sections and tabs ("… couldn't be reached, so cards here that show only on a condition may be out of date", or "… reported an error, …" when the source's `fail` turned the answer down). It stays visible when a section is collapsed or another tab is selected.
- **What's left:** a heading whose section had cards and shows none hides too. A tab whose cards are all hidden says "Nothing to show right now." (the tabs stay where they are), and a tabs card with nothing in any tab hides. A page with nothing left says so.
- A misspelled or malformed condition is an error on the card, not a hidden card. `when` goes on each card that uses it, not on a shared source. A card with `when` may leave out `map`, or give an empty one; `map` is otherwise an object with at least one field.

Paths, rules and filters are all there is: a source never runs code on your Mac.

