Ping a target every 20 seconds except 03:00 to 03:03

I'm monitoring the availability of a remote machine by pinging it every 20 sec.
Since my mobile broadband router reboots every day at 3am I'm getting false outage reports.

So what's the best way to not send the ping between 02:59:59 and 03:03:00?

I am using node-red-configurable-ping and have three cronplus nodes and two triggers:

Is there a more elegant approach?
A switch node that filters out any payload if the time is 02:59:59 to 03:03:00 maybe?

I could ping my router but it might reboot milliseconds after I do, thus still scope for false alerts.

would this not work ?

image

[{"id":"cc139d1392f41af7","type":"cronplus","z":"f65bb41a740d2610","name":"","outputField":"payload","timeZone":"","storeName":"","commandResponseMsgOutput":"output1","defaultLocation":"","defaultLocationType":"default","outputs":1,"options":[{"name":"schedule1","topic":"topic1","payloadType":"date","payload":"","expressionType":"cron","expression":"0/20 * * * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":290,"y":250,"wires":[["e1c485f77dc5ea6e"]]},{"id":"e1c485f77dc5ea6e","type":"time-range-switch","z":"f65bb41a740d2610","name":"","lat":"","lon":"","startTime":"02:59:59","endTime":"03:03:00","startOffset":0,"endOffset":0,"x":590,"y":250,"wires":[[],[]]}]
1 Like

Thanks @smanjunath211. I'll take a look at node-red-contrib-time-range-switch.

I didn't know cron-plus can do intervals shorter than a minute!

Could not an Inject node be used instead of cron plus in this case?

I have slimmed it down to a standard inject node and a function node provided by Leo AI built in to Brave Browser:

let now = new Date();
let start = new Date();
let end = new Date();
now.setMinutes(now.getMinutes() + now.getTimezoneOffset());
start.setHours(2, 59, 59);
end.setHours(3, 4, 59);

if (now >= start && now <= end) {
    msg.payload = start + " <=  " + now + " <= " + end + " Reject"
    return [msg, null]
} else {
    msg.payload = now + " not between " + start + " and " + end + " OK"
    return [null, msg]
}

The AI ran out of breath before it could come up with a non-function version.
I'm not convinced about
now.setMinutes(now.getMinutes() + now.getTimezoneOffset()), will have to wait till tonight to see which HH:MM actually get rejected.

Yeah i do something similar - i use the enhanced Inject node and the within time node and set the fail of the within time to just dump the requests so they never get to the router

Craig

Chat GPT thinks "your" code is a bit sloppy :wink:

let now = new Date();
let start = new Date(now);
let end = new Date(now);

now.setMinutes(now.getMinutes() + now.getTimezoneOffset());

start.setHours(2, 59, 59, 0);
end.setHours(3, 4, 59, 999);

if (now >= start && now <= end) {
    msg.payload = start.toISOString() + " <= " + now.toISOString() + " <= " + end.toISOString() + " Reject";
    return [msg, null];
} else {
    msg.payload = now.toISOString() + " not between " + start.toISOString() + " and " + end.toISOString() + " OK";
    return [null, msg];
}

I can't believe you told ChatGPT I wrote that code! :smile:

Jeez what's a millisecond or two?

As for the toISOString() thing, sensible though it is, I'm not planning on using that but instead seeing which timestamps are missing from a database.

1 Like

I sometimes like to see what different AI think of each others work :wink:

I hadn't come across Leo AI

It pops up every time I try and type B but mistakenly hit CTRL. Like the Sodding Paperclip of old only brighter.

1 Like

I was just going along with the OP. indeed an inject node would do the job.
I was always curious to ask, I have several cron-plus nodes in my flow, where easily i can switch to inject node, but i cannot get it to trigger at '00' of the clock. is it possible to trigger at every minute but at '00' of the clock ?
also, on memory pov, how much impact does a cron-plus node have compared to an inject node ?

Repeat at "interval between times" 00:00 and 00:00 should do that.

1 Like

Yes, It is working! new things to learn everyday.

I believe there should be a separate section of DID YOU KNOW in this forum!
tips and tricks shared by experts on a regular basis, since by now they know where new users usually get stuck at or take a longer route to achieve their goal.

As you already have cron-plus, here is an alternative no-code way (without triggers or delays)

[{"id":"d42434189dccf003","type":"cronplus","z":"153004dfa27f7bcc","name":"","outputField":"payload","timeZone":"","storeName":"","commandResponseMsgOutput":"fanOut","defaultLocation":"","defaultLocationType":"default","outputs":5,"options":[{"name":"20s","topic":"Every 20 sec","payloadType":"default","payload":"","expressionType":"cron","expression":"0/20 * * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"disable","topic":"disable","payloadType":"bool","payload":"true","expressionType":"cron","expression":"0 0 3 * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"enable","topic":"disable","payloadType":"bool","payload":"false","expressionType":"cron","expression":"0 3 3 * * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":790,"y":140,"wires":[["aae7da4a0e98475b"],["a69f36e039feaa4b"],["a69f36e039feaa4b"],[],[]]},{"id":"aae7da4a0e98475b","type":"switch","z":"153004dfa27f7bcc","name":"disabled?","property":"disabled","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1000,"y":100,"wires":[["fc01af8012d44521"],["11cbf02243ffc0d1"]]},{"id":"a69f36e039feaa4b","type":"change","z":"153004dfa27f7bcc","name":"update flow.disabled","rules":[{"t":"set","p":"disabled","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1040,"y":160,"wires":[[]]},{"id":"11cbf02243ffc0d1","type":"debug","z":"153004dfa27f7bcc","name":"do ping","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1220,"y":120,"wires":[]},{"id":"fc01af8012d44521","type":"debug","z":"153004dfa27f7bcc","name":"do nothing","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1230,"y":80,"wires":[]}]
1 Like

Can this not be done using just two inject nodes? One configured to repeat, using Interval between times, every 20 seconds between 00:00 and 02:59, and another the same, but between 03:03 and 24:00?

How do you get a repeat interval shorter than a minute with repeat between times?

I can't get it to accept start and end times other than hh:00 either :frowning:

These seem to be non-positive features of the inject node.

1 Like

Ah, good point, I had never noticed that. I withdraw my suggestion.

Yeah need the inject enhanced node for that which is what i use for most of them that do not need the total flexibility of Cronplus. I find it is easier to understand than Cronplus (as it is very close to the standard inject node - just a bit more flexible)

Craig