Skip to content

Throttle attribute-only state writes to one per minute - #148

Merged
mk-maddin merged 1 commit into
mk-maddin:masterfrom
mstoffel-sag:fix/throttle-attribute-only-writes
Oct 30, 2026
Merged

mk-maddin merged 1 commit into
mk-maddin:masterfrom
mstoffel-sag:fix/throttle-attribute-only-writes

Conversation

@mstoffel-sag

Copy link
Copy Markdown
Contributor

Problem

The charger pushes nrg about once per second, also while the car is not charging. While idle, Charging Power stays at 0, but the per-phase voltages in its attributes change on every push.

Since #140 the attributes are no longer stored. Every write still bumps last_updated, though, and the recorder stores a new states row for each one. Measured on one install (MariaDB recorder, 7 days):

Rows
sensor.wattpilot_charging_power per day ~86,000 (one per second, around the clock)
of those with state 0 (idle) ~78,000
Share of all state rows on that install ~48%
Rows accumulated since March 17.3 M

Change

  • New ChargerPlatformEntity._write_state(state). It sets the state and calls async_write_ha_state(), but skips the write when the state is unchanged and the last write was less than ATTR_ONLY_WRITE_INTERVAL (60 s, in const.py) ago.
  • async_local_push and async_local_poll call it instead of writing directly.
  • A changed state is always written at once, so values while charging stay at full resolution.
  • AGENTS.md: rule to write state through _write_state(), with the reason.

Expected effect for Charging Power: idle writes drop from ~78,000 to at most 1,440 per day.

Behavior change

While the state is unchanged, attributes shown in the UI can be up to 60 s old. Nothing else changes: no entity IDs, no configuration, no wallbox commands.

Tests

  • pytest tests/ -v --tb=short: 74 passed (Python 3.14), including two new tests in TestAttributeOnlyWriteThrottle.
  • pytest tests_ha/ -v --tb=short: 4 passed.
  • Not yet verified on a real charger. The row counts above are from the live database before the change; I'll report the measured rate after installing it.

🤖 Generated with Claude Code

Charging Power (nrg) gets new per-phase voltages pushed about once per
second, also while idle at 0 W. Unrecorded attributes still bump
last_updated, so the recorder wrote a states row every second: ~86,000
rows per day for this one entity, about half of all state rows on the
install where it was measured.

ChargerPlatformEntity._write_state() now sets the state and writes it,
but skips the write when the state is unchanged and the last write was
less than ATTR_ONLY_WRITE_INTERVAL (60 s, const.py) ago. State changes
are still written immediately. async_local_push and async_local_poll use
it instead of calling async_write_ha_state() directly.

AGENTS.md records the rule and the reason. The test stub in
tests/conftest.py gets the new constant.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mk-maddin
mk-maddin merged commit 0f15a5f into mk-maddin:master Oct 30, 2026
7 checks passed
@mstoffel-sag
mstoffel-sag deleted the fix/throttle-attribute-only-writes branch October 30, 2026 18:37
@mharizanov

Copy link
Copy Markdown

Seeing the same here on v0.4.0 (HA 2026.10.4, HAOS on a Raspberry Pi 5, SQLite recorder). In 24 hours sensor.wattpilot_charging_energy (Charging Power) wrote 44,011 state rows, about 6 % of all rows in my database. 41,394 of them had the same state (0 W, no car charging) and differed only in the per-phase voltage attributes, so this change should remove most of them.

Is a 0.4.1 release planned soon? I'd rather wait for the release than patch the files by hand. I'll post the row count once I've updated.

@Godzzi2

Godzzi2 commented Nov 15, 2026

Copy link
Copy Markdown

is there any plan to make it configurable? I updated yesterday to 0.4.1 and saw that "charging power" sensor and "max charging current" slider update very slow. Downgraded back to 0.3.9 and everything is back to normal.
Never had any issues with the database.

@mharizanov

Copy link
Copy Markdown

Row count as promised: on 0.4.1 (running since 2026-11-14), sensor.wattpilot_charging_energy wrote 1,229 rows on 2026-11-15 and 1,226 on 2026-11-16, down from 44,011 per 24 h on 0.4.0. Thanks!

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.

4 participants