-
Notifications
You must be signed in to change notification settings - Fork 2
/
presence-faker.html
505 lines (464 loc) · 18 KB
/
presence-faker.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<script type="text/javascript">
;(function () {
const calcDuration = (type) => {
// type can be 'min' or 'max'
let hours = Number($(`#node-input-${type}DurationHours`).val())
let minutes = Number($(`#node-input-${type}DurationMinutes`).val())
let seconds = Number($(`#node-input-${type}DurationSeconds`).val())
return hours * 3600 + minutes * 60 + seconds
}
RED.nodes.registerType('presence-faker', {
category: 'smart home',
color: '#FFAAAA',
defaults: {
name: { value: '' },
onPayload: {
value: '',
validate: RED.validators.typedInput('payloadType'),
},
onPayloadType: { value: 'string' },
onTopic: { value: '', required: false },
offPayload: {
value: '',
validate: RED.validators.typedInput('payloadType'),
},
offPayloadType: { value: 'string' },
offTopic: { value: '', required: false },
windowBegin: {
value: '07:30',
required: true,
validate: RED.validators.regex(
/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/
),
},
windowEnd: {
value: '17:30',
required: true,
validate: RED.validators.regex(
/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/
),
},
minDurationHours: { value: 0, required: true },
minDurationMinutes: { value: 0, required: true },
minDurationSeconds: { value: 0, required: true },
minDuration: { value: 0 },
maxDurationHours: { value: 0, required: true },
maxDurationMinutes: { value: 0, required: true },
maxDurationSeconds: { value: 0, required: true },
maxDuration: { value: 0 },
minCount: {
value: 1,
required: true,
validate: function (v) {
if (!/\d+/.exec(v)) {
return false
} else {
return true
}
},
},
maxCount: {
value: 1,
required: true,
validate: function (v) {
if (!/\d+/.exec(v)) {
return false
}
if (calcDuration('min') > calcDuration('max')) {
return false
}
return true
},
},
startupBehavior: { value: 'onMsg' },
actionOnDisable: { value: 'none' },
firstBlockType: { value: 'off' },
lastBlockType: { value: 'off' },
isLoggingEnabled: { value: false },
},
inputs: 1,
outputs: 1,
icon: 'light.png',
label: function () {
return this.name || 'presence-faker'
},
oneditprepare: function () {
fetch('/context/node/' + this.id, { method: 'GET' })
.then((response) => response.json())
.then((data) => {
if (data.memory.windowBegin || data.memory.windowEnd) {
$('#lbl-window').addClass('tooltip')
$('#hint-window').html(
'current value:<br />' +
(data.memory.windowBegin?.msg || this.windowBegin) +
' - ' +
(data.memory.windowEnd?.msg || this.windowEnd)
)
}
if (data.memory.minCount || data.memory.maxCount) {
$('#lbl-onTimes').addClass('tooltip')
$('#hint-onTimes').html(
'current value:<br />' +
(data.memory.minCount?.msg || this.minCount) +
' - ' +
(data.memory.maxCount?.msg || this.maxCount)
)
}
if (data.memory.minDuration) {
$('#lbl-onMinDuration').addClass('tooltip')
$('#hint-onMinDuration').html(
'current value:<br />' +
(data.memory.minDuration?.msg || this.minDuration) +
' sec'
)
}
if (data.memory.maxDuration) {
$('#lbl-onMaxDuration').addClass('tooltip')
$('#hint-onMaxDuration').html(
'current value:<br />' +
(data.memory.maxDuration?.msg || this.maxDuration) +
' sec'
)
}
})
.catch((e) => console.log(e))
//ON Payload
if (this.onPayloadType == null) {
if (this.onPayload == '') {
this.onPayloadType = 'date'
} else {
this.onPayloadType = 'str'
}
} else if (
this.onPayloadType === 'string' ||
this.onPayloadType === 'none'
) {
this.onPayloadType = 'str'
}
$('#node-input-onPayloadType').val(this.onPayloadType)
$('#node-input-onPayload').typedInput({
default: 'str',
typeField: $('#node-input-onPayloadType'),
types: [
//'flow',
//'global',
'str',
'num',
'bool',
'json',
//'bin',
//'date',
//'env'
],
})
$('#node-input-onPayload').typedInput('type', this.onPayloadType)
//OFF Payload
if (this.offPayloadType == null) {
if (this.offPayload == '') {
this.offPayloadType = 'date'
} else {
this.offPayloadType = 'str'
}
} else if (
this.offPayloadType === 'string' ||
this.offPayloadType === 'none'
) {
this.offPayloadType = 'str'
}
$('#node-input-offPayloadType').val(this.offPayloadType)
$('#node-input-offPayload').typedInput({
default: 'str',
typeField: $('#node-input-offPayloadType'),
types: [
//'flow',
//'global',
'str',
'num',
'bool',
'json',
//'bin',
//'date',
//'env'
],
})
$('#node-input-offPayload').typedInput('type', this.offPayloadType)
},
oneditsave: function () {
$('#node-input-minDuration').val(calcDuration('min'))
$('#node-input-maxDuration').val(calcDuration('max'))
},
})
})()
</script>
<style>
.form-row label {
width: 120px;
}
.tooltip {
color: #ff5733;
position: relative;
display: inline-block;
border-bottom: 1px dotted; /* If you want dots under the hoverable text */
}
/* Tooltip text */
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* Position the tooltip text - see examples below! */
position: absolute;
z-index: 1;
}
/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<script type="text/x-red" data-template-name="presence-faker">
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-windowBegin"><span id="lbl-window"><i class="fa fa-clock-o"></i> window <span id="hint-window" class="tooltiptext"></span></span></label>
begin: <input type="text" id="node-input-windowBegin" placeholder="HH:MM" style="width:75px">
end: <input type="text" id="node-input-windowEnd" placeholder="HH:MM" style="width:75px">
</div>
<div class="form-row">
<label for="node-input-minCount"><span id="lbl-onTimes">ON times <span id="hint-onTimes" class="tooltiptext"></span></span></label>
min: <input type="text" id="node-input-minCount" placeholder="" style="width:50px">
max: <input type="text" id="node-input-maxCount" placeholder="" style="width:50px">
</div>
<div class="form-row">
<label for="node-input-minDurationHours"><span id="lbl-onMinDuration">ON min duration <span id="hint-onMinDuration" class="tooltiptext"></span></span></label>
<select style="width:50px; margin-left:0px;" id="node-input-minDurationHours">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select> hours
<select style="width:50px; margin-left:0px;" id="node-input-minDurationMinutes">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
</select> minutes
<select style="width:50px; margin-left:0px;" id="node-input-minDurationSeconds">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
</select> seconds
<input type="hidden" id="node-input-minDuration">
</div>
<div class="form-row">
<label for="node-input-maxDurationHours"><span id="lbl-onMaxDuration">ON max duration <span id="hint-onMaxDuration" class="tooltiptext"></span></span></label>
<select style="width:50px; margin-left:0px;" id="node-input-maxDurationHours">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select> hours
<select style="width:50px; margin-left:0px;" id="node-input-maxDurationMinutes">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
</select> minutes
<select style="width:50px; margin-left:0px;" id="node-input-maxDurationSeconds">
<option value="0">00</option>
<option value="1">01</option>
<option value="2">02</option>
<option value="3">03</option>
<option value="4">04</option>
<option value="5">05</option>
<option value="6">06</option>
<option value="7">07</option>
<option value="8">08</option>
<option value="9">09</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="25">25</option>
<option value="30">30</option>
<option value="35">35</option>
<option value="40">40</option>
<option value="45">45</option>
<option value="50">50</option>
<option value="55">55</option>
</select> seconds
<input type="hidden" id="node-input-maxDuration">
</div>
<div class="form-row">
<label for="node-input-onPayload"><i class="fa fa-envelope"></i> <span data-i18n="common.label.payload">ON Payload</span></label>
<input type="text" id="node-input-onPayload" style="width:70%">
<input type="hidden" id="node-input-onPayloadType">
</div>
<div class="form-row">
<label for="node-input-onTopic"><i class="fa fa-tasks"></i> <span>ON Topic</span></label>
<input type="text" id="node-input-onTopic">
</div>
<div class="form-row">
<label for="node-input-offPayload"><i class="fa fa-envelope"></i> <span data-i18n="common.label.payload">OFF Payload</span></label>
<input type="text" id="node-input-offPayload" style="width:70%">
<input type="hidden" id="node-input-offPayloadType">
</div>
<div class="form-row">
<label for="node-input-offTopic"><i class="fa fa-tasks"></i> <span>OFF Topic</span></label>
<input type="text" id="node-input-offTopic">
</div>
<div class="form-row">
<label for="node-input-firstBlockType"><i class="fa fa-step-backward"></i> <span>first block</span></label>
<select id="node-input-firstBlockType" style="width:70%">
<option value="on">ON message</option>
<option value="off">OFF message</option>
</select>
</div>
<div class="form-row">
<label for="node-input-lastBlockType"><i class="fa fa-step-forward"></i> <span>last block</span></label>
<select id="node-input-lastBlockType" style="width:70%">
<option value="on">ON message</option>
<option value="off">OFF message</option>
</select>
</div>
<div class="form-row">
<label for="node-input-startupBehavior"><i class="fa fa-power-off"></i> <span>enable</span></label>
<select id="node-input-startupBehavior" style="width:70%">
<option value="onMsg">when TRUE is received as msg.payload</option>
<option value="onStartup">when the flow starts up</option>
</select>
</div>
<div class="form-row">
<label for="node-input-actionOnDisable"><i class="fa fa-power-off"></i> <span>when disabled</span></label>
<select id="node-input-actionOnDisable" style="width:70%">
<option value="none">do nothing</option>
<option value="on">send ON message</option>
<option value="off">send OFF message</option>
</select>
</div>
<div class="form-row">
<label for="node-input-isLoggingEnabled"><i class="fa fa-bug"></i> logging</label>
<input type="checkbox" id="node-input-isLoggingEnabled" style="width:30px; margin:0px"> log debug messages to console
</div>
</script>
<script type="text/markdown" data-help-name="presence-faker">
This node simulates home occupancy by intermittently activating and deactivating devices (e.g. lights) at random intervals.
### Inputs
: payload (string | obj) : `true` to enable or `false` to disable the presence-faker node, or an `object` to configure it.
### Outputs
: payload (string | bool | number | obj) : the output of the presence-faker node.
### Details
By default, this node remains inactive until it receives a `true` value in the incoming `msg.payload`.
When activated, it generates a random schedule of ON/OFF events, which are then transmitted as outgoing messages.
You can specify the payload and topic for these messages in the node properties.
Additionally, you have the option to configure the node to start up in an enabled state, eliminating the need
for an incoming activation message.
When the node receives a `false` value in the `msg.payload`, it deactivates itself. You can further customize the
behavior in this situation, including the type of message to be sent.
Runtime configuration adjustments are possible by passing an optional configuration object as `msg.payload` to
the presence-faker node. The following attributes can be included:
- `windowBegin`: Starting time (e.g., "00:00")
- `windowEnd`: Ending time (e.g., "23:59:59")
- `minDuration`: Minimum event duration (in seconds)
- `maxDuration`: Maximum event duration (in seconds)
- `minCount`: Minimum event count
- `maxCount`: Maximum event count
The activation and deactivation of the node are typically triggered by your home automation system, such as
based on home occupancy status, LUX sensor readings, sunset times, and more.
### Combining presence-faker with suncron
You can combine the presence-faker node with the [suncron node](https://www.npmjs.com/package/node-red-contrib-suncron)
in order to dynamically set `windowBegin` and/or `windowEnd` based on the position of the sun:
- [Example](https://gist.github.com/csuermann/604f846f4b7bc3a518dc6385c16e3a48)
</script>