Jump to content
🌟 NEW Shelly Products Reveal Video! 🌟 NEUE Shelly-Produkte-Enthüllungsvideo! 🌟 ×
NOTICE / HINWEIS: iOS 18 Update and Today Widgets ×

Script: Shelly power plug should turn off when the socket is using 700W for more than 10 minutes.


Recommended Posts

Can someone help me to write a script for my Shelly Plus Plug S?

I have a rainwater pump that recently had a leak and it kept on pumping while I was on vacation. When I arrived back home, my house was underwater. I bought the Shelly Plus Plug S to to turn the socket off after the pump is pumping for longer than 10 minutes so this can't happen again in the future.

I'm not an IT'er so I couldn't manage to write a script that works for my problem. Can someone help me please?

Best regards and thanks in advance!

Piet Santermans

Link to comment
Share on other sites

  • 2 weeks later...

let thresholdPower = 700; // Power threshold in W
let delayTime = 600000; // Delay time for turning off in milliseconds (10 minutes)
let timer = null; // Timer for delay before turning off

// Function to check power consumption
function checkPower() {
    // Call Shelly's local API to get the current power consumption
    Shelly.call("meter.get", {}, function (result) {
        let data = result;

        // Verify existence of "total_act_power"
        if (data && typeof data.total_act_power !== 'undefined') {
            let totalPower = data.total_act_power; // Total active power

            if (totalPower >= thresholdPower) {
                // If power consumption exceeds 700 W, start the delay timer
                if (timer === null) {
                    timer = Timer.set(delayTime, false, function () {
                        // Check again if power consumption is still above 700 W
                        Shelly.call("meter.get", {}, function (recheckResult) {
                            let recheckData = recheckResult;

                            if (recheckData && typeof recheckData.total_act_power !== 'undefined') {
                                let recheckPower = recheckData.total_act_power;

                                if (recheckPower >= thresholdPower) {
                                    // If power remains above 700 W, turn off the plug
                                    Shelly.call("switch.set", { id: 0, on: false });
                                    print("Power is above 700 W. Plug has been turned off.");
                                }
                            }

                            // Reset the timer
                            timer = null;
                        });
                    });
                }
            } else {
                // If power consumption drops below 700 W, cancel any pending timer
                if (timer !== null) {
                    Timer.clear(timer);
                    timer = null;
                    print("Power is below 700 W. Pending timer has been canceled.");
                }
            }
        } else {
            print("Meter data is unavailable or incorrect.");
        }
    });
}

// Run the check every 30 seconds continuously
Timer.set(30000, true, function () {
    checkPower();
});

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.

×
×
  • Erstelle neue...