# Cronplus node help

**URL:** <https://discourse.nodered.org/t/cronplus-node-help/88479>\
**Category:** General\
**Created:** [3 June 2024 23:22 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479 "2024-06-03T23:22:48Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 June 2024 23:22 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/1 "2024-06-03T23:22:48Z")

</div>

Sorry, I'm stuck.

I want an event every second week on Saturday.

Saying it starts _this_ Saturday (8 June) what do I need to do?

I will be trying ITMT, but as _Sod's law_ says: If I don't ask I won't (in the mean time) work it out.

(As said)

# I have NEARLY got it.

`0 0 7 * * 6`

But that gives me every Saturday.  
Not every alternate one.  
(Starting with this coming one)

 ![Screenshot from 2024-06-04 09-31-36](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/5/251be27acd5fc5583e66b0c938d4a79d2c457c35.png)

# Oh something else!

(Sorry.)

**It has to survive a reboot.**  
If it rebooted (powered down and left off for an unknown time) when it restarts it has to keep the same pattern.

Not just pick either Saturday (the closest one) and then do it every alternate week.  
If the coming Sat' was a _skip_ then it knows that and skips the Sat'.  
If the coming Sat' was an _action_ one, then it knows and sends the message on Sat'.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [3 June 2024 23:55 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/2 "2024-06-03T23:55:10Z")

</div>

So Andrew - confirming that you do not want a set saturday (such as the 2nd saturday of every month and then the 4th saturday) but rather if i set the job today - it would run - This Saturday and then every two weeks on a saturday ?

I am pretty sure CRON (which CronPlus is based on) - does not support this natively - what you will need to do is run a script, and at the end of the script delete the current job and add another one for 14 days in the future.

So you would submit the job (say today) to run this saturday - and then every two weeks (in case the machine was off this saturday

In the script it would do the various functions you require and at the end of the script it would

1. Update the next instance to run to be in two weeks time

as there is no way to Update a cron entry - your best bet would be to delete the job from the script and then resubmit it to run in two weeks time

This approach will not cater for the need to run the script if it is missed (i.e. the machine is turned off on a saturday when it is meant to run) and you still want to run it when the machine is next started - that becomes more difficult and requires additional files to enable a check of when it was last run etc (all of this could be translated into Node Red function nodes if required) but adds more complexity

Craig

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 June 2024 23:59 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/3 "2024-06-03T23:59:25Z")

</div>

Thanks.

Yeah, I'm looking at a lot of stuff on the net and they do _tricks_ where they do a `mod(2)` on the week number and (depending on which week it is) passes if a `0` or `1`.

I guess I could _cheat_ and send the output weekly into a `function` node that looks at the week value and depending on the value of the `mod(2)` would either pass or skip sending the message.

I am still easily confused with this _black magic_ sometimes.

I'll give that a go for now and see what happens.

Again: Thanks.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [4 June 2024 00:09 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/4 "2024-06-04T00:09:00Z")

</div>

Nope i do not think you can use the Mod trick as you have said you do not want a set week i..e week 1 or week 2 of the month - you really need a script that will self modify as above or kick off a NR function that will set a context variable and work with that.

Craig

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 00:10 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/5 "2024-06-04T00:10:37Z")

</div>

Digging, I've found this:

```auto
var todaydate = new Date();
var oneJan = new Date(todaydate.getFullYear(), 0, 1);
var numberOfDays = Math.floor((todaydate - oneJan) / (24 * 60 * 60 * 1000));
var weekOfYear = Math.ceil(( todaydate.getDay() + 1 + numberOfDays) / 7);
if (weekOfYear % 2 === 0)
{
    //even week, continue flow
    return msg;
} else
{
  //odd week, stop flow
return null;
} 

```

But!

`let numberOfDays = Math.floor((todaydate - oneJan) / (24 * 60 * 60 * 1000));`  
Errors on `todaydate` and `oneJan`

So I'm stuck at this point now.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 00:12 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/6 "2024-06-04T00:12:21Z")

</div>

Oh, my meaning of the `mod(2)` was on the bigger week value.

(from epoch)

Sorry. My bad.

Check the code I just posted as that may work in some way.  
But for now it is kind of just getting the feel for how to get the bigger week number and checking it's value.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 00:15 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/7 "2024-06-04T00:15:31Z")

</div>

# RETHINK!

Ok so the `function` node does this:

Receives a message.  
Looks at the epoch week value.  
`mod 2` the value. (0 or 1)  
Depending on the result and which of the two weeks I want to use (the result will be `0` or `1`) either returns the message or doesn't.

The code I posted is a bit of overkill I know.

Just what I found while digging.

# Thought on the above:

So this bit of code:

