Skip to content

Commit d08b687

Browse files
committed
Added resampling filter to align x axis timestamps across multiple traces
1 parent f091148 commit d08b687

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

readme.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ entities:
427427
meta: { unit_of_measurement: "delta" }
428428
};
429429
},
430+
- resample: 5m # Rebuilds data so that the timestamps in xs are exact multiples of the specified interval, and without gaps. The parameter is the length of the interval and defaults to 5 minutes (see #duration for the format). This is useful when combining data from multiple entities, as the index of each datapoint will correspond to the same instant of time across them.
430431
- filter: y !== null && +y > 0 && x > new Date(Date.now()-1000*60*60) # filter out datapoints for which this returns false. Also filters from xs, states and statistics. Same variables as map_y are in scope
431432
- force_numeric # converts number-lookinig-strings to actual js numbers and removes the rest. Any filters used after this one will receive numbers, not strings or nulls. Also removes respective elements from xs, states and statistics parameters
432433
```
@@ -523,15 +524,18 @@ Compute absolute humidity
523524
type: custom:plotly-graph-dev
524525
entities:
525526
- entity: sensor.wintergarten_clima_humidity
526-
period: 5minute # important so the datapoints align in the x axis
527527
internal: true
528528
filters:
529+
- resample: 5m # important so the datapoints align in the x axis
530+
- map_y: parseFloat(y)
529531
- store_var: relative_humidity
530532
- entity: sensor.wintergarten_clima_temperature
531533
period: 5minute
532534
name: Absolute Hty
533535
unit_of_measurement: g/m³
534536
filters:
537+
- resample: 5m
538+
- map_y: parseFloat(y)
535539
- map_y: (6.112 * Math.exp((17.67 * y)/(y+243.5)) * +vars.relative_humidity.ys[i] * 2.1674)/(273.15+y);
536540
```
537541

@@ -544,18 +548,20 @@ entities:
544548
internal: true
545549
period: 5minute # important so the datapoints align in the x axis
546550
filters:
551+
- map_y: parseFloat(y)
547552
- store_var: relative_humidity
548553
- entity: sensor.openweathermap_temperature
549554
period: 5minute
550555
name: Dew point
551556
filters:
557+
- map_y: parseFloat(y)
552558
- map_y: >-
553559
{
554560
// https://www.omnicalculator.com/physics/dew-point
555561
const a = 17.625;
556562
const b = 243.04;
557-
const T = +y;
558-
const RH = +vars.relative_humidity.ys[i];
563+
const T = y;
564+
const RH = vars.relative_humidity.ys[i];
559565
const α = Math.log(RH/100) + a*T/(b+T);
560566
const Ts = (b * α) / (a - α);
561567
return Ts;

src/filters/filters.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Datum } from "plotly.js";
22
import { linearRegressionLine, linearRegression } from "simple-statistics";
3-
import { timeUnits } from "../duration/duration";
3+
import {
4+
parseTimeDuration,
5+
TimeDurationStr,
6+
timeUnits,
7+
} from "../duration/duration";
48
import { StatisticValue } from "../recorder-types";
59
import { HassEntity, YValue } from "../types";
610

@@ -210,6 +214,30 @@ const filters = {
210214
xs: xs.map((_, i) => fn(i, xs[i], ys[i], states[i], statistics[i], vars)),
211215
});
212216
},
217+
resample:
218+
(intervalStr: TimeDurationStr = "5m") =>
219+
({ xs, ys, states, statistics }) => {
220+
const data = {
221+
xs: [] as Date[],
222+
ys: [] as YValue[],
223+
states: [] as HassEntity[],
224+
statistics: [] as StatisticValue[],
225+
};
226+
const interval = parseTimeDuration(intervalStr);
227+
const x0 = Math.floor(+xs[0] / interval) * interval;
228+
const x1 = +xs[xs.length - 1];
229+
let i = 0;
230+
for (let x = x0; x < x1; x += interval) {
231+
while (+xs[i + 1] < x && i < xs.length - 1) {
232+
i++;
233+
}
234+
data.xs.push(new Date(x));
235+
data.ys.push(ys[i]);
236+
if (states[i]) data.states.push(states[i]);
237+
if (statistics[i]) data.statistics.push(statistics[i]);
238+
}
239+
return data;
240+
},
213241
store_var:
214242
(var_name: string) =>
215243
({ vars, ...rest }) => ({ vars: { ...vars, [var_name]: rest } }),

0 commit comments

Comments
 (0)