Skip to content

Commit

Permalink
Diff aggregation (kalkih#601)
Browse files Browse the repository at this point in the history
* Add aggregation 'diff' (= last - first)

* Add `diff` to documentation

* Rewrite delta or diff if check for readability

Co-authored-by: Mischa <mspelt@outlook.com>
Co-authored-by: Jonas De Kegel <jonas@fluid.desi>
  • Loading branch information
3 people committed Jan 17, 2022
1 parent 89a9c65 commit 4aa28fc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ These buckets are converted later to single point/bar on the graph. Aggregate fu
| `last` | v0.9.0 |
| `sum` | v0.9.2 |
| `delta` | v0.9.4 | Calculates difference between max and min value
| `diff` | v0.11.0 | Calculates difference between first and last value

### Theme variables
The following theme variables can be set in your HA theme to customize the appearance of the card.
Expand Down
7 changes: 6 additions & 1 deletion src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default class Graph {
last: this._last,
sum: this._sum,
delta: this._delta,
diff: this._diff,
};

this._history = undefined;
Expand Down Expand Up @@ -246,8 +247,12 @@ export default class Graph {
return this._maximum(items) - this._minimum(items);
}

_diff(items) {
return this._last(items) - this._first(items);
}

_lastValue(items) {
if (this.aggregateFuncName === 'delta') {
if (['delta', 'diff'].includes(this.aggregateFuncName)) {
return 0;
} else {
return parseFloat(items[items.length - 1].state) || 0;
Expand Down

0 comments on commit 4aa28fc

Please sign in to comment.