-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy path_garage-auto-close.js
More file actions
67 lines (56 loc) · 3.07 KB
/
_garage-auto-close.js
File metadata and controls
67 lines (56 loc) · 3.07 KB
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
//Example garage poll event that runs every 5 minutes.
//Based on forum user ssmall's garage poll event: https://lowpowerlab.com/forum/pi-gateway/gateway-v8-10-0-released!/msg21026/#msg21026
//It sends a CLOSE message to the GarageMote after 11PM (23:00 hours) and before 6AM (06:00 hours), and only when the door has been in OPEN status more than 10 minutes.
//For safety, it sends UP TO 3x messages between this time frame - in case the GarageMote sensors are not aligned well or malfunction (causing OPEN, CLOSING.., OPEN alternations).
//example of overriding an event
exports.events = {
garageAutoClose: {
label:'Garage : Auto-CLOSE between [11PM,6AM]',
icon:'comment',
descr:'Auto Close Garage after 11PM',
nextSchedule: function(nodeAtScheduleTime) { return 300000; }, //runs every 5min
scheduledExecute: function(nodeAtScheduleTime) {
var nowDate = new Date(Date.now());
var nodeUpdated = false;
if (nodeAtScheduleTime)
{
//console.log('GARAGE POLL STATUS Node Id: ' + nodeAtScheduleTime._id + " Status: " + nodeAtScheduleTime.metrics['Status'].value);
if (nodeAtScheduleTime.metrics['Status'] &&
nodeAtScheduleTime.metrics['Status'].value == 'OPEN') {
/* Only automatically close between 11pm and 6am */
if ((nowDate.getHours() >= 23 || nowDate.getHours() <= 6) && nodeAtScheduleTime.metrics['Status'].closeCount < 3) {
if (nodeAtScheduleTime.metrics['Status'].openDate == null) {
nodeAtScheduleTime.metrics['Status'].openDate = nowDate;
nodeUpdated = true;
};
/* Elapsed time in minutes */
var elapsedTimeMinutes = Math.round(((nowDate - nodeAtScheduleTime.metrics['Status'].openDate)) / (60*1000));
console.log('GARAGE SHOULD BE CLOSED ' + elapsedTimeMinutes);
console.log('GARAGE OPEN timestamp: ' + nodeAtScheduleTime.metrics['Status'].openDate);
/* Close the door if open more than 10 minutes */
if (elapsedTimeMinutes >= 10) {
sendMessageToNode({nodeId:nodeAtScheduleTime._id, action:'CLS'});
nodeAtScheduleTime.metrics['Status'].closeCount++;
nodeAtScheduleTime.metrics['Status'].openDate = null;
nodeUpdated = true;
};
console.log('Close Count: ' + nodeAtScheduleTime.metrics['Status'].closeCount);
};
};
if (nowDate.getHours() < 23 && nowDate.getHours() > 6 && nodeAtScheduleTime.metrics['Status'].openDate) {
nodeAtScheduleTime.metrics['Status'].openDate = null;
nodeUpdated = true;
};
/* Reset the close count */
if ((nowDate.getHours() < 23 && nowDate.getHours() > 6 && nodeAtScheduleTime.metrics['Status'].closeCount > 0) || nodeAtScheduleTime.metrics['Status'].closeCount == null) {
nodeAtScheduleTime.metrics['Status'].closeCount = 0;
nodeUpdated = true;
};
if (nodeUpdated) {
nodeAtScheduleTime.modifiedByEvent = true;
return nodeAtScheduleTime;
};
};
},
}
}