Skip to content

Commit

Permalink
fix: extremas with time_delta would generate wrong results
Browse files Browse the repository at this point in the history
Closes #713
  • Loading branch information
RomRider committed Jul 4, 2024
1 parent 84357f9 commit f7ab3e8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ views:
func: avg
- entity: sensor.random_0_1000
name: All
time_delta: +10min
float_precision: 0
transform: return Number(x) + 800;
show:
Expand Down
2 changes: 1 addition & 1 deletion src/apexcharts-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ class ChartsCard extends LitElement {
? new Date(start.getTime() + this._seriesOffset[index]).getTime()
: start.getTime(),
this._seriesOffset[index] ? new Date(end.getTime() + this._seriesOffset[index]).getTime() : end.getTime(),
this._serverTimeOffset,
this._serverTimeOffset - (this._seriesTimeDelta[index] || 0),
) || {
min: [0, null],
max: [0, null],
Expand Down
10 changes: 5 additions & 5 deletions src/graphEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export default class GraphEntry {
public minMaxWithTimestamp(
start: number,
end: number,
serverOffset = 0,
offset: number,
): { min: HistoryPoint; max: HistoryPoint } | undefined {
if (!this._computedHistory || this._computedHistory.length === 0) return undefined;
if (this._computedHistory.length === 1)
Expand All @@ -153,9 +153,9 @@ export default class GraphEntry {
},
{ min: [0, null], max: [0, null] },
);
if (serverOffset) {
if (minMax.min[0]) minMax.min[0] -= serverOffset;
if (minMax.max[0]) minMax.max[0] -= serverOffset;
if (offset) {
if (minMax.min[0]) minMax.min[0] -= offset;
if (minMax.max[0]) minMax.max[0] -= offset;
}
return minMax;
}
Expand All @@ -169,7 +169,7 @@ export default class GraphEntry {
}) - 1;
if (lastHistoryIndexBeforeStart >= 0)
lastTimestampBeforeStart = this._computedHistory[lastHistoryIndexBeforeStart][0];
return this.minMaxWithTimestamp(lastTimestampBeforeStart, end);
return this.minMaxWithTimestamp(lastTimestampBeforeStart, end, 0);
}

private async _getCache(key: string, compressed: boolean): Promise<EntityEntryCache | undefined> {
Expand Down

0 comments on commit f7ab3e8

Please sign in to comment.