The moment library is great for things like this... but if you want to stick with just JS code, this should work. The trick is getting the time string into a format that the Date parser recognizes (the guide date + the "locale" time):
// use today's date, or parse the info from the scraped guide?
let guideDate = new Date().toLocaleDateString();
// we need the time string in '04:30 pm' format
let startTime = msg.payload.replace(/.*(\d{1,2}):(\d{2})\s*([aApPmM]+.*)/, "$1:$2 $3");
// turn the start datetime string into a UTC timestamp
let startDate = new Date(`${guideDate} ${startTime}`);
// subtract 10 min (as milliseconds)
let alertDate = new Date(startDate - (10 * 60 * 1000));
node.log(`Alert time is ${alertTIme.toLocaleTimeString()}`);
You now have a Date that can be used to set a notification, or compare to the current timestamp for filtering.
Exactly .. the circled part was replaced with the Function node and only because
the need to filter out the programs that are now playing (.epg-program-on-now) and also find their parent() channel number etc was so complex to do with just HTML nodes.
Say I do a nominal scan of the online EPG say 13 minutes before the usual start time.
Alas that may not really be enough but nothing is perfect and there is/are limits to how much latitude I'm going to give Foxtel on changing their start times.
And again: These are rounded values to keep the example simple.
I send into the flow what I want to watch and on which channel.
The show is published to start at 07:25.
So at 07:23 an inject node sends a message with the channel number (and other stuff more for human readability when debugging) to MORE CODE that then changes the channel on my TV that has HDMI CEC to the Foxtel box and that changes channel.
But if it is running late or early that isn't fun. I could miss the start or have the channel change and I watch more of the previous shown than I would like.
End of story.
So as I was saying:
It is 07:12 NOW, the show is said to start at 07:25.
So ideally I want the channel to change at 07:23`
The above message is received.
Time NOW is 07:12 I want the channel to change at 07:23. 11 minutes.
So a variable delay node is in the flow and is told to wait 11 minutes.
But the 11 is VARIABLE.
Determined from the NOW time and 2 minutes before the received msg.payload.startTime.
Alas I am thrown by your code as it uses tricks I don't understand.
Ah, ok, I see what you mean -- so whenever the inject node runs, you need to get the show's start time, subract 2 minutes (is that a fixed value?), and then determine how much time to send to the variable delay node?
Much of the sample code I sent would still be usable, but the delay value is calculated like this:
// use today's date, or parse the info from the scraped guide?
let guideDate = new Date().toLocaleDateString();
// we need the time string in '04:30 pm' format
let startTime = msg.payload.replace(/.*(\d{1,2}):(\d{2})\s*([aApPmM]+.*)/, "$1:$2 $3");
// turn the start datetime string into a UTC timestamp
let startDate = new Date(`${guideDate} ${startTime}`);
// subtract 2 min (as milliseconds) to get the recording timestamp
let recordDate = new Date(startDate - (2 * 60 * 1000));
// subtract the current timestamp from the recording timestamp
let delayMs = recordDate - Date.now();
node.log(`Delay time is ${delayMs} ms.`);
// don't send the delay value if it is less than 0 ms in the future
if (delayMs > 0) {
msg.payload = { delay: delayMs }; // or whatever needs to go in payload
return msg;
}
return null;
Hey Andrew - i guess the bigger question here is what are you really trying to achieve ?
You have Foxtel now - presumably with the IQ box that acts as an NVR for you and will record as required (series etc) its usually pretty good in terms of start and end times etc (i can not believe you are still paying Foxtels exorbitant fees !)
So what are you trying to achieve by sittng in front of the screen and having it change channels for you - are you about to lose the use of your arms for the remote - or is there something more to this ?
The "before start time" I take off is nominal - I think is the word.
Just enough time to not miss the start, as the actual start time is not always what happens.
I could make it 1 minute, 90 seconds, or something like that.
Just a best fit value to change channels before the program starts.
So the message on sent from this to the next node (delay) MUST have msg.delay and some other stuff set.
Sorry I am severe damage control just now and not really with it.
Alas the popups that used to happen don't that allow you to book shows to record, watch etc.
But that is all handled inside the foxtel box when I'm recording stuff.
What I try to do is WATCH (yes, I know: weird concept in this day and age) some shows.
the times they are on are not booked to anything else and so it is nice to watch them then and there rather than record them and have to find time later on.
As I can't control the Foxtel box directly, I have a SMART TV that has HDMI CEC.
So I am listening to one of the music channels - background noise - and a show I want to watch is about to start.
That way I don't have to be sitting there with the remote in my hand watching for that specific time to happen and change the channel.
As is they are very fixed times. Not too bad, but now and then Foxtel decide to move them around a few minutes here and there.
Quite annoying when the show has been moved from (say) 17:10 to 17:30. The channel changes at 17:08 and I have to either put up with that show or flick back to the music and try to remember to change at the new time.
The last part:
No it is just something I played with once and it is handy now and then.
A show is on at (again: EXAMPLE!) channel 101 @ 17:10 then at 17:45 there is another show on channel 108.
The music is playing.
2 minutes before it changes to 101.
I watch that show.
Then at 17:43 it changes to 108 and I watch the next show.
I can't/don't/getting worse at remembering what is on when.
So this changes the channels for me so I can watch the shows I want to on specific days and specific times on specific channels.
This whole thing allows a bit of flexibility with the start times.