```auto
[{"id":"91dab0a14efeb873","type":"moment","z":"60d0e3e966d484ae","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"WW","locale":"en-AU","output":"week","outputType":"msg","outTz":"Australia/Sydney","x":1790,"y":460,"wires":[["1c4ae4187049235c"]]},{"id":"10b5080bedf365cb","type":"inject","z":"60d0e3e966d484ae","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1717460918852","payloadType":"num","x":1580,"y":460,"wires":[["91dab0a14efeb873"]]},{"id":"1c4ae4187049235c","type":"debug","z":"60d0e3e966d484ae","name":"debug 2525","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2120,"y":460,"wires":[]}]

```

Given: **THIS** Saturday is a day I want to _use_.  
Given: `1717460918852` is _Now_.

The above code gives me `23`. (odd number) No big deal.

So I then look at `msg.week` (next `function` node - NOT included) and do a `mod 2` and if I get a `1` then I pass on the message.

Sound like a good idea?

Remaining questions:  
Will that survive a new year?  
Or is `WW` from the moment node give me the _week of this (actual) year_ and not \*from the given `timestamp`? `1717460918852`

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [4 June 2024 00:36 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/8 "2024-06-04T00:36:59Z")

</div>

Not at my PC to run this until later today - will do it then and have a look at your code

Craig

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 June 2024 00:47 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/9 "2024-06-04T00:47:52Z")

</div>

Edit - I think you already solved it...

According to [stackoverflow](https://stackoverflow.com/questions/64293190/how-to-get-week-number-since-epoch-like-millis), the number of weeks since Jan 1 1970 is given by

```auto
const weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)

```

So this function will alternate between 0 and 1 every week:

```auto
const weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)
msg.payload = Number(weekno) % 2 // Not sure if Number() is needed
return msg;

```

So use your weekly cron and only run the downstream code if the function returns 0

demo flow:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/479a1cda5fecec01d795c09de645f1e455003fc6.png)

```auto
[{"id":"937524d4abdadb79","type":"function","z":"5d7e1a0f2e73e424","name":"First week 8th June 07:00","func":"let timestamp = new Date(\"2024-06-08 07:00:00\").getTime()\nconst weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)\nmsg.payload = Number(weekno) % 2\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":40,"wires":[["96cdb55923eb10f9"]]},{"id":"96cdb55923eb10f9","type":"debug","z":"5d7e1a0f2e73e424","name":"0 or 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":530,"y":40,"wires":[]},{"id":"09b6e8013f76f0e5","type":"inject","z":"5d7e1a0f2e73e424","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"go","payloadType":"str","x":100,"y":40,"wires":[["937524d4abdadb79","ce38be64eda29a71","44c7b0eae01b689b","cc1a6bf62abb3035","20f3c1f767942e77"]]},{"id":"ce38be64eda29a71","type":"function","z":"5d7e1a0f2e73e424","name":"Second week 15th June 07:00","func":"let timestamp = new Date(\"2024-06-15 07:00:00\").getTime()\nconst weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)\nmsg.payload = weekno % 2\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":100,"wires":[["c375d43fa73f977d"]]},{"id":"44c7b0eae01b689b","type":"function","z":"5d7e1a0f2e73e424","name":"Third week 22nd June 07:00","func":"let timestamp = new Date(\"2024-06-22 07:00:00\").getTime()\nconst weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)\nmsg.payload = weekno % 2\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":160,"wires":[["0ace5d59302d94f8"]]},{"id":"cc1a6bf62abb3035","type":"function","z":"5d7e1a0f2e73e424","name":"Fourth week 29th June 07:00","func":"let timestamp = new Date(\"2024-06-29 07:00:00\").getTime()\nconst weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)\nmsg.payload = weekno % 2\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":220,"wires":[["48a47ed504696bcc"]]},{"id":"20f3c1f767942e77","type":"function","z":"5d7e1a0f2e73e424","name":"Fifth week 6th July 07:00","func":"let timestamp = new Date(\"2024-07-06 07:00:00\").getTime()\nconst weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)\nmsg.payload = weekno % 2\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":280,"wires":[["d967aa29bbfd863b"]]},{"id":"c375d43fa73f977d","type":"debug","z":"5d7e1a0f2e73e424","name":"0 or 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":530,"y":100,"wires":[]},{"id":"0ace5d59302d94f8","type":"debug","z":"5d7e1a0f2e73e424","name":"0 or 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":530,"y":160,"wires":[]},{"id":"48a47ed504696bcc","type":"debug","z":"5d7e1a0f2e73e424","name":"0 or 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":530,"y":220,"wires":[]},{"id":"d967aa29bbfd863b","type":"debug","z":"5d7e1a0f2e73e424","name":"0 or 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":530,"y":280,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 00:50 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/10 "2024-06-04T00:50:58Z")

</div>

If anyone else is reading this:

(and Craig)

Rather than using the `moment` node to get the week number

# (Ok, I've seen @jbudd post)

make the `function` node in the code I posted do that?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 00:52 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/11 "2024-06-04T00:52:21Z")

</div>

Thanks.

But your code - I'm still looking at it - the date changes in every `function` node.

I need it to be one function node for all dates to come.  
This year, next, and so on.

I can't afford to have function nodes for every week.

But I'll look at the maths in the node/s and see what I can learn.

I may be able to work them out but what are those two magic numbers?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 June 2024 00:57 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/12 "2024-06-04T00:57:38Z")

</div>

> [@Trying\_to\_learn](#):
>
> the date changes in every `function` node.

Yes it does but it's only a demo/proof of concept.  
For the real thing, make cronplus send the current timestamp; the function becomes

```auto
const timestamp = msg.payload
const weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)
msg.payload = Number(weekno) % 2
return msg;

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 01:03 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/13 "2024-06-04T01:03:56Z")

