From c382ae2888177cbbd9614feb980bafa33ce632b6 Mon Sep 17 00:00:00 2001 From: Joe Tretter Date: Mon, 12 Jan 2026 08:32:33 -0600 Subject: [PATCH] Fix resetting the timer also when the status hasn't changed --- src/main.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e128d89..f8175da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -111,6 +111,13 @@ void loop() { // This out pin triggering is dependent not only on movement, but also on light levels which can be configured in the app. // I set it to 255 and "below" - this triggered it for me..... //send_mqtt("occupancy_state_debug", currentState); + + // When occupancy is detected, reset the timer + if (currentState == HIGH) { + previousMotionMillis=millis(); + } + + // Send messages only on status change, we don't want to flood the network... if (currentState != previousState) { Serial.print("Status changed; currentState: "); Serial.print(currentState); @@ -119,14 +126,14 @@ void loop() { if (currentState == HIGH) { send_mqtt("motion"); digitalWrite(LED_BUILTIN,HIGH); - previousMotionMillis=millis(); } else { - send_mqtt("no_motion_ignore"); + send_mqtt("no_motion_immediate"); digitalWrite(LED_BUILTIN,LOW); } previousState = currentState; } + // Send no_motion messages every minute, indicating how long we didn't see occupancy const int secondsPerMinute=60; long minutesSinceLastMotion=(millis()-previousMotionMillis)/(millisPerSecond*secondsPerMinute); if ((previousNoMoNotifyMinutes != minutesSinceLastMotion) && (0 != minutesSinceLastMotion)) { @@ -134,5 +141,5 @@ void loop() { previousNoMoNotifyMinutes = minutesSinceLastMotion; } - delay(100); + delay(50); }