What do you mean by "local time is not really reliable"? If it is in the order of some seconds/minutes, I do not really mind.
Simply letting the circulation pump run every few hours is not really efficient, since I have a demand-controlled activation.
So this is my first approach - not the very best coding (magic numbers, many if/else cases, ...), but it seems to work as intended:
Code
let durationOffTime = 0; //measured time interval, when the circulation pump was not activated
let lastOff = 0; //timestamp of last off time
let durationRunTime = 300; //initial value (might be variable), that the circulation pump will run
Shelly.addStatusHandler(function(e) {
if (e.component === "switch:0") { //monitor switch output
if (e.delta.output === true) { //if Switch Output On...
print("Switch is on, triggered source:", e.delta.source);
durationOffTime = (Date.now() - lastOff) / 1000; //seconds duration of trigger off
if (e.delta.source === "WS_in") //...via Web Console
{
print("Output set ON via WebDemand. durationOffTime = "+durationOffTime+"s. Indenpendently, turn off after 300s");
Shelly.call("Switch.set", {'id': 0, 'on': true, 'toggle_after': 300});
}
else //...via water demand
{
print("Output set ON via switch input. durationOffTime = "+durationOffTime+"s. Turn off after "+durationRunTime+"s");
}
}
else if (e.delta.output === false) { //if Switch Off
print("Switch is now off. Set new timestamp. Triggered source:", e.delta.source);
lastOff = Date.now();
}
}
if (e.component === "input:0") { //Monitor input
if (e.delta.state === true) { //if Input is true
durationOffTime = (Date.now() - lastOff) / 1000; //seconds duration of trigger off
if (durationOffTime > 60 * 60 * 2) {durationRunTime = 300; }
else if (durationOffTime > 60 * 60) {durationRunTime = 180; }
else if (durationOffTime > 60 * 30) {durationRunTime = 60; }
else if (durationOffTime > 60 * 10) {durationRunTime = 60; }
else {durationRunTime = 0; }
print("Input switch is on. Set Output on for "+durationRunTime+" s");
if (durationRunTime>0) { Shelly.call("Switch.set", {'id': 0, 'on': true, 'toggle_after': durationRunTime}); }
}
}
});
Alles anzeigen
You might review, if you like. I am happy for any comments, ...
Moreover, I have one basic question:
The Shelly is now configured as detached switch. I activated the script via the Slider on the "Scripts"-page. Does that mean, that the script automatically restarts after a power failure/firmware upgrade/... ?
If no, how do I ensure this?