Jump to content
New translation feature for the Shelly Forum ×

Timer with ring-color an shelly plug s


FigUhr

Recommended Posts

Hello folks!

I have created a script that changes the color of the ring depending on the remaining duration of a timer.

When the plug is switched on, a timer starts that switches the plug off again after three minutes. This can be easily set in the app. Now I would like to use a script to be able to see how long the timer is still running based on the color of the ring.

Unfortunately, the script does not start automatically when the plug is switched on or it is faulty. The log does not show any errors.

// Funktion zum Setzen der Leuchtring-Farbe
function setRingColor(rgb, brightness) {
    Shelly.call(
        "PLUGS_UI.SetConfig",
        { id: 0, config: {"leds": {"mode": "switch", "colors": {"switch:0": {"on": {"rgb": rgb, "brightness": brightness}, "off": {"rgb": [0, 0, 0], "brightness": 0}}}, "power": {"brightness": 100}}}},
        function (result, code, msg, ud) {},
        null
    );
}

// Funktion zum Abfragen der Restzeit des Shelly-Timers
function getTimerRemainingTime() {
    let timerRest = 0;
    let sw = Shelly.getComponentStatus('Switch', 0);
    if (sw.timer_started_at) {
        timerRest = sw.timer_duration - ((Date.now() / 1000) - sw.timer_started_at);
    }
    return timerRest;
}

// Funktion zum Überprüfen und Steuern der Leuchtring-Farbe basierend auf der verbleibenden Zeit des Timers
function controlRingColor() {
    // Verbleibende Zeit des Timers in Sekunden abrufen
    var remainingTime = getTimerRemainingTime();

    // Farbe basierend auf verbleibender Zeit festlegen
    if (remainingTime > 120) {
        setRingColor([0, 100, 0], 100); // Grün: RGB-Wert [0, 100, 0], maximale Helligkeit
    } else if (remainingTime > 60) {
        setRingColor([0, 100, 50], 100); // Gelb: RGB-Wert [0, 100, 50], maximale Helligkeit
    } else if (remainingTime > 0) {
        setRingColor([100, 0, 0], 100); // Rot: RGB-Wert [100, 0, 0], maximale Helligkeit
    } else {
        // Timer ist abgelaufen oder wurde nicht gestartet
        // Leuchtring ausschalten
        setRingColor([0, 0, 0], 0); // Aus: RGB-Wert [0, 0, 0], minimale Helligkeit
    }
}

// Hauptprogramm
// Funktion controlRingColor aufrufen, wenn die Steckdose eingeschaltet wird
controlRingColor();

Perhaps someone can give me some food for thought as to what the problem might be?

Does the script perhaps have to be controlled or activated separately or starts with actions?

image.png.ff82227361beb16a8539727223a9625a.png

Link to comment
Share on other sites

5 hours ago, FigUhr said:

Hello folks!

I have created a script that changes the color of the ring depending on the remaining duration of a timer.

When the plug is switched on, a timer starts that switches the plug off again after three minutes. This can be easily set in the app. Now I would like to use a script to be able to see how long the timer is still running based on the color of the ring.

Unfortunately, the script does not start automatically when the plug is switched on or it is faulty. The log does not show any errors.

// Funktion zum Setzen der Leuchtring-Farbe
function setRingColor(rgb, brightness) {
    Shelly.call(
        "PLUGS_UI.SetConfig",
        { id: 0, config: {"leds": {"mode": "switch", "colors": {"switch:0": {"on": {"rgb": rgb, "brightness": brightness}, "off": {"rgb": [0, 0, 0], "brightness": 0}}}, "power": {"brightness": 100}}}},
        function (result, code, msg, ud) {},
        null
    );
}

// Funktion zum Abfragen der Restzeit des Shelly-Timers
function getTimerRemainingTime() {
    let timerRest = 0;
    let sw = Shelly.getComponentStatus('Switch', 0);
    if (sw.timer_started_at) {
        timerRest = sw.timer_duration - ((Date.now() / 1000) - sw.timer_started_at);
    }
    return timerRest;
}

