Shelly Logo Squared

Script Shelly scripts

Garage door that closes itself after you forget

Presses your opener's button from the Shelly app, and closes the door on its own when it has been left open too long. Tries once, then leaves it to you.

Updated v1.0

  • Gen2
  • Gen3
  • Gen4
garage-door-auto-close.js · 64 lines Download .js
// Garage door auto-close: closes the door when it has been open too long.// Needs a reed switch on an Add-on input to know when the door is shut. let CONFIG = {  autoCloseMin: 10,  reedInputId: 100,  openState: false,  travelSec: 30,  pulseMs: 500,  switchId: 0,}; let closeTimer = null;// Set when an auto-close didn't shut the door. The script won't try again// until someone closes it by hand, so an obstruction can't cause a loop.let gaveUp = false; function isOpen() {  let s = Shelly.getComponentStatus("input", CONFIG.reedInputId);  return s !== null && s.state === CONFIG.openState;} function releaseButton() {  Shelly.call("Switch.Set", { id: CONFIG.switchId, on: false });} function pressButton() {  Shelly.call("Switch.Set", { id: CONFIG.switchId, on: true });  Timer.set(CONFIG.pulseMs, false, releaseButton);} function checkShut() {  if (!isOpen()) return;  gaveUp = true;  print("Door is still open after auto-close. Check for something in the way.");} function autoClose() {  closeTimer = null;  if (!isOpen() || gaveUp) return;  print("Open for " + CONFIG.autoCloseMin + " minutes. Closing.");  pressButton();  Timer.set(CONFIG.travelSec * 1000, false, checkShut);} function watchDoor() {  if (closeTimer !== null) {    Timer.clear(closeTimer);    closeTimer = null;  }  if (!isOpen()) {    gaveUp = false;    return;  }  if (!gaveUp) closeTimer = Timer.set(CONFIG.autoCloseMin * 60000, false, autoClose);} function onStatus(ev) {  if (ev.component === "input:" + CONFIG.reedInputId && ev.delta.state !== undefined) watchDoor();} Shelly.addStatusHandler(onStatus);watchDoor();print("Garage door auto-close started.");

What it does

The relay is wired across your opener's push-button terminals, so a tap in the Shelly app works like pressing the button on the wall. A reed switch on the door tells the script whether it's shut. When the door opens, the script starts a timer, and if the door is still open when it runs out, it presses the button to close it.

It then waits for the door to finish moving and checks the reed switch. If the door didn't shut, because the safety beam stopped it or something is in the way, the script stops trying and prints a warning in the console. It won't press the button again until the door has been closed by hand.

Install it on your Shelly

View the full install guide

Before you install

  1. Set the relay to act like a button

    In the Shelly app, open the relay's settings and turn on Auto off after 0.5 seconds. A tap in the app then presses the opener's button once, the same as the script does.

  2. Add the reed switch

    Open the device's web page (find the IP address in the Shelly app under Device settings, then Device information). Under Add-on, choose Sensor Add-on and save, which restarts the device. Then add a digital input. It shows up as input 100.

Install the script

Shelly Smart Control

  1. 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.

  2. Tap Create new script

    If the device has no scripts yet, the screen says No scripts created.

  3. Name it, paste the code, then tap Save

    Name it garage-door-auto-close. Tap in the code area, paste the script you copied above, then tap Save (the disk icon at the top right).

  4. 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.

  5. 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

  1. 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.

  2. Click Create script

    If the device has no scripts yet, the list says so.

  3. Name it, paste the code, then click Save

    Name it garage-door-auto-close. Click in the code area, paste the script you copied above, then click Save (the disk icon above the code).

  4. 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.

  5. 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

  1. Test it

    Open the door, wait out your auto-close time and watch it close. On the device's web page, the console prints each step.

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