Apply optimizations for state updates

Have been running into issues with states not being updated properly.
Apply several optimizations to help improve this.
This commit is contained in:
Joshua Boniface 2025-03-19 20:52:04 -04:00
parent 1e2e37671d
commit 3cd2e75c16

View File

@ -68,6 +68,8 @@ esp32:
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ: "240"
CONFIG_ESP32S3_DATA_CACHE_64KB: "y"
CONFIG_ESP32S3_DATA_CACHE_LINE_64B: "y"
CONFIG_SPIRAM_CACHE_WORKAROUND: "y"
CONFIG_OPTIMIZATION_LEVEL_RELEASE: "y"
globals:
- id: gas_resistance_ceiling
@ -105,6 +107,11 @@ globals:
restore_value: yes
initial_value: "0"
- id: last_api_connected_time
type: uint32_t
restore_value: no
initial_value: "0"
script:
- id: light_off
then:
@ -166,37 +173,38 @@ script:
int light_counts = (id(occupancy_detect_mode) & ( 1 << 0 )) >> 0;
// Determine our results
bool new_state = false;
if (pir_counts & radar_counts & light_counts) {
// Logical AND of pir & radar & light
ESP_LOGD("occupancy_detect_handler", "PIR & Radar & Light: %i", pir & radar & light);
id(supersensor_occupancy).publish_state(pir & radar & light);
new_state = pir & radar & light;
} else if (pir_counts & radar_counts) {
// Logical AND of pir & radar
ESP_LOGD("occupancy_detect_handler", "PIR & Radar: %i", pir & radar);
id(supersensor_occupancy).publish_state(pir & radar);
new_state = pir & radar;
} else if (pir_counts & light_counts) {
// Logical AND of pir & light
ESP_LOGD("occupancy_detect_handler", "PIR & Light: %i", pir & light);
id(supersensor_occupancy).publish_state(pir & light);
new_state = pir & light;
} else if (radar_counts & light_counts) {
// Logical AND of radar & light
ESP_LOGD("occupancy_detect_handler", "Radar & Light: %i", radar & light);
id(supersensor_occupancy).publish_state(radar & light);
new_state = radar & light;
} else if (pir_counts) {
// Only pir
ESP_LOGD("occupancy_detect_handler", "PIR: %i", pir);
id(supersensor_occupancy).publish_state(pir);
new_state = pir;
} else if (radar_counts) {
// Only radar
ESP_LOGD("occupancy_detect_handler", "Radar: %i", radar);
id(supersensor_occupancy).publish_state(radar);
new_state = radar;
} else if (light_counts) {
// Only light
ESP_LOGD("occupancy_detect_handler", "Light: %i", light);
id(supersensor_occupancy).publish_state(light);
} else {
ESP_LOGD("occupancy_detect_handler", "None");
id(supersensor_occupancy).publish_state(false);
new_state = light;
}
ESP_LOGD("occupancy_detect_handler", "New state: %s", new_state ? "true" : "false");
// Force update even if state hasn't changed
id(supersensor_occupancy).publish_state(new_state);
// Add a delayed re-publish to ensure state propagation
if (new_state) {
id(supersensor_occupancy).publish_state(new_state);
}
- id: occupancy_clear_handler
@ -260,6 +268,7 @@ logger:
baud_rate: 115200
api:
reboot_timeout: 15min
ota:
platform: esphome
@ -275,9 +284,10 @@ mdns:
wifi:
ap: {}
domain: ""
fast_connect: false
fast_connect: true
output_power: 8.5dB
reboot_timeout: 5min
reboot_timeout: 15min
power_save_mode: none
uart:
id: ld2410_uart
@ -316,6 +326,25 @@ interval:
- logger.log: "voice assistant not running; restarting"
- voice_assistant.start_continuous:
# Regular state reporting to HASS
- interval: 30s
then:
- lambda: |-
bool current_state = id(supersensor_occupancy).state;
ESP_LOGD("state_reporter", "Republishing occupancy state: %s", current_state ? "true" : "false");
id(supersensor_occupancy).publish_state(current_state);
# API watchdog every 5 minutes
- interval: 60s
then:
- lambda: |-
if (api::global_api_server->is_connected()) {
id(last_api_connected_time) = millis();
} else if (millis() - id(last_api_connected_time) > 300000) {
ESP_LOGE("api_watchdog", "API disconnected for too long, rebooting...");
App.safe_reboot();
}
# Add optional microWakeWord support (on-device wake word)
# Doesn't work well as of 2024-07-04 so leave disabled
#micro_wake_word: