Skip to content

Commit 7a9bebe

Browse files
committed
Fix entities with offsets break autoranging
1 parent ad4a6d0 commit 7a9bebe

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ entities:
278278

279279
![Graph with offsets](docs/resources/offset-temperature.png)
280280

281+
### Caveats
282+
283+
The following exceptions apply to traces with offsets:
284+
285+
- They get their own cache, meaning that data will be fetched twice if the same entity is in the plot with a different (or no) offset.
286+
- Websocket state updates are not used to fill their cache (but a request to the server may be triggered)
287+
- `extend_to_present` is ignored (because extending to an offset present may be far into the future and that messes up with autorange)
288+
281289
### Now line
282290

283291
When using offsets, it is useful to have a line that indicates the current time. This can be done by using a lambda function that returns a line with the current time as x value and 0 and 1 as y values. The line is then hidden from the legend.

src/cache/Cache.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ async function fetchSingleRange(
9999

100100
export function getEntityKey(entity: EntityConfig) {
101101
if (isEntityIdAttrConfig(entity)) {
102-
return `${entity.entity}::attribute`;
102+
return `${entity.entity}::attribute::${entity.offset}`;
103103
} else if (isEntityIdStatisticsConfig(entity)) {
104-
return `${entity.entity}::statistics::${entity.period}`;
104+
return `${entity.entity}::statistics::${entity.period}::${entity.offset}`;
105105
} else if (isEntityIdStateConfig(entity)) {
106-
return entity.entity;
106+
return `${entity.entity}::${entity.offset}`;
107107
}
108108
throw new Error(`Entity malformed:${JSON.stringify(entity)}`);
109109
}
@@ -157,7 +157,7 @@ export default class Cache {
157157
// see https://github.com/dbuezas/lovelace-plotly-graph-card/issues/146
158158
y === "unavailable" ? null : y
159159
);
160-
if (entity.extend_to_present && xs.length > 0) {
160+
if (entity.extend_to_present && xs.length > 0 && entity.offset === 0) {
161161
xs.push(new Date(Date.now() + entity.offset));
162162
ys.push(ys[ys.length - 1]);
163163
states.push(states[states.length - 1]);

src/plotly-graph-card.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,22 @@ export class PlotlyGraph extends HTMLElement {
168168
const start = new Date(oldState?.last_updated || state.last_updated);
169169
const end = new Date(state.last_updated);
170170
const range: [number, number] = [+start, +end];
171-
let value: string | undefined;
172-
if (isEntityIdAttrConfig(entity)) {
173-
value = state.attributes[entity.attribute];
171+
let shouldAddToCache = false;
172+
if (entity.offset !== 0) {
173+
// in entities with offset, the added datapoint may be far into the future.
174+
// Therefore, adding it messes with autoranging.
175+
// TODO: unify entity caches independent of offsets and keep track of what has actually been
176+
// in the viewport
177+
shouldFetch = true;
178+
} else if (isEntityIdAttrConfig(entity)) {
179+
shouldAddToCache = true;
174180
} else if (isEntityIdStateConfig(entity)) {
175-
value = state.state;
181+
shouldAddToCache = true;
176182
} else if (isEntityIdStatisticsConfig(entity)) {
177183
shouldFetch = true;
178184
}
179185

180-
if (value !== undefined) {
186+
if (shouldAddToCache) {
181187
this.cache.add(
182188
entity,
183189
[{ state, x: new Date(end), y: null }],

0 commit comments

Comments
 (0)