Stop flooding the recorder with timestamp and false-precision noise
Measured on a real installation with two bikes: the two "Last Seen" sensors were the number one and number two writers in the entire Home Assistant database, ahead of a three-phase energy meter. The integration accounted for 26.5% of all recorder writes on that system. Last Seen moved on every BLE notification, about once a second, or 2834 rows an hour per bike. The entity exists to say how fresh the values are, which needs nothing near that resolution, so it now advances at LAST_SEEN_RESOLUTION granularity. `self._last_seen` stays exact - that is what gets persisted and restored - only the entity is throttled. Measured after: 120 rows an hour, the theoretical maximum for a 30s window. The distance sensors carried four decimals of false precision, because the ebikemotion frames divide by 10000: a range that is really "14.6" was reported as 14.6065, then 14.6529, then 14.6297, each one another state write and another database row. They are rounded to 0.1 km now. Home Assistant offers integrations no way to exclude an entity from the recorder - `entity_filter` comes from user configuration only, and `_unrecorded_attributes` covers attributes rather than states - so reducing how often the state changes is the only fix that works without asking every user to edit configuration.yaml. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FyabWZzd7HoLpyBwEa5Zzh
This commit is contained in:
co-authored by
Claude Opus 5
parent
a38b2efef5
commit
62bc1c150a
@@ -28,6 +28,17 @@ from .const import DOMAIN, MANUFACTURER, MODEL, CONF_DEVICE_NAME
|
||||
from .coordinator import MySmartBikeCoordinator
|
||||
|
||||
|
||||
def round_km(value: Any) -> float | None:
|
||||
"""Round a distance to 0.1 km.
|
||||
|
||||
The ebikemotion frames carry these as `read32 / 10000`, which yields four
|
||||
decimals of false precision - 14.6065, 14.6529, 14.6297 for a range that is
|
||||
really "14.6". Every flicker in the last digit would be another state write
|
||||
and another database row.
|
||||
"""
|
||||
return None if value is None else round(float(value), 1)
|
||||
|
||||
|
||||
def safe_get(data: dict[str, Any] | None, *keys: str) -> Any:
|
||||
"""Safely get nested dictionary values."""
|
||||
if data is None:
|
||||
@@ -108,7 +119,7 @@ SENSORS: tuple[MySmartBikeSensorEntityDescription, ...] = (
|
||||
device_class=SensorDeviceClass.DISTANCE,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
icon="mdi:counter",
|
||||
value_fn=lambda data: safe_get(data, "ebm", "odometry"),
|
||||
value_fn=lambda data: round_km(safe_get(data, "ebm", "odometry")),
|
||||
),
|
||||
MySmartBikeSensorEntityDescription(
|
||||
key="range",
|
||||
@@ -117,7 +128,7 @@ SENSORS: tuple[MySmartBikeSensorEntityDescription, ...] = (
|
||||
device_class=SensorDeviceClass.DISTANCE,
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
icon="mdi:map-marker-distance",
|
||||
value_fn=lambda data: safe_get(data, "ebm", "autonomy"),
|
||||
value_fn=lambda data: round_km(safe_get(data, "ebm", "autonomy")),
|
||||
),
|
||||
MySmartBikeSensorEntityDescription(
|
||||
key="trip_distance",
|
||||
@@ -126,7 +137,7 @@ SENSORS: tuple[MySmartBikeSensorEntityDescription, ...] = (
|
||||
device_class=SensorDeviceClass.DISTANCE,
|
||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||
icon="mdi:bike",
|
||||
value_fn=lambda data: safe_get(data, "ebm", "trip_odometry"),
|
||||
value_fn=lambda data: round_km(safe_get(data, "ebm", "trip_odometry")),
|
||||
),
|
||||
MySmartBikeSensorEntityDescription(
|
||||
key="trip_range",
|
||||
@@ -136,7 +147,7 @@ SENSORS: tuple[MySmartBikeSensorEntityDescription, ...] = (
|
||||
state_class=SensorStateClass.MEASUREMENT,
|
||||
icon="mdi:map-marker-distance",
|
||||
entity_registry_enabled_default=False,
|
||||
value_fn=lambda data: safe_get(data, "ebm", "trip_autonomy"),
|
||||
value_fn=lambda data: round_km(safe_get(data, "ebm", "trip_autonomy")),
|
||||
),
|
||||
MySmartBikeSensorEntityDescription(
|
||||
key="light",
|
||||
|
||||
Reference in New Issue
Block a user