Skip to content

Commit 8b951c5

Browse files
FrnchFrggdbuezas
andauthored
Long-term statistics support (#78)
* Long-term statistics support * Document long-term statistics support * Add name and units to statistics. Refactor fetch statistics and states in their own files. * Remove unused types * change API to yaml keys for both statistics and states. Co-authored-by: David Buezas <[email protected]>
1 parent 7da3958 commit 8b951c5

File tree

13 files changed

+554
-303
lines changed

13 files changed

+554
-303
lines changed

.pretierrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

.prettierrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
semi: true,
3+
//trailingComma: "all",
4+
singleQuote: false,
5+
printWidth: 80,
6+
tabWidth: 2,
7+
};

readme.md

Lines changed: 79 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,21 @@ For now only the only allowed chart types are:
122122
- see https://plotly.com/javascript/reference/scatter/#scatter-line for more
123123

124124
```yaml
125+
type: custom:plotly-graph
125126
entities:
126127
- entity: sensor.temperature
127128
- entity: sensor.humidity
128129
```
129130

131+
Alternatively:
132+
133+
```yaml
134+
type: custom:plotly-graph
135+
entities:
136+
- sensor.temperature
137+
- sensor.humidity
138+
```
139+
130140
## Color schemes
131141

132142
Changes default line colors.
@@ -146,17 +156,35 @@ color_scheme: dutch_field
146156

147157
### Attribute values
148158

149-
Plot the attributes of an entity by adding `::atribute_name` to the entity name
159+
Plot the attributes of an entity
150160

151161
```yaml
162+
type: custom:plotly-graph
152163
entities:
153-
- entity: climate.living::temperature
154-
- entity: climate.kitchen::temperature
164+
- entity: climate.living
165+
attribute: temperature
166+
- entity: climate.kitchen
167+
attribute: temperature
155168
```
156169

170+
### Statistics support
171+
172+
Fetch and plot long-term statistics of an entity
173+
174+
```yaml
175+
type: custom:plotly-graph
176+
entities:
177+
- entity: sensor.temperature
178+
statistic: max # `min`, `mean`, `max` or `sum`
179+
period: 5minute # `5minute`, `hour`, `day`, `month`
180+
```
181+
182+
Note that `5minute` period statistics are limited in time as normal recorder history is, contrary to other periods which keep data for years.
183+
157184
## Extra entity attributes:
158185

