Script Shelly scripts
Phone alert when a Shelly goes offline
One Shelly checks on your others every few minutes and sends a notification to your phone through ntfy when one stops answering, then again when it's back.
- Gen2
- Gen3
- Gen4
- Pro
// Offline alert: checks your other Shelly devices and sends a phone notification// through ntfy when one stops answering, and again when it's back. let CONFIG = { devices: "192.168.1.40 Garage door, 192.168.1.41 Hot water", ntfyTopic: "", checkEveryMin: 5, failsBeforeAlert: 3,}; // Each check is one small request per device, sent one at a time.let REPLY_TIMEOUT_SEC = 5;// Unsent alerts are kept and retried after each round, up to this many.let MAX_QUEUED = 10; let list = [];let next = 0;let queue = [];let sending = false; function parseDevices() { let parts = CONFIG.devices.split(","); for (let i = 0; i < parts.length; i++) { let p = parts[i].trim(); if (p === "") continue; let gap = p.indexOf(" "); let ip = gap === -1 ? p : p.slice(0, gap); let name = gap === -1 ? p : p.slice(gap + 1).trim(); list.push({ ip: ip, name: name, fails: 0, down: false }); }} function onSent(res, err) { sending = false; if (err === 0 && res.code === 200) { queue.splice(0, 1); sendNext(); } else { print("Couldn't reach ntfy. Will try again after the next check."); }} function sendNext() { if (sending || queue.length === 0 || CONFIG.ntfyTopic === "") return; sending = true; Shelly.call("HTTP.Request", { method: "POST", url: "https://ntfy.sh/" + CONFIG.ntfyTopic, body: queue[0], headers: { "Content-Type": "text/plain" }, timeout: 15, }, onSent);} function alert(msg) { print(msg); if (queue.length >= MAX_QUEUED) queue.splice(0, 1); queue.push(msg);} function onReply(res, err) { let d = list[next]; let ok = err === 0 && res.code === 200; if (ok) { if (d.down) alert(d.name + " (" + d.ip + ") is back online."); d.fails = 0; d.down = false; } else { d.fails++; if (!d.down && d.fails >= CONFIG.failsBeforeAlert) { d.down = true; alert(d.name + " (" + d.ip + ") is offline. No answer for " + d.fails + " checks in a row."); } } next++; checkNext();} function checkNext() { if (next >= list.length) { sendNext(); return; } Shelly.call("HTTP.GET", { url: "http://" + list[next].ip + "/rpc/Shelly.GetDeviceInfo", timeout: REPLY_TIMEOUT_SEC, }, onReply);} function round() { // Skip a round if the last one is still waiting on a slow device. if (next < list.length) return; next = 0; checkNext();} parseDevices();next = list.length;Timer.set(CONFIG.checkEveryMin * 60 * 1000, true, round);print("Offline alert started. Watching " + list.length + " devices.");round(); What it does
Every five minutes the script asks each device on your list for its details, one at a time. A device that misses three checks in a row sets off an alert on your phone through ntfy, with its name and address. When it answers again, a second message says it's back.
If ntfy can't be reached, for instance because the internet is down, the alert waits on the Shelly and goes out after a later check, so you still hear about it. It watches Shelly devices with Gen2 or newer firmware only, because it asks each one through the Shelly local API. It can't tell you about itself, so run it on a Shelly that's unlikely to go down with the others.
Install it on your Shelly
Before you install
- Give each watched device a fixed address
In your router, reserve each device's current IP address for it. Note the addresses down with a name for each.
- Subscribe in ntfy
Install the ntfy app and subscribe to a topic name nobody would guess, such as smith-shelly-4821.
Install the script
Install it on the Shelly that will do the checking.
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 device-offline-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 device-offline-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
- Test it
Unplug or switch off a watched device. With the defaults, the alert reaches your phone within 15 minutes.
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