Skip to content

Commit

Permalink
Workaround for wobbly sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
killerbees19 committed Aug 9, 2022
1 parent 13ba7aa commit 92e32b3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
12 changes: 12 additions & 0 deletions config/sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ base = 0:BEBEBE
;; Can be overridden at sensor level!
;lowbatt = user@example.net

;; (OPTIONAL)
;; Use min/avg/max values from last 5 minutes.
;; Default value for this option is "latest"
;; Workaround for slippy and shaky sensors.
;; Can be overridden at sensor level!
;align = latest|min|avg|max


; Text between [] is an unique key for one sensor/measurement.
; It's impossible to combine two values from one wireless sensor
Expand Down Expand Up @@ -100,6 +107,11 @@ channel = 1
;; Disable or change recipient.
;LOWBATT =

;; (OPTIONAL)
;; Override global configuration.
;; Reset or change recalculation.
;ALIGN = latest

;; (OPTIONAL)
;; By default processing of groups will stop after the first match.
;; You can change this behavior for sensors with multiple values.
Expand Down
69 changes: 63 additions & 6 deletions ism-catcher
Original file line number Diff line number Diff line change
Expand Up @@ -380,17 +380,74 @@ abstract class ISM

if($db->getPacketCount())
{
$item = $db->getPacket(-1);
$time = time();
$align = null;

if($item[ISMDB::FIELD_TIME] < (intval(time() / self::$ini['interval']) - 1) * self::$ini['interval'])
if(isset(self::$ini[$section]['ALIGN']))
{
continue;
$align = strtolower(self::$ini[$section]['ALIGN']);
}
else if(isset(self::$ini['align']))
{
$align = strtolower(self::$ini['align']);
}

if($align !== null && in_array($align, ['min', 'avg', 'max'], true))
{
$values = [];
$c = -$db->getPacketCount();
for($i = -1; $i >= $c; $i--)
{
$item = $db->getPacket($i);

if($item[ISMDB::FIELD_TIME] < (intval($time / self::$ini['interval']) - 1) * self::$ini['interval'])
{
break;
}

$values[] = $item[ISMDB::FIELD_VALUE];
}

if(!($c = count($values)))
{
continue;
}

switch($align)
{
case 'min':
$value = min($values);
break;

self::outputLine('value', $item[ISMDB::FIELD_VALUE], $section);
case 'avg':
$value = array_sum($values) / $c;
break;

case 'max':
$value = max($values);
break;

default:
throw new InvalidArgumentException($align);
}
unset($values);
}
else
{
$item = $db->getPacket(-1);

if($item[ISMDB::FIELD_TIME] < (intval($time / self::$ini['interval']) - 1) * self::$ini['interval'])
{
continue;
}

$value = $item[ISMDB::FIELD_VALUE];

// TODO: Implement INI switch to output last update time?
#self::outputLine('extinfo', date('c', $item[ISMDB::FIELD_TIME]), $section);
}

// TODO: Implement INI switch to output last update time?
#self::outputLine('extinfo', date('c', $item[ISMDB::FIELD_TIME]), $section);
self::outputLine('value', $value, $section);

if($item[ISMDB::FIELD_FLAGS] & self::FLAG_LOWBATT)
{
Expand Down

0 comments on commit 92e32b3

Please sign in to comment.