Shelly Logo Squared

Script Shelly scripts

Phone message when the power comes back, with how long it was off

When a blackout ends, your Shelly sends a notification to your phone through ntfy saying the power is back and roughly how long it was out. Handy for the fridge and freezer.

Updated v1.0

  • Gen2
  • Gen3
  • Gen4
  • Pro
power-outage-report.js · 113 lines Download .js
// Power outage report: when the power comes back, sends your phone a message through// ntfy saying how long it was off. Runs on any Gen2 or newer Shelly on mains power. let CONFIG = {  ntfyTopic: "",  placeName: "Home",  minOffMin: 1,}; // The Shelly notes the time on its own storage every 5 minutes. When it starts// again, the power went off at some point in the 5 minutes after the last note.let NOTE_EVERY_MIN = 5;// Only a start this soon after power-up counts as the power coming back, so// pressing Run by hand later doesn't send a report.let BOOT_WINDOW_SEC = 300;// After a blackout the modem is often still starting, so keep trying to send// once a minute for up to two hours.let RETRY_SEC = 60;let MAX_TRIES = 120;let KVS_KEY = "outage-note"; // Seconds since power-up when the script started.let startedAt = Shelly.getComponentStatus("sys").uptime;let message = "";let tries = 0;let clockTimer = null; function clock() {  let s = Shelly.getComponentStatus("sys");  if (s === null || typeof s.unixtime !== "number" || s.time === null) return null;  return s;} function note() {  let s = clock();  if (s === null) return;  Shelly.call("KVS.Set", { key: KVS_KEY, value: JSON.stringify(s.unixtime) });} function onSent(res, err) {  if (err === 0 && res.code === 200) {    print("Report sent.");    return;  }  tries++;  if (tries >= MAX_TRIES) {    print("Couldn't reach ntfy for two hours. Giving up on this report.");    return;  }  Timer.set(RETRY_SEC * 1000, false, send);} function send() {  if (CONFIG.ntfyTopic === "") return;  Shelly.call("HTTP.Request", {    method: "POST",    url: "https://ntfy.sh/" + CONFIG.ntfyTopic,    body: message,    headers: { "Content-Type": "text/plain" },    timeout: 15,  }, onSent);} function report(lastNote, s) {  let bootAt = s.unixtime - s.uptime;  let longest = Math.ceil((bootAt - lastNote) / 60);  let shortest = Math.max(0, longest - NOTE_EVERY_MIN);  // Report only an outage known to be at least minOffMin long, so a restart  // for a firmware update doesn't send a message.  if (shortest < CONFIG.minOffMin) {    print("Off for up to " + longest + " min, which may have been a restart. No report.");    return;  }  message = "Power is back at " + CONFIG.placeName + " (" + s.time + "). It was off for " +    shortest + " to " + longest + " min.";  print(message);  send();} function onNote(res, err) {  let s = clock();  let last = null;  if (err === 0 && res && typeof res.value === "string") {    try {      let n = JSON.parse(res.value);      if (typeof n === "number") last = n;    } catch (e) {      last = null;    }  }  if (last === null) print("No earlier note found, so nothing to report this time.");  else report(last, s);  note();  Timer.set(NOTE_EVERY_MIN * 60 * 1000, true, note);} function waitForClock() {  let s = clock();  if (s === null) return;  Timer.clear(clockTimer);  if (startedAt > BOOT_WINDOW_SEC) {    print("Started by hand, not by a power-up. Watching from now.");    note();    Timer.set(NOTE_EVERY_MIN * 60 * 1000, true, note);    return;  }  Shelly.call("KVS.Get", { key: KVS_KEY }, onNote);} print("Outage report started. Waiting for the clock.");// It can't time anything until the clock has synced, so it checks every 10 s.clockTimer = Timer.set(10 * 1000, true, waitForClock);waitForClock();

What it does

While it runs, the script saves the time on the Shelly's own storage every five minutes. When the power comes back and the Shelly starts up, it waits for its clock to sync, compares the time it started with the last time it saved, and sends a message like "Power is back at Home (14:32). It was off for 35 to 40 min." The five-minute gap is why it gives a range: the power went off at some point after the last save.

It only reports when the Shelly has just started up, so pressing Run yourself doesn't send anything. After a blackout the modem often takes a few minutes to come back, so the script tries to send once a minute for up to two hours. A restart for a firmware update counts as an outage too, but at about a minute it falls under the two-minute default and isn't reported.

Install it on your Shelly

View the full install guide

Before you install

  1. Subscribe in ntfy

    Install the ntfy app and subscribe to a topic name nobody would guess, such as smith-power-4821.

Install the script

Run on startup matters more here than anywhere, because the report is sent as the Shelly starts up.

Shelly Smart Control

  1. 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.

  2. Tap Create new script

    If the device has no scripts yet, the screen says No scripts created.

  3. Name it, paste the code, then tap Save

    Name it power-outage-report. Tap in the code area, paste the script you copied above, then tap Save (the disk icon at the top right).

  4. 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.

  5. 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

  1. 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.

  2. Click Create script

    If the device has no scripts yet, the list says so.

  3. Name it, paste the code, then click Save

    Name it power-outage-report. Click in the code area, paste the script you copied above, then click Save (the disk icon above the code).

  4. 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.

  5. 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

  1. Test it

    Switch the Shelly's circuit off at the switchboard for at least five minutes, then back on. Your phone gets a message within a minute or two of the Shelly reconnecting.

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