@avantics/node-red-home-automation 3.0.0 → 3.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.
package/README.md CHANGED
@@ -44,6 +44,17 @@ day, and wind down to the sleep settings over the three hours before bed. Times
44
44
  are the Node-RED host's local time.
45
45
  Example: [`Room lighting.json`](https://github.com/avantics-hq/node-red-home-automation/blob/main/examples/Room%20lighting.json).
46
46
 
47
+ Wire the room controller's commands to MQTT out unchanged, and observe commands
48
+ and state as they are wired. The controller watches each light's command topic
49
+ to tell its own commands from other people's, so a function that rewrites them
50
+ on the way makes them look like someone else's. To stop that becoming a loop, a
51
+ bare ON or OFF that arrives within 5 seconds of one of its own commands with that
52
+ power, before that command has come back on the topic, is taken as that command,
53
+ and the controller restyles a light after someone else's bare ON at most once
54
+ every 5 seconds. A person's bare ON or OFF in that moment is taken as the
55
+ controller's own, which changes nothing, as the light is already being set that
56
+ way; other rewrites can still be misread.
57
+
47
58
  | Node | Purpose |
48
59
  |---|---|
49
60
  | `home-occupancy-fusion` | Combines PIR motion with optional presence sensing. |
@@ -111,11 +122,23 @@ also attaches the package tarball and its SHA-256 checksum. Install a downloaded
111
122
  tarball through **Manage palette → Install → Upload module**, or with:
112
123
 
113
124
  ```sh
114
- npm install /path/to/avantics-node-red-home-automation-3.0.0.tgz
125
+ npm install /path/to/avantics-node-red-home-automation-3.0.1.tgz
115
126
  ```
116
127
 
117
128
  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.
118
129
 
130
+ ### 3.0.1
131
+
132
+ Fixes the room lighting controller looping on its own commands when something
133
+ between it and MQTT out rewrites them, for example a function that cuts a styled
134
+ ON down to `{"state":"ON"}`: it took each rewritten command for someone else's
135
+ and answered with another, without end. It now takes a bare ON or OFF that
136
+ arrives within 5 seconds of one of its own commands with that power, before that
137
+ command has come back on the topic, as that command, and restyles a light after
138
+ someone else's bare ON at most once every 5 seconds.
139
+ It also no longer marks a light manual when its own command is seen twice. Wire
140
+ its commands to MQTT out unchanged (see Room lighting).
141
+
119
142
  ### Upgrading from 2.x
120
143
 
121
144
  Version 3.0 changes some behaviour. Check these after upgrading:
@@ -4,6 +4,7 @@ export interface ExpectedCommand {
4
4
  fingerprint: string;
5
5
  expires: number;
6
6
  on?: boolean;
7
+ power?: boolean;
7
8
  echoed?: boolean;
8
9
  }
9
10
  export declare function parsePayload(payload: unknown): unknown;
@@ -17,4 +18,5 @@ export declare function checkStateReport(queue: ExpectedCommand[] | undefined, o
17
18
  export declare function checkCommandEcho(queue: ExpectedCommand[] | undefined, payload: unknown, nowMs: number): {
18
19
  queue: ExpectedCommand[];
19
20
  own: boolean;
21
+ changed: boolean;
20
22
  };
@@ -46,8 +46,10 @@ function rememberCommand(queue, payload, nowMs) {
46
46
  const requested = payload && typeof payload === "object"
47
47
  ? String(payload.state || "").toUpperCase()
48
48
  : "";
49
- if (requested === "ON" || requested === "OFF")
49
+ if (requested === "ON" || requested === "OFF") {
50
50
  entry.on = requested === "ON";
51
+ entry.power = entry.on;
52
+ }
51
53
  next.push(entry);
52
54
  return next.slice(-exports.ECHO_QUEUE_LIMIT);
53
55
  }
@@ -59,24 +61,46 @@ function checkStateReport(queue, observedOn, nowMs) {
59
61
  for (const entry of next)
60
62
  delete entry.on;
61
63
  }
62
- return {
63
- queue: next.filter((entry) => entry.on !== undefined || entry.echoed !== true),
64
- verdict
65
- };
64
+ return { queue: next, verdict };
66
65
  }
67
- // A command seen on the command topic: one of ours (its first echo), or
68
- // someone else's.
66
+ // A command seen on the command topic: one of ours, or someone else's.
67
+ // - Exactly a command we sent in the echo window is ours, even when its echo
68
+ // was already seen: a duplicate or late echo.
69
+ // - A bare ON or OFF that arrives while one of our commands with that power
70
+ // isn't echoed yet is taken as that command, changed on the way (a
71
+ // function between the controller and MQTT out that cut the payload to
72
+ // {"state":"ON"}, say): `changed`. That command is then marked echoed but
73
+ // kept, so its own exact echo, if it still comes, is ours too. Without
74
+ // this the controller answered its own command as someone else's, which
75
+ // looped.
69
76
  function checkCommandEcho(queue, payload, nowMs) {
70
77
  const next = live(queue, nowMs);
71
- const ownIndex = next.findIndex((entry) => entry.echoed !== true && entry.fingerprint === fingerprint(payload));
78
+ const print = fingerprint(payload);
79
+ let ownIndex = next.findIndex((entry) => entry.echoed !== true && entry.fingerprint === print);
80
+ if (ownIndex < 0) {
81
+ const repeat = next.findIndex((entry) => entry.echoed === true && entry.fingerprint === print);
82
+ if (repeat >= 0)
83
+ return { queue: next, own: true, changed: false };
84
+ }
85
+ let changed = false;
86
+ if (ownIndex < 0) {
87
+ const requested = payload && typeof payload === "object"
88
+ ? String(payload.state || "").toUpperCase()
89
+ : "";
90
+ if (requested === "ON" || requested === "OFF") {
91
+ for (let index = next.length - 1; index >= 0 && ownIndex < 0; index -= 1) {
92
+ if (next[index].echoed !== true && next[index].power === (requested === "ON"))
93
+ ownIndex = index;
94
+ }
95
+ changed = ownIndex >= 0;
96
+ }
97
+ }
72
98
  const own = next[ownIndex];
73
99
  if (!own)
74
- return { queue: next, own: false };
75
- // Keep an unconfirmed power command as in flight for state reports.
76
- if (own.on === undefined)
77
- next.splice(ownIndex, 1);
78
- else
79
- own.echoed = true;
80
- return { queue: next, own: true };
100
+ return { queue: next, own: false, changed: false };
101
+ // Kept until it expires: for state reports while its power is unconfirmed,
102
+ // and to know a repeat of its echo.
103
+ own.echoed = true;
104
+ return { queue: next, own: true, changed };
81
105
  }
82
106
  //# sourceMappingURL=command-echo.js.map
@@ -37,6 +37,7 @@ export interface RoomState {
37
37
  circadianEnabled: boolean;
38
38
  sleeping: boolean;
39
39
  personAt: Record<string, number>;
40
+ restyledAt: Record<string, number>;
40
41
  }
41
42
  export interface LightingEvent {
42
43
  kind?: string;
@@ -28,7 +28,9 @@ function createRoomState(config, saved = {}) {
28
28
  circadianEnabled: saved.circadianEnabled !== false,
29
29
  sleeping: saved.sleeping === undefined ? config.initialSleep : saved.sleeping === true,
30
30
  personAt: Object.fromEntries(Object.entries(saved.personAt || {})
31
- .filter(([, at]) => Number.isFinite(at)))
31
+ .filter(([, at]) => Number.isFinite(at))),
32
+ restyledAt: Object.fromEntries(Object.entries(saved.restyledAt || {})
33
+ .filter(([target, at]) => Number.isFinite(at) && config.targets.some((entry) => entry.id === target)))
32
34
  };
33
35
  }
