meshcore-ha icon indicating copy to clipboard operation
meshcore-ha copied to clipboard

Neighbour SNR sensors write a new state every few seconds (secs_ago/last_seen attributes), filling the recorder

Open michalsiman opened this issue 4 weeks ago • 1 comments

Summary

Every neighbour SNR sensor (MeshCoreNeighborSensor) writes a new state to the recorder on every coordinator update, every few seconds, even when nothing about the neighbour changed. The cause is that extra_state_attributes includes secs_ago and last_seen, which are computed from the current time, so the attributes differ on every write and Home Assistant cannot skip the state write. Each write also stores a new row in state_attributes, because the attributes are never identical twice.

Impact

On my install (meshcore-ha 3.0.4, 3 tracked repeaters, 85 neighbour sensors, default 10-day recorder retention):

  • about 150,000 recorded states per neighbour sensor over 10 days, roughly one every 5 seconds
  • the same number of distinct attribute rows per sensor (for example 154,480 distinct attributes_id for one sensor)
  • state_attributes grew to about 1.8 GB and states to about 0.5 GB, out of a 3.2 GB database; the neighbour sensors are by far the largest contributors
  • the SNR value itself only changes when the repeater is polled (every 2 hours by default)

This also means constant disk writes on SD-card and small-SSD installs.

Suggested fix

Expose the time the neighbour was last heard as a fixed timestamp instead of an age that changes every second, for example:

last_updated = data.get("last_updated")
last_heard = (last_updated or now) - (data.get("secs_ago") or 0)
...
"last_heard": datetime.fromtimestamp(int(last_heard)).isoformat(),

and drop secs_ago / last_seen from the attributes (or at least add them to _unrecorded_attributes, which stops the attribute-table growth but still records a state every few seconds). Note that now - neighbor_age(data, now) is not stable, because neighbor_age truncates to whole seconds, so the timestamp has to be computed from last_updated - secs_ago directly.

With this change applied locally, the neighbour sensors only write when the SNR, seen_48h, availability or the last-heard time actually change.

A related smaller item: the integration fires every SDK event as meshcore_raw_event, which the recorder stores by default (about 364,000 events and 300 MB of event_data in 10 days here). It may be worth documenting a recorder: exclude: event_types: [meshcore_raw_event] recommendation, or not firing them unless enabled.

michalsiman avatar Nov 05 '26 23:11 michalsiman

Confirmed. Fix in #410: secs_ago and last_seen are gone from the neighbour attributes, so a state is written only when the SNR, last_updated or seen_48h change. last_updated was already a fixed timestamp, so now() - last_updated gives the age in a template. Took your recorder note too: events.md now shows how to exclude meshcore_raw_event. Thanks for the numbers, the 154k attribute rows for one sensor made the case by itself.

awolden avatar Nov 06 '26 23:11 awolden