Script Shelly scripts
What your home's standby power costs, reported every morning
Finds the lowest power your home draws overnight and sends you a notification through ntfy each morning with what that always-on load costs a year.
- Gen3
- Gen4
- Pro
// Standby power report: finds the lowest power your home draws overnight and tells// your phone through ntfy what that always-on load costs a year. Needs a Shelly energy meter. let CONFIG = { fromTime: "01:00", toTime: "05:00", pricePerKwh: 0.30, ntfyTopic: "", meterId: 0,}; // One reading a minute, averaged over ten, so a fridge switching off for a// moment doesn't count as the house's lowest load.let SAMPLE_SEC = 60;let AVG_SAMPLES = 10;// Hours in a year, to turn watts into kilowatt-hours a year.let HOURS_A_YEAR = 8760; let recent = [];let nightLow = null; function nowTime() { let s = Shelly.getComponentStatus("sys"); return s === null ? null : s.time;} function inWindow(t) { if (CONFIG.fromTime < CONFIG.toTime) return t >= CONFIG.fromTime && t < CONFIG.toTime; return t >= CONFIG.fromTime || t < CONFIG.toTime;} function readPower() { let m = Shelly.getComponentStatus("em1", CONFIG.meterId); if (m === null || typeof m.act_power !== "number") return null; return m.act_power;} function onSent(res, err) { if (err !== 0 || res.code !== 200) print("Couldn't send the report to ntfy.");} function send(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);} function report() { let w = Math.round(nightLow); let kwh = Math.round(w * HOURS_A_YEAR / 1000); let cost = Math.round(kwh * CONFIG.pricePerKwh); send("Overnight, the lowest your home drew was " + w + " W. Left on all year, that's " + kwh + " kWh, about $" + cost + " at " + Math.round(CONFIG.pricePerKwh * 100) + "c a kWh.");} function sample() { let t = nowTime(); // Without the clock it can't tell when night is, so it does nothing. if (t === null) return; if (!inWindow(t)) { if (nightLow !== null) report(); else if (recent.length > 0) print("Too few readings overnight for a report."); recent = []; nightLow = null; return; } let p = readPower(); if (p === null) return; recent.push(p); if (recent.length > AVG_SAMPLES) recent.splice(0, 1); if (recent.length < AVG_SAMPLES) return; let sum = 0; for (let i = 0; i < recent.length; i++) sum += recent[i]; let avg = sum / recent.length; if (nightLow === null || avg < nightLow) nightLow = avg;} Timer.set(SAMPLE_SEC * 1000, true, sample);print("Standby power report started. Watching from " + CONFIG.fromTime + " to " + CONFIG.toTime + " each night."); What it does
Between 1 am and 5 am, when nobody should be using much, the script reads the meter once a minute and keeps a ten-minute average. The lowest average of the night is your standby load: the modem, chargers, appliances on standby and anything else that never turns off. At 5 am your phone gets a message with that figure in watts, the kilowatt-hours it adds up to over a year, and what that costs at your price. Ten-minute averages mean a fridge switching off for a moment doesn't count as the house's lowest load.
Turn things off one at a time and watch the next morning's figure fall to find what's drawing the most. If the script has fewer than ten minutes of readings in the quiet hours, it skips the report and says so in the console. Until the Shelly's clock has synced, it doesn't read anything.
Install it on your Shelly
Before you install
- Check the meter and its clock
Once the meter is installed, check it shows power in the Shelly app and that the device's time zone is your own.
- Subscribe in ntfy
Install the ntfy app and subscribe to a topic name nobody would guess, such as smith-power-4821.
Install the script
Install it on the meter.
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 standby-power-report. 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 standby-power-report. 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
Stop the script, set fromTime to now and toTime 15 minutes ahead, save it and run it again. At toTime your phone gets the report. Set both 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