# Storing energy data

**URL:** <https://discourse.nodered.org/t/storing-energy-data/14203>\
**Category:** General\
**Created:** [10 August 2019 20:14 UTC](https://discourse.nodered.org/t/storing-energy-data/14203 "2019-08-10T20:14:26Z")\
**Posts on this page:** 1\
**Showing post:** 33

<div class="post-metadata">

**Author:** ![jledun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jledun/32/10912_2.png) [@jledun](https://discourse.nodered.org/u/jledun)\
**Post date:** [14 August 2019 14:44 UTC](https://discourse.nodered.org/t/storing-energy-data/14203/33 "2019-08-14T14:44:24Z")

</div>

hey,

in your `settings.json`, add the following:

```auto
contextStorage: {
  storeInFile: { module: "localfilesystem"}
},

```

This will make the global.context persistent.  
Description of persistent storage can be found [here](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115).

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]

```auto
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 😉

---

_[View the full topic](https://discourse.nodered.org/t/storing-energy-data/14203)._
