Skip to content

Commit 04021bd

Browse files
committed
Added option for state_class=total sensors (statistic: "state"). Fixes #92, #93
1 parent ea5f807 commit 04021bd

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

readme.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,12 @@ Fetch and plot long-term statistics of an entity
184184
type: custom:plotly-graph
185185
entities:
186186
- entity: sensor.temperature
187-
statistic: max # `min`, `mean`, `max` or `sum`
188-
period: 5minute # `5minute`, `hour`, `day`, `month`
187+
# for entities with state_class=measurement (normal sensors, like temperature):
188+
statistic: max # `min`, `mean` of `max`
189+
# for entities with state_class=total (such as utility meters):
190+
statistic: state # `state` or `sum`
191+
192+
period: 5minute # `5minute`, `hour`, `day`, `week`, `month`
189193
```
190194
191195
Note that `5minute` period statistics are limited in time as normal recorder history is, contrary to other periods which keep data for years.
@@ -424,5 +428,3 @@ Update data every `refresh_interval` seconds. Use `0` or delete the line to disa
424428
- click on releases/new draft from tag in github
425429

426430
Automated release pending.
427-
428-

src/plotly-graph-card.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { sleep } from "./utils";
2020
import { Datum } from "plotly.js";
2121
import colorSchemes, { isColorSchemeArray } from "./color-schemes";
2222
import { parseISO } from "date-fns";
23+
import { STATISTIC_PERIODS, STATISTIC_TYPES } from "./recorder-types";
2324

2425
const componentName = isProduction ? "plotly-graph" : "plotly-graph-dev";
2526

@@ -228,13 +229,9 @@ export class PlotlyGraph extends HTMLElement {
228229
entity.lambda = window.eval(entity.lambda);
229230
}
230231
if ("statistic" in entity || "period" in entity) {
231-
const validStatistic = ["mean", "min", "max", "sum"].includes(
232-
entity.statistic || ""
233-
);
232+
const validStatistic = STATISTIC_TYPES.includes(entity.statistic!);
234233
if (!validStatistic) entity.statistic = "mean";
235-
const validPeriod = ["5minute", "hour", "day", "month"].includes(
236-
entity.period || ""
237-
);
234+
const validPeriod = STATISTIC_PERIODS.includes(entity.period!);
238235
if (!validPeriod) entity.period = "hour";
239236
}
240237
const [oldAPI_entity, oldAPI_attribute] = entity.entity.split("::");

src/recorder-types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ export interface StatisticValue {
1414
export interface Statistics {
1515
[statisticId: string]: StatisticValue[];
1616
}
17-
export type StatisticType = "state" | "sum" | "min" | "max" | "mean";
17+
export const STATISTIC_TYPES = ["state", "sum", "min", "max", "mean"] as const;
18+
export type StatisticType = typeof STATISTIC_TYPES[number];
1819

19-
export type StatisticPeriod = "5minute" | "hour" | "day" | "month";
20+
export const STATISTIC_PERIODS = [
21+
"5minute",
22+
"hour",
23+
"day",
24+
"week",
25+
"month",
26+
] as const;
27+
export type StatisticPeriod = typeof STATISTIC_PERIODS[number];

0 commit comments

Comments
 (0)