Script Shelly scripts
Phone alert when your home's public IP address changes
Checks your home's public IP address every 15 minutes and sends a notification through ntfy when your internet provider changes it. Runs on any Gen2 or newer Shelly.
- Gen2
- Gen3
- Gen4
- Pro
// Public IP change alert: checks your home's public IP address and tells your phone// through ntfy when it changes. Runs on any Gen2 or newer Shelly. let CONFIG = { ntfyTopic: "", checkEveryMin: 15,}; // ipify answers with the bare address, small enough for HTTP.GET.let IP_URL = "https://api.ipify.org";let KVS_KEY = "public-ip"; // The last address seen, saved so a restart doesn't count as a change.let lastIp = "";let failing = false; function onSent(res, err) { if (err !== 0 || res.code !== 200) print("Couldn't send the alert to ntfy.");} function alert(msg) { print(msg); if (CONFIG.ntfyTopic === "") return; Shelly.call("HTTP.Request", { method: "POST", url: "https://ntfy.sh/" + CONFIG.ntfyTopic, body: msg, headers: { "Content-Type": "text/plain" }, timeout: 15, }, onSent);} // An IPv4 address is four numbers from 0 to 255 with dots between them.function looksLikeIp(s) { let parts = s.split("."); if (parts.length !== 4) return false; for (let i = 0; i < 4; i++) { let n = Number(parts[i]); if (parts[i] === "" || isNaN(n) || n < 0 || n > 255) return false; } return true;} function onReply(res, err) { let ip = (err === 0 && res.code === 200) ? res.body.trim() : ""; // No answer, or not an address: say so once, then try again at the next check. if (!looksLikeIp(ip)) { if (!failing) print("Couldn't get the public IP address. Trying again in " + CONFIG.checkEveryMin + " min."); failing = true; return; } failing = false; if (ip === lastIp) return; if (lastIp === "") { print("Public IP address is " + ip + "."); } else { alert("Your home's public IP address changed from " + lastIp + " to " + ip + "."); } lastIp = ip; Shelly.call("KVS.Set", { key: KVS_KEY, value: ip });} function check() { Shelly.call("HTTP.GET", { url: IP_URL, timeout: 15 }, onReply);} function onLoaded(res, err) { if (err === 0 && res && typeof res.value === "string") lastIp = res.value; Timer.set(CONFIG.checkEveryMin * 60 * 1000, true, check); print("Public IP alert started. Checking every " + CONFIG.checkEveryMin + " min."); check();} Shelly.call("KVS.Get", { key: KVS_KEY }, onLoaded); What it does
Every 15 minutes the script asks ipify, a free service with no account, for the address your home shows to the internet. The first answer is saved on the Shelly. When a later answer is different, your phone gets a message with the old and new addresses, and the new one is saved. Many home internet plans don't come with a fixed address, so it can change, often after the modem restarts. That matters if you reach a camera, a game server or a VPN at home by its address, or if a work system only lets your address in.
A restart doesn't count as a change, because the last address is saved. If the check fails, the console says so once and the script tries again at the next check. It only reads IPv4 addresses.
Install it on your Shelly
Before you install
- Subscribe in ntfy
Install the ntfy app and subscribe to a topic name nobody would guess, such as smith-ip-4821.
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 public-ip-change-alert. 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 public-ip-change-alert. 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.
After you install
- Check the console
On the device's web page, the console prints Public IP address is, then your address. It prints nothing more until the address changes.
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