-
Autor
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}