Napojení Shelly zásuvky pres script, který si stahuje data přes API ze solaxu z lokánlí sitě! jde to upravit i na to aby to stahoval z cloudu …
let CONFIG = {
// adresa SOLAX dzindziku
endpoint: "http://192.168.1.125",
// seriove cislo SOLAX dzindziku
sn: "XXXXXXXXXXXXX",
// od kolika procent baterie ma zasuvka zapnout
batteryStart: 50,
// od kolika procent baterie ma zasuvka vypnout
batteryStop: 49,
//kolikrat se musi neuspesne pripojit na solax nez vypne zasuvku
numberOfFails: 3,
//cas jak dlouho se bude cekat na odpoved solaxu
httpTimeout: 10,
//jak castro se budou vycistat data ze slolaxu
pingTime: 60,
};
let endpointIdx = 0;
let failCounter = 0;
let pingTimer = null;
function pingEndpoints() {
print("-------------------------------");
Shelly.call(
"http.post",
{ url: CONFIG.endpoint, body: "optType=ReadRealTimeData&pwd=" + CONFIG.sn, timeout: CONFIG.httpTimeout },
function (response, error_code, error_message) {
//http timeout, magic number, not yet documented
if (error_code === -114 || error_code === -104) {
print("Failed to fetch ", CONFIG.endpoint);
failCounter++;
print("Rotating through endpoints");
} else {
let data_arr = JSON.parse(response["body"])["Data"];
let data_final = {
"Grid 1 Voltage": data_arr[0],
"Grid 2 Voltage": data_arr[1],
"Grid 3 Voltage": data_arr[2],
"Grid 1 Current": data_arr[3],
"Grid 2 Current": data_arr[4],
"Grid 3 Current": data_arr[5],
"Grid 1 Power": data_arr[6],
"Grid 2 Power": data_arr[7],
"Grid 3 Power": data_arr[8],
"PV1 Voltage": data_arr[10],
"PV2 Voltage": data_arr[11],
"PV1 Current": data_arr[12],
"PV2 Current": data_arr[13],
"PV1 Power": data_arr[14],
"PV2 Power": data_arr[15],
"Grid 1 Frequency": data_arr[16],
"Grid 2 Frequency": data_arr[17],
"Grid 3 Frequency": data_arr[18],
"Run mode": data_arr[19],
"Run mode text": data_arr[19],
"EPS 1 Voltage": data_arr[23],
"EPS 2 Voltage": data_arr[24],
"EPS 3 Voltage": data_arr[25],
"EPS 1 Current": data_arr[26],
"EPS 2 Current": data_arr[27],
"EPS 3 Current": data_arr[28],
"EPS 1 Power": data_arr[29],
"EPS 2 Power": data_arr[30],
"EPS 3 Power": data_arr[31],
"Battery Power": data_arr[41],
"Yield today": data_arr[70],
"Battery Remaining Capacity": data_arr[103],
"Battery Temperature": data_arr[105],
};
failCounter = 0;
if(data_final["Battery Remaining Capacity"] > CONFIG.batteryStart)
{
print("Battery Remaining Capacity > ", CONFIG.batteryStart, " [aktualne:", data_final["Battery Remaining Capacity"], "]");
Shelly.call(
"Switch.Set",
{ id: 0, on: true },
function () {print("SW on");}
);
return;
}
if(data_final["Battery Remaining Capacity"] < CONFIG.batteryStop)
{
print("Battery Remaining Capacity < ", CONFIG.batteryStop, " [aktualne:", data_final["Battery Remaining Capacity"], "]");
Shelly.call(
"Switch.Set",
{ id: 0, on: false},
function () {print("SW off");}
);
return;
}
}
if (failCounter >= CONFIG.numberOfFails) {
print("Too many fails, resetting...");
failCounter = 0;
Timer.clear(pingTimer);
//set the output with toggling back
Shelly.call(
"Switch.Set",
{ id: 0, on: false, toggle_after: CONFIG.toggleTime },
function () {}
);
return;
}
}
);
}
print("Start watchdog timer");
pingEndpoints();
pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints);
Shelly.addStatusHandler(function (status) {
//is the component a switch
if(status.name !== "switch") return;
//is it the one with id 0
if(status.id !== 0) return;
//does it have a delta.source property
if(typeof status.delta.source === "undefined") return;
//is the source a timer
if(status.delta.source !== "timer") return;
//is it turned on
if(status.delta.output !== true) return;
//start the loop to ping the endpoints again
//print("handler");
//pingTimer = Timer.set(CONFIG.pingTime * 1000, true, pingEndpoints);
});