@@ -27,17 +27,28 @@ export interface Statistics {
2727}
2828
2929export interface StatisticValue {
30- statistic_id : string ;
31- start : string ;
32- end : string ;
33- last_reset : string | null ;
34- max : number | null ;
35- mean : number | null ;
36- min : number | null ;
37- sum : number | null ;
38- state : number | null ;
30+ start : number ;
31+ end : number ;
32+ change ?: number | null ;
33+ last_reset ?: number | null ;
34+ max ? : number | null ;
35+ mean ? : number | null ;
36+ min ? : number | null ;
37+ sum ? : number | null ;
38+ state ? : number | null ;
3939}
4040
41+ const statisticTypes = [
42+ "change" ,
43+ "last_reset" ,
44+ "max" ,
45+ "mean" ,
46+ "min" ,
47+ "state" ,
48+ "sum" ,
49+ ] as const ;
50+ export type StatisticsTypes = ( typeof statisticTypes ) [ number ] [ ] ;
51+
4152export interface EnergySource {
4253 type : string ;
4354 stat_energy_from ?: string ;
@@ -94,6 +105,7 @@ const fetchStatistics = (
94105 statistic_ids ?: string [ ] ,
95106 period : "5minute" | "hour" | "day" | "week" | "month" = "hour" ,
96107 // units?: StatisticsUnitConfiguration
108+ types ?: StatisticsTypes
97109) =>
98110 hass . callWS < Statistics > ( {
99111 type : "recorder/statistics_during_period" ,
@@ -102,57 +114,49 @@ const fetchStatistics = (
102114 statistic_ids,
103115 period,
104116 // units,
117+ types,
105118 } ) ;
106119
107120const calculateStatisticSumGrowth = (
108121 values : StatisticValue [ ]
109122) : number | null => {
110- if ( ! values || values . length < 2 ) {
111- return null ;
112- }
113- const endSum = values [ values . length - 1 ] . sum ;
114- if ( endSum === null ) {
123+ let growth : number | null = null ;
124+
125+ if ( ! values ) {
115126 return null ;
116127 }
117- const startSum = values [ 0 ] . sum ;
118- if ( startSum === null ) {
119- return endSum ;
128+
129+ for ( const value of values ) {
130+ if ( value . change === null || value . change === undefined ) {
131+ continue ;
132+ }
133+ if ( growth === null ) {
134+ growth = value . change ;
135+ } else {
136+ growth += value . change ;
137+ }
120138 }
121- return endSum - startSum ;
139+
140+ return growth ;
122141} ;
123142
124143export async function getStatistics ( hass : HomeAssistant , energyData : EnergyData , devices : string [ ] ) : Promise < Record < string , number > > {
125144 const dayDifference = differenceInDays (
126145 energyData . end || new Date ( ) ,
127146 energyData . start
128147 ) ;
129- const startMinHour = addHours ( energyData . start , - 1 ) ;
130148 const period = dayDifference > 35 ? "month" : dayDifference > 2 ? "day" : "hour" ;
131149
132-
133-
134150 const data = await fetchStatistics (
135151 hass ,
136- startMinHour ,
152+ energyData . start ,
137153 energyData . end ,
138154 devices ,
139155 period ,
140- // units
156+ // units,
157+ [ "change" ]
141158 ) ;
142159
143- Object . values ( data ) . forEach ( ( stat ) => {
144- // if the start of the first value is after the requested period, we have the first data point, and should add a zero point
145- if ( stat . length && new Date ( stat [ 0 ] . start ) > startMinHour ) {
146- stat . unshift ( {
147- ...stat [ 0 ] ,
148- start : startMinHour . toISOString ( ) ,
149- end : startMinHour . toISOString ( ) ,
150- sum : 0 ,
151- state : 0 ,
152- } ) ;
153- }
154- } ) ;
155-
156160 return devices . reduce ( ( states , id ) => ( {
157161 ...states ,
158162 [ id ] : calculateStatisticSumGrowth ( data [ id ] ) ,
0 commit comments