Script Shelly scripts
Hot water or pool pump on off-peak times, with a boost switch
Runs the load only in your cheap-rate windows, such as overnight off-peak and a midday solar window. A switch gives you a one-off boost when you need it.
- Gen2
- Gen3
- Gen4
- Pro
// Off-peak load control: runs the relay in cheap-rate windows only.// A flick of a switch on the input starts or cancels a one-off boost. let CONFIG = { window1: "22:00-07:00", window2: "", boostMin: 60, boostInputId: 0, switchId: 0,}; let boostUntil = 0;let warnedClock = false; function nowSec() { return Shelly.getUptimeMs() / 1000;} function minutesOf(hhmm) { let p = hhmm.split(":"); return Number(p[0]) * 60 + Number(p[1]);} // A window can cross midnight: "22:00-07:00" covers 10 pm to 7 am.function inWindow(spec, now) { if (spec === "") return false; let parts = spec.split("-"); if (parts.length !== 2) return false; let start = minutesOf(parts[0]); let end = minutesOf(parts[1]); if (start <= end) return now >= start && now < end; return now >= start || now < end;} function update() { let want = nowSec() < boostUntil; let sys = Shelly.getComponentStatus("sys"); if (sys.time === null) { // No clock yet (it comes from the internet after a restart). Hold off // rather than guess, and say so once. if (!warnedClock) print("Waiting for the clock to sync."); warnedClock = true; } else { warnedClock = false; let now = minutesOf(sys.time); want = want || inWindow(CONFIG.window1, now) || inWindow(CONFIG.window2, now); } let sw = Shelly.getComponentStatus("switch", CONFIG.switchId); if (sw !== null && sw.output !== want) { Shelly.call("Switch.Set", { id: CONFIG.switchId, on: want }); }} function onStatus(ev) { if (ev.component !== "input:" + CONFIG.boostInputId || ev.delta.state === undefined) return; if (nowSec() < boostUntil) { boostUntil = 0; print("Boost cancelled."); } else { boostUntil = nowSec() + CONFIG.boostMin * 60; print("Boost on for " + CONFIG.boostMin + " minutes."); } update();} Shelly.addStatusHandler(onStatus);Timer.set(30000, true, update);update();print("Off-peak control started."); What it does
Every 30 seconds the script reads the device's clock and turns the relay on if the time falls inside one of your windows, and off if it doesn't. A window can run past midnight, which is how most overnight off-peak tariffs work. The second window is there for a midday solar soak, if your plan has one.
The boost switch is for the day you need hot water outside the cheap hours. Flick it and the relay runs for your boost time, then goes back to the windows. Flick it again during a boost to cancel. If the clock hasn't synced after a restart, the script waits rather than guessing, so the load doesn't run at the wrong rate.
Install it on your Shelly
Before you install
- Check the clock and time zone
The script runs on the device's local time. In the Shelly app, check the time zone under Device settings, then Location and time, and that the device shows the right time.
- Detach the input from the relay
If you've wired a boost switch, open the relay's input settings, set the input to Switch and the mode to Detached.
Install the script
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 off-peak-hot-water. 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 off-peak-hot-water. 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
- Remove any old schedules
If the relay has schedules of its own, delete them. The script turns the relay off outside your windows, so a schedule would fight it.
Changelog
v1.0 First release. Related scripts
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