Successfully Creating a Blinking Script with Shelly 1 Plus

Hey everyone, I wanted to share my journey with Shelly 1 Plus and how I finally managed to create a script that works perfectly for my needs. I’m an electrician by trade, so diving into coding wasn’t my first choice, but I’m glad I gave it a shot!

For those who might be interested, I wanted my Shelly 1 Plus to blink for 30 seconds whenever I manually turned it on via the app. At first, I thought a simple if-else script would do the trick. But as many of you know, sometimes things aren’t as straightforward as they seem. I spent hours tweaking the script, only to run into errors every time. I even reached out to ChatGPT, but it seemed like I was stuck in a loop!

After some trial and error, I finally managed to get the script working. Here’s what I learned:

  1. Start Simple: Begin with a basic script to toggle the output. This helps you understand the flow before adding complexities like timers.
  2. Use Intervals Wisely: I realized that using setInterval was the key to creating the blinking effect. By toggling the output at regular intervals, I could achieve the desired blinking pattern.
  3. Test in Segments: Instead of writing the entire script at once, test each part individually. This makes it easier to identify and fix errors.
  4. Don’t Hesitate to Seek Help: The community here is amazing. When I finally posted my issue, I received some fantastic suggestions that led me to the solution.

Here’s the final script I used:
javascript
// Blinking Script for Shelly 1 Plus
let switchState = Shelly.getComponentValue(‘relay0’);

if (switchState === true) {
let blinkInterval = 500; // Blink every 0.5 seconds
let duration = 30000; // Total blinking duration (30 seconds)
let elapsed = 0;

let blink = Shelly.setInterval(() => {
    Shelly.call("Switch.Toggle", { id: 0 });
    elapsed += blinkInterval;

    if (elapsed >= duration) {
        Shelly.clearInterval(blink);
        Shelly.call("Switch.Set", { id: 0, on: true });
    }
}, blinkInterval);

}

This script does exactly what I wanted: it toggles the output every 0.5 seconds for 30 seconds and then keeps it on. It’s been working perfectly for me!

I want to give a huge shoutout to everyone who helped me troubleshoot. Your insights and patience were invaluable. If anyone else is struggling with similar scripts, don’t hesitate to reach out—I’m happy to help!

Happy scripting everyone! :rocket: