Skip to content

Instantly share code, notes, and snippets.

@RoyBellingan
Created February 12, 2025 14:53
Show Gist options
  • Save RoyBellingan/370a0f341584df5ffdfcf917a003a6d1 to your computer and use it in GitHub Desktop.
Save RoyBellingan/370a0f341584df5ffdfcf917a003a6d1 to your computer and use it in GitHub Desktop.
empro.js
var lastAenergy = 0;
var minPower = 0;
var maxPower = 0;
function loop(){
const devInfo = Shelly.getDeviceInfo();
const mac = devInfo.mac;
const name = devInfo.name
Shelly.call("Shelly.GetStatus", {}, function(result, error_code, error_message) {
if (error_code === 0) {
// Extract the relevant power status information
// Adjust the path based on your specific Shelly device model
//print(result);
let status = result["em1:0"];
//print(result);
let aenergy = 0;
let curEnergy = 0; //status.aenergy.total;
//if device was restarted
if(lastAenergy == 0){
//we will skip this increment
aenergy = 0;
}else{
aenergy = curEnergy - lastAenergy;
}
lastAenergy = curEnergy;
var powerStatus = status.act_power;
maxPower = Math.max(maxPower,powerStatus);
minPower = Math.min(minPower,powerStatus);
var voltage = status.voltage;
var current = status.current;
var temp = 0; //status.temperature.tC;
var statusJson = {
type: "SmartPlug",
decoder: "shellyV1",
name: name,
serial: mac,
power: powerStatus,
voltage: voltage,
current: current,
energy: aenergy,
minPower: minPower,
maxPower: maxPower,
temperature: temp
};
//set current value, this is a form of reset, as putting 0 will make invalid the min
minPower = powerStatus;
maxPower = powerStatus;
let pay = JSON.stringify(statusJson);
//Shelly.call("HTTP.POST", {"url": "http://g1.carige.xyz/s2s.php", "body": pay, "timeout": 5});
Shelly.call("HTTP.POST", {"url": "http://ds.seisho.us/reading/V1", "body": pay, "timeout": 5});
// Print the power status to the console log
//print(statusJson);
} else {
// Print the error message to the console log
print("Error fetching status: " + error_message);
}
});
}
function sampler(){
Shelly.call("Shelly.GetStatus", {}, function(result, error_code, error_message) {
if (error_code != 0) {
return;
}
let status = result["em1:0"];
var power = status.act_power;
maxPower = Math.max(maxPower,power);
minPower = Math.min(minPower,power);
});
}
function init(){
Shelly.call("Shelly.GetStatus", {}, function(result, error_code, error_message) {
if (error_code != 0) {
return;
}
let status = result["em1:0"];
print(status);
var power = status.act_power;
maxPower = power;
minPower = power;
});
}
init();
loop();
Timer.set(1000, true, sampler);
Timer.set(60 * 1 * 1000, true, loop);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment