Script Shelly scripts
Scene buttons that put the room back when you press again
Turns each button on a Shelly i4 into a scene for your other Shellys. One press sets the lights and relays, the next press puts them back exactly how they were.
- Gen2
- Gen3
- Gen4
- Pro
// Scene buttons with undo: each button on the device sets a scene on your other Shellys,// and the next press puts them back how they were. Needs inputs set to Button. let CONFIG = { button1: "Movie: 192.168.1.41 light 0 20, 192.168.1.42 switch 0 off", button2: "", button3: "", button4: "", longPressAllOff: true, fadeSec: 1,}; // Seconds to wait for a device before counting it as unreachable. A press with// a dead device in it still finishes the rest of the scene.let REPLY_SEC = 5; let scenes = [];let active = [];let saved = [];let work = [];let busy = false;let batch = { label: "", ok: 0, failed: 0 }; function onFetchedState(res, err, msg, step) { work.splice(0, 1); let t = scenes[step.n].targets[step.i]; if (err !== 0 || res.code !== 200) { print("Couldn't reach " + t.ip + "."); if (step.op === "read") saved[step.n][step.i] = null; else batch.failed++; } else if (step.op === "read") { let s = JSON.parse(res.body); saved[step.n][step.i] = { on: s.output, brightness: s.brightness }; } else { batch.ok++; } runOne();} function parseLine(text) { let words = text.trim().split(" "); let parts = []; for (let i = 0; i < words.length; i++) if (words[i] !== "") parts.push(words[i]); if (parts.length !== 4) return null; let type = parts[1]; if (type !== "switch" && type !== "light") return null; let t = { ip: parts[0], type: type, id: Number(parts[2]), on: true, brightness: null }; let v = parts[3]; if (v === "off") t.on = false; else if (v === "on") t.on = true; else if (type === "light" && Number(v) >= 1 && Number(v) <= 100) t.brightness = Number(v); else return null; return t;} function runOne() { if (work.length === 0) { busy = false; print(batch.label + ": " + batch.ok + " set" + (batch.failed > 0 ? ", " + batch.failed + " couldn't be reached." : ".")); return; } let step = work[0]; let t = scenes[step.n].targets[step.i]; let kind = t.type === "light" ? "Light" : "Switch"; let url = "http://" + t.ip + "/rpc/" + kind; if (step.op === "read") { url += ".GetStatus?id=" + t.id; } else { let want = targetWants(step, t); url += ".Set?id=" + t.id + "&on=" + (want.on ? "true" : "false"); if (t.type === "light" && want.on && typeof want.brightness === "number") { url += "&brightness=" + want.brightness + "&transition_duration=" + CONFIG.fadeSec; } } Shelly.call("HTTP.GET", { url: url, timeout: REPLY_SEC }, onFetchedState, step);} function targetWants(step, t) { if (step.op === "off") return { on: false, brightness: null }; if (step.op === "restore") return saved[step.n][step.i]; return { on: t.on, brightness: t.brightness };} function scenesNamed() { let lines = [CONFIG.button1, CONFIG.button2, CONFIG.button3, CONFIG.button4]; for (let n = 0; n < lines.length; n++) { let line = lines[n]; let name = "Button " + (n + 1); let colon = line.indexOf(":"); if (colon >= 0) { name = line.slice(0, colon).trim(); line = line.slice(colon + 1); } let targets = []; let items = line.trim() === "" ? [] : line.split(","); for (let i = 0; i < items.length; i++) { let t = parseLine(items[i]); if (t === null) print(name + ": couldn't read \"" + items[i].trim() + "\". Skipped."); else targets.push(t); } scenes.push({ name: name, targets: targets }); active.push(false); saved.push([]); if (targets.length > 0) print(name + " on button " + (n + 1) + ", " + targets.length + " devices."); }} function start(label) { busy = true; batch = { label: label, ok: 0, failed: 0 }; runOne();} function setScene(n) { let s = scenes[n]; // A new scene replaces the last one, so the older one's undo no longer applies. for (let k = 0; k < active.length; k++) active[k] = false; saved[n] = []; for (let i = 0; i < s.targets.length; i++) { saved[n].push(null); work.push({ n: n, i: i, op: "read" }); } for (let i = 0; i < s.targets.length; i++) work.push({ n: n, i: i, op: "set" }); active[n] = true; start(s.name + " on");} function undoScene(n) { for (let i = 0; i < scenes[n].targets.length; i++) { if (saved[n][i] !== null) work.push({ n: n, i: i, op: "restore" }); } active[n] = false; start(scenes[n].name + " undone");} function allOff() { let seen = {}; for (let n = 0; n < scenes.length; n++) { for (let i = 0; i < scenes[n].targets.length; i++) { let t = scenes[n].targets[i]; let key = t.ip + " " + t.type + " " + t.id; if (seen[key]) continue; seen[key] = true; work.push({ n: n, i: i, op: "off" }); } active[n] = false; } start("Everything off");} function onEvent_04cb(ev) { let c = ev.info.component; if (typeof c !== "string" || c.indexOf("input:") !== 0) return; let n = Number(c.slice(6)); if (n < 0 || n >= scenes.length) return; let e = ev.info.event; if (e !== "single_push" && e !== "long_push") return; if (busy) { print("Still working on the last press."); return; } if (e === "long_push") { if (CONFIG.longPressAllOff) allOff(); return; } if (scenes[n].targets.length === 0) return; if (active[n]) undoScene(n); else setScene(n);} scenesNamed();Shelly.addEventHandler(onEvent_04cb);print("Scene buttons started. Press a button to set its scene, press again to undo."); What it does
Each button runs a scene you write in one line: which lights and relays to set, and how. Before it changes anything, the script reads how each device is right now and remembers it. Press the same button again and every device goes back to that, including a dimmer's brightness. Hold any button to switch off everything named in any scene. Shelly's own button actions can switch other devices, but they can't remember how things were.
Only the latest scene can be undone. Pressing a different button starts a new scene and forgets the last one. If a device doesn't answer within 5 seconds, the script skips it and finishes the rest, then the console says how many it couldn't reach. A device it couldn't read first is left alone on undo, so it never gets set to a guess. A press while a scene is still being sent is ignored.
Install it on your Shelly
Before you install
- Set the inputs to Button
On the i4's web page, open each input you're using and set its type to Button. Remove any actions or app scenes on those buttons, or a press will do both.
- Write down your devices
For each light or relay in a scene, note its IP address and channel number. Channel numbers start at 0, so the second channel of a 2PM is 1.
Install the script
Install it on the i4.
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 scene-buttons-with-undo. 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 scene-buttons-with-undo. 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
Press a button. The console prints the scene's name and how many devices it set. Press it again and the lights go back to how they were. Hold a button to switch everything off.
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