Skip to content

perf: rate-limit + delta-only PSU time sync#22

Open
malaiwah wants to merge 1 commit into
wildekek:mainfrom
malaiwah:fix/time-sync-delta-only
Open

perf: rate-limit + delta-only PSU time sync#22
malaiwah wants to merge 1 commit into
wildekek:mainfrom
malaiwah:fix/time-sync-delta-only

Conversation

@malaiwah
Copy link
Copy Markdown

Closes #21

Replaces the unconditional 6-write on_time_sync action with:

  • a persisted last_psu_time_sync global (restore_value: yes)
  • a 24h gate
  • a NaN guard (skip if Modbus hasn't completed first poll yet — avoids burning the 24h window on a sync that compared against uninitialised state)
  • a delta-only writer using make_call().set_value().perform() that touches only the date components that actually differ

Verification

  • esphome config passes for both YAMLs
  • Full esphome compile clean (lambdas validate; RAM 46.2% / Flash 49.5%)

Boot-time behaviour, briefly

  • ESP8266: restore_value lives in RTC RAM by default → preserved across software reboots, reset on cold boot. First boot after power-loss triggers one sync (after first poll completes — the NaN guard prevents writing against uninitialised state).
  • ESP32-C3: restore_value persists in NVS across cold boots → only one sync ever (until 24h drift).

Disclosure

Drafted with Claude (Anthropic). Author-reviewed.

The original on_time_sync action fired six Modbus writes (year, month,
day, hour, minute, second) every time the HA time component synced —
which can be on every reconnect. That contended with sensor polling
and wore the PSU's flash for no benefit on a clock that hasn't moved.

This change:
  * persists the last successful sync timestamp in flash (global)
  * skips entirely if the last sync is < 24h old AND the PSU clock
    is already within 30 s of HA time
  * only writes the individual date/time registers that actually differ

Behaviour on a freshly-flashed device or after a clock-drift event is
unchanged. Steady-state cost drops from ~6 writes per reconnect to 0.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Time sync writes 6 Modbus registers on every HA reconnect

1 participant