// Funktion zum Überprüfen und Steuern der Leuchtring-Farbe basierend auf der verbleibenden Zeit des Timers
function controlRingColor() {
    // Verbleibende Zeit des Timers in Sekunden abrufen
    var remainingTime = getTimerRemainingTime();

    // Farbe basierend auf verbleibender Zeit festlegen
    if (remainingTime > 120) {
        setRingColor([0, 100, 0], 100); // Grün: RGB-Wert [0, 100, 0], maximale Helligkeit
    } else if (remainingTime > 60) {
        setRingColor([0, 100, 50], 100); // Gelb: RGB-Wert [0, 100, 50], maximale Helligkeit
    } else if (remainingTime > 0) {
        setRingColor([100, 0, 0], 100); // Rot: RGB-Wert [100, 0, 0], maximale Helligkeit
    } else {
        // Timer ist abgelaufen oder wurde nicht gestartet
        // Leuchtring ausschalten
        setRingColor([0, 0, 0], 0); // Aus: RGB-Wert [0, 0, 0], minimale Helligkeit
    }
}

// Hauptprogramm
// Funktion controlRingColor aufrufen, wenn die Steckdose eingeschaltet wird
controlRingColor();

Perhaps someone can give me some food for thought as to what the problem might be?

Does the script perhaps have to be controlled or activated separately or starts with actions?

image.png.ff82227361beb16a8539727223a9625a.png

Update your device to fw 1.30 and then try again. 

Link to comment
Share on other sites

Posted (edited)

This version works

// Sample + PLUG S by FU_Synch (No adjustments needed, recommend factory reset)
// Switch on = 18sec(green)
// After 12sec(yellow)
// After 6 sec(red)
// with "0" no light and switch Off
//
let remainingTime = 0;
///  
Shelly.addStatusHandler(function(e) {
    if (e.component === "switch:0") {
        if (e.delta.output === true) {
            remainingTime = 18; // Setze Abschaltzeit auf 180 Sekunden
            print("Switch is on, triggered source:", e.delta.source);
        } else if (e.delta.output === false) {
            remainingTime = 0;
            print("Switch is off, triggered source:", e.delta.source);
        }
    }
});
///
function timerOneSecond(user_data) {
    // Farbe basierend auf verbleibender Zeit festlegen
    if (remainingTime > 12) {
        setRingColor([0, 100, 0], 100); // Grün: RGB-Wert [0, 100, 0], maximale Helligkeit
    } else if (remainingTime > 6) {
        setRingColor([100, 100, 0], 100); // Gelb: RGB-Wert [100, 100, 0], maximale Helligkeit
    } else if (remainingTime > 0) {
        setRingColor([100, 0, 0], 100); // Rot: RGB-Wert [100, 0, 0], maximale Helligkeit
    } else {
        // Timer ist abgelaufen oder wurde nicht gestartet
        // Leuchtring ausschalten
        setRingColor([0, 0, 0], 0); // Aus: RGB-Wert [0, 0, 0], minimale Helligkeit       
        Shelly.call("Switch.set", {
            'id': 0,
            'on': false
        });
    }
    //
    if (remainingTime > 0) {
        remainingTime = remainingTime - 1;
    }
}
///
Timer.set(1000, true, timerOneSecond, null);
///
/// Funktion zum Setzen der Leuchtring-Farbe
function setRingColor(rgb, brightness) {
    Shelly.call(
        "PLUGS_UI.SetConfig", {
            id: 0,
            config: {
                "leds": {
                    "mode": "switch",
                    "colors": {
                        "switch:0": {
                            "on": {
                                "rgb": rgb,
                                "brightness": brightness
                            },
                            "off": {
                                "rgb": [0, 0, 0],
                                "brightness": 0
                            }
                        }
                    },
                    "power": {
                        "brightness": 100
                    }
                }
            }
        },
        function(result, code, msg, ud) {},
        null
    );
}

 

Many thanks to FU_Synch at https://smarthome-forum.eu/forum/thread/23144-led-ring-farbe-und-andere-einstellungen-des-plug-s-plus-mit-script-methode-setco/#post269888

Edited by FigUhr
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...