mirror of
https://github.com/Akkudoktor-EOS/EOS.git
synced 2026-12-02 18:36:28 +01:00
fix(devices): use the nearest valid measurement of a key
DataSequence.key_to_value took the record nearest in time and returned the key from it, even when that record did not hold the key. Feeders write measurement keys one at a time, seconds apart, and every write creates its own record. A battery temperature written at 10:00:00 and a SoC written at 10:00:01 then gave no temperature for a lookup at 10:01, so battery temperature, SoC factor, charge/discharge power and the inverter temperature fell back to the reference operating point (25 degC, SoC 0.5, 45 degC). Profiles with a temperature or SoC dependent charge limit were evaluated there, e.g. with too high a charge power for a cold battery. key_to_value now returns the nearest value of the key itself: records where the key is None or NaN are skipped, as dropna does for the other key accessors. The search window and its bounds are unchanged; without a window only an exact match counts. Additional changes Developer docs describe key_to_value as the nearest valid value of a key Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
committed by
Bobby Noelte
co-authored by
Claude Opus 5.5
parent
12527597b1
commit
b2c5820925
@@ -1077,12 +1077,17 @@ class DataSequence(DataABC, DatabaseRecordProtocolMixin[DataRecordT], Generic[Da
|
||||
) -> Optional[float]:
|
||||
"""Returns the value corresponding to the specified key that is nearest to the given datetime.
|
||||
|
||||
Only records with a value for the key count. Records where the key is None
|
||||
or NaN are skipped, e.g. a record that was written for another key a few
|
||||
seconds later.
|
||||
|
||||
Args:
|
||||
key (str): The key of the attribute in DataRecord to extract.
|
||||
target_datetime (datetime): The datetime to search for.
|
||||
time_window: Optional total width of the symmetric search window centered on
|
||||
``target_datetime``. If provided and no exact match exists, the nearest
|
||||
record within this window is returned.
|
||||
``target_datetime``. If provided, the nearest valid value within this
|
||||
window is returned; of two equally near values the later one. If not
|
||||
provided, only a record at exactly ``target_datetime`` counts.
|
||||
|
||||
Returns:
|
||||
Optional[float]: The value nearest to the given datetime, or None if no valid records are found.
|
||||
@@ -1093,11 +1098,35 @@ class DataSequence(DataABC, DatabaseRecordProtocolMixin[DataRecordT], Generic[Da
|
||||
self._validate_key(key)
|
||||
|
||||
# Ensure datetime objects are normalized
|
||||
db_target = DatabaseTimestamp.from_datetime(to_datetime(target_datetime))
|
||||
target = to_datetime(target_datetime)
|
||||
db_target = DatabaseTimestamp.from_datetime(target)
|
||||
|
||||
record = await self.db_get_record(db_target, time_window=time_window)
|
||||
if time_window is None:
|
||||
record = await self.db_get_record(db_target)
|
||||
value = getattr(record, key, None)
|
||||
return None if value is None or pd.isna(value) else value
|
||||
|
||||
return getattr(record, key, None)
|
||||
search_start, search_end = self._search_window(db_target, time_window)
|
||||
if isinstance(search_end, DatabaseTimestamp):
|
||||
# Like db_get_record(), accept a value at exactly half the window after the target.
|
||||
search_end = self._db_timestamp_after(search_end)
|
||||
max_distance = time_window.total_seconds() / 2
|
||||
|
||||
nearest_value = None
|
||||
async for record in self.db_iterate_records(search_start, search_end):
|
||||
value = getattr(record, key, None)
|
||||
if record.date_time is None or value is None or pd.isna(value):
|
||||
continue
|
||||
distance = compare_datetimes(record.date_time, target).time_diff
|
||||
if abs(distance) <= max_distance:
|
||||
# Records come in ascending order: before the target each valid value is
|
||||
# nearer than the previous one.
|
||||
nearest_value, max_distance = value, abs(distance)
|
||||
if distance >= 0:
|
||||
# The first valid value at or after the target is the nearest on this side.
|
||||
break
|
||||
|
||||
return nearest_value
|
||||
|
||||
async def key_to_lists(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user