159186
```yaml
187+
type: custom:plotly-graph
160188
entities:
161189
- entity: sensor.temperature_in_celsius
162190
name: living temperature in Farenheit # Overrides the entity name
@@ -191,72 +219,73 @@ entities:
191219
#### Normalisation wrt to first fetched value
192220

193221
```yaml
194-
- entity: sensor.my_sensor
195-
lambda: |-
196-
(ys) => ys.map(y => y/ys[0])
222+
- entity: sensor.my_sensor
223+
lambda: |-
224+
(ys) => ys.map(y => y/ys[0])
197225
```
198226

199227
note: `ys[0]` represents the first "known" value, which is the value furthest to the past among the downloaded data. This value will change if you scroll, zoom out, change the hours_to_show, or just let time pass.
200228

201229
#### Accumulated value
202230

203231
```yaml
204-
- entity: sensor.my_sensor
205-
unit_of_measurement: "total pulses"
206-
lambda: |-
207-
(ys) => {
208-
let accumulator = 0;
209-
return ys.map(y => accumulator + y)
210-
}
232+
- entity: sensor.my_sensor
233+
unit_of_measurement: "total pulses"
234+
lambda: |-
235+
(ys) => {
236+
let accumulator = 0;
237+
return ys.map(y => accumulator + y)
238+
}
211239
```
212240

213241
#### Derivative
214242

215243
```yaml
216-
- entity: sensor.my_sensor
217-
unit_of_measurement: "pulses / second"
218-
lambda: |-
219-
(ys, xs) => {
220-
let last = {
221-
x: new Date(),
222-
y: 0,
223-
}
224-
return ys.map((y, index) => {
225-
const x = xs[index];
226-
const dateDelta = x - last.x;
227-
const yDelta = (y - last.y) / dateDelta;
228-
last = { x, y };
229-
return yDelta;
230-
})
244+
- entity: sensor.my_sensor
245+
unit_of_measurement: "pulses / second"
246+
lambda: |-
247+
(ys, xs) => {
248+
let last = {
249+
x: new Date(),
250+
y: 0,
231251
}
252+
return ys.map((y, index) => {
253+
const x = xs[index];
254+
const dateDelta = x - last.x;
255+
const yDelta = (y - last.y) / dateDelta;
256+
last = { x, y };
257+
return yDelta;
258+
})
259+
}
232260
```
233261

234262
#### Right hand riemann integration
235263

236264
```yaml
237-
- entity: sensor.my_sensor
238-
unit_of_measurement: "kWh"
239-
lambda: |-
240-
(ys, xs) => {
241-
let accumulator = 0;
242-
let last = {
243-
x: new Date(),
244-
y: 0,
245-
}
246-
return ys.map((y, index) => {
247-
const x = xs[index]
248-
const dateDelta = x - last.x;
249-
accumulator += last.y * dateDelta;
250-
last = { x, y };
251-
return accumulator;
252-
})
265+
- entity: sensor.my_sensor
266+
unit_of_measurement: "kWh"
267+
lambda: |-
268+
(ys, xs) => {
269+
let accumulator = 0;
270+
let last = {
271+
x: new Date(),
272+
y: 0,
253273
}
274+
return ys.map((y, index) => {
275+
const x = xs[index]
276+
const dateDelta = x - last.x;
277+
accumulator += last.y * dateDelta;
278+
last = { x, y };
279+
return accumulator;
280+
})
281+
}
254282
```
255283

256284
#### Access all entity attributes inside lambda
257285

258286
```yaml
259-
- entity: climate.wintergarten_floor::valve
287+
- entity: climate.wintergarten_floor
288+
attribute: valve
260289
unit_of_measurement: °C
261290
lambda: |-
262291
(ys, xs, entity) =>
@@ -282,6 +311,7 @@ note: `ys[0]` represents the first "known" value, which is the value furthest to
282311
default configurations for all entities and all yaxes (e.g yaxis, yaxis2, yaxis3, etc).
283312

284313
```yaml
314+
type: custom:plotly-graph
285315
entities:
286316
- sensor.temperature1
287317
- sensor.temperature2
@@ -304,6 +334,7 @@ Anything from https://plotly.com/javascript/reference/layout/.
304334
Use this if you want to use plotly default layout instead. Very useful for heavy customization while following pure plotly examples.
305335

306336
```yaml
337+
type: custom:plotly-graph
307338
entities:
308339
- entity: sensor.temperature_in_celsius
309340
no_default_layout: true
@@ -312,6 +343,7 @@ no_default_layout: true
312343
### disable Home Assistant themes:
313344

314345
```yaml
346+
type: custom:plotly-graph
315347
entities:
316348
- entity: sensor.temperature_in_celsius
317349
no_theme: true
@@ -328,6 +360,7 @@ When true, will tell HA to only fetch datapoints with a different state as the o
328360
More here: https://developers.home-assistant.io/docs/api/rest/ under `/api/history/period/<timestamp>`
329361

330362
Caveats:
363+
331364
1. zana-37 repoorts that `minimal_response: false` needs to be set to get all non-significant datapoints [here](https://github.com/dbuezas/lovelace-plotly-graph-card/issues/34#issuecomment-1085083597).
332365
2. This configuration will be ignored (will be true) while fetching [Attribute Values](#Attribute-Values).
333366

@@ -341,6 +374,7 @@ When true, tell HA to only return last_changed and state for states other than t
341374
More here: https://developers.home-assistant.io/docs/api/rest/ under `/api/history/period/<timestamp>`
342375

343376
Caveats:
377+
344378
1. This configuration will be ignored (will be true) while fetching [Attribute Values](#Attribute-Values).
345379

346380
```yaml

0 commit comments

Comments
 (0)