34
36
  // A circadian light command for this moment.
@@ -260,6 +262,11 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
260
262
  if (msg.retain !== true && event.retained !== true && target && payload) {
261
263
  const echo = (0, command_echo_1.checkCommandEcho)(state.expected[target.id], payload, nowMs);
262
264
  state.expected[target.id] = echo.queue;
265
+ if (echo.changed) {
266
+ // Our own command, changed on the way to MQTT (the command path
267
+ // should pass it through as it is): never answered as someone else's.
268
+ trace("command-observed", "ignored", "mutated-own-command", { target: target.id, state: String(payload.state || "").toUpperCase() });
269
+ }
263
270
  if (!echo.own) {
264
271
  const requestedState = String(payload.state || "").toUpperCase();
265
272
  if (requestedState === "OFF") {
@@ -272,9 +279,20 @@ function transitionRoom(saved, msg, config, nowValue = new Date()) {
272
279
  setControl(target.id, { state: "manual" }, "external-values");
273
280
  }
274
281
  else if (state.circadianEnabled && controlOf(target.id) === "in-control") {
275
- const corrective = command(target.id, adaptivePayload("ON", false, state.sleeping, now, config), "external-bare-on");
276
- if (corrective)
277
- outputs.push(corrective);
282
+ // At most one restyle per light per echo window, whatever comes
283
+ // back: a bare ON delivered twice (two subscriptions, or a QoS
284
+ // redelivery) can't make the room answer itself in a loop.
285
+ const last = state.restyledAt[target.id];
286
+ if (last !== undefined && nowMs - last >= 0 && nowMs - last < command_echo_1.ECHO_WINDOW_MS) {
287
+ trace("external-command", "suppressed", "restyled-recently", { target: target.id });
288
+ }
289
+ else {
290
+ const corrective = command(target.id, adaptivePayload("ON", false, state.sleeping, now, config), "external-bare-on");
291
+ if (corrective) {
292
+ outputs.push(corrective);
293
+ state.restyledAt[target.id] = nowMs;
294
+ }
295
+ }
278
296
  }
279
297
  }
280
298
  else if (requestedState === "TOGGLE") {
@@ -81,6 +81,11 @@
81
81
  <script type="text/html" data-help-name="home-room-lighting-controller">
82
82
  <p>Owns room MQTT commands, adaptive colour and brightness, manual overrides, discovery and bounded tracing.</p>
83
83
  <h3>Publishing</h3>
84
+ <p>Wire its commands to MQTT out unchanged. It watches each light's command topic to tell its own commands from
85
+ other people's, so a function that rewrites them on the way makes them look like someone else's. A bare ON or OFF
86
+ that arrives within 5 seconds of one of its own commands with that power, before that command has come back on the
87
+ topic, is taken as that command (traced as <code>mutated-own-command</code>), and it restyles a light after someone else's bare ON at most once every 5
88
+ seconds, so it never answers itself in a loop.</p>
84
89
  <p>The room always commands its lights on each target's own command topic. Two settings add what it publishes
85
90
  for other systems to see; both are named after the <b>Area</b>, which is needed only when one of them is on.</p>
86
91
  <dl class="message-properties">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@avantics/node-red-home-automation",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Deterministic home automation components for Node-RED",
5
5
  "keywords": [
6
6
  "node-red",