</div>

Oh, ok.

_We_ were going at it in two different ways.

My latest post (the code) sends a FIXED value for _now_.  
That means that the _week % 2_ will always be `1`. (no big deal)  
And the `moment` node gets the week number.  
Alas I don't know if that is relative to the year. But anyway....

So your way gets _now_ from the `cronplus` node and compared it to ....... _those magic numbers_.  
(just quickly: why the `_` and not a `,` as separators?  
`345,600,000` you show as `345_600_000` Any reason? Just asking.

As you are using `timestamp` the weeks will alternate `0` and `1`. Ok, with you there.

So your code kind of skips what I did by basing it on _now_ (this weekend's week value) and uses the `epoch` one.  
Ok, good.

Sorry, I seem to be slow on the uptake just now.

Starting to make sense.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 June 2024 01:08 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/14 "2024-06-04T01:08:35Z")

</div>

> [@Trying\_to\_learn](#):
>
> (just quickly: why the `_` and not a `,` as separators?  
> `345,600,000` you show as `345_600_000` Any reason? Just asking.

Umm. Good question!  
The stackoverflow post i linked to has underscores.  
I don't think commas would work, I am surprised underscores do. 😀  
Best not to use either - 345600000 and 604800000

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 01:10 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/15 "2024-06-04T01:10:38Z")

</div>

No problems.

So this is what I have for the `function` node now:

```auto
const timestamp = msg.payload
const weekno = Math.floor((timestamp + 345_600_000) / 604_800_000)
const result_ = Number(weekno) % 2
if (result_ == 0)
{
    // return message
    return msg;
}
else
// do nothing
return;

```

Sorry, I've _expanded_ it a bit only for personal clarity and the `payload` is going to be changed here rather than in another node.

But is that right?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 June 2024 01:16 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/16 "2024-06-04T01:16:12Z")

</div>

I don't think you need to return from the function node if the flow stops there, so the `else return` is redundant.

Another way to do it is a switch node  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/914a1f256afc7cb094849ef2f7c2e7c3f0f7212d.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 01:23 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/17 "2024-06-04T01:23:46Z")

</div>

Ok, you got me there.

I only added it for clarity.

Will remove it.

But

```auto
if (result_ == 0)
{
    // return message
    msg.payload = "this is needed at the next stage"
    return msg;
}

```

Is more like how it really is.

And THANKS for that help.

It went from a somewhat complex thing to somewhat easy.

I just got an error from the code I found:

```auto
let todaydate = new Date();
let oneJan = new Date(2024);
let numberOfDays = Math.floor((todaydate - oneJan) / (24 * 60 * 60 * 1000));
let weekOfYear = Math.ceil(( todaydate.getDay() + 1 + numberOfDays) / 7);
if (weekOfYear % 2 === 0)
{
    //even week, continue flow
    return msg;
} else
{
    //odd week, stop flow
    return null;
} 

```

showed up as this for me:

 ![Screenshot from 2024-06-04 11-22-32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/438c57a72231d5096e1816fa1ab2cb0a0a8f9268.png)

**Line 4**  
And I didn't know why.

_indulge me the `node.warn` that was me trying to find what was going on_

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 June 2024 02:02 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/18 "2024-06-04T02:02:58Z")

</div>

Todaydate and oneJan are date objects so you can't just subtract one from the other.

But this is legal _code_. At this time of night I can't tell what impact it has on your calculation

```auto
let todaydate = new Date().getTime(); // milliseconds since Jan 1970
let oneJan = new Date(2024).getTime();
let numberOfDays = Math.floor((todaydate - oneJan) / (24 * 60 * 60 * 1000));

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [4 June 2024 03:04 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/19 "2024-06-04T03:04:59Z")

</div>

No problem.

I wasn't _blaming_ you for that.

I was just saying I found that code - which supposedly works - and it threw an error at me so the whole thing fell apart.

Hmmmm.... seems I didn't copy ALL the code.  
Works now.

But I still prefer your way. 😉

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 June 2024 03:05 UTC](https://discourse.nodered.org/t/cronplus-node-help/88479/20 "2024-06-18T03:05:27Z")

</div>

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