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_DEFAULT_CPU_FREQ_MHZ: "240"
CONFIG_ESP32S3_DATA_CACHE_64KB: "y" CONFIG_ESP32S3_DATA_CACHE_64KB: "y"
CONFIG_ESP32S3_DATA_CACHE_LINE_64B: "y" CONFIG_ESP32S3_DATA_CACHE_LINE_64B: "y"
CONFIG_SPIRAM_CACHE_WORKAROUND: "y"
CONFIG_OPTIMIZATION_LEVEL_RELEASE: "y"
globals: globals:
- id: gas_resistance_ceiling - id: gas_resistance_ceiling
@ -105,6 +107,11 @@ globals:
restore_value: yes restore_value: yes
initial_value: "0" initial_value: "0"
- id: last_api_connected_time
type: uint32_t
restore_value: no
initial_value: "0"
script: script:
- id: light_off - id: light_off
then: then:
@ -166,37 +173,38 @@ script:
int light_counts = (id(occupancy_detect_mode) & ( 1 << 0 )) >> 0; int light_counts = (id(occupancy_detect_mode) & ( 1 << 0 )) >> 0;
// Determine our results // Determine our results
bool new_state = false;
if (pir_counts & radar_counts & light_counts) { if (pir_counts & radar_counts & light_counts) {
// Logical AND of pir & radar & light // Logical AND of pir & radar & light
ESP_LOGD("occupancy_detect_handler", "PIR & Radar & Light: %i", pir & radar & light); new_state = pir & radar & light;
id(supersensor_occupancy).publish_state(pir & radar & light);
} else if (pir_counts & radar_counts) { } else if (pir_counts & radar_counts) {
// Logical AND of pir & radar // Logical AND of pir & radar
ESP_LOGD("occupancy_detect_handler", "PIR & Radar: %i", pir & radar); new_state = pir & radar;
id(supersensor_occupancy).publish_state(pir & radar);
} else if (pir_counts & light_counts) { } else if (pir_counts & light_counts) {
// Logical AND of pir & light // Logical AND of pir & light
ESP_LOGD("occupancy_detect_handler", "PIR & Light: %i", pir & light); new_state = pir & light;
id(supersensor_occupancy).publish_state(pir & light);
} else if (radar_counts & light_counts) { } else if (radar_counts & light_counts) {
// Logical AND of radar & light // Logical AND of radar & light
ESP_LOGD("occupancy_detect_handler", "Radar & Light: %i", radar & light); new_state = radar & light;
id(supersensor_occupancy).publish_state(radar & light);
} else if (pir_counts) { } else if (pir_counts) {
// Only pir // Only pir
ESP_LOGD("occupancy_detect_handler", "PIR: %i", pir); new_state = pir;
id(supersensor_occupancy).publish_state(pir);
} else if (radar_counts) { } else if (radar_counts) {
// Only radar // Only radar
ESP_LOGD("occupancy_detect_handler", "Radar: %i", radar); new_state = radar;
id(supersensor_occupancy).publish_state(radar);
} else if (light_counts) { } else if (light_counts) {
// Only light // Only light
ESP_LOGD("occupancy_detect_handler", "Light: %i", light); new_state = light;
id(supersensor_occupancy).publish_state(light); }
} else {
ESP_LOGD("occupancy_detect_handler", "None"); ESP_LOGD("occupancy_detect_handler", "New state: %s", new_state ? "true" : "false");
id(supersensor_occupancy).publish_state(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 - id: occupancy_clear_handler
@ -260,6 +268,7 @@ logger:
baud_rate: 115200 baud_rate: 115200
api: api:
reboot_timeout: 15min
ota: ota:
platform: esphome platform: esphome
@ -275,9 +284,10 @@ mdns:
wifi: wifi:
ap: {} ap: {}
domain: "" domain: ""
fast_connect: false fast_connect: true
output_power: 8.5dB output_power: 8.5dB
reboot_timeout: 5min reboot_timeout: 15min
power_save_mode: none
uart: uart:
id: ld2410_uart id: ld2410_uart
@ -316,6 +326,25 @@ interval:
- logger.log: "voice assistant not running; restarting" - logger.log: "voice assistant not running; restarting"
- voice_assistant.start_continuous: - 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) # Add optional microWakeWord support (on-device wake word)
# Doesn't work well as of 2024-07-04 so leave disabled # Doesn't work well as of 2024-07-04 so leave disabled
#micro_wake_word: #micro_wake_word: