@avantics/node-red-home-automation 3.0.1 → 4.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/README.md +278 -13
  2. package/dist/lib/activity-lighting-controller.d.ts +7 -1
  3. package/dist/lib/activity-lighting-controller.js +37 -3
  4. package/dist/lib/adopted-request.d.ts +26 -0
  5. package/dist/lib/adopted-request.js +44 -0
  6. package/dist/lib/avr-controller.d.ts +8 -1
  7. package/dist/lib/avr-controller.js +27 -3
  8. package/dist/lib/avr-publishing.d.ts +24 -0
  9. package/dist/lib/avr-publishing.js +80 -0
  10. package/dist/lib/backlighting-controller.d.ts +7 -1
  11. package/dist/lib/backlighting-controller.js +26 -3
  12. package/dist/lib/circadian.d.ts +1 -0
  13. package/dist/lib/circadian.js +3 -0
  14. package/dist/lib/cover-availability.d.ts +7 -0
  15. package/dist/lib/cover-availability.js +24 -0
  16. package/dist/lib/cover-controller.d.ts +13 -1
  17. package/dist/lib/cover-controller.js +145 -31
  18. package/dist/lib/cover-group.d.ts +62 -0
  19. package/dist/lib/cover-group.js +125 -0
  20. package/dist/lib/cover-models.d.ts +37 -0
  21. package/dist/lib/cover-models.js +83 -0
  22. package/dist/lib/cover-publishing.d.ts +40 -0
  23. package/dist/lib/cover-publishing.js +102 -0
  24. package/dist/lib/cover-requests.d.ts +21 -0
  25. package/dist/lib/cover-requests.js +49 -0
  26. package/dist/lib/display-mode-controller.d.ts +4 -0
  27. package/dist/lib/display-mode-controller.js +26 -2
  28. package/dist/lib/ha-discovery.d.ts +27 -0
  29. package/dist/lib/ha-discovery.js +91 -0
  30. package/dist/lib/house-schedule-publishing.d.ts +23 -0
  31. package/dist/lib/house-schedule-publishing.js +71 -0
  32. package/dist/lib/house-schedule.d.ts +23 -0
  33. package/dist/lib/house-schedule.js +29 -0
  34. package/dist/lib/motion-policy.d.ts +9 -0
  35. package/dist/lib/motion-policy.js +45 -11
  36. package/dist/lib/motion-publishing.d.ts +28 -0
  37. package/dist/lib/motion-publishing.js +92 -0
  38. package/dist/lib/occupancy-publishing.d.ts +10 -0
  39. package/dist/lib/occupancy-publishing.js +45 -0
  40. package/dist/lib/open-close-cover.d.ts +92 -0
  41. package/dist/lib/open-close-cover.js +287 -0
  42. package/dist/lib/publishing-node.d.ts +39 -0
  43. package/dist/lib/publishing-node.js +72 -0
  44. package/dist/lib/retained-topics.d.ts +10 -0
  45. package/dist/lib/retained-topics.js +19 -0
  46. package/dist/lib/room-controller.d.ts +9 -0
  47. package/dist/lib/room-controller.js +47 -56
  48. package/dist/lib/room-lighting-discovery.d.ts +20 -1
  49. package/dist/lib/room-lighting-discovery.js +135 -13
  50. package/dist/lib/room-publishing.d.ts +42 -0
  51. package/dist/lib/room-publishing.js +182 -0
  52. package/dist/lib/room-schedule.d.ts +8 -0
  53. package/dist/lib/room-schedule.js +41 -0
  54. package/dist/lib/settings.d.ts +2 -0
  55. package/dist/lib/settings.js +20 -0
  56. package/dist/lib/source-capability-controller.d.ts +8 -1
  57. package/dist/lib/source-capability-controller.js +29 -3
  58. package/dist/lib/theatre-activity-controller.d.ts +0 -1
  59. package/dist/lib/theatre-activity-controller.js +27 -24
  60. package/dist/lib/theatre-ha-discovery.d.ts +3 -4
  61. package/dist/lib/theatre-ha-discovery.js +46 -36
  62. package/dist/lib/theatre-state-contract.d.ts +6 -0
  63. package/dist/lib/theatre-state-contract.js +83 -47
  64. package/dist/lib/theatre-state.d.ts +1 -1
  65. package/dist/lib/theatre-state.js +1 -1
  66. package/dist/lib/topic-names.d.ts +4 -0
  67. package/dist/lib/topic-names.js +31 -0
  68. package/dist/nodes/activity-lighting-controller.js +5 -3
  69. package/dist/nodes/adapter.d.ts +6 -1
  70. package/dist/nodes/adapter.js +65 -1
  71. package/dist/nodes/avr-controller.html +59 -5
  72. package/dist/nodes/avr-controller.js +74 -4
  73. package/dist/nodes/backlighting-controller.js +5 -3
  74. package/dist/nodes/cover-controller.d.ts +3 -0
  75. package/dist/nodes/cover-controller.html +181 -0
  76. package/dist/nodes/cover-controller.js +178 -0
  77. package/dist/nodes/cover-group.d.ts +3 -0
  78. package/dist/nodes/cover-group.html +89 -0
  79. package/dist/nodes/cover-group.js +84 -0
  80. package/dist/nodes/flows-started.d.ts +4 -0
  81. package/dist/nodes/flows-started.js +39 -0
  82. package/dist/nodes/house-schedule-mqtt.d.ts +3 -0
  83. package/dist/nodes/house-schedule-mqtt.html +61 -0
  84. package/dist/nodes/house-schedule-mqtt.js +80 -0
  85. package/dist/nodes/house-schedule.d.ts +3 -0
  86. package/dist/nodes/house-schedule.html +31 -0
  87. package/dist/nodes/house-schedule.js +51 -0
  88. package/dist/nodes/motion-policy.html +81 -5
  89. package/dist/nodes/motion-policy.js +69 -8
  90. package/dist/nodes/occupancy-fusion.html +69 -14
  91. package/dist/nodes/occupancy-fusion.js +46 -4
  92. package/dist/nodes/publishing-lifecycle.d.ts +25 -0
  93. package/dist/nodes/publishing-lifecycle.js +38 -0
  94. package/dist/nodes/room-controller.html +49 -5
  95. package/dist/nodes/room-controller.js +67 -8
  96. package/dist/nodes/shared-schedule.d.ts +6 -0
  97. package/dist/nodes/shared-schedule.js +15 -0
  98. package/dist/nodes/source-capability-controller.js +5 -3
  99. package/dist/nodes/theatre-activity-controller.html +1 -3
  100. package/dist/nodes/theatre-activity-controller.js +1 -2
  101. package/dist/nodes/theatre-state-contract.html +28 -5
  102. package/dist/nodes/theatre-state-contract.js +22 -4
  103. package/examples/Cover control.json +380 -0
  104. package/examples/Cover pair.json +230 -0
  105. package/examples/Home theatre activity.json +4 -4
  106. package/examples/House schedule.json +164 -0
  107. package/examples/Room lighting.json +4 -3
  108. package/package.json +6 -2
