Shelly Plug S:
1. Home->Output(0)->Automations->Timers->Auto OFF (in seconds)=120
2. Scripts->Create script->chargingfinished.js
JavaScript:
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
| //
// to see the countdown in the app or webinterface configure Automations->Timers->AUTO-Off to trickleChargeTime
let CONFIG = {
trickleChargePowerThreshold: 10, // power (unit: Watt) level separating normal charging and trickle changing
trickleChargeTime: 120, // time (unit: seconds) to continue charging after finished regular charging
};
let lastPower = 0;
let lastSwitchStatus = 0;
let periodCounter = 0;
let maxPower = 0;
let PERIODS = 5; // for robustness sample 5 times in trickleChargeTime period
function switch_getstatus() {
Shelly.call(
"switch.getstatus",
{ id: 0 },
function (result, error_code, error_message, user_data) {
lastSwitchStatus = result.output;
lastPower = result.apower;
},
null
);
}
function switch_set(on) {
Shelly.call(
"switch.set",
{ id: 0, on: on},
function (result, code, msg, ud) {},
null
);
}
Timer.set(1000 * CONFIG.trickleChargeTime / PERIODS , true, function () {
switch_getstatus();
if(lastSwitchStatus) {
if(lastPower > maxPower) {
maxPower = lastPower;
}
if(maxPower > 2 * lastPower) {
print("Switch off (power=" + lastPower + ", maxpower=" + maxPower + ")");
switch_set(false); // Switch off. May overlap with Automations->Timers->AUTO-Off without harm.
} else {
if(lastPower > CONFIG.trickleChargePowerThreshold) {
print("Reset countdown timer (power=" + lastPower + ", maxpower=" + maxPower + ")")
periodCounter = 0;
switch_set(true); // Switching on resets countdown timer when Automations->Timers->AUTO-Off is configured
} else {
periodCounter = periodCounter + 1;
if(periodCounter >= PERIODS) {
print("Switch off (power=" + lastPower + ", maxpower=" + maxPower + ")");
periodCounter = 0;
switch_set(false); // Switch off. May overlap with Automations->Timers->AUTO-Off without harm.
}
}
}
} else {
maxPower = 0;
}
}); |
3. Scripts->chargingfinished.js->Run on startup
Na het indrukken van het knopje gaat een timer lopen die na 120 seconden de stekker uit zet (fail safe). Het script meet het energieverbruik en reset de timer telkens zolang het boven de grens zit (hier: hoger dan 50% van gemeten max sinds aanzetten en niet lager dan 10W).
Hieronder een voorbeeld van het opladen van een fietsaccu vanavond (gemeten met de Shelly en doorgestuurd naar home automation). Je ziet de max van 180W en afkappen bij 90W.
Zowel met de automatiseringsfeatures van de Shelly Plug S als scripting kun je eindeloos varieren.
[
Voor 25% gewijzigd door
Rukapul op 12-08-2025 23:25
]