# Totalizer difference example

**URL:** <https://discourse.nodered.org/t/totalizer-difference-example/40949>\
**Category:** General\
**Created:** [13 February 2021 22:34 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949 "2021-02-13T22:34:17Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [18 February 2021 21:10 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/21 "2021-02-18T21:10:25Z")

</div>

Probably a silly question, but do I have to restart NR for the contextStorage setting to take affect? I'm assuming so, but I changed the code and it didn't complain so I'm not sure if it's currently doing what I think it is.

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [18 February 2021 21:34 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/22 "2021-02-18T21:34:32Z")

</div>

Well after modifying the settings.js file and restarting NR, I found it wouldn't load. I don't know what I did wrong, but it definitely wasn't happy. I can provide the error log but all I did was add the lines I posted before and restarted the service.

```auto
    contextStorage: {
    	default : { module: "memory" },
		storeInFile: { module: "localfilesystem"},
		memoryOnly : { module: "memory" }
    },

```

In the flow, I changed "file" to "storeInFile".

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 February 2021 22:01 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/23 "2021-02-18T22:01:21Z")

</div>

> [@bkenobi](#):
>
> all I did was add the lines I posted

Did you put them in the right place? There should be commented out lines there already that you uncomment and modify accordingly.

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [18 February 2021 22:08 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/24 "2021-02-18T22:08:31Z")

</div>

I put them right after the original lines.

```auto
    // Context Storage
    // The following property can be used to enable context storage. The config$
    // provided here will enable file-based context that flushes to disk every $
    // Refer to the documentation for further options: https://nodered.org/docs$
    //
    //contextStorage: {
    // default: {
    // module:"localfilesystem"
    // },
    //},
    contextStorage: {
        default : { module: "memory" },
        storeInFile: { module: "localfilesystem"}
        memoryOnly : { module: "memory" }
    },

```

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [18 February 2021 22:13 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/25 "2021-02-18T22:13:45Z")

</div>

I saw a difference in formatting once I posted it here. I changed it slightly and it seems to load now.

```auto
   default: "memoryOnly",
   memoryOnly: { module: 'memory' },
   file: { module: 'localfilesystem' }
},

```

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [18 February 2021 22:16 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/26 "2021-02-18T22:16:44Z")

</div>

Ok, it looks like I got it now since it indicates "file" as in the context list.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/90c03d892ab65c97eefa6d5b29e79d260fda8390.png)

This does make me wonder if I should consider storing it with maybe 2 digits of precision though. I wonder if round off error will cause me issues in the future.

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [18 February 2021 23:04 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/27 "2021-02-18T23:04:26Z")

</div>

I decided to go back one step and instead of recording the calculated accumulated kWh, I am now recording the sensor input which is the current number of impulses since roll over. That way I have a nice integer to store and compare to which eliminates the issue entirely. My complete function is now:

```auto
// kWh = 0.001 * (accumulated pulses) * Kh
// impulses resets to 0 at 65535 so need to add logic to watch for this.
// Ideally the kWh measurment should be converted to a derivative (kW)
// and then the hourly or daily kWh would be stored

var Kh = 24.0;
var RollOver = 65535;
var Delta = 0;
var OldValue = context.get("OldReading","file");
if (OldValue===undefined) {
    OldValue = msg.payload;
}

// calculate accumulated kWh for debug
var kWhmsg = {};
kWhmsg.topic = "rtl_433/Blueline_PowerCost_Monitor/kWh";
kWhmsg.payload = 0.001 * (msg.payload) * Kh;

Delta = msg.payload - OldValue;

if (Delta < 0) Delta += RollOver;

context.set("OldReading", msg.payload,"file");

if (Delta > 0) {
    msg.payload = 0.001 * Delta * Kh;
    return [msg, kWhmsg];
} else {
    return [null, kWhmsg];
}

```

I opted to include a second output with the accumulated kWh value for now as a debug tool. I'll probably disable it when I'm happy with the overall flow but it seems like it could be helpful for now anyway.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 February 2021 00:17 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/28 "2021-02-19T00:17:13Z")

</div>

> [@bkenobi](#):
>
> ```auto
> contextStorage: {
> default : { module: "memory" },
> storeInFile: { module: "localfilesystem"}
> memoryOnly : { module: "memory" }
> },
> 
> ```

You were missing a comma at the end of the `storesInFile` line

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 February 2021 11:08 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/29 "2021-02-19T11:08:59Z")

</div>

> [@bkenobi](#):
>
> My complete function is now

That looks pretty good. A couple of points, I think that RollOver should be 65536 not 65535. Consider if subsequent messages contain 65535 and then 0. OldValue will be 65535 so Delta will be 0 - 65535 which is -65535. Rollover is then added to that, which gives 0 when it should be 1. Setting RollOver to 65536 will sort that.

A minor issue with  
`if (OldValue===undefined) {`  
That is generally considered not the best way to check for undefined as, unfortunately, the symbol `undefined` can actually be overwritten. The recommended way is to use  
`if (typeof OldValue === "undefined") {`

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [19 February 2021 15:56 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/30 "2021-02-19T15:56:23Z")

</div>

I was debating the rollover value and had gone back and forth several times. The documentation says the max value is 65535 since it's base 0. If the maximum is reached and then the next step is 1, I guess it would go to 0. As you say, if I set the rollover to 65535 the rollover step will lose 1 unit. I built a quick spreadsheet to confirm and that is correct. It also means my weather station that rolls over at 99.99 needs to have the rollover value set to 100.

I corrected the undefined line as well. Thanks for the help!

---

<div class="post-metadata">

**Author:** ![bkenobi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bkenobi/32/59825_2.png) [@bkenobi](https://discourse.nodered.org/u/bkenobi)\
**Post date:** [19 February 2021 16:01 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/31 "2021-02-19T16:01:32Z")

</div>

I could start a new thread, but just one quick question. I want to backup all of my flows in case I need to restore from scratch. I tried exporting all flows to a file, but when I restore that json file it seems to only restore a single flow rather than everything. Is there a way to restore the whole thing (replace existing flows if necessary) or restore parts of a backed up set of flows? I will start searching the docs, just wondering if there's a simple answer that is common knowledge.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 February 2021 16:13 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/32 "2021-02-19T16:13:25Z")

</div>

It is 65536 because is a 16 bit counter so the max value is 0xffff, which is 65535

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 April 2021 16:14 UTC](https://discourse.nodered.org/t/totalizer-difference-example/40949/33 "2021-04-20T16:14:07Z")

</div>

Closed on 20 April 2021 16:14 UTC

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

[Previous page](https://discourse.nodered.org/t/totalizer-difference-example/40949.md?page=1)
