node-red-contrib-power-saver 5.1.1 → 5.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-power-saver",
3
- "version": "5.1.1",
3
+ "version": "5.1.2",
4
4
  "description": "A module for Node-RED that you can use to turn on and off a switch based on power prices",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -82,7 +82,8 @@ function makePlanFromPriceData(node, msg, config, doPlanning, calcSavings) {
82
82
  onOff: onOff[i],
83
83
  saving: savings[i],
84
84
  }));
85
- const schedule = makeSchedule(onOff, startTimes, endTime);
85
+ const fullSchedule = makeSchedule(onOff, startTimes, endTime);
86
+ const schedule = trimScheduleToStart(fullSchedule, priceData[0].start);
86
87
  addLastSwitchIfNoSchedule(schedule, minutes, config);
87
88
 
88
89
  plan = {
@@ -158,6 +159,19 @@ function loadDataJustBefore(node, dateDayBefore) {
158
159
  };
159
160
  }
160
161
 
162
+ function trimScheduleToStart(schedule, startTime) {
163
+ const startDT = DateTime.fromISO(startTime);
164
+ const idx = schedule.findIndex((e) => DateTime.fromISO(e.time) >= startDT);
165
+ if (idx === -1) return schedule;
166
+ const trimmed = schedule.slice(idx);
167
+ if (DateTime.fromISO(trimmed[0].time) > startDT) {
168
+ const initialState = (idx > 0 ? schedule[idx - 1] : schedule[0]).value;
169
+ const countMinutes = DateTime.fromISO(trimmed[0].time).diff(startDT, "minutes").minutes;
170
+ trimmed.unshift({ time: startDT.toISO(), value: initialState, countMinutes });
171
+ }
172
+ return trimmed;
173
+ }
174
+
161
175
  function deleteSavedScheduleBefore(node, day, checkDays = 0) {
162
176
  let date = day;
163
177
  let data = null;
package/src/utils.js CHANGED
@@ -215,7 +215,7 @@ function loadDayData(node, date) {
215
215
  // Load saved schedule for the date (YYYY-MM-DD)
216
216
  // Return null if not found
217
217
  const key = date.toISODate();
218
- const saved = node.context().get(key);
218
+ const saved = node.context().get(key, node.contextStorage);
219
219
  const res = saved ?? {
220
220
  schedule: [],
221
221
  minutes: [],