Jump to content
NOTICE / HINWEIS: iOS 18 Update and Today Widgets ×

accessing the power meter in PlugS scripts


stockholm

Recommended Posts

hi,
I want to stop charging my mobile phone when the charging current exceeds a threshold.

On the web interface's home screen, the plug shows power meter values when the phone is charged. How can I access those values from within the scripting interface to turn the device off?

Here is my attempt, and i get the message that the status can not be read
 

// Schwellwerte definieren
let powerThreshold = 5.0;      // Leistung in Watt
let currentThreshold = 0.025;  // Strom in Ampere
let checkInterval = 60;        // Überprüfungsintervall in Sekunden

// Funktion zur Überprüfung von Leistung und Strom
function checkPowerAndCurrent() {
    // Liste möglicher Komponentennamen für 'emeter'
    let emeterComponents = ["emeter", "emeter_0"];
    let emeterStatus = null;

    // Iteriere über mögliche Komponentennamen und versuche, den Status abzurufen
    for (let i = 0; i < emeterComponents.length; i++) {
        emeterStatus = Shelly.getComponentStatus(emeterComponents[i], 0);
        if (emeterStatus) {
            print("Verwendet 'emeter' Komponente: ", emeterComponents[i]);
            break;
        }
    }

    if (emeterStatus && typeof emeterStatus === 'object') {
        let currentPower = emeterStatus.power;
        let currentCurrent = emeterStatus.current;
        
        // Überprüfen, ob Leistung und Strom unter den Schwellwerten liegen
        if (currentPower < powerThreshold && currentCurrent < currentThreshold) {
            // Plug ausschalten
            Shelly.call("Switch.Set", { id: 0, on: false }, function (result) {
                if (result.code === 0) {
                    print("Plug wurde ausgeschaltet, da die Bedingungen erfüllt sind.");
                } else {
                    print("Fehler beim Ausschalten des Plugs: ", result.message);
                }
            });
        } else {
            print("Bedingungen nicht erfüllt. Aktuelle Leistung: ", currentPower, "W, Strom: ", currentCurrent, "A");
        }
    } else {
        print("Fehler: Konnte den Status des 'emeter' nicht abrufen.");

        // Zusätzliche Debugging-Informationen
        Shelly.call("Shelly.GetStatus", {}, function (status) {
            if (status && status.code === 0) {
                print("Gesamter Gerätestatus: ", JSON.stringify(status.data));
            } else {
                print("Fehler: Konnte den Gesamten Gerätestatus nicht abrufen.");
            }
        });
    }
}

// Timer setzen, um die Funktion in festgelegten Intervallen auszuführen
Timer.set(checkInterval * 1000, true, checkPowerAndCurrent);

As you can see, I have already tried to figure out the name myself, to no avail. Please help!

Link to comment
Share on other sites

Don't do : emeterStatus = Shelly.getComponentStatus(emeterComponents[i], 0);

But rather : Shelly.call("Switch.GetStatus",{id:0},monitor_energy);

With : function monitor_energy(result, error_code, error_message){
if(error_code === 0){

....etc

You will get  result.current (and other meters, see https://shelly-api-docs.shelly.cloud/gen2/ComponentsAndServices/Switch#status )

The current charge follow a curve. It increases a little bit at beginning of charge and then slowly decreases. You must find when it goes below a given value 3 minutes in a row (to avoid stopping the charge if a short drop in charge happens) which represents approximately 80% of charge of your phone.

NB : Android 15 will have a feature to automatically stop the charge at 80% 😉

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...