I have a function that checks for weather alerts (freezing rain, snow storm, etc...) and if found, announces it (voice) over the house speakers. Once announced the function must wait a certain period (set via GUI option, IE 4 hrs) before repeating, else I'll hear it over and over again.
So when the announcement occurs, I log the date (time) and then have the function hold off until the last speak time plus the GUI wait period has elapsed. The last announce time and wait period are stored in context.flow.
So what I expect to always find in context.flow last alert is a date. like this:
"Tue Feb 08 2022 12:33:46 GMT-0500 (Eastern Standard Time)"
But periodically the function fails and throws an error indicating
"TypeError: last_walert1.getTime is not a function"
and when I check the context data for last alert I find the date stored looks like this
2022-02-07T16:41:53.989Z
which is what is causing the .getTime()
error. (NOTE: the examples are not meant to be the same date, just to show how format differs).
Here is the code piece to determine if announce is okay:
date = new Date();
now = date.getTime();
alert_interval = flow.get("alert_interval") || 20; // 15 minute increments
alert_ms = alert_interval * 15 * 60000; // 20 = 300 mins or 5 hrs
last_walert1 = flow.get("last_walert_WA1") || new Date(now - alert_ms);
t_remain = ((last_walert1.getTime() + alert_mins - now) < 1) ? 0 : (last_walert1.getTime() + alert_mins - now);
context.flow.wa_speak = (t_remain <= 0) ? true : false;
Here is the code piece (in a different function node) with flow.set
date = new Date();
if (context.flow.wa_speak && !context.global.Quiet_Time) flow.set("last_walert_WA1", date );
I need to find where the errant date format is coming from and being set however I can't find it and was hoping understanding the "bad" date format might help.