Input Processor Enhancement Request

Hi - I would like to propose an enhancement to the 'Total pulse count to pulse increment' input processor.

At the moment, if the total pulse count is less than the last value, the processor assumes that the count in the 'transmitter' has been reset to zero, and that the new total pulse count should be treated as the number of pulses that has been received by the transmitter since the reset.

However, I have another scenario:  if the transmitter has incorrectly received too many pulses, then it should be possible to reduce the total held by the transmitter to compensate for the error.  In my case, this could arise because I am counting pulses of a reed switch connected to a gas meter, and my 'debounce' hardware is not perfect.  I therefore need to reduce the total to match that of the gas meter - and I would then like the processor to generate a negative number of pulses.

I had thought of adding a parameter to the processor, but I could also have a (rare!) scenario where the gas meter is replaced and the total reset to zero. My proposal is therefore as follows:  The assumption is that if the transmitter has been reset, then the new total number of pulses sent will be a small percentage of the previous total.  Similarly, if the total has been reduced to adjust for an error, then the reduction will be a small percentage of the total. So my suggestion is that if the new value is more than half the previous value, then this is treated as a negative adjustment, otherwise it is treated as the first value after a reset.

Here is the code (from input_processor.php) that I am using:

public function pulse_diff($feedid,$time_now,$value)
{
    $value = $this->signed2unsigned(false,false, $value);
    if($value>0)
    {
        $pulse_diff = 0;
        $last = $this->feed->get_timevalue($feedid);
        $last['time'] = strtotime($last['time']);
        if ($last['time']) {
            // Need to handle resets of the pulse value (and negative 2**15?)
            if ($value >= $last['value']) {
                $pulse_diff = $value - $last['value'];
            } else {
                if ($value >= $last['value'] / 2)
                {
                    //Assume that this is a negative adjustment to the total number of pulses
                    $pulse_diff = $value - $last['value'];
                }
                else
                {
                    //Assume that the total number of pulses has been reset
                    $pulse_diff = $value;
                }
            }
        }
        // Save to allow next difference calc.
        $this->feed->insert_data($feedid,$time_now,$time_now,$value);
        return $pulse_diff;
    }
}
engeeaitch's picture

Re: Input Processor Enhancement Request

Hi - does anyone else think this is a good (or bad) idea?

Thanks,

Nick

Robert Wall's picture

Re: Input Processor Enhancement Request

The flaw I see in your algorithm is, what is the situation when the stored count is small? Any automatic adjustment like this is bound to be wrong in some circumstances, and I can't see an absolutely foolproof algorithm that will satisfy all use cases.

A "feature" like this MUST be carefully documented, else someone is bound to report the "bug" sooner or later.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.