• # Strange delta calculations

Hey hey. This is something which has been driving me insane lately, I've tried all sorts of different things and it always seems incorrect.

I have a Mango ES which is polling a device over MODBUS every 10 seconds. It reads an accumulated pulse count from this device. I log the accumulated pulse count every 5 minutes. I have a meta point which then is attempting to calculate the delta in the last 5 mins. This delta calculation has been triggered on both Cron and on logged event, both yield the same thing. The deltas which the mango calculates range from bang on to way off and I can't figure out why.

Included is a spreadsheet, 6 hours of data from my mango. The column "Mango Delta" is the delta which the mango has calculated. The column "Mango Raw Value" is the MODBUS point raw value which I am reading from the mango with its associated timestamp. The column "Excel Delta" is just "cell2 - cell1" done in Excel. I have another system which I am sending the raw data to which confirms for me that the deltas I get from Mango are whacked.

Also included is my meta point script. "pulse" is the raw pulse value from the MODBUS device, pulsefactor is not used in this script and the entire thing is triggered every 5 mins based on CRON.

``````// Calculate the interval value since last logging (5 mins)
// This script should only trigger when pulse is logged, which is set to 5 min
// Pulsefactor is used to calculate the actual consumed amount
// where pulsefactor is the meters pulse weight

// Local variables section
// Control what parts of this script run
weight = false;     // If true this script will apply pulse rate
interval = true;   // If true this script will calculate interval data
hydro = false;      // If true this is a calculation for electricity

// To protect against calculating zeros, check the values of the factor variables
if(weight) {
if(pulsefactor.value <= 0) {
pfactor = 1;
tfactor = 1;
} else {
pfactor = pulsefactor.value;
if(hydro){
tfactor = 12;
} else {
tfactor = 1;
}
}
} else {
pfactor = 1;
tfactor = 1;
}

if (interval) {
// If a value requires compensation for rollover (ex 65535) enter here
rollover = 0;

// If the value rolled over and the rollover value was entered then calculate
// the interval data using the rollover value
if((pulse.past(MINUTE, 5).minimumValue > pulse.value) && (rollover > 0)) {
return((rollover - pulse.past(MINUTE, 5).minimumValue + pulse.value) * pfactor * tfactor);

// In the case that the rollover value was not specified but the value still
// rolled over just return the current reading (error present)
} else if((pulse.past(MINUTE, 5).minimumValue > pulse.value) && (rollover = 0)) {
return(pulse.value  * pfactor * tfactor);

// Normally we do this, which is to return the delta over the last 5 mins
} else {
return(pulse.past(MINUTE, 5).delta  * pfactor * tfactor);
}
} else {
return(pulse.value  * pfactor * tfactor);
}

``````

I don't know if there's a bug here or what the case may be but I just need some guidance on what is going on and whether this will work or not.

• Forgot to add that the values the mango is calculating don't even seem to make sense. If this is running every 5 minutes I would expect that it's either going to be correct or double what I expect. What's weird also is that it's wrong a few times in a row and then it's correct, then wrong, then correct again...

Posts 2Views 338