node-red-contrib-power-saver 5.1.1 → 5.1.3

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.3",
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;
@@ -201,6 +201,10 @@ module.exports = function (RED) {
201
201
 
202
202
  // Set lights to night level after delay
203
203
  nightActivationTimer = setTimeout(() => {
204
+ if (state.timedOut || !funcs.isNightMode(nodeConfig)) {
205
+ debugLog("Skipping night level change: lights are off or night mode no longer active");
206
+ return;
207
+ }
204
208
  debugLog("Applying night level to lights");
205
209
  const level = funcs.findCurrentLevel(nodeConfig, nodeWrapper);
206
210
  if (level !== null) {
@@ -223,6 +227,10 @@ module.exports = function (RED) {
223
227
 
224
228
  // Set lights to away level after delay
225
229
  awayActivationTimer = setTimeout(() => {
230
+ if (state.timedOut || !funcs.isAwayMode(nodeConfig)) {
231
+ debugLog("Skipping away level change: lights are off or away mode no longer active");
232
+ return;
233
+ }
226
234
  debugLog("Applying away level to lights");
227
235
  const level =
228
236
  nodeConfig.awaySensor.level !== null && nodeConfig.awaySensor.level !== undefined
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: [],