Bento Spaces documentation
16 — Automations
Climate rules that run on the server, not in a tab. The first slice is a thermostat for connector-backed ACs: profiles own the thresholds, a rule picks one profile for one device, and a minute tick decides whether to power the unit on or off.
Where it lives
| Piece | File |
|---|---|
| profile/rule model | source/Bento.Api/Models/Automations.cs |
| validation | source/Bento.Api/Services/AutomationValidator.cs |
| decision logic (pure) | source/Bento.Api/Services/AutomationEngine.cs |
| connector IO, logging, tick | source/Bento.Api/Services/AutomationRunner.cs |
| storage | source/Bento.Api/Storage/AutomationStore.cs → data/users/{id}/automations.json |
| HTTP | source/Bento.Api/Endpoints/AutomationEndpoints.cs |
| UI | web/src/features/automations/AutomationsDialog.js |
Profiles and rules
{
"profiles": [
{ "id": "p_cold", "name": "Cold", "season": "heat", "min": 21, "max": 24, "cooldownMinutes": 10 },
{ "id": "p_hot", "name": "Hot", "season": "cool", "min": 23, "max": 26, "cooldownMinutes": 10 }
],
"rules": [
{
"id": "r_living",
"name": "Living",
"connectorId": "c_…",
"deviceId": "living",
"enabled": true,
"defaultProfileId": "p_cold",
"overrideMinutes": 60,
"season": {
"winterFrom": "10-15", "winterTo": "04-15",
"winterProfileId": "p_cold", "summerProfileId": "p_hot"
},
"weather": {
"metric": "outdoorTemp",
"brackets": [
{ "max": 15, "profileId": "p_cold" },
{ "profileId": "p_hot" }
]
}
}
]
}
deviceId is optional: without it the rule talks to a single-device gree-ac bridge (/api/state, /api/power); with it, to one child (/api/devices/{id}/…).
How a tick decides
Order matters, and every branch is deterministic:
- Disabled → nothing.
- Offline (
online: false) → nothing. - No
currentTemp→ nothing. A stale or absent sensor is never a reason to guess. - Manual override — the observed power differs from the previous observation and we did
- not command in the last two minutes. The rule pauses for
overrideMinutes(default 60). - Paused → nothing until the pause ends.
- Profile — first weather bracket that matches, else the seasonal window, else
defaultProfileId. A profile switch alone never sends a command; the next tick compares the- new thresholds against the current reading.
- Hysteresis — heat: on at/below
min, off at/abovemax; cool: mirrored. Between the - two, nothing. That gap is the point.
- Cooldown — no second command inside
cooldownMinutes, even if the reading moved. - Otherwise
POST …/powerand log it.
Weather brackets are ordered, first match wins, the last entry (no max) is the fallback. A boundary is damped by 1°: at 15.0/15.1 the previous bracket stays. Only a one-bracket crossing is damped; a jump is real weather.
The seasonal window wraps the year (15 Oct → 15 Apr). Dates are MM-DD and compared in UTC. GET /api/automations/preview runs the same engine against a supplied reading and clock, which is what the API smoke tests use to prove every branch.
Running
AutomationRunner is a hosted service: once a minute, for every account with rules, it reads each enabled rule's device, evaluates and (if needed) commands. It starts 20 s after boot.
- state (
lastPower,lastCommandAt,activeBracket, pause, log) is stored per rule - the log keeps the last 20 actions with the reading that caused them
- a failed read logs one
connector_errorand moves on; it never retries in a tight loop
The dialog's Run now calls the same path immediately, so "why did it do that?" is answerable without waiting for the tick.
Limits
- Not an offline queue. If the bridge is unreachable, nothing happens; no command is retried
- later with a stale reading.
- One rule per device is not enforced. Two rules on the same device will fight; the UI does
- not prevent it yet.
- The sensor is intake air.
currentTempreads high while heating and low while cooling; - fine for on/off, not for ±0.5° regulation.
outdoorTempis the unit's own sensor, not a forecast. Some firmware reportsnull; then- the weather block is ignored and the seasonal/default profile wins.
- UTC season boundary. The window flips at midnight UTC, not local midnight.
- 60-second resolution. A rule reacts within about a minute.
- No push service. A command writes a notification to the signed-in user's bell feed (and
- the rule log); there is no email or OS push. Mute the
automationgroup in the bell's - Preferences to keep the feed quiet while the rule log still records every action.
API
| Method | Path | Notes |
|---|---|---|
GET | /api/automations | { rev, profiles, rules, state }; account only |
PUT | /api/automations | replace profiles and rules; 422 with errors[] |
POST | /api/automations/preview | { rule, profiles, reading, state?, now? } → { decision } |
POST | /api/automations/{ruleId}/run | execute once against the live connector |
Limits: 12 profiles, 24 rules, 8 weather brackets, cooldown 1–120 min, override 5–1440 min, thresholds 5–35 °C.
Acceptance
- ✓A rule toggles one AC from a server tick with the browser closed
- ✓Hysteresis, cooldown and manual override each have a deterministic test
- ✓A stale or offline reading produces no command
- ✓The seasonal window flips heat/cool without a second rule
- ✓A weather bracket selects a different profile and the boundary does not flap
- ✓A profile switch alone never sends a command
- ✓The dialog shows profiles, rules, the last action, and Run now
- ✓An in-app notification when a rule acts (OS push is tracked in Open work)