Skip to content

Instantly share code, notes, and snippets.

@Dzhuneyt
Created November 12, 2023 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dzhuneyt/21e4826c05b8c587ac6bb58b1d486fb0 to your computer and use it in GitHub Desktop.
Save Dzhuneyt/21e4826c05b8c587ac6bb58b1d486fb0 to your computer and use it in GitHub Desktop.
Shelly Auto Reboot if no Internet Connection
let CONFIG = {
endpoints: [
"https://global.gcping.com/ping",
"https://us-central1-5tkroniexa-uc.a.run.app/ping",
],
//number of failures that trigger the reset
numberOfFails: 5,
//time in seconds after which the http request is considered failed
httpTimeout: 10,
//time in seconds for the relay to be off
toggleTime: 30,
//time in seconds to retry a "ping"
pingTime: 60,
};
let endpointIdx = 0;
let failCounter = 0;
let pingTimer = null;
function pingEndpoints() {
Shelly.call(
"http.get",
{ url: CONFIG.endpoints[endpointIdx], timeout: CONFIG.httpTimeout },
function (response, error_code, error_message) {
//http timeout, magic number, not yet documented
if (error_code === -114 || error_code === -104) {
print("Failed to fetch ", CONFIG.endpoints[endpointIdx]);
failCounter++;
print("Rotating through endpoints");
endpointIdx++;
endpointIdx = endpointIdx % CONFIG.endpoints.length;
} else {
failCounter = 0;
print("Ping succeeded")
}
if (failCounter >= CONFIG.numberOfFails) {
print("Too many fails, resetting...");
failCounter = 0;
Timer.clear(pingTimer);
//set the output with toggling back
Shelly.call("Shelly.Reboot");
return;
}
}
);
}
print("Start watchdog timer");
pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints);
Shelly.addStatusHandler(function (status) {
//is the component a switch
if(status.name !== "switch") return;
//is it the one with id 0
if(status.id !== 0) return;
//does it have a delta.source property
if(typeof status.delta.source === "undefined") return;
//is the source a timer
if(status.delta.source !== "timer") return;
//is it turned on
if(status.delta.output !== true) return;
//start the loop to ping the endpoints again
pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment