node-red-contrib-power-saver 1.0.6 → 2.0.0

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.
@@ -1,3 +1,4 @@
1
+ const { DateTime } = require("luxon");
1
2
  const expect = require("expect");
2
3
  const {
3
4
  sortedIndex,
@@ -9,6 +10,8 @@ const {
9
10
  makeSchedule,
10
11
  fillArray,
11
12
  convertMsg,
13
+ extractPlanForDate,
14
+ isSameDate,
12
15
  } = require("../utils");
13
16
 
14
17
  describe("utils", () => {
@@ -232,4 +235,108 @@ describe("utils", () => {
232
235
  ...msgStdTodayOnly.payload,
233
236
  });
234
237
  });
238
+
239
+ it("can compare dates", () => {
240
+ const date1 = "2021-06-20T01:50:00.000+02:00";
241
+ expect(isSameDate(date1, "2021-06-20T01:50:00.000+02:00")).toBeTruthy();
242
+ expect(isSameDate(date1, "2021-06-21T01:50:00.000+02:00")).toBeFalsy();
243
+ expect(isSameDate(date1, "2021-06-20")).toBeTruthy();
244
+ expect(isSameDate(date1, "2021-06-21")).toBeFalsy();
245
+ });
246
+
247
+ it("can extract plan for a date", () => {
248
+ const plan = {
249
+ hours: [
250
+ {
251
+ price: 0.3,
252
+ onOff: true,
253
+ start: "2021-06-20T01:50:00.000+02:00",
254
+ saving: 1,
255
+ },
256
+ {
257
+ price: 0.4,
258
+ onOff: false,
259
+ start: "2021-06-20T01:50:00.010+02:00",
260
+ saving: 2,
261
+ },
262
+ {
263
+ price: 0.2,
264
+ onOff: false,
265
+ start: "2021-06-21T01:50:00.180+02:00",
266
+ saving: 3,
267
+ },
268
+ {
269
+ price: 0.85,
270
+ onOff: true,
271
+ start: "2021-06-21T01:50:00.190+02:00",
272
+ saving: null,
273
+ },
274
+ ],
275
+ schedule: [
276
+ {
277
+ time: "2021-06-20T01:50:00.000+02:00",
278
+ value: true,
279
+ },
280
+ {
281
+ time: "2021-06-21T01:50:00.020+02:00",
282
+ value: false,
283
+ },
284
+ {
285
+ time: "2021-06-21T01:50:00.040+02:00",
286
+ value: false,
287
+ },
288
+ ],
289
+ };
290
+ const part1 = {
291
+ hours: [
292
+ {
293
+ price: 0.3,
294
+ onOff: true,
295
+ start: "2021-06-20T01:50:00.000+02:00",
296
+ saving: 1,
297
+ },
298
+ {
299
+ price: 0.4,
300
+ onOff: false,
301
+ start: "2021-06-20T01:50:00.010+02:00",
302
+ saving: 2,
303
+ },
304
+ ],
305
+ schedule: [
306
+ {
307
+ time: "2021-06-20T01:50:00.000+02:00",
308
+ value: true,
309
+ },
310
+ ],
311
+ };
312
+ const part2 = {
313
+ hours: [
314
+ {
315
+ price: 0.2,
316
+ onOff: false,
317
+ start: "2021-06-21T01:50:00.180+02:00",
318
+ saving: 3,
319
+ },
320
+ {
321
+ price: 0.85,
322
+ onOff: true,
323
+ start: "2021-06-21T01:50:00.190+02:00",
324
+ saving: null,
325
+ },
326
+ ],
327
+ schedule: [
328
+ {
329
+ time: "2021-06-21T01:50:00.020+02:00",
330
+ value: false,
331
+ },
332
+ {
333
+ time: "2021-06-21T01:50:00.040+02:00",
334
+ value: false,
335
+ },
336
+ ],
337
+ };
338
+ expect(extractPlanForDate(plan, "2021-06-20T01:50:00.000+02:00")).toEqual(
339
+ part1
340
+ );
341
+ });
235
342
  });
package/utils.js CHANGED
@@ -1,3 +1,5 @@
1
+ const { DateTime } = require("luxon");
2
+
1
3
  /**
2
4
  * Get today and tomorrow data out of the input message.
3
5
  * Can accept 3 types of messages: Tibber, Nordpool or plain payload with data already converted.
@@ -19,6 +21,12 @@ function convertMsg(msg) {
19
21
  value: v.value,
20
22
  start: v.start,
21
23
  }));
24
+ } else if (msg.data?.attributes?.raw_today) {
25
+ source = "Nordpool";
26
+ today = msg.data.attributes.raw_today.map((v) => ({
27
+ value: v.value,
28
+ start: v.start,
29
+ }));
22
30
  } else {
23
31
  source = "Other";
24
32
  today = msg.payload?.today || [];
@@ -81,10 +89,10 @@ function getDiffToNextOn(values, onOff, nextOn = null) {
81
89
  const res = values.map((p, i, a) => {
82
90
  for (let n = i + 1; n < a.length; n++) {
83
91
  if (onOff[n]) {
84
- return values[i] - values[n];
92
+ return Math.round((values[i] - values[n]) * 10000) / 10000;
85
93
  }
86
94
  }
87
- return p - nextOnValue;
95
+ return Math.round((p - nextOnValue) * 10000) / 10000;
88
96
  });
89
97
  return res;
90
98
  }
@@ -198,6 +206,19 @@ function fillArray(value, count) {
198
206
  return res;
199
207
  }
200
208
 
209
+ function extractPlanForDate(plan, day) {
210
+ const part = {};
211
+ part.hours = plan.hours.filter((h) => isSameDate(day, h.start));
212
+ part.schedule = plan.schedule.filter((s) => isSameDate(day, s.time));
213
+ return part;
214
+ }
215
+
216
+ function isSameDate(date1, date2) {
217
+ return (
218
+ DateTime.fromISO(date1).toISODate() === DateTime.fromISO(date2).toISODate()
219
+ );
220
+ }
221
+
201
222
  module.exports = {
202
223
  sortedIndex,
203
224
  getDiffToNextOn,
@@ -208,4 +229,6 @@ module.exports = {
208
229
  makeSchedule,
209
230
  fillArray,
210
231
  convertMsg,
232
+ extractPlanForDate,
233
+ isSameDate,
211
234
  };