I've added a new function to my dashboard, where I can schedule start/stop sessions for my EV charger. So far, I have the time selectors and set/cancel buttons which all seem to work OK.
The resulting messages into node-RED from the front-end takes the format of;
Could I gather ideas what the best approach would be to manipulate those messages into a format that I could use to inject a dynamic schedule/clear schedule into Cron+
Yeah, I’m using uibuilder and not DB2, and also wanted something less complex, because it’s for use on a phone.
Yes, I’m familiar with that, but dates requires an actual formatted timestamp, and my interface is just producing the hour to switch on or off (a number between 1 and 24).
So I somehow need to turn both start & end numbers into future timestamps, and that's where I'm stuck
I guess that I should have explored this issue before spending a whole day writing the front end, but I am where I am…
The scheduling doesn't change. Just steal the bits I did in that flow?!?!
You can add hours to a date epoch adding x*1000*60*60 to Date.now() (epochs are accepted in the add schedule code)
I'm not at a computer to provide code but this is very likely one that chatgpt can help with. Ooooo chatgpt can write this on my phone too. Let's see....
It said
// Get the current date and time
const now = new Date();
// Extract the start and end hours from the incoming message payload
const startHour = msg.payload.start;
const endHour = msg.payload.end;
// Create the start date
const startDate = new Date(now);
startDate.setHours(startHour, 0, 0, 0); // Set hour, minute, second, and millisecond
// Check if the start date is in the past, if so, move it to the next day
if (startDate.getTime() < now.getTime()) {
startDate.setDate(startDate.getDate() + 1);
}
// Create the end date
const endDate = new Date(now);
endDate.setHours(endHour, 0, 0, 0);
// Check if the end date is in the past, if so, move it to the next day
if (endDate.getTime() < now.getTime()) {
endDate.setDate(endDate.getDate() + 1);
}
// Ensure the end date is after the start date, handling cases that cross midnight
if (endDate.getTime() < startDate.getTime()) {
endDate.setDate(endDate.getDate() + 1);
}