From 50f6c2be568f54a02e77f60f0d319141b8eace20 Mon Sep 17 00:00:00 2001 From: Mrinal Kalakrishnan Date: Tue, 16 Jun 2026 01:42:29 -0700 Subject: [PATCH] =?UTF-8?q?Ignore=20out-of-range=20Nanit=20sensor=20readin?= =?UTF-8?q?gs=20(-1000=20=C2=B0C=20sentinel)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Nanit feed occasionally reports ValueMilli=-1000000 (-1000 °C) as an invalid-reading sentinel. processSensorData stored it without any bounds check, so the bridge published -1000 to babies//temperature. In Home Assistant this shows as -1768 °F, corrupting history and — for setups where a thermostat reads the sensor — risking a spurious HVAC call. Validate at ingestion: accept temperature within -20..60 °C and humidity within 0..100 %, otherwise log a warning and drop the reading so the prior value is retained. Range-based so it is robust to whatever firmware condition produces the sentinel. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/app/websocket_handlers.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/app/websocket_handlers.go b/pkg/app/websocket_handlers.go index eba4039..c3b0d09 100644 --- a/pkg/app/websocket_handlers.go +++ b/pkg/app/websocket_handlers.go @@ -14,10 +14,21 @@ func processSensorData(babyUID string, sensorData []*client.SensorData, stateMan stateUpdate := baby.State{} for _, sensorDataSet := range sensorData { if *sensorDataSet.SensorType == client.SensorType_TEMPERATURE { - stateUpdate.SetTemperatureMilli(*sensorDataSet.ValueMilli) + // Nanit occasionally reports -1000000 (-1000 °C) as an invalid-reading + // sentinel. Ignore physically impossible values (valid room range + // -20..60 °C) so we don't publish garbage to MQTT. + if v := *sensorDataSet.ValueMilli; v > -20000 && v < 60000 { + stateUpdate.SetTemperatureMilli(v) + } else { + log.Warn().Int32("valueMilli", v).Str("babyUID", babyUID).Msg("Ignoring out-of-range temperature reading") + } } if *sensorDataSet.SensorType == client.SensorType_HUMIDITY { - stateUpdate.SetHumidityMilli(*sensorDataSet.ValueMilli) + if v := *sensorDataSet.ValueMilli; v >= 0 && v <= 100000 { + stateUpdate.SetHumidityMilli(v) + } else { + log.Warn().Int32("valueMilli", v).Str("babyUID", babyUID).Msg("Ignoring out-of-range humidity reading") + } } if *sensorDataSet.SensorType == client.SensorType_NIGHT { stateUpdate.SetIsNight(*sensorDataSet.Value == 1)