@balmli/homey-utility-prices 0.0.0-stage → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +10 -0
- package/.nvmrc +1 -0
- package/AGENT.md +37 -0
- package/QUARTER_HOUR_USAGE.md +60 -0
- package/README.md +85 -2
- package/dist/Heating.d.ts +5 -0
- package/dist/Heating.js +60 -0
- package/dist/NordpoolApi.d.ts +35 -0
- package/dist/NordpoolApi.js +254 -0
- package/dist/PriceApi.d.ts +61 -0
- package/dist/PriceApi.js +404 -0
- package/dist/PriceComparer.d.ts +235 -0
- package/dist/PriceComparer.js +476 -0
- package/dist/PriceFetcher.d.ts +41 -0
- package/dist/PriceFetcher.js +293 -0
- package/dist/PricesFetchClient.d.ts +86 -0
- package/dist/PricesFetchClient.js +392 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +41 -0
- package/dist/types.d.ts +99 -0
- package/dist/types.js +171 -0
- package/package.json +60 -5
package/dist/PriceApi.js
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.PriceApi = void 0;
|
|
18
|
+
var moment_timezone_1 = __importDefault(require("moment-timezone"));
|
|
19
|
+
var Heating_1 = require("./Heating");
|
|
20
|
+
var PriceApi = /** @class */ (function () {
|
|
21
|
+
function PriceApi() {
|
|
22
|
+
var _this = this;
|
|
23
|
+
this.toHour = function (aDate) {
|
|
24
|
+
return (0, moment_timezone_1.default)(aDate).startOf('hour').toISOString();
|
|
25
|
+
};
|
|
26
|
+
this.currentPrice = function (prices, aDate) {
|
|
27
|
+
var ts = (0, moment_timezone_1.default)(aDate);
|
|
28
|
+
return prices.find(function (p) { return _this.isInSlot(p, ts); });
|
|
29
|
+
};
|
|
30
|
+
this.pricesStarting = function (prices, aDate, startHour, num_hours) {
|
|
31
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
32
|
+
var slotCount = _this.hoursToSlotCount(num_hours, slotMinutes);
|
|
33
|
+
var dayStart = (0, moment_timezone_1.default)(aDate).startOf('day');
|
|
34
|
+
var startingAt = _this.alignToSlotStart((0, moment_timezone_1.default)(dayStart).add(startHour, 'hours'), slotMinutes);
|
|
35
|
+
return prices
|
|
36
|
+
.filter(function (p) { return p.startsAt.isSameOrAfter(startingAt); })
|
|
37
|
+
.slice(0, slotCount);
|
|
38
|
+
};
|
|
39
|
+
this.pricesSorted = function (prices, aDate) {
|
|
40
|
+
return _this.pricesStarting(prices, aDate, 0, 24)
|
|
41
|
+
.concat()
|
|
42
|
+
.sort(function (a, b) { return a.price - b.price; });
|
|
43
|
+
};
|
|
44
|
+
this.priceRatio = function (prices, aDate) {
|
|
45
|
+
var slotsToday = _this.pricesStarting(prices, aDate, 0, 24)
|
|
46
|
+
.concat()
|
|
47
|
+
.sort(function (a, b) { return a.price - b.price; });
|
|
48
|
+
if (slotsToday.length <= 1) {
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
51
|
+
var current = _this.currentPrice(prices, aDate);
|
|
52
|
+
if (!current) {
|
|
53
|
+
return 0;
|
|
54
|
+
}
|
|
55
|
+
var withIndex = slotsToday.findIndex(function (p) { return p.startsAt.isSame(current.startsAt); });
|
|
56
|
+
if (withIndex === -1) {
|
|
57
|
+
return 0;
|
|
58
|
+
}
|
|
59
|
+
return Math.round((1 - withIndex / (slotsToday.length - 1)) * 1000000) / 1000000;
|
|
60
|
+
};
|
|
61
|
+
this.priceLevels = [
|
|
62
|
+
{
|
|
63
|
+
code: 'VERY_CHEAP',
|
|
64
|
+
eval: 'X <= 0.60',
|
|
65
|
+
description: 'The price is smaller or equal to 60 % compared to average price.'
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
code: 'CHEAP',
|
|
69
|
+
eval: 'X > 0.60 && X <= 0.90',
|
|
70
|
+
description: 'The price is greater than 60 % and smaller or equal to 90 % compared to average price.'
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
code: 'NORMAL',
|
|
74
|
+
eval: 'X > 0.90 && X < 1.15',
|
|
75
|
+
description: 'The price is greater than 90 % and smaller than 115 % compared to average price.'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
code: 'EXPENSIVE',
|
|
79
|
+
eval: 'X >= 1.15 && X < 1.40',
|
|
80
|
+
description: 'The price is greater or equal to 115 % and smaller than 140 % compared to average price.'
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
code: 'VERY_EXPENSIVE',
|
|
84
|
+
eval: 'X >= 1.40',
|
|
85
|
+
description: 'The price is greater or equal to 140 % compared to average price.'
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
this.priceLevel = function (prices, aDate) {
|
|
89
|
+
var price = _this.currentPrice(prices, aDate);
|
|
90
|
+
var averagePrice = _this.averagePricesStarting(prices, aDate, 0, 24);
|
|
91
|
+
if (price && averagePrice !== 0) {
|
|
92
|
+
try {
|
|
93
|
+
var share = price.price / averagePrice;
|
|
94
|
+
for (var _i = 0, _a = _this.priceLevels; _i < _a.length; _i++) {
|
|
95
|
+
var pl = _a[_i];
|
|
96
|
+
if (eval(pl.eval.split("X").join(String(share)))) {
|
|
97
|
+
return pl;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
this.priceHighLow = function (prices, aDate) {
|
|
106
|
+
var sorted = _this.pricesSorted(prices, aDate);
|
|
107
|
+
var high = sorted.length > 0 ? sorted[sorted.length - 1] : undefined;
|
|
108
|
+
var low = sorted.length > 0 ? sorted[0] : undefined;
|
|
109
|
+
var diffPercentage = high && low && low.price !== 0 ? (high.price - low.price) / low.price * 100 : 0;
|
|
110
|
+
var diffAmount = high && low ? high.price - low.price : 0;
|
|
111
|
+
return {
|
|
112
|
+
high: high,
|
|
113
|
+
low: low,
|
|
114
|
+
diffPercentage: diffPercentage,
|
|
115
|
+
diffAmount: diffAmount
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
this.priceNextHours = function (prices, aDate, num_hours) {
|
|
119
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
120
|
+
var slotCount = _this.hoursToSlotCount(num_hours, slotMinutes);
|
|
121
|
+
var nextSlotStart = _this.alignToSlotStart((0, moment_timezone_1.default)(aDate).add(slotMinutes, 'minutes'), slotMinutes);
|
|
122
|
+
return prices
|
|
123
|
+
.concat()
|
|
124
|
+
.filter(function (p) { return p.startsAt.isSameOrAfter(nextSlotStart); })
|
|
125
|
+
.slice(0, slotCount);
|
|
126
|
+
};
|
|
127
|
+
// TODO: return undefined for missing average?
|
|
128
|
+
this.averagePricesStarting = function (prices, aDate, startHour, num_hours) {
|
|
129
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
130
|
+
var slotCount = _this.hoursToSlotCount(num_hours, slotMinutes);
|
|
131
|
+
var dayStart = (0, moment_timezone_1.default)(aDate).startOf('day');
|
|
132
|
+
var startingAt = _this.alignToSlotStart((0, moment_timezone_1.default)(dayStart).add(startHour, 'hours'), slotMinutes);
|
|
133
|
+
var arr = prices
|
|
134
|
+
.filter(function (p) { return p.startsAt.isSameOrAfter(startingAt); })
|
|
135
|
+
.slice(0, slotCount)
|
|
136
|
+
.map(function (p) { return p.price; });
|
|
137
|
+
if (arr.length === 0) {
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
return Math.round(arr.reduce(function (a, b) { return a + b; }, 0) / arr.length * 10000000) / 10000000;
|
|
141
|
+
};
|
|
142
|
+
this.checkAveragePrice = function (price, averagePrice, below, percentage) {
|
|
143
|
+
return (price - averagePrice) / averagePrice * 100 * (below ? -1 : 1) > percentage;
|
|
144
|
+
};
|
|
145
|
+
this.checkLowPrice = function (prices, low_hours, aDate) {
|
|
146
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
147
|
+
var slotCount = _this.hoursToSlotCount(low_hours, slotMinutes);
|
|
148
|
+
return prices
|
|
149
|
+
.concat()
|
|
150
|
+
.sort(function (a, b) { return a.price - b.price; })
|
|
151
|
+
.slice(0, slotCount)
|
|
152
|
+
.filter(function (p) { return _this.isInSlot(p, aDate); });
|
|
153
|
+
};
|
|
154
|
+
this.checkHighPrice = function (prices, high_hours, aDate) {
|
|
155
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
156
|
+
var slotCount = _this.hoursToSlotCount(high_hours, slotMinutes);
|
|
157
|
+
return prices
|
|
158
|
+
.concat()
|
|
159
|
+
.sort(function (a, b) { return b.price - a.price; })
|
|
160
|
+
.slice(0, slotCount)
|
|
161
|
+
.filter(function (p) { return _this.isInSlot(p, aDate); });
|
|
162
|
+
};
|
|
163
|
+
this.checkHighPrice2 = function (prices, high_hours, aDate, state, filter) {
|
|
164
|
+
if (filter === void 0) { filter = true; }
|
|
165
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
166
|
+
var slotCount = _this.hoursToSlotCount(high_hours, slotMinutes);
|
|
167
|
+
var slotsPerHour = Math.max(1, Math.round(60 / slotMinutes));
|
|
168
|
+
var downSample = slotMinutes >= 60 ? Math.max(2, slotsPerHour) : slotsPerHour;
|
|
169
|
+
return prices
|
|
170
|
+
.map(function (p) {
|
|
171
|
+
var heating = (0, Heating_1.calcHeating)(p.startsAt, state.atHome, state.homeOverride, state.heatingOptions);
|
|
172
|
+
return __assign(__assign({}, p), { heating: heating });
|
|
173
|
+
})
|
|
174
|
+
.filter(function (p) { return p.heating && p.heating.heating === false; })
|
|
175
|
+
.filter(function (_p, idx) { return idx % downSample === 0; })
|
|
176
|
+
.sort(function (a, b) { return b.price - a.price; })
|
|
177
|
+
.slice(0, slotCount)
|
|
178
|
+
.filter(function (p) { return !filter || filter && _this.isInSlot(p, aDate); });
|
|
179
|
+
};
|
|
180
|
+
this.minOfHighestPrices = function (prices, num_hours) {
|
|
181
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
182
|
+
var slotCount = _this.hoursToSlotCount(num_hours, slotMinutes);
|
|
183
|
+
var arr = prices
|
|
184
|
+
.concat()
|
|
185
|
+
.sort(function (a, b) { return b.price - a.price; })
|
|
186
|
+
.slice(0, slotCount);
|
|
187
|
+
if (arr.length > 0) {
|
|
188
|
+
return arr[arr.length - 1];
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
this.maxOfLowestPrices = function (prices, num_hours) {
|
|
192
|
+
var slotMinutes = _this.getSlotMinutes(prices);
|
|
193
|
+
var slotCount = _this.hoursToSlotCount(num_hours, slotMinutes);
|
|
194
|
+
var arr = prices
|
|
195
|
+
.concat()
|
|
196
|
+
.sort(function (a, b) { return a.price - b.price; })
|
|
197
|
+
.slice(0, slotCount);
|
|
198
|
+
if (arr.length > 0) {
|
|
199
|
+
return arr[arr.length - 1];
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
this.pricesAmongLowest = function (prices, aDate, startHour, numHours, numLowestHours) {
|
|
203
|
+
// Finds prices starting at 00:00 today
|
|
204
|
+
var pricesToday = _this.pricesStarting(prices, aDate, 0, 24);
|
|
205
|
+
if (pricesToday.length === 0) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
// Maximum price of lowest hours today
|
|
209
|
+
var maxOfPeriodToday = _this.maxOfLowestPrices(pricesToday, numLowestHours);
|
|
210
|
+
// X following prices
|
|
211
|
+
var pricesFollowing = _this.pricesStarting(prices, aDate, startHour, numHours);
|
|
212
|
+
if (pricesFollowing.length === 0) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
// Maximum price of X following prices
|
|
216
|
+
var maxOfFollowing = _this.maxOfLowestPrices(pricesFollowing, numHours);
|
|
217
|
+
return !!maxOfPeriodToday && !!maxOfFollowing && maxOfFollowing.price <= maxOfPeriodToday.price;
|
|
218
|
+
};
|
|
219
|
+
this.pricesAmongHighest = function (prices, aDate, startHour, numHours, numHighestHours) {
|
|
220
|
+
// Finds prices starting at 00:00 today
|
|
221
|
+
var pricesToday = _this.pricesStarting(prices, aDate, 0, 24);
|
|
222
|
+
if (pricesToday.length === 0) {
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
// Minimum price of highest hours today
|
|
226
|
+
var minOfPeriodToday = _this.minOfHighestPrices(pricesToday, numHighestHours);
|
|
227
|
+
// X following prices
|
|
228
|
+
var pricesFollowing = _this.pricesStarting(prices, aDate, startHour, numHours);
|
|
229
|
+
if (pricesFollowing.length === 0) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
// Minimum price of X following prices
|
|
233
|
+
var minOfFollowing = _this.minOfHighestPrices(pricesFollowing, numHours);
|
|
234
|
+
return !!minOfPeriodToday && !!minOfFollowing && minOfFollowing.price >= minOfPeriodToday.price;
|
|
235
|
+
};
|
|
236
|
+
this.daysPeriod = function (aDate, start, end) {
|
|
237
|
+
var startHour;
|
|
238
|
+
var endHour;
|
|
239
|
+
if (typeof start === 'string') {
|
|
240
|
+
var starts = start.split(':');
|
|
241
|
+
startHour = parseInt(starts[0]) + parseInt(starts[1]) / 60;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
startHour = start;
|
|
245
|
+
}
|
|
246
|
+
if (typeof end === 'string') {
|
|
247
|
+
var ends = end.split(':');
|
|
248
|
+
endHour = parseInt(ends[0]) + parseInt(ends[1]) / 60;
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
endHour = end;
|
|
252
|
+
}
|
|
253
|
+
if (startHour === endHour) {
|
|
254
|
+
endHour += 24;
|
|
255
|
+
}
|
|
256
|
+
var startTs = (0, moment_timezone_1.default)(aDate).startOf('day').add(startHour, 'hour').startOf('minute');
|
|
257
|
+
var endTs = (0, moment_timezone_1.default)(aDate).startOf('day').add(endHour, 'hour').startOf('minute');
|
|
258
|
+
if (startHour >= endHour) {
|
|
259
|
+
if ((0, moment_timezone_1.default)(aDate).isSameOrBefore(endTs)) {
|
|
260
|
+
startTs.add(-1, 'day');
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
endTs.add(1, 'day');
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return { startTs: startTs, endTs: endTs };
|
|
267
|
+
};
|
|
268
|
+
this.pricesLowestInPeriod = function (prices, aDate, startingAt, endingAt, numLowestHours) {
|
|
269
|
+
if (!_this.inPeriod(aDate, startingAt, endingAt)) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
// Finds prices in period
|
|
273
|
+
var pricesInPeriod = prices
|
|
274
|
+
.concat()
|
|
275
|
+
.filter(function (p) { return p.startsAt.isSameOrAfter(startingAt)
|
|
276
|
+
&& p.startsAt.isBefore(endingAt); });
|
|
277
|
+
if (pricesInPeriod.length === 0) {
|
|
278
|
+
// No prices
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
// Maximum price of the lowest
|
|
282
|
+
var maxOfPeriod = _this.maxOfLowestPrices(pricesInPeriod, numLowestHours);
|
|
283
|
+
var current = _this.currentPrice(prices, aDate);
|
|
284
|
+
return !!maxOfPeriod && !!current && current.price <= maxOfPeriod.price;
|
|
285
|
+
};
|
|
286
|
+
this.inPeriod = function (aDate, startingAt, endingAt) {
|
|
287
|
+
return !(0, moment_timezone_1.default)(aDate).isBefore(startingAt) && !(0, moment_timezone_1.default)(aDate).isAfter(endingAt);
|
|
288
|
+
};
|
|
289
|
+
this.pricesHighestInPeriod = function (prices, aDate, startingAt, endingAt, numHighestHours) {
|
|
290
|
+
if (!_this.inPeriod(aDate, startingAt, endingAt)) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
// Finds prices in period
|
|
294
|
+
var pricesInPeriod = prices
|
|
295
|
+
.concat()
|
|
296
|
+
.filter(function (p) { return p.startsAt.isSameOrAfter(startingAt)
|
|
297
|
+
&& p.startsAt.isBefore(endingAt); });
|
|
298
|
+
if (pricesInPeriod.length === 0) {
|
|
299
|
+
// No prices
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
// Minimum price of the highest
|
|
303
|
+
var minOfPeriod = _this.minOfHighestPrices(pricesInPeriod, numHighestHours);
|
|
304
|
+
var current = _this.currentPrice(prices, aDate);
|
|
305
|
+
return !!minOfPeriod && !!current && current.price >= minOfPeriod.price;
|
|
306
|
+
};
|
|
307
|
+
this.checkSumPrices = function (prices, aDate, startingAt, endingAt, hours, low) {
|
|
308
|
+
if (low === void 0) { low = true; }
|
|
309
|
+
if (!_this.inPeriod(aDate, startingAt, endingAt)) {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
var pricesInPeriod = prices
|
|
313
|
+
.filter(function (p) { return p.startsAt.isSameOrAfter(startingAt)
|
|
314
|
+
&& p.startsAt.isBefore(endingAt); });
|
|
315
|
+
if (pricesInPeriod.length === 0) {
|
|
316
|
+
// No prices
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
var slotMinutes = _this.getSlotMinutes(pricesInPeriod);
|
|
320
|
+
var slotCount = _this.hoursToSlotCount(hours, slotMinutes);
|
|
321
|
+
if (slotCount === 0 || pricesInPeriod.length < slotCount) {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
var sumPrices = [];
|
|
325
|
+
for (var a = 0; a <= pricesInPeriod.length - slotCount; a++) {
|
|
326
|
+
var val = {
|
|
327
|
+
startsAt: (0, moment_timezone_1.default)(pricesInPeriod[a].startsAt),
|
|
328
|
+
endsAt: (0, moment_timezone_1.default)(pricesInPeriod[a].startsAt).add(slotCount * slotMinutes, 'minutes'),
|
|
329
|
+
price: 0
|
|
330
|
+
};
|
|
331
|
+
for (var b = 0; b < slotCount; b++) {
|
|
332
|
+
val.price += pricesInPeriod[a + b].price;
|
|
333
|
+
}
|
|
334
|
+
sumPrices.push(val);
|
|
335
|
+
}
|
|
336
|
+
var arr = low ?
|
|
337
|
+
sumPrices
|
|
338
|
+
.sort(function (a, b) { return a.price - b.price; }) :
|
|
339
|
+
sumPrices
|
|
340
|
+
.sort(function (a, b) { return b.price - a.price; });
|
|
341
|
+
if (arr.length > 0
|
|
342
|
+
&& (0, moment_timezone_1.default)(aDate).isSameOrAfter(arr[0].startsAt)
|
|
343
|
+
&& (0, moment_timezone_1.default)(aDate).isBefore(arr[0].endsAt)) {
|
|
344
|
+
return arr[0];
|
|
345
|
+
}
|
|
346
|
+
};
|
|
347
|
+
this.currentPriceLowerThanNext = function (prices, aDate, numHours) {
|
|
348
|
+
var pricesFollowing = _this.priceNextHours(prices, aDate, numHours);
|
|
349
|
+
if (pricesFollowing.length === 0) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
var minOfPeriod = _this.minOfHighestPrices(pricesFollowing, numHours);
|
|
353
|
+
var current = _this.currentPrice(prices, aDate);
|
|
354
|
+
return !!minOfPeriod && !!current && current.price <= minOfPeriod.price;
|
|
355
|
+
};
|
|
356
|
+
this.currentPriceHigherThanNext = function (prices, aDate, numHours) {
|
|
357
|
+
var pricesFollowing = _this.priceNextHours(prices, aDate, numHours);
|
|
358
|
+
if (pricesFollowing.length === 0) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
var maxOfPeriod = _this.maxOfLowestPrices(pricesFollowing, numHours);
|
|
362
|
+
var current = _this.currentPrice(prices, aDate);
|
|
363
|
+
return !!maxOfPeriod && !!current && current.price >= maxOfPeriod.price;
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
PriceApi.prototype.getSlotMinutes = function (prices) {
|
|
367
|
+
var withDuration = prices.find(function (p) { return typeof p.durationMinutes === 'number' && p.durationMinutes > 0; });
|
|
368
|
+
return withDuration ? withDuration.durationMinutes : 60;
|
|
369
|
+
};
|
|
370
|
+
PriceApi.prototype.hoursToSlotCount = function (hours, slotMinutes) {
|
|
371
|
+
if (hours <= 0) {
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
var totalMinutes = Math.round(hours * 60);
|
|
375
|
+
if (totalMinutes % slotMinutes !== 0) {
|
|
376
|
+
throw new Error("Requested hours (".concat(hours, ") is not aligned with slot duration ").concat(slotMinutes));
|
|
377
|
+
}
|
|
378
|
+
return totalMinutes / slotMinutes;
|
|
379
|
+
};
|
|
380
|
+
PriceApi.prototype.alignToSlotStart = function (aDate, slotMinutes) {
|
|
381
|
+
var ts = (0, moment_timezone_1.default)(aDate).startOf('minute');
|
|
382
|
+
var dayStart = (0, moment_timezone_1.default)(ts).startOf('day');
|
|
383
|
+
var minutes = ts.diff(dayStart, 'minutes');
|
|
384
|
+
var remainder = minutes % slotMinutes;
|
|
385
|
+
if (remainder === 0) {
|
|
386
|
+
return ts;
|
|
387
|
+
}
|
|
388
|
+
return (0, moment_timezone_1.default)(ts).add(-remainder, 'minutes');
|
|
389
|
+
};
|
|
390
|
+
PriceApi.prototype.resolveEndsAt = function (price) {
|
|
391
|
+
if (price.endsAt) {
|
|
392
|
+
return (0, moment_timezone_1.default)(price.endsAt);
|
|
393
|
+
}
|
|
394
|
+
var duration = price.durationMinutes || 60;
|
|
395
|
+
return (0, moment_timezone_1.default)(price.startsAt).add(duration, 'minutes');
|
|
396
|
+
};
|
|
397
|
+
PriceApi.prototype.isInSlot = function (price, aDate) {
|
|
398
|
+
var ts = (0, moment_timezone_1.default)(aDate);
|
|
399
|
+
var endsAt = this.resolveEndsAt(price);
|
|
400
|
+
return !ts.isBefore(price.startsAt) && ts.isBefore(endsAt);
|
|
401
|
+
};
|
|
402
|
+
return PriceApi;
|
|
403
|
+
}());
|
|
404
|
+
exports.PriceApi = PriceApi;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { Moment } from 'moment-timezone';
|
|
2
|
+
import { NordpoolPrices } from "./types";
|
|
3
|
+
import { PriceApi } from "./PriceApi";
|
|
4
|
+
export declare class PriceComparer {
|
|
5
|
+
_prices: NordpoolPrices | undefined;
|
|
6
|
+
_priceApi: PriceApi;
|
|
7
|
+
_debug: boolean;
|
|
8
|
+
constructor(priceApi: PriceApi);
|
|
9
|
+
updatePrices(prices: NordpoolPrices): void;
|
|
10
|
+
debug(debug: boolean): void;
|
|
11
|
+
logDebug(msg: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* Current price is !{{among|not among}} the [[high_hours]] hours of the days highest prices.
|
|
14
|
+
*
|
|
15
|
+
* Condition to check if the current price is among the today's highest prices.
|
|
16
|
+
* Specify the number of hours with highest prices to compare against.
|
|
17
|
+
*
|
|
18
|
+
* @param args
|
|
19
|
+
* @param state
|
|
20
|
+
* @param curTime
|
|
21
|
+
*/
|
|
22
|
+
highHoursComparer(args: {
|
|
23
|
+
high_hours: number;
|
|
24
|
+
}, state: {
|
|
25
|
+
high_price: boolean;
|
|
26
|
+
}, curTime?: Moment): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Current price is !{{among|not among}} the [[low_hours]] hours of the days lowest prices.
|
|
29
|
+
*
|
|
30
|
+
* Condition to check if the current price is among the today's lowest prices.
|
|
31
|
+
* Specify the number of hours with lowest prices to compare against.
|
|
32
|
+
*
|
|
33
|
+
* @param args
|
|
34
|
+
* @param state
|
|
35
|
+
* @param curTime
|
|
36
|
+
*/
|
|
37
|
+
lowHoursComparer(args: {
|
|
38
|
+
low_hours: number;
|
|
39
|
+
}, state: {
|
|
40
|
+
low_price: boolean;
|
|
41
|
+
}, curTime?: Moment): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Check if current price is X % below / above average price for today or average price for next Y hours.
|
|
44
|
+
*
|
|
45
|
+
* @param args
|
|
46
|
+
* @param state
|
|
47
|
+
* @param curTime
|
|
48
|
+
*/
|
|
49
|
+
priceAvgComparer(args: {
|
|
50
|
+
percentage: number;
|
|
51
|
+
hours?: number;
|
|
52
|
+
}, state: {
|
|
53
|
+
below: boolean;
|
|
54
|
+
}, curTime?: Moment): boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Current price is !{{|not}} among the [[low_hours]] hours of lowest between [[start]] and [[end]].
|
|
57
|
+
*
|
|
58
|
+
* Condition to check if the current price is among the X lowest in a period.
|
|
59
|
+
*
|
|
60
|
+
* @param args
|
|
61
|
+
* @param state
|
|
62
|
+
* @param curTime
|
|
63
|
+
*/
|
|
64
|
+
priceAmongLowestComparer(args: {
|
|
65
|
+
low_hours: number;
|
|
66
|
+
hours?: number;
|
|
67
|
+
}, state: any, curTime?: Moment): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* The following [[hours]] hours are !{{|not}} among the [[high_hours]] hours of today's highest prices.
|
|
70
|
+
*
|
|
71
|
+
* Condition to check if one of the following hours are among the today's highest prices.
|
|
72
|
+
* Specify the number of hours from now, including the current hour and the number of hours
|
|
73
|
+
* with highest prices to compare against.
|
|
74
|
+
*
|
|
75
|
+
* @param args
|
|
76
|
+
* @param state
|
|
77
|
+
* @param curTime
|
|
78
|
+
*/
|
|
79
|
+
priceAmongHighestComparer(args: {
|
|
80
|
+
high_hours: number;
|
|
81
|
+
hours?: number;
|
|
82
|
+
}, state: any, curTime?: Moment): boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Current price is !{{|not}} among the [[low_hours]] hours of lowest between [[start]] and [[end]].
|
|
85
|
+
*
|
|
86
|
+
* Condition to check if the current price is among the X lowest in a period.
|
|
87
|
+
*
|
|
88
|
+
* @param args
|
|
89
|
+
* @param state
|
|
90
|
+
* @param curTime
|
|
91
|
+
*/
|
|
92
|
+
priceLowestInPeriodComparer(args: {
|
|
93
|
+
start: number | string;
|
|
94
|
+
end: number | string;
|
|
95
|
+
low_hours: number;
|
|
96
|
+
}, state: any, curTime?: Moment): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Current price is !{{|not}} among the [[high_hours]] hours of highest between [[start]] and [[end]].
|
|
99
|
+
*
|
|
100
|
+
* Condition to check if the current price is among the X highest in a period.
|
|
101
|
+
*
|
|
102
|
+
* @param args
|
|
103
|
+
* @param state
|
|
104
|
+
* @param curTime
|
|
105
|
+
*/
|
|
106
|
+
priceHighestInPeriodComparer(args: {
|
|
107
|
+
start: number | string;
|
|
108
|
+
end: number | string;
|
|
109
|
+
high_hours: number;
|
|
110
|
+
}, state: any, curTime?: Moment): boolean;
|
|
111
|
+
/**
|
|
112
|
+
* The following [[hours]] consecutive hours have !{{|not}} the lowest total price between [[start]] and [[end]].
|
|
113
|
+
*
|
|
114
|
+
* Condition for checking whether the next hours in a row have the lowest total price in a period.
|
|
115
|
+
* Use '00:00' for both start and end for the current day.
|
|
116
|
+
*
|
|
117
|
+
* @param args
|
|
118
|
+
* @param state
|
|
119
|
+
* @param curTime
|
|
120
|
+
*/
|
|
121
|
+
priceLowestNextHoursComparer(args: {
|
|
122
|
+
start: number | string;
|
|
123
|
+
end: number | string;
|
|
124
|
+
hours: number;
|
|
125
|
+
}, state: any, curTime?: Moment): boolean;
|
|
126
|
+
/**
|
|
127
|
+
* The following [[hours]] consecutive hours have !{{|not}} the highest total price between [[start]] and [[end]].
|
|
128
|
+
*
|
|
129
|
+
* Condition for checking whether the next hours in a row have the highest total price in a period.
|
|
130
|
+
* Use '00:00' for both start and end for the current day.
|
|
131
|
+
*
|
|
132
|
+
* @param args
|
|
133
|
+
* @param state
|
|
134
|
+
* @param curTime
|
|
135
|
+
*/
|
|
136
|
+
priceHighestNextHoursComparer(args: {
|
|
137
|
+
start: number | string;
|
|
138
|
+
end: number | string;
|
|
139
|
+
hours: number;
|
|
140
|
+
}, state: any, curTime?: Moment): boolean;
|
|
141
|
+
/**
|
|
142
|
+
* Difference between highest and lowest price is !{{less|more}} than [[percentage]] %.
|
|
143
|
+
*
|
|
144
|
+
* Condition to check if the difference between the highest and the lowest price for
|
|
145
|
+
* the whole day is less or more than X percent.
|
|
146
|
+
*
|
|
147
|
+
* @param args
|
|
148
|
+
* @param state
|
|
149
|
+
* @param curTime
|
|
150
|
+
*/
|
|
151
|
+
priceDiffHighLowComparer(args: {
|
|
152
|
+
percentage: number;
|
|
153
|
+
}, state: any, curTime?: Moment): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Difference between highest and lowest price is !{{less|more}} than [[amount]].
|
|
156
|
+
*
|
|
157
|
+
* Condition to check if the difference between the highest and the lowest price
|
|
158
|
+
* for the whole day is less or more than X.
|
|
159
|
+
*
|
|
160
|
+
* @param args
|
|
161
|
+
* @param state
|
|
162
|
+
* @param curTime
|
|
163
|
+
*/
|
|
164
|
+
priceDiffHighLowComparer2(args: {
|
|
165
|
+
amount: number;
|
|
166
|
+
}, state: any, curTime?: Moment): boolean;
|
|
167
|
+
/**
|
|
168
|
+
* Current price is !{{|not}} lower than prices next [[hours]] hours.
|
|
169
|
+
*
|
|
170
|
+
* Condition to check if the current price is lower than prices next hours.
|
|
171
|
+
*
|
|
172
|
+
* @param args
|
|
173
|
+
* @param state
|
|
174
|
+
* @param curTime
|
|
175
|
+
*/
|
|
176
|
+
priceLowerNextHoursComparer(args: {
|
|
177
|
+
hours: number;
|
|
178
|
+
}, state: any, curTime?: Moment): boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Current price is !{{|not}} higher than prices next [[hours]] hours.
|
|
181
|
+
*
|
|
182
|
+
* Condition to check if the current price is higher than prices next hours.
|
|
183
|
+
*
|
|
184
|
+
* @param args
|
|
185
|
+
* @param state
|
|
186
|
+
* @param curTime
|
|
187
|
+
*/
|
|
188
|
+
priceHigherNextHoursComparer(args: {
|
|
189
|
+
hours: number;
|
|
190
|
+
}, state: any, curTime?: Moment): boolean;
|
|
191
|
+
/**
|
|
192
|
+
* High prices [[high_hours]] hours of the day.
|
|
193
|
+
*
|
|
194
|
+
* Triggers when the utility price changes, and the price is among the highest prices of the day.
|
|
195
|
+
*
|
|
196
|
+
* @param args
|
|
197
|
+
* @param state
|
|
198
|
+
* @param curTime
|
|
199
|
+
*/
|
|
200
|
+
heatingOffHighPriceComparer(args: {
|
|
201
|
+
high_hours: number;
|
|
202
|
+
}, state: {
|
|
203
|
+
high_price: boolean;
|
|
204
|
+
}, curTime?: Moment): boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Current price is !{{|not}} among the [[num_hours]] highest / lowest hours before [[time]]
|
|
207
|
+
*
|
|
208
|
+
* Condition to check if the current price is among the X highest / lowest hours before a time of day.
|
|
209
|
+
*
|
|
210
|
+
* @param args
|
|
211
|
+
* @param state
|
|
212
|
+
* @param curTime
|
|
213
|
+
*/
|
|
214
|
+
currentPriceAmongBeforeTimeComparer(args: {
|
|
215
|
+
num_hours: number;
|
|
216
|
+
time: number | string;
|
|
217
|
+
}, state: {
|
|
218
|
+
high_price: boolean;
|
|
219
|
+
}, curTime?: Moment): boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Current price is !{{|not}} among the [[num_hours]] highest / lowest hours in the next [[next_hours]] hours
|
|
222
|
+
*
|
|
223
|
+
* Condition to check if the current price is among the X highest / lowest hours the next Y hours.
|
|
224
|
+
*
|
|
225
|
+
* @param args
|
|
226
|
+
* @param state
|
|
227
|
+
* @param curTime
|
|
228
|
+
*/
|
|
229
|
+
currentPriceAmongNextHoursComparer(args: {
|
|
230
|
+
num_hours: number;
|
|
231
|
+
next_hours: number;
|
|
232
|
+
}, state: {
|
|
233
|
+
high_price: boolean;
|
|
234
|
+
}, curTime?: Moment): boolean;
|
|
235
|
+
}
|