Totalizer difference example

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.

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.

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

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

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

I put them right after the original lines.

    // 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" }
    },

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

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

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

image

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.

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:

// 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!

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

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") {

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!

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.

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

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