Jump to content
Gen2 Devices: FW Update Required / Gen2 Geräte: FW-Update erforderlich ×

Help Needed with Shelly Script to Automatically Open Door When Doorbell is Pressed for 10 Seconds


Recommended Posts


Hi everyone,

I'm working on an IoT home automation project where I have a Shelly 1 Plus device connected to my doorbell. The goal is to automatically open the door if the doorbell is pressed continuously for 10 seconds.

I've written the following script, but I'm encountering some issues:

  1. The script seems to run and log events even when the doorbell is not pressed.
  2. I want to ensure the door opens after the doorbell has been pressed for 10 seconds and closes 1 second later.
     
let buttonPressed = false;
let buttonPressedTime = 0;
let timer_handle = null;
let thresholdTime = 10000; // 10 seconds in milliseconds

// Function to handle doorbell press event
function handleDoorbellPress(state) {
  print("handleDoorbellPress called with state: " + state); // Debug log
  if (state === "pressed") {
    if (!buttonPressed) {
      buttonPressed = true;
      buttonPressedTime = Date.now();
      print("Doorbell pressed, starting timer...");
      // Start a timer that checks every second
      timer_handle = Timer.setInterval(1000, function() {
        let elapsedTime = Date.now() - buttonPressedTime;
        print("Elapsed time: " + elapsedTime + " ms");
        
        // If the doorbell has been pressed for at least 10 seconds
        if (elapsedTime >= thresholdTime) {
          print("10 second interval completed, opening the door...");
          // Perform the action to open the door
          Shelly.call("Switch.Set", { id: 0, on: true }, function (res, error_code, error_message) {
            if (error_code === 0) {
              print("Door opened successfully.");
            } else {
              print("Error opening the door: " + error_message);
            }
          });

          // Turn off the output after 1 second
          Timer.setTimeout(function() {
            print("Turning off the switch...");
            Shelly.call("Switch.Set", { id: 0, on: false });
          }, 1000);

          // Reset doorbell state and clear the timer
          Timer.clearInterval(timer_handle);
          buttonPressed = false;
        }
      });
    }
  } else if (state === "released") {
    if (buttonPressed) {
      buttonPressed = false;
      print("Doorbell released, reset...");
      Timer.clearInterval(timer_handle);
    }
  }
}

// Listen for changes in the doorbell input state
Shelly.addEventHandler(function(event, user_data) {
  print("Event detected: " + JSON.stringify(event)); // Debug log
  if (event.component === "input" && event.id === 0) { // Assuming input 0 is the doorbell
    if (event.info.state === true) { // Doorbell pressed
      handleDoorbellPress("pressed");
    } else if (event.info.state === false) { // Doorbell released
      handleDoorbellPress("released");
    }
  } else {
    print("Event ignored: " + JSON.stringify(event)); // Log for irrelevant events
  }
});

Logs I'm Seeing:
 

Event detected: {"component":"input:0","name":"input","id":0,"now":<timestamp>,"info":{"component":"input:0","id":0,"event":"toggle","state":true,"ts":<timestamp>}}
Event ignored: {"component":"input:0","name":"input","id":0,"now":<timestamp>,"info":{"component":"input:0","id":0,"event":"toggle","state":true,"ts":<timestamp>}}
Event detected: {"component":"input:0","name":"input","id":0,"now":<timestamp>,"info":{"component":"input:0","id":0,"event":"toggle","state":false,"ts":<timestamp>}}
Event ignored: {"component":"input:0","name":"input","id":0,"now":<timestamp>,"info":{"component":"input:0","id":0,"event":"toggle","state":false,"ts":<timestamp>}}

It seems like the doorbell press is being detected and logged, but I'm not sure why the action to open the door isn't consistently triggered. Additionally, the logs indicate events even when the doorbell isn't pressed.
Is my approach to handle the doorbell press and release states correctly implemented Are there any issues with how I'm using the Timer.setInterval and Timer.setTimeout functions?

Any insights or suggestions to improve the script and achieve the desired behavior would be greatly appreciated.

Thank you!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...