Script Shelly scripts
Internet watchdog that restarts your modem
Checks the internet every minute and, when it drops, turns the modem off and back on by itself. Backs off during a real outage instead of cycling forever.
- Gen2
- Gen3
- Gen4
- Pro
// Internet watchdog: restarts the modem when the internet drops.// Runs on the Shelly itself. No cloud account needed. let CONFIG = { checkUrl: "http://connectivitycheck.gstatic.com/generate_204", backupUrl: "http://cp.cloudflare.com/", checkEverySec: 60, failsBeforeRestart: 3, offForSec: 20, switchId: 0,}; // After a restart, wait this long before checking again: modems take a few// minutes to resync. It doubles after each restart that does not fix the// connection (up to an hour), so an outage at the exchange does not have the// modem cycling all night.let BOOT_WAIT_SEC = 300;let MAX_WAIT_SEC = 3600; let fails = 0;let waitSec = BOOT_WAIT_SEC;let quietUntil = 0; function nowSec() { return Shelly.getUptimeMs() / 1000;} function answered(res, err) { return err === 0 && res && res.code >= 200 && res.code < 400;} function healthy() { if (fails > 0) print("Internet is back."); fails = 0; waitSec = BOOT_WAIT_SEC;} function restartModem() { print("Internet down for " + fails + " checks. Restarting the modem."); Shelly.call("Switch.Set", { id: CONFIG.switchId, on: false, toggle_after: CONFIG.offForSec, }); quietUntil = nowSec() + CONFIG.offForSec + waitSec; waitSec = Math.min(waitSec * 2, MAX_WAIT_SEC); fails = 0;} // Callbacks are named functions rather than nested ones: Shelly's script// engine can crash on more than two or three levels of nesting.function onBackupReply(res, err) { if (answered(res, err)) return healthy(); fails++; print("Check failed (" + fails + " of " + CONFIG.failsBeforeRestart + ")"); if (fails >= CONFIG.failsBeforeRestart) restartModem();} function onFirstReply(res, err) { if (answered(res, err)) return healthy(); Shelly.call("HTTP.GET", { url: CONFIG.backupUrl, timeout: 10 }, onBackupReply);} function check() { if (nowSec() < quietUntil) return; Shelly.call("HTTP.GET", { url: CONFIG.checkUrl, timeout: 10 }, onFirstReply);} Timer.set(CONFIG.checkEverySec * 1000, true, check);print("Internet watchdog started."); What it does
Every minute the script asks two addresses on the internet for a reply. If neither answers three times in a row, it switches the relay off for twenty seconds and back on, which restarts the modem. It then gives the modem five minutes to reconnect before it checks again.
If a restart does not bring the internet back, the outage is probably at the exchange rather than in your modem, so the script waits twice as long before trying again, up to an hour between restarts. Once the connection returns, everything resets. It runs entirely on the Shelly, so it keeps working when the internet, the Shelly cloud and your phone are all offline.
Install it on your Shelly
Before you install
- Set the relay to come back on
In the Shelly app, open the relay's settings and set its power-on default to On, so the modem always has power after a blackout.
Install the script
Shelly Smart Control
- Open the device and tap Scripts
In Shelly Smart Control, open the room, then the device. Tap Scripts, the { } icon in the menu down the left.
- Tap Create new script
If the device has no scripts yet, the screen says No scripts created.
- Name it, paste the code, then tap Save
Name it internet-watchdog. Tap in the code area, paste the script you copied above, then tap Save (the disk icon at the top right).
- Tap the play button
The play button lights up once the script is saved. Tap it and a message says the script has been started.
- Close the editor and turn on Run on startup
Back on the Scripts screen, turn on Run on startup next to your script, so it starts again after a power cut or a firmware update.
Direct access via IP
- Open the device's web page and click Scripts
Find the IP address in the Shelly app under Device settings, then Device information, and type it into a browser on the same Wi-Fi. Click Scripts in the menu on the left.
- Click Create script
If the device has no scripts yet, the list says so.
- Name it, paste the code, then click Save
Name it internet-watchdog. Click in the code area, paste the script you copied above, then click Save (the disk icon above the code).
- Click Run and check the console
Run appears once the script is saved. The console under the code then prints what the script is doing.
- Turn on Run on startup
Click Scripts again and turn on Run on startup next to your script, so it starts again after a power cut or a firmware update.
Changelog
v1.0 First release. Still need a hand?
We answer most messages within one business day. If your question is about an existing order, have your order number ready. It speeds things up.
Contact us