node-red-contrib-unifi-ultimate 1.1.16 → 1.1.18
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 +3 -0
- package/examples/unifi-protect-alarm-state.json +84 -0
- package/nodes/unifi-access-device.html +7 -0
- package/nodes/unifi-access-device.js +8 -6
- package/nodes/unifi-network-control-poe.js +5 -4
- package/nodes/unifi-network-device.html +9 -1
- package/nodes/unifi-network-device.js +15 -13
- package/nodes/unifi-network-presence.js +4 -3
- package/nodes/unifi-network-restart.js +3 -2
- package/nodes/unifi-protect-config.js +2 -1
- package/nodes/unifi-protect-device.html +15 -2
- package/nodes/unifi-protect-device.js +53 -17
- package/nodes/utils/common-utils.js +21 -1
- package/nodes/utils/unifi-protect-alarm.js +59 -0
- package/nodes/utils/unifi-protect-device-registry.js +10 -1
- package/nodes/utils/unifi-protect-field-metadata.js +12 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -149,6 +149,7 @@ Things you can do:
|
|
|
149
149
|
- Monitor bridges, Link Stations, alarm hubs, key fobs, sirens, relays, and speakers.
|
|
150
150
|
- Play or stop a siren, control relay outputs, and test speaker or siren sound.
|
|
151
151
|
- Read and select Protect Arm Profiles, then arm or disarm the local Alarm Manager from the NVR control.
|
|
152
|
+
- Poll Alarm Hub armed state or NVR local alarm status with **Read Alarm State**, or follow device updates with **Receive Events → Armed / Alarm status (local)**. These are separate alarm sources; the NVR arm commands require the local Alarm Manager. See [the polling example](examples/unifi-protect-alarm-state.json).
|
|
152
153
|
|
|
153
154
|
### Read license plates (LPR)
|
|
154
155
|
|
|
@@ -202,6 +203,8 @@ Every node has **two outputs**:
|
|
|
202
203
|
|
|
203
204
|
When an error occurs, the node status turns red and the error message comes out of the second output. Connect it to a **debug** node to see what happened, or wire it to any notification logic in your flow.
|
|
204
205
|
|
|
206
|
+
**Startup and missing readings** — Protect, Access and Network device nodes have an advanced **Emit startup states and undefined payloads** checkbox, disabled by default, including for existing flows. With it disabled, initial state snapshots and missing, null or `undefined` readings are suppressed. Valid `false` and `0` readings still pass through. Enable the checkbox to allow startup snapshots and missing payloads. Protect observables never replay an old value when a fresh reading is unavailable; LPR and event-photo actions continue to emit only actual readings or photos.
|
|
207
|
+
|
|
205
208
|
**Repeat periodically** — for read actions, you can tick _Emit periodically_ in the node editor to have the node send the result automatically at a fixed interval, without needing an Inject node. The **Presence Detection** node offers a similar **Resend (s)** field: set it above `0` to re-emit the last known presence value on that cadence (even when unchanged). Resent messages carry `msg.eventName = "repeat"` so you can tell them apart from real state changes; leave it at `0` to disable.
|
|
206
209
|
|
|
207
210
|
## Example Flows
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "flow-protect-alarm-poll",
|
|
4
|
+
"type": "tab",
|
|
5
|
+
"label": "Protect Alarm State",
|
|
6
|
+
"disabled": false,
|
|
7
|
+
"info": "Select your Protect connection and Alarm Hub in Read Alarm State. Polls every 60 seconds and after deployment. Hub payload is boolean; NVR payload is the local armMode.status string. Requires a version with Read Alarm State support."
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"id": "inject-protect-alarm-poll",
|
|
11
|
+
"type": "inject",
|
|
12
|
+
"z": "flow-protect-alarm-poll",
|
|
13
|
+
"name": "Poll every 60 seconds",
|
|
14
|
+
"props": [],
|
|
15
|
+
"repeat": "60",
|
|
16
|
+
"crontab": "",
|
|
17
|
+
"once": true,
|
|
18
|
+
"onceDelay": 5,
|
|
19
|
+
"topic": "",
|
|
20
|
+
"x": 170,
|
|
21
|
+
"y": 120,
|
|
22
|
+
"wires": [
|
|
23
|
+
[
|
|
24
|
+
"read-protect-alarm-state"
|
|
25
|
+
]
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"id": "read-protect-alarm-state",
|
|
30
|
+
"type": "unifi-protect-device",
|
|
31
|
+
"z": "flow-protect-alarm-poll",
|
|
32
|
+
"name": "Read Alarm State (Hub)",
|
|
33
|
+
"server": "",
|
|
34
|
+
"deviceType": "alarmHub",
|
|
35
|
+
"deviceId": "",
|
|
36
|
+
"deviceName": "",
|
|
37
|
+
"inputs": 1,
|
|
38
|
+
"capability": "getAlarmState",
|
|
39
|
+
"capabilityConfig": "{}",
|
|
40
|
+
"emitStartupAndUndefined": false,
|
|
41
|
+
"autoEmit": false,
|
|
42
|
+
"autoEmitInterval": 60,
|
|
43
|
+
"x": 440,
|
|
44
|
+
"y": 120,
|
|
45
|
+
"wires": [
|
|
46
|
+
[
|
|
47
|
+
"debug-protect-alarm-status"
|
|
48
|
+
],
|
|
49
|
+
[
|
|
50
|
+
"debug-protect-alarm-error"
|
|
51
|
+
]
|
|
52
|
+
]
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"id": "debug-protect-alarm-status",
|
|
56
|
+
"type": "debug",
|
|
57
|
+
"z": "flow-protect-alarm-poll",
|
|
58
|
+
"name": "Current alarm status",
|
|
59
|
+
"active": true,
|
|
60
|
+
"tosidebar": true,
|
|
61
|
+
"console": false,
|
|
62
|
+
"tostatus": false,
|
|
63
|
+
"complete": "payload",
|
|
64
|
+
"targetType": "msg",
|
|
65
|
+
"x": 990,
|
|
66
|
+
"y": 100,
|
|
67
|
+
"wires": []
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"id": "debug-protect-alarm-error",
|
|
71
|
+
"type": "debug",
|
|
72
|
+
"z": "flow-protect-alarm-poll",
|
|
73
|
+
"name": "Error / missing alarm state",
|
|
74
|
+
"active": true,
|
|
75
|
+
"tosidebar": true,
|
|
76
|
+
"console": false,
|
|
77
|
+
"tostatus": false,
|
|
78
|
+
"complete": "true",
|
|
79
|
+
"targetType": "full",
|
|
80
|
+
"x": 730,
|
|
81
|
+
"y": 200,
|
|
82
|
+
"wires": []
|
|
83
|
+
}
|
|
84
|
+
]
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
inputs: { value: 1 },
|
|
16
16
|
capability: { value: "observe", required: true },
|
|
17
17
|
capabilityConfig: { value: "{}" },
|
|
18
|
+
emitStartupAndUndefined: { value: false },
|
|
18
19
|
autoEmit: { value: false },
|
|
19
20
|
autoEmitInterval: { value: 60, validate: RED.validators.number() }
|
|
20
21
|
},
|
|
@@ -1165,6 +1166,11 @@
|
|
|
1165
1166
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
1166
1167
|
<input type="text" id="node-input-name" placeholder="Node name">
|
|
1167
1168
|
</div>
|
|
1169
|
+
<div class="form-row">
|
|
1170
|
+
<label for="node-input-emitStartupAndUndefined"><i class="fa fa-sign-out"></i> Output</label>
|
|
1171
|
+
<input type="checkbox" id="node-input-emitStartupAndUndefined" style="display:inline-block; width:auto; vertical-align:top;">
|
|
1172
|
+
<label for="node-input-emitStartupAndUndefined" style="width:auto;">Emit startup states and undefined payloads</label>
|
|
1173
|
+
</div>
|
|
1168
1174
|
<div class="form-row" id="node-row-autoEmit">
|
|
1169
1175
|
<label for="node-input-autoEmit"><i class="fa fa-repeat"></i> Repeat</label>
|
|
1170
1176
|
<input type="checkbox" id="node-input-autoEmit" style="display:inline-block; width:auto; vertical-align:top;">
|
|
@@ -1179,6 +1185,7 @@
|
|
|
1179
1185
|
</script>
|
|
1180
1186
|
|
|
1181
1187
|
<script type="text/html" data-help-name="unifi-access-device">
|
|
1188
|
+
<p><b>Emit startup states and undefined payloads</b> is disabled by default, including in existing flows. Enable it to emit the initial device state and readings with a missing, null or undefined payload. Otherwise these messages are suppressed; false and zero remain valid values. Missing readings never replay a previous value.</p>
|
|
1182
1189
|
<p><a href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
|
|
1183
1190
|
|
|
1184
1191
|
<p>Use this node to execute a configured UniFi Access action or listen for live Access events.</p>
|
|
@@ -21,7 +21,8 @@ const {
|
|
|
21
21
|
extractDeviceNameFromPayload,
|
|
22
22
|
attachDeviceNameToPayload,
|
|
23
23
|
attachDetails,
|
|
24
|
-
buildErrorOutputMessage
|
|
24
|
+
buildErrorOutputMessage,
|
|
25
|
+
sendWithPayload
|
|
25
26
|
} = require("./utils/common-utils");
|
|
26
27
|
const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
|
|
27
28
|
|
|
@@ -98,6 +99,7 @@ module.exports = function(RED) {
|
|
|
98
99
|
node.deviceId = config.deviceId || "";
|
|
99
100
|
node.capability = config.capability || "observe";
|
|
100
101
|
node.capabilityConfig = config.capabilityConfig || "{}";
|
|
102
|
+
node.emitStartupAndUndefined = parseBoolean(config.emitStartupAndUndefined);
|
|
101
103
|
node.autoEmit = parseBoolean(config.autoEmit);
|
|
102
104
|
node.autoEmitIntervalSeconds = parseIntervalSeconds(config.autoEmitInterval, 60);
|
|
103
105
|
node.autoEmitTimer = null;
|
|
@@ -115,7 +117,7 @@ module.exports = function(RED) {
|
|
|
115
117
|
const output = { payload };
|
|
116
118
|
decorateOutputMessage(output, node.currentDevice, eventName);
|
|
117
119
|
attachDetails(output, { ...details, unifiAccess: buildBaseMetadata(node.deviceType, node.deviceId, node.capability, { source: details.monitor.source }) });
|
|
118
|
-
node.send(output);
|
|
120
|
+
sendWithPayload(node.send.bind(node), output, node.emitStartupAndUndefined);
|
|
119
121
|
}
|
|
120
122
|
});
|
|
121
123
|
node.getSimpleMonitorDescriptor = () => node.isObserving && isSimpleMonitor() && node.deviceId ? {
|
|
@@ -163,10 +165,10 @@ module.exports = function(RED) {
|
|
|
163
165
|
// event messages are available, forward them in sequence.
|
|
164
166
|
try {
|
|
165
167
|
if (stateMsg) {
|
|
166
|
-
send
|
|
168
|
+
sendWithPayload(send, stateMsg, node.emitStartupAndUndefined);
|
|
167
169
|
}
|
|
168
170
|
if (eventMsg) {
|
|
169
|
-
send
|
|
171
|
+
sendWithPayload(send, eventMsg, node.emitStartupAndUndefined);
|
|
170
172
|
}
|
|
171
173
|
} catch (error) {
|
|
172
174
|
node.warn(`Access output send failed: ${error && error.message ? error.message : error}`);
|
|
@@ -207,6 +209,7 @@ module.exports = function(RED) {
|
|
|
207
209
|
});
|
|
208
210
|
|
|
209
211
|
setNodeStatus({ fill: "green", shape: "dot", text: buildNodeStatus(deviceType, payload) });
|
|
212
|
+
if (source === "startup" && !node.emitStartupAndUndefined) return;
|
|
210
213
|
sendOutputs(send, stateMsg, null);
|
|
211
214
|
}
|
|
212
215
|
|
|
@@ -406,8 +409,7 @@ module.exports = function(RED) {
|
|
|
406
409
|
return;
|
|
407
410
|
}
|
|
408
411
|
|
|
409
|
-
//
|
|
410
|
-
// before websocket events arrive.
|
|
412
|
+
// Load the initial snapshot; output at startup is opt-in.
|
|
411
413
|
fetchDeviceState(node.deviceType, node.deviceId, "observe", node.send.bind(node), "startup").catch(() => {
|
|
412
414
|
});
|
|
413
415
|
}
|
|
@@ -13,7 +13,8 @@ const {
|
|
|
13
13
|
extractDeviceNameFromPayload,
|
|
14
14
|
attachDeviceNameToPayload,
|
|
15
15
|
attachDetails,
|
|
16
|
-
buildErrorOutputMessage
|
|
16
|
+
buildErrorOutputMessage,
|
|
17
|
+
sendWithPayload
|
|
17
18
|
} = require("./utils/common-utils");
|
|
18
19
|
const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
|
|
19
20
|
|
|
@@ -864,7 +865,7 @@ module.exports = function(RED) {
|
|
|
864
865
|
shape: "dot",
|
|
865
866
|
text: `p${portIdx} ${formatPowerText(powerW)}W`
|
|
866
867
|
});
|
|
867
|
-
send
|
|
868
|
+
sendWithPayload(send, output);
|
|
868
869
|
}
|
|
869
870
|
|
|
870
871
|
function handlePowerObservationEvent(send, action, event) {
|
|
@@ -919,7 +920,7 @@ module.exports = function(RED) {
|
|
|
919
920
|
shape: "dot",
|
|
920
921
|
text: `p${portIdx} ${formatPowerText(normalizedPowerW)}W`
|
|
921
922
|
});
|
|
922
|
-
send
|
|
923
|
+
sendWithPayload(send, output);
|
|
923
924
|
}
|
|
924
925
|
|
|
925
926
|
function stopPowerObservation() {
|
|
@@ -1074,7 +1075,7 @@ module.exports = function(RED) {
|
|
|
1074
1075
|
setNodeStatus({ fill: "yellow", shape: "dot", text: `${effectiveAction.payloadAction} ${okCount}/${results.length}` });
|
|
1075
1076
|
}
|
|
1076
1077
|
|
|
1077
|
-
send
|
|
1078
|
+
sendWithPayload(send, output);
|
|
1078
1079
|
}
|
|
1079
1080
|
|
|
1080
1081
|
node.on("input", async function(msg, send, done) {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
inputs: { value: 1 },
|
|
16
16
|
capability: { value: "observe", required: true },
|
|
17
17
|
capabilityConfig: { value: "{}" },
|
|
18
|
+
emitStartupAndUndefined: { value: false },
|
|
18
19
|
autoEmit: { value: false },
|
|
19
20
|
autoEmitInterval: { value: 60, validate: RED.validators.number() }
|
|
20
21
|
},
|
|
@@ -1236,6 +1237,7 @@
|
|
|
1236
1237
|
refreshEditor();
|
|
1237
1238
|
}
|
|
1238
1239
|
});
|
|
1240
|
+
|
|
1239
1241
|
</script>
|
|
1240
1242
|
|
|
1241
1243
|
<script type="text/html" data-template-name="unifi-network-device">
|
|
@@ -1279,6 +1281,11 @@
|
|
|
1279
1281
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
1280
1282
|
<input type="text" id="node-input-name" placeholder="Node name">
|
|
1281
1283
|
</div>
|
|
1284
|
+
<div class="form-row">
|
|
1285
|
+
<label for="node-input-emitStartupAndUndefined"><i class="fa fa-sign-out"></i> Output</label>
|
|
1286
|
+
<input type="checkbox" id="node-input-emitStartupAndUndefined" style="display:inline-block; width:auto; vertical-align:top;">
|
|
1287
|
+
<label for="node-input-emitStartupAndUndefined" style="width:auto;">Emit startup states and undefined payloads</label>
|
|
1288
|
+
</div>
|
|
1282
1289
|
<div class="form-row" id="node-row-autoEmit">
|
|
1283
1290
|
<label for="node-input-autoEmit"><i class="fa fa-repeat"></i> Repeat</label>
|
|
1284
1291
|
<input type="checkbox" id="node-input-autoEmit" style="display:inline-block; width:auto; vertical-align:top;">
|
|
@@ -1293,6 +1300,7 @@
|
|
|
1293
1300
|
</script>
|
|
1294
1301
|
|
|
1295
1302
|
<script type="text/html" data-help-name="unifi-network-device">
|
|
1303
|
+
<p><b>Emit startup states and undefined payloads</b> is disabled by default, including in existing flows. Enable it to emit the initial device state and readings with a missing, null or undefined payload. Otherwise these messages are suppressed; false and zero remain valid values. Missing readings never replay a previous value.</p>
|
|
1296
1304
|
<p><a href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
|
|
1297
1305
|
|
|
1298
1306
|
<p>Use this node to execute a configured UniFi Network action on a selected site, UniFi device, or client.</p>
|
|
@@ -1394,4 +1402,4 @@ msg.payload = { state: "failover", interface: "eth9", at: "2026-09-12T12:00:00.0
|
|
|
1394
1402
|
"payload": null,
|
|
1395
1403
|
"error": { "message": "Request timed out" }
|
|
1396
1404
|
}</code></pre>
|
|
1397
|
-
</script>
|
|
1405
|
+
</script>
|
|
@@ -20,7 +20,8 @@ const {
|
|
|
20
20
|
extractDeviceNameFromPayload,
|
|
21
21
|
attachDeviceNameToPayload,
|
|
22
22
|
attachDetails,
|
|
23
|
-
buildErrorOutputMessage
|
|
23
|
+
buildErrorOutputMessage,
|
|
24
|
+
sendWithPayload
|
|
24
25
|
} = require("./utils/common-utils");
|
|
25
26
|
const UNOFFICIAL_NETWORK_STREAM_CAPABILITY = "observeUnofficialEvents";
|
|
26
27
|
const UNOFFICIAL_POLL_INTERVAL_MS = 3000;
|
|
@@ -502,6 +503,7 @@ module.exports = function(RED) {
|
|
|
502
503
|
node.deviceId = config.deviceId || "";
|
|
503
504
|
node.capability = config.capability || "observe";
|
|
504
505
|
node.capabilityConfig = config.capabilityConfig || "{}";
|
|
506
|
+
node.emitStartupAndUndefined = parseBoolean(config.emitStartupAndUndefined);
|
|
505
507
|
node.autoEmit = parseBoolean(config.autoEmit);
|
|
506
508
|
node.autoEmitIntervalSeconds = parseIntervalSeconds(config.autoEmitInterval, 60);
|
|
507
509
|
node.autoEmitTimer = null;
|
|
@@ -519,7 +521,7 @@ module.exports = function(RED) {
|
|
|
519
521
|
const output = { payload };
|
|
520
522
|
decorateOutputMessage(output, node.currentDevice, eventName);
|
|
521
523
|
attachDetails(output, { ...details, unifiNetwork: buildBaseMetadata(node.deviceType, node.deviceId, node.capability, { source: details.monitor.source }) });
|
|
522
|
-
node.send(output);
|
|
524
|
+
sendWithPayload(node.send.bind(node), output, node.emitStartupAndUndefined);
|
|
523
525
|
}
|
|
524
526
|
});
|
|
525
527
|
node.getSimpleMonitorDescriptor = () => node.isObserving && isSimpleMonitor() && node.deviceId ? {
|
|
@@ -616,7 +618,8 @@ module.exports = function(RED) {
|
|
|
616
618
|
decorateOutputMessage(outputMsg, payload, source || "observe");
|
|
617
619
|
|
|
618
620
|
setNodeStatus({ fill: "green", shape: "dot", text: buildNodeStatus(deviceType, payload) });
|
|
619
|
-
|
|
621
|
+
if (source === "startup" && !node.emitStartupAndUndefined) return;
|
|
622
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
620
623
|
}
|
|
621
624
|
|
|
622
625
|
async function fetchDeviceTemperatures(deviceType, deviceId, capabilityId, send) {
|
|
@@ -660,7 +663,7 @@ module.exports = function(RED) {
|
|
|
660
663
|
? `${statusTemperatureC} C`
|
|
661
664
|
: "no temperature";
|
|
662
665
|
setNodeStatus({ fill: temperatures.length > 0 ? "green" : "yellow", shape: "dot", text: statusText });
|
|
663
|
-
send
|
|
666
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
664
667
|
}
|
|
665
668
|
|
|
666
669
|
async function executeConfiguredCapabilityRequest(deviceType, deviceId, capabilityId, capabilityConfig) {
|
|
@@ -742,7 +745,7 @@ module.exports = function(RED) {
|
|
|
742
745
|
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
743
746
|
|
|
744
747
|
setNodeStatus({ fill: online ? "green" : "yellow", shape: "dot", text: online ? "online" : "offline" });
|
|
745
|
-
send
|
|
748
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
746
749
|
}
|
|
747
750
|
|
|
748
751
|
async function countOnlineClients(deviceType, deviceId, capabilityId, send) {
|
|
@@ -777,7 +780,7 @@ module.exports = function(RED) {
|
|
|
777
780
|
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
778
781
|
|
|
779
782
|
setNodeStatus({ fill: "green", shape: "dot", text: `${onlineClients.length} online` });
|
|
780
|
-
send
|
|
783
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
781
784
|
}
|
|
782
785
|
|
|
783
786
|
async function fetchDeviceSimpleStatistic(deviceType, deviceId, capabilityId, capabilityConfig, send) {
|
|
@@ -836,7 +839,7 @@ module.exports = function(RED) {
|
|
|
836
839
|
? `${metric.label} n/a`
|
|
837
840
|
: `${metric.label} ${payloadValue}${metric.unit}`;
|
|
838
841
|
setNodeStatus({ fill: payloadValue === null ? "yellow" : "green", shape: "dot", text: statusText });
|
|
839
|
-
send
|
|
842
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
840
843
|
}
|
|
841
844
|
|
|
842
845
|
async function createGuestVoucher(deviceType, deviceId, capabilityId, capabilityConfig, send) {
|
|
@@ -882,7 +885,7 @@ module.exports = function(RED) {
|
|
|
882
885
|
decorateOutputMessage(outputMsg, outputMsg.payload, `request:${capabilityId}`);
|
|
883
886
|
|
|
884
887
|
setNodeStatus({ fill: vouchers.length > 0 ? "green" : "yellow", shape: "dot", text: `${vouchers.length} voucher` });
|
|
885
|
-
send
|
|
888
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
886
889
|
}
|
|
887
890
|
|
|
888
891
|
async function invokeCapability(send, triggerSource) {
|
|
@@ -977,7 +980,7 @@ module.exports = function(RED) {
|
|
|
977
980
|
decorateOutputMessage(outputMsg, responseData, `request:${capabilityId}`);
|
|
978
981
|
|
|
979
982
|
setNodeStatus({ fill: "green", shape: "dot", text: `${capability.label}` });
|
|
980
|
-
send
|
|
983
|
+
sendWithPayload(send, outputMsg, node.emitStartupAndUndefined);
|
|
981
984
|
}
|
|
982
985
|
|
|
983
986
|
function resolveConfiguredCapabilityDefinition() {
|
|
@@ -1052,8 +1055,7 @@ module.exports = function(RED) {
|
|
|
1052
1055
|
}
|
|
1053
1056
|
const capabilityId = resolveCapabilityId(node.capability);
|
|
1054
1057
|
|
|
1055
|
-
//
|
|
1056
|
-
// as soon as Node-RED starts.
|
|
1058
|
+
// Load the initial snapshot; output at startup is opt-in.
|
|
1057
1059
|
fetchDeviceState(node.deviceType, node.deviceId, capabilityId, node.send.bind(node), "startup").catch(() => {
|
|
1058
1060
|
});
|
|
1059
1061
|
refreshUnofficialPollingFallback();
|
|
@@ -1163,7 +1165,7 @@ module.exports = function(RED) {
|
|
|
1163
1165
|
})
|
|
1164
1166
|
});
|
|
1165
1167
|
decorateOutputMessage(output, output.payload, eventName);
|
|
1166
|
-
node.send(output);
|
|
1168
|
+
sendWithPayload(node.send.bind(node), output, node.emitStartupAndUndefined);
|
|
1167
1169
|
} catch (error) {
|
|
1168
1170
|
}
|
|
1169
1171
|
};
|
|
@@ -1241,7 +1243,7 @@ module.exports = function(RED) {
|
|
|
1241
1243
|
})
|
|
1242
1244
|
});
|
|
1243
1245
|
decorateOutputMessage(output, output.payload, resolvedEventName);
|
|
1244
|
-
node.send(output);
|
|
1246
|
+
sendWithPayload(node.send.bind(node), output, node.emitStartupAndUndefined);
|
|
1245
1247
|
} catch (error) {
|
|
1246
1248
|
}
|
|
1247
1249
|
};
|
|
@@ -10,7 +10,8 @@ const {
|
|
|
10
10
|
extractDeviceNameFromPayload,
|
|
11
11
|
attachDetails,
|
|
12
12
|
buildErrorOutputMessage,
|
|
13
|
-
parseBoolean
|
|
13
|
+
parseBoolean,
|
|
14
|
+
sendWithPayload
|
|
14
15
|
} = require("./utils/common-utils");
|
|
15
16
|
const DEFAULT_REQUEST_TIMEOUT_MS = 8000;
|
|
16
17
|
|
|
@@ -105,7 +106,7 @@ function setupNetworkClientWatch(node, config) {
|
|
|
105
106
|
};
|
|
106
107
|
attachDetails(outputMsg, { client });
|
|
107
108
|
|
|
108
|
-
node.send([outputMsg, null]);
|
|
109
|
+
sendWithPayload(node.send.bind(node), [outputMsg, null]);
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
function diffAndEmit(currentClients) {
|
|
@@ -389,7 +390,7 @@ module.exports = function(RED) {
|
|
|
389
390
|
}
|
|
390
391
|
|
|
391
392
|
try {
|
|
392
|
-
node.send(outputMsg);
|
|
393
|
+
sendWithPayload(node.send.bind(node), outputMsg);
|
|
393
394
|
} catch (error) {
|
|
394
395
|
node.warn(`Presence output send failed: ${error && error.message ? error.message : error}`);
|
|
395
396
|
}
|
|
@@ -7,7 +7,8 @@ const {
|
|
|
7
7
|
appendStatusTimestamp,
|
|
8
8
|
resolveNodeName,
|
|
9
9
|
attachDetails,
|
|
10
|
-
buildErrorOutputMessage
|
|
10
|
+
buildErrorOutputMessage,
|
|
11
|
+
sendWithPayload
|
|
11
12
|
} = require("./utils/common-utils");
|
|
12
13
|
|
|
13
14
|
const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
|
|
@@ -343,7 +344,7 @@ module.exports = function(RED) {
|
|
|
343
344
|
setNodeStatus({ fill: "yellow", shape: "ring", text: `${succeeded}/${results.length} ok` });
|
|
344
345
|
}
|
|
345
346
|
|
|
346
|
-
send
|
|
347
|
+
sendWithPayload(send, [output, null]);
|
|
347
348
|
}
|
|
348
349
|
|
|
349
350
|
node.on("input", async function(msg, send, done) {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { registerConnectionCheck } = require("./utils/unifi-connection-check");
|
|
4
4
|
const { actionGuidance } = require("./utils/unifi-action-guidance");
|
|
5
|
+
const { buildRequestError } = require("./utils/unifi-protect-alarm");
|
|
5
6
|
|
|
6
7
|
const {
|
|
7
8
|
buildBaseUrlFromHost,
|
|
@@ -411,7 +412,7 @@ module.exports = function(RED) {
|
|
|
411
412
|
method: "GET"
|
|
412
413
|
});
|
|
413
414
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
414
|
-
throw
|
|
415
|
+
throw buildRequestError(response, "GET", path, [...Object.values(node.credentials || {}), node.getApiKey()]);
|
|
415
416
|
}
|
|
416
417
|
return response.payload;
|
|
417
418
|
};
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
inputs: { value: 1 },
|
|
16
16
|
capability: { value: "observe", required: true },
|
|
17
17
|
capabilityConfig: { value: "{}" },
|
|
18
|
+
emitStartupAndUndefined: { value: false },
|
|
18
19
|
autoEmit: { value: false },
|
|
19
20
|
autoEmitInterval: { value: 60, validate: RED.validators.number() }
|
|
20
21
|
},
|
|
@@ -1173,6 +1174,11 @@
|
|
|
1173
1174
|
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
1174
1175
|
<input type="text" id="node-input-name" placeholder="Node name">
|
|
1175
1176
|
</div>
|
|
1177
|
+
<div class="form-row">
|
|
1178
|
+
<label for="node-input-emitStartupAndUndefined"><i class="fa fa-sign-out"></i> Output</label>
|
|
1179
|
+
<input type="checkbox" id="node-input-emitStartupAndUndefined" style="display:inline-block; width:auto; vertical-align:top;">
|
|
1180
|
+
<label for="node-input-emitStartupAndUndefined" style="width:auto;">Emit startup states and undefined payloads</label>
|
|
1181
|
+
</div>
|
|
1176
1182
|
<div class="form-row" id="node-row-autoEmit">
|
|
1177
1183
|
<label for="node-input-autoEmit"><i class="fa fa-repeat"></i> Repeat</label>
|
|
1178
1184
|
<input type="checkbox" id="node-input-autoEmit" style="display:inline-block; width:auto; vertical-align:top;">
|
|
@@ -1184,9 +1190,13 @@
|
|
|
1184
1190
|
<span style="margin-left:8px;">seconds</span>
|
|
1185
1191
|
</div>
|
|
1186
1192
|
</div>
|
|
1193
|
+
<br/>
|
|
1194
|
+
<br/>
|
|
1195
|
+
<br/>
|
|
1187
1196
|
</script>
|
|
1188
1197
|
|
|
1189
1198
|
<script type="text/html" data-help-name="unifi-protect-device">
|
|
1199
|
+
<p><b>Emit startup states and undefined payloads</b> is disabled by default, including in existing flows. Enable it to emit the initial device state and readings with a missing, null or undefined payload. Otherwise these messages are suppressed; false and zero remain valid values. Missing readings never replay a previous value.</p>
|
|
1190
1200
|
<p><a href="https://youtube.com/playlist?list=PL9Yh1bjbLAYrWKtMlopN0swuQXbdJ8MFJ&si=4MmW1nNTCLrJtEHv" target="_blank" rel="noopener noreferrer"><img src="resources/node-red-contrib-unifi-ultimate/logos/youtube-icon.svg" alt="YouTube" width="24" height="18" style="vertical-align:text-bottom; margin-right:6px;">Youtube Tutorials</a></p>
|
|
1191
1201
|
|
|
1192
1202
|
<p>Use this node to execute a configured UniFi Protect action or listen for live Protect events.</p>
|
|
@@ -1199,7 +1209,10 @@
|
|
|
1199
1209
|
<p>Action highlights: <code>Receive Events</code>, <code>Read Device State</code>, <code>Take Snapshot</code>, <code>Show Doorbell Message</code>, siren play/stop/test, relay output control, speaker sound test, and application/alarm actions on <code>NVR</code>.</p>
|
|
1200
1210
|
<p>Protect resources include cameras, sensors, lights, chimes, viewers, live views, NVRs, bridges, Link Stations, alarm hubs, key fobs, relays, sirens, and speakers. The newer device families expose read state and events without the generic raw-update action.</p>
|
|
1201
1211
|
<p><code>Play Siren</code> uses a duration in seconds. <code>Control Relay Output</code> requires an explicit output and on/off state; auto-off applies only when turning an output on. <code>Trigger Alarm Hub Output</code> can toggle an output or set it on/off, with an optional delay and duration. Sound-test actions use a configured volume and never read control values from the incoming message.</p>
|
|
1202
|
-
<p>
|
|
1212
|
+
<p>The <code>NVR</code> arm profile commands control Protect's local Alarm Manager; they do not necessarily control the alarm managed by an Alarm Hub or a global Alarm Manager. Use <code>Read Arm Profiles</code>, <code>Select Arm Profile</code>, <code>Arm Alarm</code>, and <code>Disarm Alarm</code>. These endpoints require Protect's local Alarm Manager.</p>
|
|
1213
|
+
<p><code>Read Alarm State</code> is available on <code>Alarm Hub</code> and <code>NVR</code>. On an Alarm Hub it reads <code>alarmHub.armed</code> and emits <code>true</code> for <code>on</code> or <code>false</code> for <code>off</code>. On an NVR it emits the local Alarm Manager's <code>armMode.status</code> string without converting transitional or unknown statuses to a boolean. The full device remains in <code>msg.details.device</code>. Missing or unsupported state goes to output 2; it never means disarmed. Trigger this action with an Inject node or enable periodic reads for recovery after missed updates or controller restarts.</p>
|
|
1214
|
+
<p>For live state, choose <code>Receive Events → Armed</code> on an Alarm Hub, or <code>Receive Events → Alarm status (local)</code> on an NVR. These values follow device updates, rather than assuming a dedicated arm/disarm event exists. Enable startup emission if you need an initial value after deploy. Delivery depends on Protect publishing device updates; use periodic <code>Read Alarm State</code> for recovery. NVR state and Hub state are separate sources and can differ.</p>
|
|
1215
|
+
<p>Arm/Disarm commands use the configured Protect API key and its account permissions. The optional local username/password used for recorded events does not grant permissions to these commands. HTTP 401 suggests checking the key; 403 suggests checking permissions. HTTP 400 alone does not establish a permission problem: check the selected profile and whether the local Alarm Manager is in use. Request failures include HTTP status, method, path and a bounded, redacted API message when available in <code>msg.details.response</code>. No automatic fallback to raw updates, relay outputs or undocumented alarm controls is performed.</p>
|
|
1203
1216
|
<p><code>Receive Events</code> performs an initial fetch and then subscribes to streams managed by the shared Protect config node. The config node keeps websocket channels and forwards matching updates to runtime nodes. In this mode, the input pin is hidden and listening starts automatically after deploy/restart.</p>
|
|
1204
1217
|
<p>For <code>Sensor</code>, <code>Camera</code>, <code>Light</code>, <code>Key Fob</code>, <code>Relay</code>, and <code>Alarm Hub</code> observables, <code>All</code> emits every event for the selected device. Choosing a specific observable normalizes the main output to a derived value and keeps the original context in <code>msg.details.raw</code>. Event observables emit <code>true/false</code>; sensor metrics such as <code>Temperature</code>, <code>Humidity</code>, <code>Light Level</code>, and <code>Battery Level</code> emit numeric values. <code>License Plate (LPR)</code> emits recognized plate text.</p>
|
|
1205
1218
|
<p>For <code>Key Fob</code>, the event list includes <code>Arm (1)</code>, <code>Night (2)</code>, <code>Disarm (3)</code>, <code>Panic (4)</code>, <code>Left</code>, and <code>Right</code>. The two side buttons have no printed number. Selecting one emits <code>true</code> only for that button; the original <code>sensorButtonPressed</code> event remains available in <code>msg.details.raw</code>.</p>
|
|
@@ -1305,4 +1318,4 @@ XY456ZT = Delivery van</code></pre>
|
|
|
1305
1318
|
"payload": null,
|
|
1306
1319
|
"error": { "message": "Request timed out" }
|
|
1307
1320
|
}</code></pre>
|
|
1308
|
-
</script>
|
|
1321
|
+
</script>
|
|
@@ -18,10 +18,13 @@ const {
|
|
|
18
18
|
extractDeviceNameFromPayload,
|
|
19
19
|
attachDeviceNameToPayload,
|
|
20
20
|
attachDetails,
|
|
21
|
-
buildErrorOutputMessage
|
|
21
|
+
buildErrorOutputMessage,
|
|
22
|
+
sendWithPayload,
|
|
23
|
+
hasPayloadValue
|
|
22
24
|
} = require("./utils/common-utils");
|
|
23
25
|
const { parseKnownPlates, namePlate, matchesDetection, describeDetection } = require("./utils/unifi-protect-detections");
|
|
24
26
|
const { LICENSE_PLATE_EVENT_TYPES, extractLicensePlates, isLicensePlateEvent } = require("./utils/unifi-protect-lpr");
|
|
27
|
+
const { readAlarmState, matchesDeviceUpdate, mergeDeviceUpdate, buildRequestError } = require("./utils/unifi-protect-alarm");
|
|
25
28
|
const DEFAULT_REQUEST_TIMEOUT_MS = 15000;
|
|
26
29
|
const LPR_FOLLOW_UP_DELAYS_MS = [150, 350, 750, 1500, 3000];
|
|
27
30
|
|
|
@@ -106,6 +109,7 @@ module.exports = function(RED) {
|
|
|
106
109
|
node.deviceId = config.deviceId || "";
|
|
107
110
|
node.capability = config.capability || "observe";
|
|
108
111
|
node.capabilityConfig = config.capabilityConfig || "{}";
|
|
112
|
+
node.emitStartupAndUndefined = parseBoolean(config.emitStartupAndUndefined);
|
|
109
113
|
node.autoEmit = parseBoolean(config.autoEmit);
|
|
110
114
|
node.autoEmitIntervalSeconds = parseIntervalSeconds(config.autoEmitInterval, 60);
|
|
111
115
|
node.autoEmitTimer = null;
|
|
@@ -140,6 +144,12 @@ module.exports = function(RED) {
|
|
|
140
144
|
});
|
|
141
145
|
}
|
|
142
146
|
|
|
147
|
+
function protectErrorOutput(error) {
|
|
148
|
+
const message = buildErrorOutputMessage(error, node.name);
|
|
149
|
+
if (error.protectResponse) attachDetails(message, { response: error.protectResponse });
|
|
150
|
+
return message;
|
|
151
|
+
}
|
|
152
|
+
|
|
143
153
|
function resolveOutputDeviceName(payload) {
|
|
144
154
|
const extracted = extractDeviceNameFromPayload(payload);
|
|
145
155
|
if (extracted) {
|
|
@@ -164,10 +174,10 @@ module.exports = function(RED) {
|
|
|
164
174
|
// event messages are available, forward them in sequence.
|
|
165
175
|
try {
|
|
166
176
|
if (stateMsg) {
|
|
167
|
-
send
|
|
177
|
+
sendWithPayload(send, stateMsg, node.emitStartupAndUndefined);
|
|
168
178
|
}
|
|
169
179
|
if (eventMsg) {
|
|
170
|
-
send
|
|
180
|
+
sendWithPayload(send, eventMsg, node.emitStartupAndUndefined);
|
|
171
181
|
}
|
|
172
182
|
} catch (error) {
|
|
173
183
|
node.warn(`Protect output send failed: ${error && error.message ? error.message : error}`);
|
|
@@ -194,7 +204,8 @@ module.exports = function(RED) {
|
|
|
194
204
|
if (observable) {
|
|
195
205
|
// Observables let the node collapse complex Protect payloads into
|
|
196
206
|
// a stable typed value while preserving the original context in details.raw.
|
|
197
|
-
const observableValue = resolveObservableState(deviceType, payload, observable
|
|
207
|
+
const observableValue = resolveObservableState(deviceType, payload, observable);
|
|
208
|
+
if (!node.emitStartupAndUndefined && !hasPayloadValue(observableValue)) return null;
|
|
198
209
|
node.currentObservableValue = observableValue;
|
|
199
210
|
|
|
200
211
|
const outputMsg = {
|
|
@@ -254,6 +265,7 @@ module.exports = function(RED) {
|
|
|
254
265
|
const stateMsg = buildObservedStateMessage(deviceType, deviceId, capabilityConfig, payload, source);
|
|
255
266
|
|
|
256
267
|
setNodeStatus({ fill: "green", shape: "dot", text: buildNodeStatus(deviceType, payload) });
|
|
268
|
+
if (source === "startup" && !node.emitStartupAndUndefined) return;
|
|
257
269
|
sendOutputs(send, stateMsg, null);
|
|
258
270
|
}
|
|
259
271
|
|
|
@@ -293,7 +305,7 @@ module.exports = function(RED) {
|
|
|
293
305
|
const { events, ...history } = result;
|
|
294
306
|
attachDetails(output, { history, unifiProtect: buildBaseMetadata(deviceType, deviceId, capabilityId, { source: "history" }) });
|
|
295
307
|
setNodeStatus({ fill: result.hasMore ? "yellow" : "green", shape: "dot", text: `${events.length} detections${result.hasMore ? "; more available" : ""}` });
|
|
296
|
-
send
|
|
308
|
+
sendWithPayload(send, output, node.emitStartupAndUndefined);
|
|
297
309
|
return;
|
|
298
310
|
}
|
|
299
311
|
|
|
@@ -319,7 +331,9 @@ module.exports = function(RED) {
|
|
|
319
331
|
|
|
320
332
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
321
333
|
setNodeStatus({ fill: "yellow", shape: "ring", text: `${response.statusCode}` });
|
|
322
|
-
|
|
334
|
+
const secrets = Object.values(node.server.credentials || {});
|
|
335
|
+
if (typeof node.server.getApiKey === "function") secrets.push(node.server.getApiKey());
|
|
336
|
+
throw buildRequestError(response, request.method, request.path, secrets);
|
|
323
337
|
}
|
|
324
338
|
|
|
325
339
|
if (response.payload && typeof response.payload === "object" && !Array.isArray(response.payload)) {
|
|
@@ -330,6 +344,13 @@ module.exports = function(RED) {
|
|
|
330
344
|
payload: response.payload
|
|
331
345
|
};
|
|
332
346
|
decorateOutputMessage(stateMsg, response.payload, `request:${capabilityId}`);
|
|
347
|
+
if (capabilityId === "getAlarmState") {
|
|
348
|
+
const alarmState = readAlarmState(deviceType, response.payload);
|
|
349
|
+
if (alarmState === undefined) {
|
|
350
|
+
throw new Error(`The ${deviceType} response has no supported alarm state. Check the Protect version and alarm mode; missing state does not mean disarmed.`);
|
|
351
|
+
}
|
|
352
|
+
stateMsg.payload = alarmState;
|
|
353
|
+
}
|
|
333
354
|
attachDetails(stateMsg, {
|
|
334
355
|
response: {
|
|
335
356
|
statusCode: response.statusCode,
|
|
@@ -405,9 +426,10 @@ module.exports = function(RED) {
|
|
|
405
426
|
}
|
|
406
427
|
|
|
407
428
|
fetchDeviceState(node.deviceType, node.deviceId, parseCapabilityConfig(node.capabilityConfig), node.send.bind(node), "startup").catch((error) => {
|
|
408
|
-
if (node.isObserving && (isPhotoObservation() || isLicensePlateObservation())) {
|
|
429
|
+
if (node.isObserving && (isPhotoObservation() || isLicensePlateObservation() || node.deviceType === "alarmHub" || node.deviceType === "nvr")) {
|
|
409
430
|
setNodeStatus({ fill: "red", shape: "ring", text: "check camera configuration" });
|
|
410
|
-
node.
|
|
431
|
+
if (node.deviceType === "alarmHub" || node.deviceType === "nvr") setNodeStatus({ fill: "red", shape: "ring", text: "alarm state unavailable" });
|
|
432
|
+
node.send([null, protectErrorOutput(error)]);
|
|
411
433
|
}
|
|
412
434
|
});
|
|
413
435
|
}
|
|
@@ -439,7 +461,7 @@ module.exports = function(RED) {
|
|
|
439
461
|
invokeCapability(node.send.bind(node), "interval")
|
|
440
462
|
.catch((error) => {
|
|
441
463
|
setNodeStatus({ fill: "red", shape: "ring", text: "auto error" });
|
|
442
|
-
node.send([null,
|
|
464
|
+
node.send([null, protectErrorOutput(error)]);
|
|
443
465
|
node.error(error);
|
|
444
466
|
})
|
|
445
467
|
.finally(() => {
|
|
@@ -469,7 +491,7 @@ module.exports = function(RED) {
|
|
|
469
491
|
}
|
|
470
492
|
} catch (error) {
|
|
471
493
|
setNodeStatus({ fill: "red", shape: "ring", text: "error" });
|
|
472
|
-
node.send([null,
|
|
494
|
+
node.send([null, protectErrorOutput(error)]);
|
|
473
495
|
if (typeof done === "function") {
|
|
474
496
|
done(error);
|
|
475
497
|
} else {
|
|
@@ -488,19 +510,32 @@ module.exports = function(RED) {
|
|
|
488
510
|
// Only react to device updates that match both the selected
|
|
489
511
|
// model family and the exact configured device id.
|
|
490
512
|
const deviceDefinition = getDeviceTypeDefinition(node.deviceType);
|
|
491
|
-
|
|
513
|
+
const alarmDevice = node.deviceType === "alarmHub" || node.deviceType === "nvr";
|
|
514
|
+
if (!deviceDefinition || (alarmDevice
|
|
515
|
+
? !matchesDeviceUpdate(node.deviceType, node.deviceId, item, deviceDefinition.modelKey)
|
|
516
|
+
: item.modelKey !== deviceDefinition.modelKey || item.id !== node.deviceId)) {
|
|
492
517
|
return;
|
|
493
518
|
}
|
|
494
519
|
|
|
495
|
-
|
|
520
|
+
if (alarmDevice && ["remove", "delete"].includes(update.type)) {
|
|
521
|
+
node.currentDevice = null;
|
|
522
|
+
node.currentObservableValue = undefined;
|
|
523
|
+
setNodeStatus({ fill: "yellow", shape: "ring", text: "device removed" });
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
node.currentDevice = alarmDevice ? mergeDeviceUpdate(node.currentDevice, item) : item;
|
|
496
527
|
if (isLicensePlateObservation() || isPhotoObservation()) return;
|
|
497
528
|
const capabilityConfig = parseCapabilityConfig(node.capabilityConfig);
|
|
529
|
+
const observable = resolveConfiguredObservable(capabilityConfig);
|
|
530
|
+
// An unrelated partial update must not replay an old alarm value.
|
|
531
|
+
if ((node.deviceType === "alarmHub" && observable === "armed" && !(item.alarmHub && Object.prototype.hasOwnProperty.call(item.alarmHub, "armed")))
|
|
532
|
+
|| (node.deviceType === "nvr" && observable === "armStatus" && !(item.armMode && Object.prototype.hasOwnProperty.call(item.armMode, "status")))) return;
|
|
498
533
|
setNodeStatus({ fill: "green", shape: "dot", text: buildNodeStatus(node.deviceType, item) });
|
|
499
534
|
sendOutputs(node.send.bind(node), buildObservedStateMessage(
|
|
500
535
|
node.deviceType,
|
|
501
536
|
node.deviceId,
|
|
502
537
|
capabilityConfig,
|
|
503
|
-
|
|
538
|
+
node.currentDevice,
|
|
504
539
|
"devices",
|
|
505
540
|
{ updateType: update.type || "" },
|
|
506
541
|
update.type || "devices"
|
|
@@ -688,7 +723,7 @@ module.exports = function(RED) {
|
|
|
688
723
|
decorateOutputMessage(output, node.currentDevice, "detectionWithImage");
|
|
689
724
|
attachDetails(output, { raw: event, unifiProtect: buildBaseMetadata(node.deviceType, node.deviceId, node.capability, { source: "events" }) });
|
|
690
725
|
state.sent = true;
|
|
691
|
-
node.send(output);
|
|
726
|
+
sendWithPayload(node.send.bind(node), output, node.emitStartupAndUndefined);
|
|
692
727
|
setNodeStatus({ fill: "blue", shape: "dot", text: "event photo received" });
|
|
693
728
|
}).catch((error) => {
|
|
694
729
|
if (!node.isObserving) return;
|
|
@@ -726,6 +761,7 @@ module.exports = function(RED) {
|
|
|
726
761
|
// state output directly instead of only the event payload.
|
|
727
762
|
const observation = resolveObservableEventValue(node.deviceType, item, observable, observableScopeId);
|
|
728
763
|
if (observation.matched) {
|
|
764
|
+
if (!node.emitStartupAndUndefined && !hasPayloadValue(observation.value)) return;
|
|
729
765
|
const resolvedDeviceName = resolveOutputDeviceName(node.currentDevice);
|
|
730
766
|
node.currentObservableValue = observation.value;
|
|
731
767
|
setNodeStatus({ fill: "blue", shape: "ring", text: `${item.type || "event"}` });
|
|
@@ -776,9 +812,9 @@ module.exports = function(RED) {
|
|
|
776
812
|
return;
|
|
777
813
|
}
|
|
778
814
|
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
815
|
+
// No value for the selected observable: do not emit a raw
|
|
816
|
+
// event as an alternative downstream command.
|
|
817
|
+
return;
|
|
782
818
|
}
|
|
783
819
|
|
|
784
820
|
setNodeStatus({ fill: "blue", shape: "ring", text: `${item.type || "event"}` });
|
|
@@ -131,6 +131,24 @@ function attachDetails(outputMsg, details) {
|
|
|
131
131
|
};
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
+
// Missing readings must never become downstream commands. Keep false and zero.
|
|
135
|
+
function hasPayloadValue(value) {
|
|
136
|
+
return value !== undefined && value !== null
|
|
137
|
+
&& !(typeof value === "string" && value.trim().toLowerCase() === "undefined");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function sendWithPayload(send, messages, allowMissing = false) {
|
|
141
|
+
function filter(message) {
|
|
142
|
+
if (Array.isArray(message)) {
|
|
143
|
+
const filtered = message.map(filter);
|
|
144
|
+
return filtered.some((item) => item !== null) ? filtered : null;
|
|
145
|
+
}
|
|
146
|
+
return message && (allowMissing || hasPayloadValue(message.payload)) ? message : null;
|
|
147
|
+
}
|
|
148
|
+
const filtered = filter(messages);
|
|
149
|
+
if (filtered !== null) send(filtered);
|
|
150
|
+
}
|
|
151
|
+
|
|
134
152
|
function buildErrorOutputMessage(error, nodeName) {
|
|
135
153
|
return {
|
|
136
154
|
topic: String(nodeName || "").trim() || undefined,
|
|
@@ -153,5 +171,7 @@ module.exports = {
|
|
|
153
171
|
extractDeviceNameFromPayload,
|
|
154
172
|
attachDeviceNameToPayload,
|
|
155
173
|
attachDetails,
|
|
156
|
-
buildErrorOutputMessage
|
|
174
|
+
buildErrorOutputMessage,
|
|
175
|
+
hasPayloadValue,
|
|
176
|
+
sendWithPayload
|
|
157
177
|
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function readAlarmState(deviceType, device) {
|
|
4
|
+
if (deviceType === "alarmHub") {
|
|
5
|
+
const value = device && device.alarmHub && device.alarmHub.armed;
|
|
6
|
+
if (value === true || value === "on") return true;
|
|
7
|
+
if (value === false || value === "off") return false;
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const status = device && device.armMode && device.armMode.status;
|
|
11
|
+
return deviceType === "nvr" && typeof status === "string" && status.trim()
|
|
12
|
+
? status : undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function matchesDeviceUpdate(deviceType, deviceId, item, modelKey) {
|
|
16
|
+
if (!item || item.id !== deviceId) return false;
|
|
17
|
+
// Partial updates may omit the model key. IDs still scope them to the
|
|
18
|
+
// configured device. Protect also represents Alarm Hubs as Link Stations.
|
|
19
|
+
return !item.modelKey || item.modelKey === modelKey
|
|
20
|
+
|| (deviceType === "alarmHub" && item.modelKey === "linkStation")
|
|
21
|
+
|| (deviceType === "alarmHub" && item.modelKey === "linkstation");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function mergeDeviceUpdate(previous, update) {
|
|
25
|
+
const result = { ...(previous || {}) };
|
|
26
|
+
for (const [key, value] of Object.entries(update)) {
|
|
27
|
+
if (["__proto__", "constructor", "prototype"].includes(key)) continue;
|
|
28
|
+
result[key] = value && typeof value === "object" && !Array.isArray(value)
|
|
29
|
+
? mergeDeviceUpdate(result[key] && typeof result[key] === "object" ? result[key] : null, value)
|
|
30
|
+
: value;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function buildRequestError(response, method, path, secrets) {
|
|
36
|
+
const statusCode = Number(response.statusCode);
|
|
37
|
+
const body = response.payload;
|
|
38
|
+
const detail = body && typeof body === "object"
|
|
39
|
+
? (body.message || (body.error && body.error.message) || (typeof body.error === "string" ? body.error : ""))
|
|
40
|
+
: typeof body === "string" ? body : "";
|
|
41
|
+
let apiMessage = typeof detail === "string" ? detail : "";
|
|
42
|
+
for (const secret of secrets || []) {
|
|
43
|
+
if (typeof secret === "string" && secret) apiMessage = apiMessage.split(secret).join("[redacted]");
|
|
44
|
+
}
|
|
45
|
+
apiMessage = apiMessage.replace(/(bearer\s+)\S+/gi, "$1[redacted]")
|
|
46
|
+
.replace(/((?:password|token|api[-_ ]?key|authorization|cookie)\s*[=:]\s*)[^\s,;]+/gi, "$1[redacted]")
|
|
47
|
+
.replace(/[\r\n\t]+/g, " ").slice(0, 500);
|
|
48
|
+
let hint = "";
|
|
49
|
+
if (statusCode === 401) hint = " Check the Protect API key and its validity.";
|
|
50
|
+
if (statusCode === 403) hint = " Check the Protect permissions of the account associated with the API key.";
|
|
51
|
+
if (statusCode === 400 && path.startsWith("/v1/arm-profiles")) {
|
|
52
|
+
hint = " Arm profile commands require Protect's local Alarm Manager; check the alarm mode and selected profile. A 400 response alone does not identify a permission failure.";
|
|
53
|
+
}
|
|
54
|
+
const error = new Error(`UniFi Protect request failed with status ${statusCode}.${hint}${apiMessage ? ` API: ${apiMessage}` : ""}`);
|
|
55
|
+
error.protectResponse = { statusCode, method, path, ...(apiMessage ? { apiMessage } : {}) };
|
|
56
|
+
return error;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = { readAlarmState, matchesDeviceUpdate, mergeDeviceUpdate, buildRequestError };
|
|
@@ -9,6 +9,7 @@ const {
|
|
|
9
9
|
} = require("./unifi-protect-field-metadata");
|
|
10
10
|
const { DETECTION_FIELD, KNOWN_PLATES_FIELD } = require("./unifi-protect-detections");
|
|
11
11
|
const { LICENSE_PLATE_EVENT_TYPES, extractLicensePlates } = require("./unifi-protect-lpr");
|
|
12
|
+
const { readAlarmState } = require("./unifi-protect-alarm");
|
|
12
13
|
|
|
13
14
|
// Central Protect registry used by:
|
|
14
15
|
// - the editor for device/capability discovery
|
|
@@ -360,6 +361,7 @@ const RELAY_OBSERVABLE_DEFINITIONS = [
|
|
|
360
361
|
];
|
|
361
362
|
|
|
362
363
|
const ALARM_HUB_OBSERVABLE_DEFINITIONS = [
|
|
364
|
+
{ id: "armed", label: "Armed", eventTypes: [] },
|
|
363
365
|
{
|
|
364
366
|
id: "motion",
|
|
365
367
|
label: "Motion input",
|
|
@@ -724,6 +726,7 @@ const TYPE_CAPABILITIES = {
|
|
|
724
726
|
createSetPropertyCapability()
|
|
725
727
|
],
|
|
726
728
|
nvr: [
|
|
729
|
+
{ id: "getAlarmState", label: "Read Alarm State", description: "Read the local Alarm Manager status from the NVR.", method: "GET", pathKind: "detail", mode: "request" },
|
|
727
730
|
{
|
|
728
731
|
id: "getApplicationInfo",
|
|
729
732
|
label: "Read Application Info",
|
|
@@ -792,6 +795,7 @@ const TYPE_CAPABILITIES = {
|
|
|
792
795
|
bridge: [],
|
|
793
796
|
linkStation: [],
|
|
794
797
|
alarmHub: [
|
|
798
|
+
{ id: "getAlarmState", label: "Read Alarm State", description: "Read the selected Alarm Hub's armed state.", method: "GET", pathKind: "detail", mode: "request" },
|
|
795
799
|
{
|
|
796
800
|
id: "triggerAlarmHubOutput",
|
|
797
801
|
label: "Trigger Alarm Hub Output",
|
|
@@ -1923,7 +1927,8 @@ function getObservableDefinitions(deviceType) {
|
|
|
1923
1927
|
light: LIGHT_OBSERVABLE_DEFINITIONS,
|
|
1924
1928
|
fob: FOB_OBSERVABLE_DEFINITIONS,
|
|
1925
1929
|
relay: RELAY_OBSERVABLE_DEFINITIONS,
|
|
1926
|
-
alarmHub: ALARM_HUB_OBSERVABLE_DEFINITIONS
|
|
1930
|
+
alarmHub: ALARM_HUB_OBSERVABLE_DEFINITIONS,
|
|
1931
|
+
nvr: [{ id: "armStatus", label: "Alarm status (local)", eventTypes: [] }]
|
|
1927
1932
|
};
|
|
1928
1933
|
|
|
1929
1934
|
return definitionsByDeviceType[String(deviceType || "").trim()] || [];
|
|
@@ -1952,6 +1957,10 @@ function resolveSelectedObservable(options, configuredObservable) {
|
|
|
1952
1957
|
}
|
|
1953
1958
|
|
|
1954
1959
|
function resolveObservableState(deviceType, device, observable, fallbackValue) {
|
|
1960
|
+
if ((deviceType === "alarmHub" && observable === "armed") || (deviceType === "nvr" && observable === "armStatus")) {
|
|
1961
|
+
const value = readAlarmState(deviceType, device);
|
|
1962
|
+
return value === undefined ? fallbackValue : value;
|
|
1963
|
+
}
|
|
1955
1964
|
if (deviceType === "sensor") {
|
|
1956
1965
|
return resolveSensorObservableState(device, observable, fallbackValue);
|
|
1957
1966
|
}
|
|
@@ -28,6 +28,13 @@ const COMMON_FIELD_METADATA = {
|
|
|
28
28
|
|
|
29
29
|
// Device-family-specific metadata overrides or extends the common dictionary.
|
|
30
30
|
const TYPE_FIELD_METADATA = {
|
|
31
|
+
nvr: {
|
|
32
|
+
armStatus: {
|
|
33
|
+
label: "Alarm Status (Local)",
|
|
34
|
+
description: "Local Alarm Manager status from armMode.status. Kept as a string, including transitional states; it may differ from an Alarm Hub's armed state.",
|
|
35
|
+
source: "official"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
31
38
|
camera: {
|
|
32
39
|
isMicEnabled: {
|
|
33
40
|
label: "Microphone Enabled",
|
|
@@ -366,6 +373,11 @@ const OBSERVABLE_METADATA = {
|
|
|
366
373
|
}
|
|
367
374
|
},
|
|
368
375
|
alarmHub: {
|
|
376
|
+
armed: {
|
|
377
|
+
label: "Armed",
|
|
378
|
+
description: "Current Alarm Hub armed state: on becomes true and off becomes false. Updated by device state messages, not a dedicated arm/disarm event.",
|
|
379
|
+
source: "official"
|
|
380
|
+
},
|
|
369
381
|
motion: {
|
|
370
382
|
label: "Motion Input",
|
|
371
383
|
description: "True while an Alarm Hub motion input is active.",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-unifi-ultimate",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.18",
|
|
4
4
|
"description": "Node-RED nodes for Ubiquiti UniFi integrations, including UniFi Protect, UniFi Access, and UniFi Network.",
|
|
5
5
|
"files": [
|
|
6
6
|
"nodes/",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
68
|
"scripts": {
|
|
69
|
-
"test": "node --check nodes/unifi-access-config.js && node --check nodes/unifi-access-device.js && node --check nodes/unifi-network-config.js && node --check nodes/unifi-network-control-poe.js && node --check nodes/unifi-network-device.js && node --check nodes/unifi-network-presence.js && node --check nodes/unifi-network-restart.js && node --check nodes/unifi-protect-config.js && node --check nodes/unifi-protect-device.js && node --check nodes/utils/common-utils.js && node --check nodes/utils/http-response-utils.js && node --check nodes/utils/unifi-access-utils.js && node --check nodes/utils/unifi-access-device-registry.js && node --check nodes/utils/unifi-network-utils.js && node --check nodes/utils/unifi-network-device-registry.js && node --check nodes/utils/unifi-network-presence-utils.js && node --check nodes/utils/unifi-protect-utils.js && node --check nodes/utils/unifi-protect-history.js && node --check nodes/utils/unifi-protect-lpr.js && node --check nodes/utils/unifi-protect-field-metadata.js && node --check nodes/utils/unifi-protect-device-registry.js && node --check nodes/utils/knx-ai-camera-registry.js && node --check scripts/unifi-registry-discovery-common.js && node --check scripts/discover-unifi-network-registry.js && node --check scripts/discover-unifi-protect-registry.js && node --check scripts/discover-unifi-access-registry.js && node --check nodes/utils/unifi-simple-monitor.js && node --check nodes/utils/unifi-network-monitor.js && node --check nodes/utils/unifi-protect-detections.js && node --check nodes/utils/unifi-connection-check.js && node --check nodes/utils/unifi-action-guidance.js && node --check resources/editor/unifi-connection-check.js && jest",
|
|
69
|
+
"test": "node --check nodes/unifi-access-config.js && node --check nodes/unifi-access-device.js && node --check nodes/unifi-network-config.js && node --check nodes/unifi-network-control-poe.js && node --check nodes/unifi-network-device.js && node --check nodes/unifi-network-presence.js && node --check nodes/unifi-network-restart.js && node --check nodes/unifi-protect-config.js && node --check nodes/unifi-protect-device.js && node --check nodes/utils/common-utils.js && node --check nodes/utils/http-response-utils.js && node --check nodes/utils/unifi-access-utils.js && node --check nodes/utils/unifi-access-device-registry.js && node --check nodes/utils/unifi-network-utils.js && node --check nodes/utils/unifi-network-device-registry.js && node --check nodes/utils/unifi-network-presence-utils.js && node --check nodes/utils/unifi-protect-utils.js && node --check nodes/utils/unifi-protect-history.js && node --check nodes/utils/unifi-protect-alarm.js && node --check nodes/utils/unifi-protect-lpr.js && node --check nodes/utils/unifi-protect-field-metadata.js && node --check nodes/utils/unifi-protect-device-registry.js && node --check nodes/utils/knx-ai-camera-registry.js && node --check scripts/unifi-registry-discovery-common.js && node --check scripts/discover-unifi-network-registry.js && node --check scripts/discover-unifi-protect-registry.js && node --check scripts/discover-unifi-access-registry.js && node --check nodes/utils/unifi-simple-monitor.js && node --check nodes/utils/unifi-network-monitor.js && node --check nodes/utils/unifi-protect-detections.js && node --check nodes/utils/unifi-connection-check.js && node --check nodes/utils/unifi-action-guidance.js && node --check resources/editor/unifi-connection-check.js && jest",
|
|
70
70
|
"registry:discover:network": "node scripts/discover-unifi-network-registry.js",
|
|
71
71
|
"registry:discover:protect": "node scripts/discover-unifi-protect-registry.js",
|
|
72
72
|
"registry:discover:access": "node scripts/discover-unifi-access-registry.js"
|
|
@@ -82,4 +82,4 @@
|
|
|
82
82
|
"bugs": {
|
|
83
83
|
"url": "https://github.com/Supergiovane/node-red-contrib-unifi-ultimate/issues"
|
|
84
84
|
}
|
|
85
|
-
}
|
|
85
|
+
}
|