hey,
in your settings.json
, add the following:
contextStorage: {
storeInFile: { module: "localfilesystem"}
},
This will make the global.context persistent.
Description of persistent storage can be found here.
Then the total amount of power will be stored between node-red's restarts.
Then you just have to modify your code to detect when Shelly is reset:
[EDIT]
let storedTotalAmount = global.get("totalAmount", "storeInFile") || 0;
const previousShellyValue = global.get("previousShellyValue", "storeInFile") || 0
// first run: init value in persistent storage
if (storedTotalAmount <= 0) global.set("totalAmount", 0, "storeInFile");
if (previousShellyValue <= 0) global.set("previousShellyValue", 0, "storeInFile");
if (msg.payload < previousShellyValue) {
// here we detect a reset of Shelly system
storedTotalAmount = storedTotalAmount + msg.payload;
}else{
// note that if there's no change in Shelly's payload, the difference will be 0
storedTotalAmount = storedTotalAmount + msg.payload - previousShellyValue;
}
global.set("totalAmount", storedTotalAmount, "storeInFile");
global.set("previousShellyValue", msg.payload, "storeInFile");
msg.payload = storedTotalAmount;
return msg;
Correct me if I'm wrong (and of course, I was, at first edit )
Hope it helps