package/README.md CHANGED
@@ -10,6 +10,11 @@ State-aware Node-RED controllers for reliable lighting and home-theatre automati
10
10
 
11
11
  Most home automations translate an event directly into a command. This package adds a deterministic control layer between them: it tracks desired and observed state, preserves manual control, confirms outcomes, retries bounded failures, and rejects stale work.
12
12
 
13
+ > [!NOTE]
14
+ > **Under active development.** This package is changing quickly. Versions follow semver, but an update can
15
+ > still change behaviour in ways that aren't marked as breaking. Read the release notes before updating, pin the
16
+ > version you run, and check your flows after each update.
17
+
13
18
  ## What it solves
14
19
 
15
20
  - **Occupancy without flicker:** combine PIR motion with optional presence sensing before changing room state.
@@ -19,7 +24,7 @@ Most home automations translate an event directly into a command. This package a
19
24
  - **Honest source readiness:** model controllable, externally controlled, and unavailable playback sources without claiming success from an input selection alone.
20
25
  - **Owned theatre atmosphere:** apply activity lighting only to active, non-manual room lights and confirm TV backlighting independently.
21
26
  - **Bounded receiver volume:** coordinate absolute and relative requests through one transport owner while preserving later physical adjustments.
22
- - **Recoverable automation:** adopt one fresh, uniquely matching physical activity after lost state; otherwise coordinate a safe Off request.
27
+ - **Recoverable automation:** adopt one fresh, uniquely matching physical activity after lost state; otherwise report the activity as unknown and leave the devices alone until the next request.
23
28
 
24
29
  ![Architecture diagram. Inputs (wall switches, motion and presence sensors, illuminance, and theatre requests over MQTT) feed two groups of nodes. Room lighting: occupancy-fusion, motion-lighting-policy and switch-lighting-policy feed room-lighting-controller, which sends set commands to the lights (for example Zigbee2MQTT) and reads back their observed state. Theatre activities: theatre-state-contract passes activity intents to theatre-activity-controller and publishes retained theatre state and Home Assistant discovery; your flow routes each theatreCommand by target to the theatre equipment controllers (AVR, display mode, source capability, backlighting), which command the devices, or to activity-lighting-controller, which holds room lights; both report target state back to the activity controller.](https://avantics-hq.github.io/node-red-home-automation/images/architecture.svg)
25
30
 
@@ -28,7 +33,7 @@ commands, and reports done only when fresh observations confirm them.
28
33
 
29
34
  ## Included nodes
30
35
 
31
- The nodes come in three groups. Each group has an importable example flow
36
+ The nodes come in four groups. Each group has an importable example flow
32
37
  (**Import → Examples** in the Node-RED editor).
33
38
 
34
39
  ### Room lighting
@@ -37,7 +42,12 @@ Motion, presence, illuminance and wall switches drive one room's lights, with
37
42
  circadian styling and manual ownership. Wire occupancy fusion into the motion
38
43
  policy, wall switches into the switch policy, and both policies into the room
39
44
  controller. The room controller keeps a light a person set after the room went
40
- vacant, so motion doesn't switch it straight off. Circadian
45
+ vacant, so motion doesn't switch it straight off. Without a Node-RED context
46
+ store, a motion node that restarts or is redeployed doesn't know whether the
47
+ room is occupied, so it counts the room as vacant from then: someone sitting
48
+ still in a lit room has the motion-lit lights switched off after the vacancy
49
+ delay unless motion is seen. After a restart the room has also forgotten which
50
+ lights a person set, so it doesn't keep those either. Circadian
41
51
  levels follow the room controller's wake and bed times (default 07:00 and
42
52
  23:00): they rise over the hour after waking, stay bright and cool through the
43
53
  day, and wind down to the sleep settings over the three hours before bed. Times
@@ -62,6 +72,21 @@ way; other rewrites can still be misread.
62
72
  | `home-switch-lighting-policy` | Converts raw edges or pre-classified button actions into lighting intent while exposing input freshness and availability. |
