-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationScript.js
More file actions
70 lines (60 loc) · 2.8 KB
/
Copy pathNotificationScript.js
File metadata and controls
70 lines (60 loc) · 2.8 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
68
69
70
/**
* Shelly Wallbox Notification Script
*
* This script retrieves stored power usage data from KVS, sends it to a webhook,
* and resets the counters. Typically run on a yearly schedule.
*/
// Configuration
let webhookUrl = "YOUR_WEBHOOK_URL"; // Replace with your actual webhook URL
let scriptId = Script.id;
// Retrieve and send power usage data
console.log("Notification script started");
// Get power usage for both users
Shelly.call("KVS.Get", {key: "powerUsageBlue"}, function(getBlueWallbox, error_code, error_message) {
if (error_code !== 0) {
console.log("Error getting blue power usage:", error_message);
return;
}
let bluePower = getBlueWallbox.value || 0;
console.log("Blue user power usage:", bluePower, "Wh");
Shelly.call("KVS.Get", {key: "powerUsageRed"}, function(getRedWallbox, error_code_red, error_message_red) {
if (error_code_red !== 0) {
console.log("Error getting red power usage:", error_message_red);
return;
}
let redPower = getRedWallbox.value || 0;
console.log("Red user power usage:", redPower, "Wh");
// Send data to webhook
let url = webhookUrl + "?bluePower=" + bluePower + "&redPower=" + redPower;
Shelly.call("HTTP.GET", {url: url}, function(response, error_code_http, error_message_http) {
if (error_code_http !== 0) {
console.log("Error sending webhook:", error_message_http);
} else {
console.log("Webhook sent successfully");
// Reset KVS values after successful webhook
Shelly.call("KVS.Set", { key: "powerUsageBlue", value: 0 }, function(result, error_code_set, error_message_set) {
if (error_code_set !== 0) {
console.log("Error resetting blue power usage:", error_message_set);
} else {
console.log("Blue power usage reset");
}
});
Shelly.call("KVS.Set", { key: "powerUsageRed", value: 0 }, function(result, error_code_set, error_message_set) {
if (error_code_set !== 0) {
console.log("Error resetting red power usage:", error_message_set);
} else {
console.log("Red power usage reset");
}
});
}
});
// Stop the script
Shelly.call("Script.Stop", {id: scriptId}, function(stop, error_code_stop, error_message_stop) {
if (error_code_stop !== 0) {
console.log("Error stopping script:", error_message_stop);
} else {
console.log("Script stopped");
}
});
});
});