Shelly Smarthome Contest 2021 - shelly-tools/shelly-script-examples GitHub Wiki
by Thorsten Eurich
The idea behind this project is to monitor a router's internet connection and restart the router if the connection fails for a defined period of time.
Some routers can't properly re-connect to the internet when their connection gets lost. If the router is on a remote place (e.g. a holiday house) it's not possible to restart the router manually.
Let's take a new Shelly Gen2 Device with its powerful scripting engine and monitor the internet connection. We can do this with a small script which fetches an URL somewhere in the internet. If that URL isn't reachable for a defined time period we assume our internet connection is broken and could not be re-established. The script will then turn off the relay for a short time which causes the connected router to restart.
- A Shelly Gen2 device such as the new Shelly Plus 1.
- At least Firmware 0.9 Beta1
- A router with Wifi and allmost permanent Internet connection
- Router connected to the Shelly's Output.
- a Website with just a few bytes of content which we can monitor.
First of all we need to wire the Shelly behind the outlet where the router is connected to. Then join the Shelly to the routers Wifi. Once you're connected to the Wifi go to the local HTTP interface of the Shelly, e.g. http://<shelly-ip>
(These steps are well documented in the manual, therefore I won't go into details)
We need to setup an auto-on timer first. That ensures the router is always powered on and if we shutdown the relay with our script it automatically turns on again.
In my setup I use a 2 seconds timer:
Next click on the <script> button, then click "Add Script"
Give the new script a speaking name such a "InternetMonitor"
then copy the content below to the open script window:
// a remote URL with just a few bytes of content in order to check if internet is still available.
// CONFIG START
let remoteurl = 'https://gist.githubusercontent.com/eurich/6e84e85c11401c4e28a2676492d846b7/raw/5d577bc2dfadfa13887f9b2ec146fe1b2ee2f5b6/gistfile1.txt';
// number of times the check is done before internet is considered as down.
let maxfails = 5;
// checks the internet connection every x minutes, recommended is 5 or more
let interval = 5;
// CONFIG END
// no need to change anything below this line.
let alertTimer = '';
let failcounter = 0;
function startMonitor() {
alertTimer = Timer.set(interval *60 * 1000,
true,
function () {
Shelly.call("HTTP.GET", {
url: remoteurl
},
function (res, error_code, error_msg, ud) {
if (error_code !== 0) {
if (failcounter === maxfails) {
print("Restart");
restartRelay();
failcounter = 0;
} else {
print("fail");
failcounter++;
}
}
},
null
);
},
null
);
}
function restartRelay() {
Shelly.call(
"switch.set",
{ id: 0, on: false, toggle: 2},
function (result, code, msg, ud) {
},
null
);
}
startMonitor();
The above script has got 3 configurable variables.. You'll find them between the // CONFIG START
and // CONFIG END
block. Please check the above comments and change it to fit your own needs.
Attention: Be careful with the URL. the URL should point to a website with just a few bytes of content. I've used a github gist for testing but it's recommended to point to another URL just because my gist could be deleted somewhere in the future.
Then Click the "Save and Run" button.
As soon as you've saved and run the script go back to the scripts overview and enable your script. This is important because the script will automatically start, even if the Shelly is rebooted or in case of a temporary power failure.
That's it. Enyoy and have fun 😃