diff --git a/app/Models/Sensor.php b/app/Models/Sensor.php index 9926ceb393..0aba83c81f 100644 --- a/app/Models/Sensor.php +++ b/app/Models/Sensor.php @@ -99,7 +99,7 @@ class Sensor extends DeviceRelatedModel implements Keyable public function guessLimits(): void { - $this->sensor_limit = match ($this->sensor_class) { + $this->sensor_limit_low = match ($this->sensor_class) { 'temperature' => $this->sensor_current - 10, 'voltage' => $this->sensor_current * 0.85, 'humidity' => 30, @@ -110,7 +110,7 @@ class Sensor extends DeviceRelatedModel implements Keyable default => null, }; - $this->sensor_limit_low = match ($this->sensor_class) { + $this->sensor_limit = match ($this->sensor_class) { 'temperature' => $this->sensor_current + 20, 'voltage' => $this->sensor_current * 1.15, 'humidity' => 70, diff --git a/app/Observers/SensorObserver.php b/app/Observers/SensorObserver.php index ea31c2188d..43504d45bb 100644 --- a/app/Observers/SensorObserver.php +++ b/app/Observers/SensorObserver.php @@ -5,6 +5,7 @@ namespace App\Observers; use App\Models\Eventlog; use App\Models\Sensor; use Illuminate\Foundation\Application; +use Illuminate\Support\Facades\Log; use LibreNMS\Enum\Severity; class SensorObserver @@ -20,6 +21,8 @@ class SensorObserver { // fix inverted limits if ($sensor->sensor_limit !== null && $sensor->sensor_limit_low !== null && $sensor->sensor_limit_low > $sensor->sensor_limit) { + Log::error('Fixing swapped sensor limits'); + // Fix high/low thresholds (i.e. on negative numbers) [$sensor->sensor_limit, $sensor->sensor_limit_low] = [$sensor->sensor_limit_low, $sensor->sensor_limit]; } @@ -29,6 +32,14 @@ class SensorObserver } } + public function creating(Sensor $sensor): void + { + $guess_limits = \LibreNMS\Config::get('sensors.guess_limits', true); + if ($guess_limits && $sensor->sensor_current !== null && $sensor->sensor_limit === null && $sensor->sensor_limit_low === null) { + $sensor->guessLimits(); + } + } + /** * Handle the service "created" event. * @@ -37,11 +48,6 @@ class SensorObserver */ public function created(Sensor $sensor): void { - $guess_limits = \LibreNMS\Config::get('sensors.guess_limits', true); - if ($guess_limits && $sensor->sensor_current !== null && $sensor->sensor_limit === null && $sensor->sensor_limit_low === null) { - $sensor->guessLimits(); - } - EventLog::log('Sensor Added: ' . $sensor->sensor_class . ' ' . $sensor->sensor_type . ' ' . $sensor->sensor_index . ' ' . $sensor->sensor_descr, $sensor->device_id, 'sensor', Severity::Notice, $sensor->sensor_id); if ($this->runningInConsole) {