Node-red-contrib-ui-time-scheduler - Cascade settings function

Hi All,

I have a bunch of electric heaters in a small office with 3 rooms. The rooms are not in use all the time so I am using node-red-contrib-ui-time-scheduler to control when the heaters are on/off.

For times when all the heaters need to be on at the same times, I created the following function which reads the scheduler config passed to it and clones the settings for the first timer (output 0) to all other outputs, so every smart plug is setup the same. This saves time in programming everything.

The scheduler config comes from reading the settings saved in a file, which can be picked up and injected into the procedure using a read file node.

The scheduler's author explains how the config file can be saved and retrieved here:

This makes the complete flow:

[Trigger button on dashboard] --> [Read file] --> [Procedure] --> [node-red-contrib-ui-time-scheduler]

Hope this is of use to someone.

/*

Timer-cascade

V1.0 N. Kendrick 14-Apr-2024 (linker3000@gmail.com)

For node-red-contrib-ui-time-scheduler

Read the settings for output 0 and duplicate them for the other outputs.
The resulting payload can be loaded into the timer and/or saved to a config file.

No warranties, use at your own risk etc..

No formal support.

MIT Licence

*/

// Read the config file in the msg payload. Exit gracefully if it's empty
// or invalid.
try {
var jsonData = JSON.parse(msg.payload);} catch (e) {
  return [null];
}

// Filter the timers array to keep only objects where output is "0"
const filteredTimers = jsonData.timers.filter(timer => timer.output === "0");

// This copies output 0 to outputs 1-4 (i <= 5). Adjust the loop value for 
// different numbers of outputs.
const copiedTimers = [];
filteredTimers.forEach((timer, index) => {
  for (let i = 1; i <= 5; i++) {
    const copy = { ...timer };
    copy.output = i.toString();
    copiedTimers.push(copy);
  }
});

// Construct the new payload with the modified timers array
const newPayload = {
  "timers": filteredTimers.concat(copiedTimers),
  "settings": jsonData.settings
};

msg.payload = JSON.stringify(newPayload);
return msg;