Script Shelly scripts
Phone alert when your pressure pump keeps starting, a sign of a leak
A Shelly that measures power counts how often your house or tank pressure pump starts, and messages your phone through ntfy when it starts too often or runs too long.
- Gen2
- Gen3
- Gen4
- Pro
// Pressure pump short-cycling alert: counts how often the pump starts and messages your// phone through ntfy when it starts too often or runs too long, as a leak or a failed// pressure tank makes it do. Needs a Shelly relay that measures power, left on. let CONFIG = { pumpName: "Pressure pump", switchId: 0, runningWatts: 200, maxStartsPerHour: 12, maxRunMin: 30, stopOnLongRun: false, ntfyTopic: "",}; // Power is read this often. A short-cycling pump can run for only a few seconds.let CHECK_SEC = 2;// After a starts alert, the next one waits this long, so a leak doesn't send a message// for every start.let ALERT_GAP_SEC = 3600;let HOUR_SEC = 3600; let running = false;let runStart = 0;let starts = [];let lastStartsAlert = -ALERT_GAP_SEC;let longAlerted = false;let stopped = false;let queue = [];let sending = false; function secsFromBoot() { return Shelly.getComponentStatus("sys").uptime;} function startLoop() { let st = readOutput(); if (st === null) { print("There's no output " + CONFIG.switchId + " on this Shelly. Check switchId."); return; } if (typeof st.apower !== "number") { print("This output doesn't measure power, so the script can't watch the pump."); return; } if (CONFIG.ntfyTopic === "") print("No ntfy topic set. Alerts show in this console only."); Timer.set(CHECK_SEC * 1000, true, check_5b2d); print("Pump alert started for " + CONFIG.pumpName + ". Alerts above " + CONFIG.maxStartsPerHour + " starts an hour or " + CONFIG.maxRunMin + " min running.");} function readOutput() { return Shelly.getComponentStatus("switch", CONFIG.switchId);} // Drops starts older than an hour, so the list is always the last hour's.function trimWindow(now) { while (starts.length > 0 && now - starts[0] > HOUR_SEC) starts.splice(0, 1);} 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 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 shortly."); }} function alert(msg) { print(msg); if (queue.length >= 3) queue.splice(0, 1); queue.push(msg);} function onStart(now) { running = true; runStart = now; longAlerted = false; starts.push(now); trimWindow(now); if (starts.length > CONFIG.maxStartsPerHour && now - lastStartsAlert >= ALERT_GAP_SEC) { lastStartsAlert = now; alert(CONFIG.pumpName + ": started " + starts.length + " times in the last hour. " + "Check for a dripping tap, a leaking pipe or toilet, or a pressure tank that needs air."); }} function onStop(now) { running = false; trimWindow(now); print(CONFIG.pumpName + ": ran " + (now - runStart) + " s, " + starts.length + (starts.length === 1 ? " start" : " starts") + " in the last hour.");} function check_5b2d() { let st = readOutput(); let now = secsFromBoot(); if (!st.output) { // Switched off at the Shelly, by this script or a person: nothing to watch. if (running) onStop(now); sendNext(); return; } stopped = false; let nowRunning = st.apower >= CONFIG.runningWatts; if (nowRunning && !running) onStart(now); if (!nowRunning && running) onStop(now); let ran = now - runStart; if (running && !longAlerted && ran >= CONFIG.maxRunMin * 60) { longAlerted = true; if (CONFIG.stopOnLongRun && !stopped) { stopped = true; Shelly.call("Switch.Set", { id: CONFIG.switchId, on: false }); alert(CONFIG.pumpName + ": ran " + Math.round(ran / 60) + " min without stopping, " + "so it has been switched off. Check for a burst pipe or a tap left on, then turn it back on in the Shelly app."); } else { alert(CONFIG.pumpName + ": has run " + Math.round(ran / 60) + " min without stopping. " + "Check for a burst pipe or a tap left on."); } } sendNext();} startLoop(); What it does
A house or rainwater tank pump starts when the pressure in the pipes drops and stops when it builds back up. With nobody using water, it shouldn't start at all. A dripping tap, a leaking toilet or a split pipe lets the pressure bleed away, and the pump starts again and again to make it up. A pressure tank that has lost its air does the same, in short bursts of a few seconds. Either way the pump starts far more often than it should.
The script reads the pump's power every two seconds and counts each start. When the starts in the last hour pass your limit, your phone gets a message such as "Tank pump: started 14 times in the last hour. Check for a dripping tap, a leaking pipe or toilet, or a pressure tank that needs air." After that it waits an hour before sending another, so a leak doesn't flood your phone. A separate message comes when the pump runs longer than you set without stopping, as it does with a burst pipe or a tap left on. Set stopOnLongRun to true and it switches the pump off then too, and leaves it off until you turn it back on, so a burst pipe can't empty the tank. With it left at false, the script never switches anything.
Install it on your Shelly
Before you install
- Set the relay to come back on after a blackout
In the Shelly app, open the relay's settings and set its power-on default to On, so the pump has power when the power returns.
- Find the pump's running draw
Turn on a tap and watch the relay's power reading in the app. Note the steady figure while the pump runs, and set runningWatts to about half of it.
- Subscribe in ntfy
Install the ntfy app and subscribe to a topic name nobody would guess, such as smith-pump-4821.
Install the script
Install it on the relay the pump is wired through.
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 pump-short-cycling-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 pump-short-cycling-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
- Watch a normal day
The console prints a line such as Tank pump: ran 45 s, 3 starts in the last hour, for every run. Use a day of these to set maxStartsPerHour and maxRunMin.
- Test the alert
Stop the script, set maxStartsPerHour to 2, save it and run it again. Turn a tap on and off three times, waiting for the pump to stop each time. Your phone gets the starts message. Set it back afterwards.
Changelog
v1.1 Tidied the internal function names. It works exactly as before. 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