Activate output with a variable time depending on output-off-duration

  • Hello everyone,

    I would like to set the output of my "Shelly Plus 1 PM Mini" ON when the input is ON depending on the previous Output-OFF-duration. To make it clearer:

    I have a circulation pump: if it has been running, let's say, 2 minutes ago it should only run for 10 seconds, because everything is stationary already.

    If it has been running, let's say, 10 hours ago, it should run for 5 minutes, and so on...

    So I have the following newbie questions:

    1) How can I measure time?

    2) Can I handle output seperately if triggered by input and triggered by Web Interface (it should always run for 5 Minutes via App interface).

    I am happy for any help - this will be my first script, so please be patient with me :)

  • shelldownmaster 2. November 2023 um 14:57

    Hat den Titel des Themas von „Activate output with a variable time depending on input-off-duration“ zu „Activate output with a variable time depending on output-off-duration“ geändert.
  • Hi, @shelldownmaster

    So you want a timestamp-based switch-on, this is possible. However, since Shellys local time is not really reliable, this approach is not ideal.

    It would be easier and more reliable to simply run your pump every X hours, no script required, just create a scheduled job.

    Answer:

    1. with a timer, event timestamps, or a date object

    2. yes

    Shelly Script Language Features | Shelly Technical Documentation
    Since version 0.9.0
    shelly-api-docs.shelly.cloud
    JavaScript Date Reference
    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript,…
    www.w3schools.com
    What is a timestamp?
    Timestamps show when something happened, such as when information was changed on a computer. Learn more about timestamps, what they look like and their uses.
    www.techtarget.com
    Tutorial | Shelly Technical Documentation
    Since version 0.9.0
    shelly-api-docs.shelly.cloud

    6 Mal editiert, zuletzt von _[Deleted]_ (2. November 2023 um 15:56)

  • What do you mean by "local time is not really reliable"? If it is in the order of some seconds/minutes, I do not really mind.

    Simply letting the circulation pump run every few hours is not really efficient, since I have a demand-controlled activation.

    So this is my first approach - not the very best coding (magic numbers, many if/else cases, ...), but it seems to work as intended:

    You might review, if you like. I am happy for any comments, ...


    Moreover, I have one basic question:

    The Shelly is now configured as detached switch. I activated the script via the Slider on the "Scripts"-page. Does that mean, that the script automatically restarts after a power failure/firmware upgrade/... ?

    If no, how do I ensure this?

  • Yah if you switch the slider to "on," the script will restart when the device boots up. Bear in mind that if errors occur, sometimes for no reason, it will stop your script.

    To avoid unexpected stops after a reboot, it's wise to delay your primary code by a few seconds. And try using some try{yourCode;}catch(e){print(e);} Blocks, to catch some errors before they can stop your script.

    try...catch - JavaScript | MDN
    The try...catch statement is comprised of a try block and either a catch block, a finally block, or both. The code in the try block is executed first, and if…
    developer.mozilla.org

    A Backup plan, to keep your script running, could be to set up a scheduled job that will attempt to start your script every x hours, or a second script thats monitor your first script. ;)

    _[Deleted]_
    16. Oktober 2023 um 21:23

    If the Shelly is disconnected from the power supply, the local time is no longer running, and any kind of load shifts the clock by a few milliseconds, so without a timeserver or online access, sooner or later there will be deviations, sometimes the Shellys even lose their time completely for no reason, usually when they try to connect to a timeserver which does not respond.

    Also, Shellys sometimes restart for no reason. Every time the Shelly reboots, your script loses the last off time.

    To be honest, this can happen so often that I think it would be better to just let the pump run every x hours and to reset this timer when your switch is triggerd. But well, I guess everyone has their own priorities.

    4 Mal editiert, zuletzt von _[Deleted]_ (2. November 2023 um 20:21)

  • Ahh, and here is some code demonstrating how to identify various trigger.

    2 Mal editiert, zuletzt von _[Deleted]_ (2. November 2023 um 20:55)