-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmonitor_remote_consumption.js
103 lines (89 loc) · 3.21 KB
/
monitor_remote_consumption.js
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
/**
* @copyright shelly-tools contributors
* @license GNU Affero General Public License (https://www.gnu.org/licenses/agpl-3.0.de.html)
* @authors https://github.com/shelly-tools/shelly-script-examples/graphs/contributors
*
* This script is intended to measure power consumption of a remote Shelly Plug / Plug-S / Shelly1PM
* and send a telegram push notification once the power consumption stops for a given time period.
* the idea behind is to monitor a washing machine or dryer and send a notification once the appliance
* has finished.
*/
// CONFIG START
// IP from the remote shelly plug / plug-s we'd like to monitor
let remoteip = '192.168.178.205';
// your telegram api key
let telegramApiKey = 'YOUR API KEY HERE';
// your telegram chat id to push notifications
let telegramChatID = 'YOUR CHAT ID HERE';
// the text for the notification once the appliance has finished
let telegramText = 'Washing machine has finished!';
// the number of consecutive times the check will run until the appliance is considered as finished power consumption has to be below "minUsage"
let timesInactive = 5; // in minutes
// minimum watts usage .. above this value the appliance is considered as "started"..
// below this value and timesInactive is reached the appliance is considered as finished.
let minUsage = 10; // Watts
// CONFIG END
// Do not change code below this line!
let countInactive = 0;
let alertTimer = null;
let active = false;
let stopped = false;
function startMonitor() {
alertTimer = Timer.set(60 * 1000,
true,
function () {
Shelly.call("HTTP.GET", {
url: 'http://' + remoteip + '/status'
},
function (res, error_code, error_msg, ud) {
if (error_code !== 0)
{
// Not read response if there is an error, to avoid that the script stops
}
else if (res.code === 200) {
let st = JSON.parse(res.body);
let current = st.meters[0].power;
if (current > minUsage) {
countInactive = 0;
activate();
}
if (active) {
isReady(current);
}
};
},
null
);
},
null
);
}
function activate() {
if (active === false) {
active = true;
print('appliance started');
}
}
function isReady(usage) {
if (active && usage < minUsage) {
countInactive = countInactive + 1;
}
if (active && countInactive > timesInactive) {
countInactive = 0;
active = false;
sendTelegramMessage();
print('appliance finished');
}
}
function sendTelegramMessage() {
Shelly.call(
"http.get", {
url: 'https://api.telegram.org/bot' + telegramApiKey + '/sendMessage?chat_id=' + telegramChatID + '&text="' + telegramText + '"'
},
function (response, error_code, error_message, ud) {
print(JSON.stringify(response));
},
null
);
};
startMonitor();