Script Shelly scripts
Pool pump that runs longer on hot days, from the weather forecast
Starts the pool pump at the time you choose and sets how long it runs from today's forecast top temperature. Uses the free Open-Meteo forecast, with no account.
- Gen2
- Gen3
- Gen4
- Pro
// Pool pump by the forecast: runs the pump longer on hot days and shorter on mild// ones, from a start time you choose. Uses the free Open-Meteo forecast, with no account. let CONFIG = { latitude: -37.81, longitude: 144.96, startAt: "10:00", minHours: 3, maxHours: 8, mildTemp: 20, hotTemp: 32, hoursIfNoForecast: 6, switchId: 0,}; // Check the clock every 20 seconds, so the run starts within the minute set.let CHECK_EVERY_SEC = 20;// If the forecast doesn't come back, ask once more two minutes later.let RETRY_SEC = 120; let lastRunDate = -1;let retried = false; // Local date (days since 1970) and "HH:MM", or null before the clock has synced.function localNow() { let s = Shelly.getComponentStatus("sys"); if (s === null || typeof s.unixtime !== "number" || s.time === null) return null; return { date: Math.floor((s.unixtime + (s.utc_offset || 0)) / 86400), time: s.time };} function forecastUrl() { return "https://api.open-meteo.com/v1/forecast?latitude=" + CONFIG.latitude + "&longitude=" + CONFIG.longitude + "&daily=temperature_2m_max&timezone=auto&forecast_days=1";} // Straight line from minHours at mildTemp to maxHours at hotTemp, to the// nearest quarter hour, never outside the two.function hoursFor(temp) { let share = (temp - CONFIG.mildTemp) / (CONFIG.hotTemp - CONFIG.mildTemp); if (share < 0) share = 0; if (share > 1) share = 1; let h = CONFIG.minHours + share * (CONFIG.maxHours - CONFIG.minHours); return Math.round(h * 4) / 4;} function run(hours, reason) { print("Running the pump for " + hours + " h. " + reason); // The relay switches itself off after the run, so the pump stops even if the // script stops or the Shelly loses Wi-Fi. Shelly.call("Switch.Set", { id: CONFIG.switchId, on: true, toggle_after: Math.round(hours * 3600), });} function noForecast(why) { if (!retried) { retried = true; print("Couldn't get the forecast (" + why + "). Trying again in 2 min."); Timer.set(RETRY_SEC * 1000, false, fetchForecast); return; } run(CONFIG.hoursIfNoForecast, "No forecast, so running the usual time.");} function onForecast(res, err) { if (err !== 0 || res.code !== 200) return noForecast("error " + (err !== 0 ? err : res.code)); let temp = null; try { temp = JSON.parse(res.body).daily.temperature_2m_max[0]; } catch (e) { return noForecast("unreadable reply"); } if (typeof temp !== "number") return noForecast("no temperature in the reply"); run(hoursFor(temp), "Forecast top of " + temp + " C today.");} function fetchForecast() { Shelly.call("HTTP.GET", { url: forecastUrl(), timeout: 15 }, onForecast);} function check() { let now = localNow(); // Without the clock it can't tell the time, so it does nothing. if (now === null) return; if (now.time !== CONFIG.startAt || now.date === lastRunDate) return; lastRunDate = now.date; retried = false; fetchForecast();} Timer.set(CHECK_EVERY_SEC * 1000, true, check);print("Pool pump by forecast started. Runs from " + CONFIG.startAt + " each day, " + CONFIG.minHours + " to " + CONFIG.maxHours + " h."); What it does
Each day at the start time, the script asks Open-Meteo for today's top temperature at your pool and works out how long to run. At the mild temperature or below it runs the shortest time, at the hot temperature or above the longest, and in between it scales evenly, to the nearest quarter hour. With the defaults, a 20 °C day gets 3 hours, a 26 °C day 5½ and a 32 °C day 8.
The relay is told to switch itself off after the run, so the pump stops even if the script stops or the Wi-Fi drops. If the forecast doesn't come back, the script asks again two minutes later, then runs for your usual hours. It does nothing until the Shelly's clock has synced, so a power cut can't start the pump at the wrong time. Check your pool builder's or pump maker's advice for the hours that suit your pool before you set the range.
Install it on your Shelly
Before you install
- Set the relay to stay off after a blackout
In the Shelly app, open the relay's settings and set its power-on default to Off, so a power cut doesn't start a run outside your hours.
- Find your pool's location
In Google Maps, right-click your pool and click the numbers at the top of the menu to copy them. The first is the latitude, the second the longitude.
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 pool-pump-forecast. 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 pool-pump-forecast. 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 startAt two minutes ahead, save it and run it again. On the device's web page, the console prints the forecast and how long the pump will run. Set the time 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