Script Shelly scripts
Phone alert when you lose a phase on a three-phase supply
Watches all three phases on a Shelly Pro 3EM and sends your phone an alert through ntfy when one drops out or the phase voltages drift apart, then again when it's fixed.
- Pro
// Three-phase loss alert: tells your phone through ntfy when a phase drops out or the// phase voltages drift apart, and again when it's fixed. Needs a Shelly Pro 3EM. let CONFIG = { lostBelowVolts: 180, spreadVolts: 20, holdSec: 30, ntfyTopic: "", placeName: "Home", meterId: 0,}; // Read the meter every 5 seconds, so a short dip is seen without filling the log.let READ_EVERY_SEC = 5;// A lost phase has to come this far back above lostBelowVolts, and a wide spread// this far back under spreadVolts, before the all-clear, so a reading that sits on// the line can't send alert after alert.let RECOVER_VOLTS = 5;// The meter runs off phase C, so losing C (or the whole supply) restarts it. A start// within this many seconds of power-up is reported as a restart.let FRESH_BOOT_SEC = 300;// Unsent messages are kept and retried after each reading, up to this many.let MAX_QUEUED = 10; let PHASES = ["a", "b", "c"];let NAMES = { a: "A", b: "B", c: "C" };let lostSince = { a: 0, b: 0, c: 0 };let lostAlerted = { a: false, b: false, c: false };let spreadSince = 0;let spreadAlerted = false;let worst = 0;let bootChecked = false;let queue = [];let sending = false;let warnedNoMeter = false; function onFirstSent(res, err) { sending = false; if (err === 0 && res.code === 200) { queue.splice(0, 1); sendNext(); } else { print("Couldn't reach ntfy. Will try again shortly."); }} function readLiveStatus() { let s = Shelly.getComponentStatus("em", CONFIG.meterId); if (s === null || typeof s.a_voltage === "undefined") return null; return s;} function phaseOk(v) { return typeof v === "number" && v >= CONFIG.lostBelowVolts;} function keepWorst(spread) { if (spread > worst) worst = spread;} 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, }, onFirstSent);} function alertPhone(msg) { msg = CONFIG.placeName + ": " + msg; print(msg); if (queue.length >= MAX_QUEUED) queue.splice(0, 1); queue.push(msg);} function volts(v) { return typeof v === "number" ? v.toFixed(1) + " V" : "no reading";} function allVolts(s) { return "A " + volts(s.a_voltage) + ", B " + volts(s.b_voltage) + ", C " + volts(s.c_voltage);} function took(secs) { return secs < 60 ? "under a minute" : "about " + Math.round(secs / 60) + " min";} function checkPhase(p, v, now) { let name = NAMES[p]; if (!phaseOk(v)) { if (lostSince[p] === 0) { lostSince[p] = now; print("Phase " + name + " reads " + volts(v) + ". Watching."); } if (!lostAlerted[p] && now - lostSince[p] >= CONFIG.holdSec) { lostAlerted[p] = true; alertPhone("Phase " + name + " is down (" + volts(v) + ") for over " + CONFIG.holdSec + " s. Anything on that phase has no power."); } } else if (lostSince[p] !== 0 && (v >= CONFIG.lostBelowVolts + RECOVER_VOLTS || !lostAlerted[p])) { if (lostAlerted[p]) { alertPhone("Phase " + name + " is back at " + volts(v) + " after " + took(now - lostSince[p]) + "."); } else { print("Phase " + name + " back to " + volts(v) + " before the hold time. No alert."); } lostSince[p] = 0; lostAlerted[p] = false; }} function checkSpread(s, now) { let a = s.a_voltage, b = s.b_voltage, c = s.c_voltage; // Only compare phases that are all live. A lost phase has its own alert. if (!phaseOk(a) || !phaseOk(b) || !phaseOk(c)) return; let hi = a, lo = a; if (b > hi) hi = b; if (c > hi) hi = c; if (b < lo) lo = b; if (c < lo) lo = c; let spread = hi - lo; if (spread > CONFIG.spreadVolts) { if (spreadSince === 0) { spreadSince = now; worst = 0; print("Phases are " + spread.toFixed(1) + " V apart (" + allVolts(s) + "). Watching."); } keepWorst(spread); if (!spreadAlerted && now - spreadSince >= CONFIG.holdSec) { spreadAlerted = true; alertPhone("Phase voltages are " + spread.toFixed(1) + " V apart for over " + CONFIG.holdSec + " s: " + allVolts(s) + ". Worth an electrician's look."); } } else if (spreadSince !== 0 && (spread <= CONFIG.spreadVolts - RECOVER_VOLTS || !spreadAlerted)) { if (spreadAlerted) { alertPhone("Phase voltages are back together, " + spread.toFixed(1) + " V apart, after " + took(now - spreadSince) + ". The widest gap was " + worst.toFixed(1) + " V."); } else { print("Phases back together before the hold time. No alert."); } spreadSince = 0; spreadAlerted = false; }} function check_24a0() { let s = readLiveStatus(); if (s === null) { if (!warnedNoMeter) print("No three-phase reading. Check meterId, and that the meter is set to three-phase."); warnedNoMeter = true; return; } warnedNoMeter = false; let now = Shelly.getComponentStatus("sys").uptime; if (!bootChecked) { bootChecked = true; if (now < FRESH_BOOT_SEC) { alertPhone("The meter has just restarted. If nobody restarted it, phase C or the whole " + "supply was off. Now: " + allVolts(s) + "."); } } for (let i = 0; i < PHASES.length; i++) { checkPhase(PHASES[i], s[PHASES[i] + "_voltage"], now); } checkSpread(s, now); sendNext();} Timer.set(READ_EVERY_SEC * 1000, true, check_24a0);let first = readLiveStatus();print("Phase alert started. " + (first === null ? "No reading yet." : allVolts(first) + "."));check_24a0(); What it does
Every 5 seconds the script reads the voltage on each phase. If phase A or B drops below 180 V for 30 seconds, your phone gets an alert naming the phase, and a second message when it's back with how long it was off. It also compares the three phases. When they drift more than 20 V apart and stay that way, you're told the three readings, because healthy phases sit within a few volts of each other.
The meter takes its own power from phase C, so if C goes, the meter goes with it and can't send anything. When it starts again, the script sends a message saying the meter restarted, which covers C and a whole-supply outage. The script only tells you. It doesn't switch anything off. If you need equipment protected from a lost phase, ask your electrician about a phase monitoring relay. If ntfy can't be reached, the alert waits on the Shelly and goes out once it can.
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-phases-4821.
Install the script
Install it on the Pro 3EM.
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 three-phase-loss-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 three-phase-loss-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
On the meter's web page, the console prints Phase alert started with all three voltages. To see an alert, stop the script, set spreadVolts to 0, save it and run it again, then wait out the hold time. Set it back afterwards.
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