Script Shelly scripts
Cap how many hours a heater or pump runs each day
Adds up how long a Shelly relay has been on since midnight and switches it off for the rest of the day once your daily allowance is used, however it was turned on.
- Gen2
- Gen3
- Gen4
- Pro
// Daily run limit: adds up how long a relay has been on since midnight and switches// it off for the rest of the day once your allowance is used. Needs a Shelly relay. let CONFIG = { maxHours: 4, switchId: 0,}; // How often the on-time is added up. The relay goes off within this long of the limit.let TICK_SEC = 10;// Save the count every few minutes rather than every tick, to spare the flash memory.let SAVE_EVERY_SEC = 300;let KVS_KEY = "daily-run-limit"; // The local date and the seconds the relay has been on so far that day. Saved,// so a restart mid-day doesn't reset the allowance.let day = { date: -1, onSec: 0 };let lastUptime = -1;let sinceSave = 0;let limitHit = false; function localDate() { let s = Shelly.getComponentStatus("sys"); if (s === null || typeof s.unixtime !== "number" || s.time === null) return -1; return Math.floor((s.unixtime + (s.utc_offset || 0)) / 86400);} function isOn() { let s = Shelly.getComponentStatus("switch", CONFIG.switchId); return s !== null && s.output === true;} function save() { sinceSave = 0; Shelly.call("KVS.Set", { key: KVS_KEY, value: JSON.stringify(day) });} function hours(sec) { return (sec / 3600).toFixed(1);} function switchOff() { Shelly.call("Switch.Set", { id: CONFIG.switchId, on: false });} function tick() { let up = Shelly.getComponentStatus("sys").uptime; let step = lastUptime < 0 ? 0 : up - lastUptime; lastUptime = up; let date = localDate(); // Without the clock it can't tell when the day starts, so it doesn't count or switch. if (date === -1) return; if (day.date !== date) { if (day.date === date - 1) print("Yesterday's run time: " + hours(day.onSec) + " h."); day = { date: date, onSec: 0 }; limitHit = false; save(); print("New day. Allowance of " + CONFIG.maxHours + " h."); } if (isOn()) { day.onSec += step; sinceSave += step; } if (sinceSave >= SAVE_EVERY_SEC) save(); if (day.onSec >= CONFIG.maxHours * 3600 && !limitHit) { limitHit = true; save(); print("Used today's " + CONFIG.maxHours + " h. Off until midnight."); } if (limitHit && isOn()) switchOff();} // Turned back on after the limit, from the app, a schedule or a switch: off it goes.function onStatus(ev) { if (ev.component !== "switch:" + CONFIG.switchId) return; if (!limitHit || ev.delta.output !== true) return; print("Today's allowance is used, so switching it back off."); switchOff();} function start() { Timer.set(TICK_SEC * 1000, true, tick); Shelly.addStatusHandler(onStatus); print("Daily run limit started. " + CONFIG.maxHours + " h a day, " + hours(day.onSec) + " h used so far today."); tick();} function onLoaded(res, err) { if (err === 0 && res && typeof res.value === "string") { try { let saved = JSON.parse(res.value); if (typeof saved.date === "number") day = saved; } catch (e) { print("Couldn't read the saved count. Starting from zero."); } } start();} Shelly.call("KVS.Get", { key: KVS_KEY }, onLoaded); What it does
Every 10 seconds the script checks whether the relay is on and adds the time to today's total. It doesn't matter how it came on: the app, a schedule, a wall switch or a voice assistant all count. When the total reaches your limit, the relay switches off, and if anyone turns it back on before midnight it switches straight off again. At midnight the console prints yesterday's total and the allowance starts again. Use it to stop a bathroom heater or a towel rail running all day, or to keep a pump to its hours whatever the schedule says.
The count is saved on the Shelly every five minutes and when the limit is reached, so a restart carries on from where it was. Until the Shelly's clock has synced, the script doesn't count or switch anything, because it can't tell when the day started.
Install it on your Shelly
Before you install
- Check the device's clock
In the Shelly app, open the relay's settings and check its time zone is your own. The allowance starts again at midnight on the Shelly's clock.
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 daily-run-limit. 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 daily-run-limit. 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 maxHours to 0.01, which is 36 seconds, save it and run it again. Turn the relay on. Within a minute it switches off and the console prints Used today's 0.01 h. Turn it on again and it goes straight back off. Set maxHours back afterwards and restart the script.
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