Fix resetting the timer also when the status hasn't changed

This commit is contained in:
2026-01-12 08:32:33 -06:00
parent 7a493759ba
commit c382ae2888

View File

@@ -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. // 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..... // I set it to 255 and "below" - this triggered it for me.....
//send_mqtt("occupancy_state_debug", currentState); //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) { if (currentState != previousState) {
Serial.print("Status changed; currentState: "); Serial.print("Status changed; currentState: ");
Serial.print(currentState); Serial.print(currentState);
@@ -119,14 +126,14 @@ void loop() {
if (currentState == HIGH) { if (currentState == HIGH) {
send_mqtt("motion"); send_mqtt("motion");
digitalWrite(LED_BUILTIN,HIGH); digitalWrite(LED_BUILTIN,HIGH);
previousMotionMillis=millis();
} else { } else {
send_mqtt("no_motion_ignore"); send_mqtt("no_motion_immediate");
digitalWrite(LED_BUILTIN,LOW); digitalWrite(LED_BUILTIN,LOW);
} }
previousState = currentState; previousState = currentState;
} }
// Send no_motion messages every minute, indicating how long we didn't see occupancy
const int secondsPerMinute=60; const int secondsPerMinute=60;
long minutesSinceLastMotion=(millis()-previousMotionMillis)/(millisPerSecond*secondsPerMinute); long minutesSinceLastMotion=(millis()-previousMotionMillis)/(millisPerSecond*secondsPerMinute);
if ((previousNoMoNotifyMinutes != minutesSinceLastMotion) && (0 != minutesSinceLastMotion)) { if ((previousNoMoNotifyMinutes != minutesSinceLastMotion) && (0 != minutesSinceLastMotion)) {
@@ -134,5 +141,5 @@ void loop() {
previousNoMoNotifyMinutes = minutesSinceLastMotion; previousNoMoNotifyMinutes = minutesSinceLastMotion;
} }
delay(100); delay(50);
} }