Date() format quandry

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 && ! 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.

possibly when you do flow.get("last_walert_WA1") and save it into last_walert1
what you get back is a string? .. its not a JS Date object that gets saved in context (i think)

so try to convert it back to Date before your calculations .. so getTime() will be available

last_walert1 = new Date(flow.get("last_walert_WA1")) || new Date(now - alert_ms);


A good suggestion, thank you, I'll try that. Since the error occurs periodically (not quickly but regularly) I'll have to wait a bit to see if this resolves the issue.

Can't you achieve that with a delay node?
Untitled 7

I could however there are 3 diff periods and it's a little more complicated then what I showed (was keeping it to just the problem). The code queries 3 weather feeds and parses for many things (some I don't care about). I might get a snow storm warning and yes, I'd want that to be delayed for say 4 hrs before I hear it again however, if there is a subsequent freezing rain warning, or (avoid) travel advisory, or severe cold warning, those should not wait until the next preset 4 hr query. That's why each has its own "recheck" setting.

It actually works very well. Particularly helpful when the kids are here in the winter and have an hour drive home after dinner. It does the same in the summer (severe thunder storms, heat warning, high winds, etc...). I don't like checking the weather myself so I love that "Jessie" (my name for my home computer voice) does it for me.

EDIT: Just remembered. The other problem with the delay node plan is if the 4 hours was last triggered at say, 3pm, and the weather warning was issued at say, 3:04pm, I wouldn't know about it for 4 hours.