-
Notifications
You must be signed in to change notification settings - Fork 16
/
fetchprices.js
executable file
·443 lines (381 loc) · 15.7 KB
/
fetchprices.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/env node
'use strict';
const programName = 'fetchprices';
const fs = require('fs');
const axios = require('axios');
const MQTTClient = require('./mqtt/mqtt');
const UniCache = require('./misc/unicache');
const { addZero, skewDays, loadYaml, getNextDate } = require('./misc/util.js');
const { format, formatISO, parseISO } = require('date-fns');
// Specific for ENTSO-E
const convert = require('xml-js');
const { exit } = require('process');
const config = loadYaml('./config.yaml');
const regionMap = loadYaml('./priceregions.yaml');
//const nordPoolUrl = config.nordpoolBaseUrl || 'https://www.nordpoolgroup.com/api/marketdata/page/10';
const nordPoolUrl = `https://dataportal-api.nordpoolgroup.com/api/DayAheadPrices?market=DayAhead`;
//const url = `${nordPoolUrl}&deliveryArea=${this.regionCode}¤cy=${this.priceCurrency}&date=${urlDate}`;
const baseUrl = config.entsoeBaseUrl || 'https://web-api.tp.entsoe.eu/api';
const entsoeToken = config.priceAccessToken || null;
//const priceRegion = config.priceRegion || 8; // Oslo
const region = config.regionCode || 'NO1';
const regionCode = regionMap[region];
const priceFetchPriority = config.priceFetchPriority || 'nordpool';
const pricePath = config.priceFilePath || './data/prices';
const pricePrefix = 'prices-';
const priceCurrency = config.priceCurrency || 'NOK';
const currencyPath = config.currencyFilePath || './data/currencies';
const currencyPrefix = 'currencies-';
// Common constants
const debug = config.DEBUG || false;
const priceTopic = config.priceTopic || 'elwiz/prices';
const keepDays = config.keepDays || 7;
const spotVatPercent = config.spotVatPercent || 0;
const supplierDayPrice = config.supplierDayPrice || 0;
const supplierMonthPrice = config.supplierMonthPrice || 0;
const supplierVatPercent = config.supplierVatPercent || 0;
const gridDayPrice = config.gridDayPrice || 0;
const gridMonthPrice = config.gridMonthPrice || 0;
const gridVatPercent = config.gridVatPercent || 0;
//const dayHoursStart = parseInt(config.dayHoursStart.split(':')[0]) || 6;
//const dayHoursEnd = parseInt(config.dayHoursEnd.split(':')[0]) || 22;
const dayHoursStart = config.dayHoursStart || 6;
const dayHoursEnd = config.dayHoursEnd || 22;
const energyDayPrice = config.energyDayPrice || 0;
const energyNightPrice = config.energyNightPrice || 0;
const cacheType = config.cacheType || 'file';
const mqttUrl = config.mqttUrl || 'mqtt://localhost:1883';
const mqttOpts = config.mqttOptions;
const mqttClient = new MQTTClient(mqttUrl, mqttOpts, 'hassPublish');
let gridDayHourPrice;
let gridNightHourPrice;
let gridFixedPrice;
let supplierFixedPrice;
const runNodeSchedule = config.runNodeSchedule;
const scheduleHours = config.scheduleHours;
const scheduleMinutes = config.scheduleMinutes;
let schedule;
let runSchedule;
if (runNodeSchedule) {
schedule = require('node-schedule');
runSchedule = new schedule.RecurrenceRule();
runSchedule.hour = scheduleHours;
runSchedule.minute = scheduleMinutes;
}
// UniCache options
const PRICE_DB_PREFIX = pricePrefix || 'prices-';
const PRICE_DB_OPTIONS = {
cacheType: cacheType,
syncOnWrite: true,
savePath: pricePath,
};
const priceDb = new UniCache(PRICE_DB_PREFIX, PRICE_DB_OPTIONS);
const CURR_DB_PREFIX = currencyPrefix || 'currencies-';
const CURR_DB_OPTIONS = {
cacheType: cacheType,
syncOnWrite: false,
savePath: currencyPath,
};
const currencyDb = new UniCache(`${CURR_DB_PREFIX}latest`, CURR_DB_OPTIONS);
const nordPoolOpts = {
headers: {
accept: 'application/json',
'Content-Type': 'text/json',
},
json: true,
};
const entsoeOpts = {
method: 'get',
headers: {
accept: 'application/xml',
'Content-Type': 'application/xml',
},
};
let runCounter = 0;
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
let currencyRate;
async function getCurrencyRate(currency) {
if (await currencyDb.existsObject(`${CURR_DB_PREFIX}latest`)) {
const obj = await currencyDb.retrieveObject(`${CURR_DB_PREFIX}latest`);
let ret = obj.rates[currency];
return ret;
} else {
console.log('Error: no currency object present');
console.log(`Please run "./fetch-eu-currencies.js"`);
exit(0);
}
}
async function nordpoolDate(offset) {
const oneDay = 24 * 60 * 60 * 1000;
const now = new Date();
const date = new Date(now.getTime() + oneDay * offset);
const ret = format(date, 'yyyy-MM-dd');
return ret;
}
function xnordpoolDate(offset) {
let date = new Date();
date.setHours(0, 0, 0, 0); // Set to local midnight
date.setDate(date.getDate() + offset); // Apply day offset
const timezoneOffset = date.getTimezoneOffset() * 60000;
// Adjust the local midnight to UTC by adding the timezone offset
const utcDate = new Date(date.getTime() + timezoneOffset);
// Format the UTC date as 'yyyyMMddHHmm'
const formattedUtcDate = format(utcDate, 'yyyy-MM-dd');
return formattedUtcDate;
}
function entsoeDate(offset) {
let date = new Date();
date.setHours(0, 0, 0, 0); // Set to local midnight
date.setDate(date.getDate() + offset); // Apply day offset
const timezoneOffset = date.getTimezoneOffset() * 60000;
// Adjust the local midnight to UTC by adding the timezone offset
const utcDate = new Date(date.getTime() + timezoneOffset);
// Format the UTC date as 'yyyyMMddHHmm'
const formattedUtcDate = format(utcDate, 'yyyyMMddHHmm');
return formattedUtcDate;
}
function utcToLocalDateTime(isoString) {
// If no argument is provided, use the current time
const date = isoString ? parseISO(isoString) : new Date();
return formatISO(date, { representation: 'complete' });
}
function averageCalc(arr, key, start = 0, end) {
if (end === undefined) {
end = arr.length - 1;
}
start = start < 0 ? 0 : start;
end = end >= arr.length ? arr.length - 1 : end;
let sum = 0;
let count = 0;
for (let i = start; i <= end; i++) {
if (arr[i] && arr[i][key] !== undefined) {
sum += arr[i][key];
count++;
}
}
return count > 0 ? sum / count : null;
}
async function getNordPoolPrices(dayOffset) {
const priceDate = skewDays(dayOffset);
const priceName = PRICE_DB_PREFIX + priceDate;
const missingPrice = !(await priceDb.existsObject(priceName));
let oneDayPrices;
if (missingPrice) {
const url = `${nordPoolUrl}&deliveryArea=${region}¤cy=${priceCurrency}&date=${await nordpoolDate(dayOffset)}`;
console.log(`Fetching: ${url}`);
console.log(`Fetching: ${priceName}`);
try {
const response = await axios.get(url, nordPoolOpts);
if (response.status === 200 && response.data) {
const data = response.data;
const hourly = data.multiAreaEntries;
let minPrice = 9999;
let maxPrice = 0;
oneDayPrices = {
priceDate: priceDate,
priceProvider: 'Nord Pool',
priceProviderUrl: url,
hourly: [],
daily: {},
};
for (let curHour = 0; curHour <= 23; curHour++) {
const floatingPrice =
curHour >= dayHoursStart && curHour < dayHoursEnd ? gridDayHourPrice : gridNightHourPrice;
let spotPrice = hourly[curHour].entryPerArea[region] / 1000;
spotPrice += (spotPrice * spotVatPercent) / 100;
const priceObj = {
startTime: utcToLocalDateTime(hourly[curHour].deliveryStart),
ensTime: utcToLocalDateTime(hourly[curHour].deliveryEnd),
spotPrice: parseFloat(spotPrice.toFixed(4)),
floatingPrice: floatingPrice,
fixedPrice: gridFixedPrice + supplierFixedPrice
}
oneDayPrices.hourly.push(priceObj);
minPrice = spotPrice < minPrice ? spotPrice : minPrice;
maxPrice = spotPrice > maxPrice ? spotPrice : maxPrice;
}
oneDayPrices.daily = {
minPrice: parseFloat((minPrice + (minPrice * spotVatPercent) / 100).toFixed(4)),
maxPrice: parseFloat((maxPrice + (maxPrice * spotVatPercent) / 100).toFixed(4)),
avgPrice: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice').toFixed(4)),
peakPrice: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice', dayHoursStart, dayHoursEnd - 1).toFixed(4)),
offPeakPrice1: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice', 0, dayHoursStart - 1).toFixed(4)),
offPeakPrice2: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice', dayHoursEnd, 23).toFixed(4)),
};
// Store to cache
await priceDb.createObject(priceName, oneDayPrices);
} else {
console.log(`getNordPoolPrices: Day ahead prices are not ready: ${priceName}`);
}
} catch (err) {
if (err.response) {
//console.log('Error:', err.response.status, err.response.statusText);
if (debug) console.log(`Headers:\n${err.response.headers}`);
}
}
return true;
} else {
return false;
}
}
function entsoeUrl(entsoeToken, region, periodStart, periodEnd) {
return `${baseUrl}?documentType=A44&securityToken=${entsoeToken}&in_Domain=${region}&out_Domain=${region}&periodStart=${periodStart}&periodEnd=${periodEnd}`;
}
async function getEntsoePrices(dayOffset) {
const priceDate = skewDays(dayOffset);
const priceName = PRICE_DB_PREFIX + priceDate;
const missingPrice = !(await priceDb.existsObject(priceName));
let oneDayPrices;
if (missingPrice) {
const url = entsoeUrl(entsoeToken, regionCode, entsoeDate(dayOffset), entsoeDate(dayOffset + 1));
await axios.get(url, entsoeOpts)
.then(async function (body) {
const result = convert.xml2js(body.data, { compact: true, spaces: 4 });
if (result.Publication_MarketDocument !== undefined) {
const realMeat = result.Publication_MarketDocument.TimeSeries.Period;
if (realMeat !== undefined) {
console.log(`Fetching: ${priceName}`);
} else {
console.log(`Prices are not available: ${priceDate}`);
return; // Exit the function early if prices are not available
}
let minPrice = 9999;
let maxPrice = 0;
oneDayPrices = {
priceDate: priceDate,
priceProvider: "ENTSO-E",
priceProviderUrl: entsoeUrl("*****", priceRegion, entsoeDate(dayOffset), entsoeDate(dayOffset + 1)),
hourly: [],
daily: {},
};
for (let curHour = 0; curHour <= 23; curHour++) {
const floatingPrice =
curHour >= dayHoursStart && curHour < dayHoursEnd ? gridDayHourPrice : gridNightHourPrice;
let spotPrice = (realMeat.Point[curHour]['price.amount']._text * currencyRate) / 1000;
spotPrice += (spotPrice * spotVatPercent) / 100;
const priceObj = {
startTime: utcToLocalDateTime(hourly[curHour].deliveryStart),
ensTime: utcToLocalDateTime(hourly[curHour].deliveryEnd),
spotPrice: parseFloat(spotPrice.toFixed(4)),
floatingPrice: floatingPrice,
fixedPrice: gridFixedPrice + supplierFixedPrice,
};
oneDayPrices.hourly.push(priceObj);
minPrice = spotPrice < minPrice ? spotPrice : minPrice;
maxPrice = spotPrice > maxPrice ? spotPrice : maxPrice;
}
oneDayPrices.daily = {
minPrice: parseFloat((minPrice + (minPrice * spotVatPercent) / 100).toFixed(4)),
maxPrice: parseFloat((maxPrice + (maxPrice * spotVatPercent) / 100).toFixed(4)),
avgPrice: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice').toFixed(4)),
peakPrice: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice', dayHoursStart, dayHoursEnd - 1).toFixed(4)),
offPeakPrice1: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice', 0, dayHoursStart - 1).toFixed(4)),
offPeakPrice2: parseFloat(averageCalc(oneDayPrices.hourly, 'spotPrice', dayHoursEnd, 23).toFixed(4)),
};
// Store to cache
await priceDb.createObject(priceName, oneDayPrices);
} else {
console.log(`getEntsoePrices: Day ahead prices are not ready: ${priceDate}`);
}
})
.catch(function (err) {
if (err.response) {
if (debug) console.log(`Headers:\n${err.response.headers}`);
console.log(`Error:' ${err.response.status}: ${err.response.statusText}`);
if (err.response.status === 401) {
console.log('The Entso-E API requires an access token. Please see https://transparency.entsoe.eu/content/static_content/download?path=/Static%20content/API-Token-Management.pdf');
}
process.exit(1);
}
});
return true;
} else {
return false;
}
}
async function publishMqtt(priceDate, priceObject) {
await mqttClient.waitForConnect();
const topic = `${priceTopic}/${priceDate}`;
try {
if (priceObject === null) {
// Remove old retained prices
await mqttClient.publish(topic, '', { retain: true, qos: 1 });
console.log(`${programName}: MQTT message removed: ${PRICE_DB_PREFIX}${priceDate}`);
} else {
// Publish today and next day prices
await mqttClient.publish(
topic,
JSON.stringify(priceObject, debug ? null : undefined, 2),
{ retain: true, qos: 1 }
);
console.log(`${programName}: MQTT message published: ${PRICE_DB_PREFIX}${priceDate}`);
}
} catch (err) {
console.log(`${programName}: MQTT message error`, err);
}
}
async function retireDays(offset) {
offset *= -1;
const priceDate = skewDays(offset);
const keys = await priceDb.dbKeys(PRICE_DB_PREFIX + '*');
keys.forEach(async (key) => {
if (key <= `${PRICE_DB_PREFIX}${priceDate}`) {
await priceDb.deleteObject(key);
}
});
}
async function init() {
let nightPrice = energyNightPrice + (energyNightPrice * gridVatPercent) / 100;
gridNightHourPrice = parseFloat(nightPrice.toFixed(4));
let dayPrice = energyDayPrice + (energyDayPrice * gridVatPercent) / 100;
gridDayHourPrice = parseFloat(dayPrice.toFixed(4));
let fixedPrice = gridDayPrice / 24;
fixedPrice += gridMonthPrice / 720;
fixedPrice += (fixedPrice * gridVatPercent) / 100;
gridFixedPrice = parseFloat(fixedPrice.toFixed(4));
fixedPrice = supplierDayPrice / 24;
fixedPrice += supplierMonthPrice / 720;
fixedPrice += (fixedPrice * supplierVatPercent) / 100;
supplierFixedPrice = parseFloat(fixedPrice.toFixed(4));
}
async function run() {
if (runNodeSchedule) {
console.log('Fetch prices scheduled run...');
}
await retireDays(keepDays);
for (let i = (keepDays - 1) * -1; i <= 1; i++) {
if (!await priceDb.existsObject(`${PRICE_DB_PREFIX}${skewDays(i)}`)) {
if (priceFetchPriority === "nordpool") {
const success = await getNordPoolPrices(i);
if (!success) {
currencyRate = await getCurrencyRate(priceCurrency);
await getEntsoePrices(i);
}
} else {
currencyRate = await getCurrencyRate(priceCurrency);
const success = await getEntsoePrices(i);
if (!success) {
await getNordPoolPrices(i);
}
}
}
}
await delay(2000);
if (await priceDb.existsObject(`${PRICE_DB_PREFIX}${skewDays(1)}`)) {
console.log('NextDayAvailable')
await publishMqtt(skewDays(-1), null);
//await publishMqtt(skewDays(-1), await priceDb.retrieveObject(`${PRICE_DB_PREFIX}${skewDays(-1)}`));
await publishMqtt(skewDays(0), await priceDb.retrieveObject(`${PRICE_DB_PREFIX}${skewDays(0)}`));
await publishMqtt(skewDays(1), await priceDb.retrieveObject(`${PRICE_DB_PREFIX}${skewDays(1)}`));
} else {
await publishMqtt(skewDays(-2), null);
await publishMqtt(skewDays(-1), await priceDb.retrieveObject(`${PRICE_DB_PREFIX}${skewDays(-1)}`));
await publishMqtt(skewDays(0), await priceDb.retrieveObject(`${PRICE_DB_PREFIX}${skewDays(0)}`));
}
}
init();
if (runNodeSchedule) {
schedule.scheduleJob(runSchedule, run);
}
console.log(`${programName}: Fetch prices starting...`);
run();