Shelly 4 PRO PM script

  • Hi All,

    I am writing a script for the Shelly 4 PRO PM that is expected to collect status of each single switch and sum the voltage of the four switch.

    When I try to get the status of a switch I noticed that:
    a) the javascript steps are executed in a sequence I didn't expect (I expected steps 1, 2, 3 while they are executed as 1, 3, 2);

    b) the status of the Shelly is only available within the scope of the callback function, and cannot be assigned to a binding outside the environment of the function, while I expected that the callback to be able to access the global scope. In other words: the parameter r cannot be assigned to r2.

    Can someone explain the reason of the two issues? How can I use the result of the callback outside of the Shelly.call?

    Please find below the code and the colole output.


    Thanks

    R

    ------------------------------------------------

    let r2 = {temp:0, volt: 0};

    let userdata2 = null;

    print("Step 1 - r2:", JSON.stringify(r2));

    let risultato = Shelly.call("Switch.GetStatus",

    {"id": 0},

    function (r, error_code, error_message, userdata1)

    {

    print("r:",JSON.stringify(r));

    r2.temp = r.temperature.tC;

    r2.volt = r.voltage;

    print("Step 2 - r2:",JSON.stringify(r2));

    return r;

    },

    userdata2);

    print("Step 3 - r2", JSON.stringify(r2));

    -----------------------------------------------

    CONSOLE OUTPUT:

    Step 1 - r2: {"volt":0,"temp":0} 17:04:39.523

    Step 3 - r2 {"volt":0,"temp":0} 17:04:39.534

    r: {"temperature":{"tF":84.600000,"tC":29.200000},"aenergy":{"minute_ts":0,"by_minute":[0.131000,0,0],"total":0},"pf":0,"current":0,"voltage":234.200000,"apower":0,"output":false,"source":"init","id":0} 17:04:39.616

    Step 2 - r2: {"volt":234.200000,"temp":29.200000}

  • Same behaviour, my console output is:

    ---

    Step 1 - r2: {"volt":0,"temp":0}18:01:45.660

    Step 3 - r2 {"volt":0,"temp":0}18:01:45.672

    r: {"temperature":{"tF":105.700000,"tC":41},"aenergy":{"minute_ts":1642438904,"by_minute":[592.156000,788.928000,789.454000],"total":742.088000},"pf":-0.470000,"current":0.317000,"voltage":231.300000,"apower":46.500000,"output":true,"source":"init","id":0}18:01:45.757

    Step 2 - r2: {"volt":231.300000,"temp":41}

    ---

    I unfortunately have zero experience with ShellyPro Scripting, but want to get a similar (albeit slightly different) result as you are looking for -- I want a way to "ask" via http for the power consumption sum of all four meters, returned ideally via a JSON object.

    But I don't really understand the syntax of the scripting and how I could then do a http request to get the summary from the device.. :-/ can you help, given that you're clearly ahead of me in regards to writing scripts?

  • Hi

    It seems you expect to have the total power consuption available through the Shelly 4 PRO PM web server. I fear this means extending the web server functionality and my understanding it is not possible at the moment.

    Maybe someone from Shelly could confirm.

  • This works:

    Shelly Script is asynchronous as generally JavaScript is. As the code is single threaded, the ability to provide delayed execution is achieved via callbacks, invoked when a synchronous (Timers) or asynchronous(RPC, HTTP calls) events occur.

    So in the provided example the printing of values outside of the callback happens before the callback is executed. If you need to use the results of the response call a function from within the callback.

    Thanks Mircho. Do you have an idea if my request (summing up power utilization across channels and accessing this via http / rpc request) is somehow doable via the onboard scripting?

  • so the approach should be to:

    a) use 4 Shelly.call to store each single switch power in the switchData strucure

    b) sum them in the time callback function and then print

    Correct?

  • Dieses Thema enthält 2 weitere Beiträge, die nur für registrierte Benutzer sichtbar sind.