Whats the best interval node?

I want a node where it should do the work of cron but should have interval functionality which cron node lacks.

Hi @Josh

My suggestion here is to use the below, but one of our very own mods

node-red-contrib-cron-plus (node) - Node-RED (nodered.org)

2 Likes

Can you clarify?

Cron "all about" intervals!

The inject node has this:

CRON+ has lots of options, basically everything that CRON can do:

and more:
image

Lets say that I have a set the cron node to trigger every 40 seconds.

Let current time be 12:00:00 hrs - The cron node starts , The first trigger will be in 12:00:40 . The second trigger should be (for every 40 seconds) 12:01:20 . But the cron node will trigger only in 12:01:40 .

Let me know if you need more clarity.

What does */40 * * * * * do for you?

The inject node has this:

But I cant dynamically set intervals using inject node :frowning:

Inject can do that.

Enhanced inject has more options (an insane amount! :slight_smile: )


It is part of the node-red-contrib-sun-position node.

OK, but I don't think you asked for that at the start.

image

As you can clearly see that the sequence is reset every 0th minute or second.

Maybe this?

Also part of node-red-contrib-sun-position

Not tried that but I think you can probably configure it to do it.

That's why I asked (I'm not at computer)

In these cases, I do multiple entries set to start 40 secs apart.

Something like

  • */0 0/3 * * * *
  • */20 1/3 * * * *
  • */40 0/3 * * * *

It's a Cron thing.

Other node might be better suited for this scenario.

Or you could DIY it with setInterval in a function

function doTimer(msg1) {
    node.send(msg1)
}

const timers = context.get('timers') || {}
const name = msg.topic

if ('start' in msg) {
    if (timers[name]) {
        clearInterval(timers[name])
    }
    timers[name] = setInterval(doTimer, msg.interval, msg)
}

if ('stop' in msg && timers[name]) {
    clearInterval(timers[name])
}

context.set('timers', timers)

Another inelegant option is cronplus injecting every 20 seconds (*/20 * * * * * *) and a function node to discard every other inject.

let cnt = context.get("counter") ?? 0
cnt += 1
cnt %= 2
context.set("counter", cnt)
if (cnt === 0) return msg;
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.