63
73
  | `home-room-lighting-controller` | Owns the room's light commands, adaptive styling, manual and held lights, and lighting state. |
64
74
 
75
+ One house schedule can set the wake and bed times for many rooms. It is a
76
+ configuration node, like an MQTT broker: choose it as a room controller's
77
+ **Schedule** and the room follows it, hearing of every change without wires.
78
+ Those rooms then have no wake and bed time entities of their own in Home
79
+ Assistant. Rooms that need their own times (a child's room, say) choose none.
80
+ Deleting the house schedule in the editor returns its rooms to their own times.
81
+ The optional house schedule MQTT node publishes the schedule's times and Home
82
+ Assistant time entities, and takes changes from them.
83
+ Example: [`House schedule.json`](https://github.com/avantics-hq/node-red-home-automation/blob/main/examples/House%20schedule.json).
84
+
85
+ | Node | Purpose |
86
+ |---|---|
87
+ | `home-house-schedule` | A configuration node: one wake and bed time for every room that uses it. |
88
+ | `home-house-schedule-mqtt` | Publishes a house schedule for Home Assistant and changes it from Home Assistant. |
89
+
65
90
  ### Home theatre equipment
66
91
 
67
92
  Each controller owns one kind of device. It accepts a generation-scoped intent,
@@ -102,6 +127,59 @@ Pure TypeScript policy modules live in `lib/`; thin Node-RED runtime and editor
102
127
 
103
128
  Importable example flows for the Node-RED editor live in [`examples/`](https://github.com/avantics-hq/node-red-home-automation/tree/main/examples) (**Import → Examples**). Executable, deployment-neutral contract examples live in [`contract-examples/`](https://github.com/avantics-hq/node-red-home-automation/tree/main/contract-examples) and exercise the same reducers as the runtime adapters.
104
129
 
130
+
131
+ ### Covers
132
+
133
+ One cover controller owns one position cover: a blind, shutter or curtain motor
134
+ that takes and reports a position from 0 (closed) to 100 (open). Send it
135
+ `"open"`, `"close"`, `"stop"`, a position, or the name of a preset such as
136
+ `partial`. It confirms a move from what the cover reports, retries a bounded
137
+ number of times, and leaves a cover a person moved where they put it. A move is
138
+ confirmed only once the cover has stopped at the target, from a `stopped`
139
+ motion report or the same position for the settle time (with or without
140
+ another report), so a transport that echoes the requested position at once
141
+ doesn't end the move early. Set the settle time to at least how often the
142
+ motor reports while it travels: otherwise an echo can be confirmed before the
143
+ first travel report arrives. An
144
+ automatic target, such as a position computed from the weather, sets
145
+ `msg.respectManual = true` so it waits while a person's move holds; a target
146
+ the cover is already at is confirmed without a command. A button can send
147
+ `msg.stopIfMoving = true` with `"open"` or `"close"` to stop the cover if it is
148
+ moving and do the request otherwise. The cover's availability report
149
+ (`{kind: "availability", online}`) keeps requests from going to an offline
150
+ device: they are answered `unavailable`, and the cover is read again when it is
151
+ back. A motor with no reliable position can be set to *open, close and
152
+ stop only*: it confirms a move from a reported end state, or from a move that
153
+ ran for its full travel time, and says when it could only judge by travel time
154
+ or could not confirm at all. Its third output says whether the cover is
155
+ moving, so other policies can react. To pause motion lighting while a blind
156
+ moves, send it to the motion policy's pause input
157
+ (`msg.source = "pause"`, `msg.payload` true or false, `msg.owner` naming the
158
+ cover), as the example does; a pause lasts at most the motion policy's
159
+ **Maximum pause** unless sent again; never its enable (`area-enabled`), which is the
160
+ user's own switch. Device commands come out in a neutral
161
+ shape (`set_position`, `stop` or `observe`); the example flow maps them to and
162
+ from a Zigbee2MQTT cover. Battery and charging status are kept as reported
163
+ facts and never acted on.
164
+
165
+ Switched on, a cover node publishes its state (and position) under
166
+ `home/<area>/cover/<name>/`, named by its **Name in topics** (no default: give
167
+ each cover in a room its own), and announces a Home Assistant cover entity. Its open,
168
+ close, stop and set position come back to the cover node on its own set topics
169
+ as requests like any other, carried out and confirmed as above; nothing
170
+ commands the device through Home Assistant. A cover group has no entity of its
171
+ own: Home Assistant can group the covers' entities itself.
172
+
173
+ A cover group moves several covers as one, such as the left and right of a
174
+ curtain: it sends each member the request on that member's own output, labelled
175
+ from its Members setting, and reports ready only when every member confirms,
176
+ or, if only some do, which did what.
177
+
178
+ | Node | Purpose |
179
+ |---|---|
180
+ | `home-cover-controller` | Owns one cover's moves (a position cover, or open, close and stop only), confirmed from what it reports, with presets, manual ownership and a moving signal. |
181
+ | `home-cover-group` | Moves several covers as one, with an honest result when only some confirm. |
182
+
105
183
  ## Requirements
106
184
 
107
185
  - Node.js 22.9 or newer
@@ -122,11 +200,59 @@ also attaches the package tarball and its SHA-256 checksum. Install a downloaded
122
200
  tarball through **Manage palette → Install → Upload module**, or with:
123
201
 
124
202
  ```sh
125
- npm install /path/to/avantics-node-red-home-automation-3.0.1.tgz
203
+ npm install /path/to/avantics-node-red-home-automation-4.0.1.tgz
126
204
  ```
127
205
 
128
206
  Add the installed nodes from the **home automation** section of the Node-RED palette, then configure the room, device hosts, MQTT topics, source mappings, and timing limits for your deployment.
129
207
 
208
+ ### 4.0.1
209
+
210
+ Fixes the cover controller ending a move too early when a transport echoes the
211
+ requested position as soon as the command is sent, before the motor moves: the
212
+ cover showed as stopped while it was still travelling, and the travel after it
213
+ was taken for a person's move. A move now confirms only once the cover has
214
+ stopped at the target, from a `stopped` motion report or the same position for
215
+ the settle time. Set the settle time to at least how often the motor reports
216
+ while it travels (see Covers).
217
+
218
+ ### 4.0.0
219
+
220
+ Adds cover control, a house schedule, and Home Assistant entities for every
221
+ publishing node. Check these after upgrading from 3.0:
222
+
223
+ - **Breaking: theatre recovery never switches a running activity off.** After a
224
+ restart or redeploy with unclear evidence, the activity is reported as unknown
225
+ and the devices are left alone until the next request. The "Recovery Off
226
+ activity" setting is gone, and a saved value is ignored. The metric
227
+ `recoveriesSafeOff` is now `recoveriesUnknown` (in `<base>/metrics`, shown as
228
+ attributes of the "Transition latency" sensor), and the event
229
+ `recovery_safe_off` is now `recovery_unknown`. Update any dashboard, template
230
+ or flow that uses the old names.
231
+ - **Breaking: the theatre's readiness moved topics.** It is now on
232
+ `<base>/readiness`, and its Home Assistant sensor is "Readiness".
233
+ `<base>/availability` now carries `online` or `offline` (see "Availability"
234
+ below).
235
+ - **Breaking for areas that aren't identifiers:** a room or theatre contract whose
236
+ area has spaces or other characters now publishes under the lowercase form,
237
+ for example `home/living_room/…` instead of `home/Living Room/…` (see
238
+ "Discovery" below).
239
+ - **Without a context store, a restart or redeploy is gentler:** a room leaves
240
+ its lights alone until something names them, motion lighting starts its
241
+ vacancy countdown, and a theatre device re-learns its activity from the
242
+ theatre instead of being marked unresponsive. A [file context store](#recommended-setup-a-file-context-store) keeps state
243
+ across restarts instead.
244
+ - **A Catch node fires once per input error,** not twice.
245
+ - **New nodes:** a cover controller for blinds, shutters and curtains, a cover
246
+ group, a house schedule that sets many rooms' wake and bed times, and a house
247
+ schedule MQTT node for Home Assistant (see "Covers" and "Room lighting").
248
+ - **New outputs, off until you switch publishing on:** occupancy fusion and the
249
+ motion policy gain a second output, and the AVR controller a third, for their
250
+ MQTT state and Home Assistant entities. Saved nodes are unchanged.
251
+ - **Home Assistant:** availability, entity names that survive area renames, the
252
+ room's sleep switch and wake and bed times, receiver volume, and cleanup of a
253
+ deleted node's entities, described under "Upgrading from 2.x" below.
254
+ - **Other flows can pause motion lighting** without changing the user's enable.
255
+
130
256
  ### 3.0.1
131
257
 
132
258
  Fixes the room lighting controller looping on its own commands when something
@@ -141,7 +267,7 @@ its commands to MQTT out unchanged (see Room lighting).
141
267
 
142
268
  ### Upgrading from 2.x
143
269
 
144
- Version 3.0 changes some behaviour. Check these after upgrading:
270
+ Version 3.0 changed some behaviour, and later versions added to it. Check these after upgrading from 2.x, along with the notes above:
145
271
 
146
272
  - **Wall switches go through the switch lighting policy node.** The motion lighting policy no longer handles them,
147
273
  and ignores switch messages, so rewire any wall switch that feeds a motion node. The room lighting controller now
@@ -150,17 +276,76 @@ Version 3.0 changes some behaviour. Check these after upgrading:
150
276
  minutes before. A redeploy with a changed vacancy delay or lux cutoff now takes effect.
151
277
  - **Settings are checked when you deploy.** An emptied or unusable setting shows "invalid configuration", and the
152
278
  node's log says which setting is wrong. A setting your flow saved before it existed still takes its default.
153
- - **No node assumes a room.** An empty area is allowed, except on the theatre state contract, which needs one to name
154
- its topics. A room lighting controller with no area still controls its lights, but publishes no state or discovery.
279
+ - **No node assumes a room.** An empty area is allowed. A node that publishes needs one where it names something:
280
+ the theatre state contract for its default base topic (`home/<area>/theatre`), and a room lighting controller for
281
+ the topics it publishes. A theatre state contract added from now on doesn't need one for Home Assistant
282
+ discovery: its entity IDs then come from its display name. With an area, it suggests that area to Home Assistant,
283
+ which creates it if no area of that name exists. One saved before needs an area for discovery, as before. A room
284
+ lighting controller with no area still controls its lights, but publishes no state or discovery.
155
285
  - **MQTT state publishing and Home Assistant discovery are opt-in** on the room lighting controller and the theatre
156
286
  state contract. New nodes start with them off. Nodes in a flow saved before 3.0 keep publishing until you untick
157
287
  the setting. Switching discovery off removes the entities from Home Assistant.
288
+ - **Discovery** goes under the **Discovery prefix** setting (`homeassistant` by default, as in Home Assistant) on
289
+ both nodes, and every entry names this package and its version as its origin. Topics and IDs use the area in
290
+ lowercase, with each run of spaces or other characters (anything but letters, digits, `_` and `-`) as `_`:
291
+ "Living Room" publishes under `home/living_room/…`, and "living-room" is unchanged.
292
+ Names shown in Home Assistant keep the area as you typed it.
293
+ - **Entity names.** A room lighting controller or theatre state contract you add from now on names its Home
294
+ Assistant device, unique IDs and discovery topics after its Node-RED node ID (`nrha_<node id>…`), so renaming its
295
+ area or display name updates the same entities. Nodes in flows saved before this keep naming them after the area,
296
+ exactly as before, so nothing changes for them in Home Assistant. Either way, the entity ID Home Assistant first
297
+ gives an entity comes from the area, with `-` as `_`.
298
+ Importing or pasting a node in the editor keeps its saved settings, including its ID scheme and publishing, so
299
+ an older node restored from a backup keeps its area-named entities. Only a node added from the palette starts as
300
+ new.
301
+ - **Occupancy fusion can publish too**, off unless you switch it on: its occupancy as retained `ON`/`OFF` on
302
+ `home/<area>/occupancy/state`, with availability, and an occupancy binary sensor in Home Assistant. These go on its
303
+ new second output, so wire that to an MQTT out node; the first output still feeds the motion policy.
304
+ - **The motion lighting policy can publish too**, off unless you switch it on: whether this node's motion lighting
305
+ is enabled and its vacancy delay in seconds (`home/<area>/motion/enabled/state` and `…/delay/state`), which it also
306
+ takes from their `/set` topics, and a switch and a vacancy delay number in Home Assistant. These go on its new
307
+ second output; the shared global enable is not published.
308
+ - **The AVR controller can publish what the receiver reports**, off unless you switch it on: power, source and
309
+ volume, retained under `home/<area>/receiver/`, with availability, and Home Assistant power and source sensors
310
+ and a volume number (see "Receiver volume ownership"). These go on its new
311
+ third output, apart from the receiver commands and the theatre's state.
312
+ - **Topics no longer published are cleared.** Once the flows have started, and again at its startup message, a node
313
+ clears any retained topic it published before and no longer does, for example after an area, base topic,
314
+ discovery prefix or target change. It remembers what it published in its node context, which lasts across
315
+ deploys and restarts only with a context store configured (see "Recommended setup"). Without that record, an occupancy, motion, receiver or
316
+ house schedule MQTT node named after itself (node-based IDs) that publishes state still clears its own Home Assistant
317
+ entities it no longer publishes, such as after you switch discovery off; it never clears topics another node could
318
+ publish. Other old topics, after an area, base topic, discovery prefix or target change, or after you switch
319
+ publishing off entirely, are cleared only from the record. A node that never gets a startup message
320
+ keeps clearing its old topics at every start, so to move an area from one older room node to another, send the old
321
+ node a startup message, or delete it.
322
+ - **Send each publishing node a startup message once, a few seconds after deploy.** The example flows do this with an
323
+ inject node set to fire once after 3 seconds: by then the MQTT broker node has usually connected, and a message
324
+ sent before it connects is dropped, along with the discovery and state it carried.
325
+ - **Removing entities:** deleting a node clears its Home Assistant entities and retained state, but only when you
326
+ deploy with "Modified nodes" or "Modified flows". On a full deploy (the deploy button's default), Node-RED stops the
327
+ MQTT out node at the same moment, so the clearing never reaches the broker. Disabling a node or its flow keeps its
328
+ entities, so they come back unchanged when you enable it again. To remove a node's entities after a full deploy,
329
+ or to remove the entities of a flow you copied to another Node-RED (the copy's nodes have new IDs, so new
330
+ entities), delete the device in Home Assistant: Settings → Devices & services → Devices, open the device, then
331
+ Delete. Home Assistant then clears its retained discovery entry itself.
332
+ - **Availability:** each publishing node sends a retained `online` once the flows have started (and again at its
333
+ startup message) and `offline` when it stops, on `home/<area>/lighting/availability` (room) or
334
+ `<base>/availability` (theatre), and its Home Assistant entities follow it. These get through when the node is
335
+ deployed on its own ("Modified nodes" or "Modified flows"), while the MQTT connection stays up. On a full deploy, a
336
+ restart or a shutdown, Node-RED stops the MQTT out node at the same moment and reconnects to the broker afterwards,
337
+ so set the MQTT broker node's **Birth message** to `online`, and its **Close message** and **Will message** (which
338
+ covers crashes) to `offline`, all retained, on the same topic; with several publishing nodes, the broker can cover
339
+ one of them. The theatre's readiness (`unknown`, `available`, `degraded`
340
+ or `unavailable`), which used `<base>/availability` before, is now on `<base>/readiness`.
341
+
342
+ ## Theatre MQTT contract
158
343
 
159
344
  The whole-theatre MQTT contract uses non-retained requests at
160
345
  `home/<area>/theatre/request`; every request supplies a unique `requestId` and an
161
346
  explicit configured activity. Controller output is retained under `desired`,
162
- `current`, `phase`, `failure`, `availability`, `metrics`, `slo`, and
163
- `health/<target>`. Metrics expose transition latency and outcome counts plus
347
+ `current`, `phase`, `transition` (when the latest transition started and finished, and which
348
+ targets it left degraded), `failure`, `readiness`, `metrics`, `slo`, and `health/<target>`. Metrics expose transition latency and outcome counts plus
164
349
  per-target retry, fallback, confirmation-latency, failure, manual-override, and
165
350
  availability evidence. The SLO evaluator keeps a bounded outcome window,
166
351
  publishes success rate plus p50/p95 transition latency, and exposes an
@@ -187,10 +372,62 @@ second ahead of Node-RED's clock is treated as observed on receipt and traced as
187
372
 
188
373
  Unsolicited display, AVR, source, and backlighting changes latch manual
189
374
  ownership instead of issuing corrective commands. A newer explicit activity
190
- request resumes automation. Recovery adopts physical state only when fresh
375
+ request resumes automation. A target controller that starts with nothing
376
+ saved (a redeploy or restart without a context store) takes the activity's
377
+ wanted values from the activity controller's next refresh, which carries them
378
+ as the intent did, and only reads its device for it: it reports ready when
379
+ the device already matches, and a manual override (reason `adopted-mismatch`)
380
+ when it doesn't, sending no command until the next request.
381
+ Recovery adopts physical state only when fresh
191
382
  display, AVR, source, and backlighting evidence matches exactly one configured
192
- profile; missing or ambiguous startup evidence invokes the configured Off
193
- activity.
383
+ profile. With missing or ambiguous startup evidence the activity is unknown,
384
+ and nothing is commanded until the next request: a redeploy or restart, which
385
+ starts the controller with nothing saved unless a context store is configured,
386
+ never switches a running activity off. While it is unknown, the theatre's
387
+ phase shows `idle` (the controller isn't working on anything) and its current
388
+ activity `unknown` until the next request, even if something is playing.
389
+ (Earlier versions ran a configured Off activity here; that setting is gone,
390
+ and a saved value is ignored.)
391
+
392
+ ## Recommended setup: a file context store
393
+
394
+ The nodes keep what they are doing in Node-RED's node context. By default
395
+ Node-RED keeps context in memory and deletes a node's context whenever the
396
+ node stops: on a full deploy, on a "Modified flows" deploy of its flow (which
397
+ restarts every node in that flow, changed or not), on a "Modified nodes"
398
+ deploy of the node itself, and on every restart. Configure the file context
399
+ store so it survives. In `settings.js` in your Node-RED user directory, set:
400
+
401
+ ```js
402
+ contextStorage: {
403
+ default: { module: "localfilesystem" }
404
+ },
405
+ ```
406
+
407
+ then restart Node-RED. The file store writes changes to disk every 30 seconds
408
+ by default, so a change made just before Node-RED stops can still be lost.
409
+
410
+ Without a context store, after a node is redeployed or Node-RED restarts:
411
+
412
+ - **A room forgets which lights are held or manual:** every light an activity
413
+ held, or a person set by hand, is back under the room's control, so the next
414
+ refresh restyles an activity's lights and lights a person set.
415
+ - **Changes made at runtime go back to the node's settings:** for example motion
416
+ lighting's enable, vacancy delay and lux cutoff, a room's circadian lighting,
417
+ sleep, and wake and bed times, and the house schedule's times, set from Home
418
+ Assistant or a message.
419
+ - **The theatre starts its counts again:** its counters and operational SLO
420
+ samples restart, and the activity controller no longer knows the current
421
+ activity. At its next startup recovery it works it out from what the devices
422
+ report, and if no single activity matches, it switches to the recovery off
423
+ activity.
424
+ - **Work in progress is not recovered:** a motion off-timer that was running,
425
+ or a theatre device change still being confirmed. The node starts afresh
426
+ and goes by what it next observes.
427
+ - **Old retained topics are not cleared:** a node can't remember what it
428
+ published before, so after you change an area, base topic, discovery prefix
429
+ or targets, the old retained topics and Home Assistant entities stay until
430
+ you remove them yourself.
194
431
 
195
432
  ## Room light control
196
433
 
@@ -201,6 +438,16 @@ The room lighting controller gives each target one control state:
201
438
  refresh and circadian changes leave it alone until it is released.
202
439
  - `manual`: a person changed the light.
203
440
 
441
+ The control states live in the node's context. A room controller that starts
442
+ with nothing saved (its first deploy, or a redeploy or restart without a
443
+ context store) can't know which lights an activity held or a person set, so it
444
+ leaves every light as it is: the periodic refresh restyles a light again only
445
+ once an intent or release names it, someone switches it on with a bare ON, or
446
+ circadian lighting is switched on. A light that is on at that start and that
447
+ nothing names can keep its start-time style all day; to bring it back into step,
448
+ switch circadian lighting on (for example with the Home Assistant circadian
449
+ switch), or let motion or a wall switch name it.
450
+
204
451
  Add `owner` to an intent to override the targets it switches on. With
205
452
  `preserveManual: true`, manual lights are skipped:
206
453
 
@@ -253,7 +500,14 @@ Flows that filter that output by topic must pass the `/control` topics through.
253
500
  It also publishes the circadian state on `home/<area>/circadian/state`, and
254
501
  acts on ON or OFF sent to `home/<area>/circadian/set`, which is where the Home
255
502
  Assistant circadian switch sends them: wire an MQTT in node subscribed to that
256
- topic into the room controller.
503
+ topic into the room controller. In the same way it publishes and acts on sleep
504
+ (`home/<area>/sleep/state` and `/set`, ON or OFF) and the wake and bed times
505
+ (`home/<area>/wake-time/state` and `/set`, likewise `bed-time`, as `HH:MM:SS`);
506
+ subscribing to `home/<area>/+/set` covers all four. A time set from Home
507
+ Assistant lasts until the node is deployed with a different setting for it.
508
+ With discovery on, Home Assistant also gets a sleep switch, wake and bed time
509
+ entities (under the device's configuration), and a diagnostic sensor per light
510
+ showing its control state, with the owner as an attribute.
257
511
 
258
512
  ## Receiver volume ownership
259
513
 
@@ -277,6 +531,17 @@ Once confirmed, it releases the volume target; a later physical or external
277
531
  adjustment becomes the new baseline instead of being corrected. A request still pending when Node-RED restarts is reported as
278
532
  `volume_failed` with reason `restart`, not replayed.
279
533
 
534
+ With publishing and discovery on, Home Assistant gets a volume number. A
535
+ value set there arrives on `home/<area>/receiver/volume/set`; values arriving
536
+ within 0.3 seconds of each other are settled to the last, which becomes an
537
+ ordinary absolute request (`msg.avr = {kind: "volume", mode: "absolute", ...}`),
538
+ so it is bounded, sent and confirmed in the same way, and never sent to the
539
+ receiver directly. The number shows the confirmed volume. A theatre activity
540
+ owns the receiver's power and source, not its volume: a volume request made
541
+ while an activity is switching power or source waits until that is done, and
542
+ one made while the receiver is off (an activity's `off`, say) is rejected
543
+ with `receiver-not-ready`, as any request would be.
544
+
280
545
  ## Contract examples
281
546
 
282
547
  Build and run all seven public contracts:
@@ -48,6 +48,7 @@ export interface ActivityLightingState {
48
48
  pending: ActivityLightingPending | null;
49
49
  failure: ActivityLightingFailure | null;
50
50
  blockedGeneration: number | null;
51
+ adoptedGeneration: number | null;
51
52
  }
52
53
  export type ActivityLightingEvent = {
53
54
  kind: "intent";
@@ -73,7 +74,12 @@ export type ActivityLightingEvent = {
73
74
  kind: "tick";
74
75
  generation: number;
75
76
  } | {
76
- kind: "startup" | "refresh";
77
+ kind: "startup";
78
+ } | {
79
+ kind: "refresh";
80
+ requestId?: string | null;
81
+ generation?: number;
82
+ profile?: string;
77
83
  };
78
84
  export interface ActivityLightingMessage {
79
85
  activityLighting?: ActivityLightingEvent;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.normaliseLightingProfiles = normaliseLightingProfiles;
4
4
  exports.normaliseActivityLightingConfig = normaliseActivityLightingConfig;
5
5
  exports.transitionActivityLighting = transitionActivityLighting;
6
+ const adopted_request_1 = require("./adopted-request");
6
7
  const settings_1 = require("./settings");
7
8
  const observation_time_1 = require("./observation-time");
8
9
  const target_state_1 = require("./target-state");
@@ -79,7 +80,8 @@ function initialState(saved = {}, profiles = {}) {
79
80
  observed: Object.fromEntries(Object.entries(saved.observed || {}).map(([target, observation]) => [target, { ...observation }])),
80
81
  pending: saved.pending && typeof saved.pending === "object" ? { ...saved.pending } : null,
81
82
  failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
82
- blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null
83
+ blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null,
84
+ adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved)
83
85
  };
84
86
  }
85
87
  function profileFor(state, config) {
@@ -179,6 +181,14 @@ function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue =
179
181
  continue;
180
182
  }
181
183
  if (!valuesMatch(profile.values, observation.values)) {
184
+ if ((0, adopted_request_1.isAdopted)(state)) {
185
+ // An adopted request the lights don't match: someone else's, and
186
+ // never re-applied.
187
+ state.pending = null;
188
+ state.phase = "manual_override";
189
+ trace("observation", "manual_override", adopted_request_1.ADOPTED_MISMATCH, { target });
190
+ return;
191
+ }
182
192
  state.phase = "confirming";
183
193
  return;
184
194
  }
@@ -293,6 +303,19 @@ function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue =
293
303
  trace("observation", "accepted", null, { target: observation.target });
294
304
  reconcile();
295
305
  }
306
+ // A refresh with the activity's wanted values, reaching a node with
307
+ // nothing desired (it started with nothing saved): adopt them, and observe
308
+ // below as at startup (see adopted-request.ts). Which lights it held is
309
+ // unknown, so the next activity releases every light it holds.
310
+ const adoptable = event.kind === "refresh" ? (0, adopted_request_1.adoptableRefresh)(event, state) : null;
311
+ const adopted = adoptable !== null && event.kind === "refresh" && typeof event.profile === "string" && Object.hasOwn(config.profiles, event.profile);
312
+ if (adopted && event.kind === "refresh") {
313
+ state.desired = { ...adoptable, profile: event.profile, requestedAt: now.toISOString() };
314
+ state.generation = adoptable.generation;
315
+ state.adoptedGeneration = adoptable.generation;
316
+ state.applied = { profile: event.profile, targets: null };
317
+ trace("refresh", "adopted", "nothing-desired", { profile: event.profile, generation: adoptable.generation });
318
+ }
296
319
  if (event.kind === "intent") {
297
320
  acceptIntent(event);
298
321
  }
@@ -336,7 +359,7 @@ function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue =
336
359
  state.phase = "confirming";
337
360
  // After a restart nothing else would end an unanswered observe: give
338
361
  // it a deadline, so it re-applies and then fails as a live request.
339
- if (event.kind === "startup" && state.blockedGeneration !== state.generation) {
362
+ if ((event.kind === "startup" || adopted) && state.blockedGeneration !== state.generation) {
340
363
  state.pending = { attempt: 0, deadlineAt: nowMs + config.confirmationTimeoutMs };
341
364
  }
342
365
  trace(event.kind, "observing", null, { profile: state.desired.profile });
@@ -349,12 +372,23 @@ function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue =
349
372
  else {
350
373
  throw new Error("unknown activity lighting event kind");
351
374
  }
375
+ // An adopted request only reads the lights (see adopted-request.ts): an
376
+ // apply means they aren't as wanted.
377
+ const held = (0, adopted_request_1.readsOnly)(state, commands, (command) => command.operation === "observe");
378
+ if (held.mismatch) {
379
+ commands.splice(0, commands.length, ...held.commands);
380
+ state.pending = null;
381
+ state.phase = "manual_override";
382
+ trace("command", "withheld", adopted_request_1.ADOPTED_MISMATCH);
383
+ }
352
384
  if (state.desired) {
385
+ const manual = state.phase === "manual_override";
353
386
  const theatre = (0, target_state_1.targetStateEvent)({
354
387
  target: "lighting",
355
388
  requestId: state.desired.requestId,
356
389
  generation: state.generation,
357
- status: (0, target_state_1.asTargetStatus)(state.phase, "confirming"),
390
+ status: manual ? "transitioning" : (0, target_state_1.asTargetStatus)(state.phase, "confirming"),
391
+ classification: manual ? "manual_override" : null,
358
392
  observedAt: now.toISOString(),
359
393
  details: {
360
394
  profile: state.desired.profile,
@@ -0,0 +1,26 @@
1
+ export declare const ADOPTED_MISMATCH = "adopted-mismatch";
2
+ export interface AdoptedRequest {
3
+ requestId: string;
4
+ generation: number;
5
+ }
6
+ export declare function adoptableRefresh(event: {
7
+ kind: string;
8
+ requestId?: unknown;
9
+ generation?: unknown;
10
+ }, state: {
11
+ desired: unknown;
12
+ generation: number;
13
+ }): AdoptedRequest | null;
14
+ export interface AdoptableState {
15
+ generation: number;
16
+ adoptedGeneration: number | null;
17
+ phase: string;
18
+ }
19
+ export declare function savedAdoptedGeneration(saved: {
20
+ adoptedGeneration?: unknown;
21
+ }): number | null;
22
+ export declare function isAdopted(state: AdoptableState): boolean;
23
+ export declare function readsOnly<Command>(state: AdoptableState, commands: Command[], reads: (command: Command) => boolean): {
24
+ commands: Command[];
25
+ mismatch: boolean;
26
+ };
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ADOPTED_MISMATCH = void 0;
4
+ exports.adoptableRefresh = adoptableRefresh;
5
+ exports.savedAdoptedGeneration = savedAdoptedGeneration;
6
+ exports.isAdopted = isAdopted;
7
+ exports.readsOnly = readsOnly;
8
+ // A theatre device controller that starts with nothing saved (a redeploy or
9
+ // restart without a context store) adopts the request the theatre activity
10
+ // controller's refresh carries, with its wanted values, so it can report on
11
+ // it. It only ever reads the device for that request: a device found in the
12
+ // wanted state reports ready, and one that isn't is a manual override, with
13
+ // nothing commanded until a new explicit request. The five device reducers
14
+ // share these rules so they stay the same.
15
+ exports.ADOPTED_MISMATCH = "adopted-mismatch";
16
+ // The request a refresh carries, if this node can adopt it: it has nothing
17
+ // desired, and the generation isn't older than its own. Each reducer checks
18
+ // the wanted values itself.
19
+ function adoptableRefresh(event, state) {
20
+ if (event.kind !== "refresh" || state.desired)
21
+ return null;
22
+ if (typeof event.requestId !== "string" || event.requestId === "")
23
+ return null;
24
+ if (!Number.isInteger(event.generation) || event.generation < Math.max(1, state.generation))
25
+ return null;
26
+ return { requestId: event.requestId, generation: event.generation };
27
+ }
28
+ function savedAdoptedGeneration(saved) {
29
+ return Number.isInteger(saved.adoptedGeneration) ? saved.adoptedGeneration : null;
30
+ }
31
+ function isAdopted(state) {
32
+ return state.adoptedGeneration !== null && state.adoptedGeneration === state.generation;
33
+ }
34
+ // For an adopted request, keeps only the commands that read the device. Any
35
+ // other command means the reducer tried to bring the device to the wanted
36
+ // state, so it isn't in it: the result says so, and the caller records a
37
+ // manual override instead.
38
+ function readsOnly(state, commands, reads) {
39
+ if (!isAdopted(state))
40
+ return { commands, mismatch: false };
41
+ const kept = commands.filter(reads);
42
+ return { commands: kept, mismatch: kept.length !== commands.length };
43
+ }
44
+ //# sourceMappingURL=adopted-request.js.map
@@ -68,9 +68,16 @@ export interface AvrState {
68
68
  recentVolumeRequestIds: string[];
69
69
  volumeMetrics: AvrVolumeMetrics;
70
70
  lastAttempt: number;
71
+ adoptedGeneration: number | null;
71
72
  }
72
73
  export type AvrInputEvent = {
73
- kind: "startup" | "refresh";
74
+ kind: "startup";
75
+ } | {
76
+ kind: "refresh";
77
+ requestId?: string | null;
78
+ generation?: number;
79
+ power?: string;
80
+ source?: string | null;
74
81
  } | {
75
82
  kind: "observed";
76
83
  line?: unknown;