# Running a flow every 20 days

**URL:** <https://discourse.nodered.org/t/running-a-flow-every-20-days/69454>\
**Category:** General\
**Created:** [23 October 2022 08:57 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454 "2022-10-23T08:57:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [23 October 2022 08:57 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454/1 "2022-10-23T08:57:06Z")

</div>

Hi!  
I have to empty the tank of my dehumidifier every 20 days. I need a reminder to do it!  
So, I want to send a telegram message every 20 days to remind me of this.  
With cronplus, I have tried the \*/20 for the days but it ends up being 1st and 21st of the month (kind of modulo rounded by month).  
My alternative would be to program in cronplus the next dynamic schedule 20 days from the current execution.  
Is there a better way in cronplus or else to do this "every 20 days" ? The inject method with a frequency of 480 hours is reset by the restart of the flows, so, this one is not usable. I think.  
Thanks.

---

<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:** [23 October 2022 09:06 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454/2 "2022-10-23T09:06:06Z")

</div>

(Maybe edit the title to something like: How to get a 20 day time between signals)

As is it is a bit confusing.

~~And sorry I can't help. That is a good question.~~

How about this:

```auto
[{"id":"0345eaaca6d49d66","type":"trigger","z":"0918ee609bf69fc7","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-480","extend":false,"overrideDelay":false,"units":"hr","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":2600,"y":3900,"wires":[[]]}]

```

---

<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:** [23 October 2022 09:38 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454/3 "2022-10-23T09:38:44Z")

</div>

I think you should use persistent context to store the date it was last sent and inject (timestamp) by cronplus once a day.

This function might do the job

```auto
const now = msg.payload;
const then = context.get('lastsent') || 0
const interval = 20 * 24 * 60 * 60 * 1000 // milliseconds!
if ( now - then > interval) {
    context.set ('lastsent', now)
    return msg
}

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 October 2022 10:45 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454/4 "2022-10-23T10:45:45Z")

</div>

The best solution would be something that doesnt rely on "remembering" anything (as that memory can get lost).

A simple way is to fire a CRON or INJECT once per day (at the time you wish to be alerted) then calculate the number of days since epoch then modulo 20.

e.g...

```auto
const mod = (parseInt(Date.now() / 1000 / 60 / 60 / 24) % 20) 
if (mod === 0) {
  // today is the day
  msg.payload = "whatever you want"
  return msg
}

```

You can always spice it up with some node status

![chrome_aqtjLijFHf](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9fbed26e82d7b222646cf5dd19f7035784535b39.gif)

Demo flow (import using `CTRL-I`)

```auto
[{"id":"0e0b79405aa2abd7","type":"inject","z":"10ece3ab98e93683","name":"23rd 12:00","props":[{"p":"date","v":"1666522800000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1580,"wires":[["da9711c8a01c1732"]]},{"id":"c99f1b00b380c42d","type":"cronplus","z":"10ece3ab98e93683","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","defaultLocation":"","defaultLocationType":"default","outputs":1,"options":[{"name":"noon","topic":"noon","payloadType":"str","payload":"Fill the tank","expressionType":"cron","expression":"0 0 12 * * *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"}],"x":2130,"y":1480,"wires":[["da9711c8a01c1732"]]},{"id":"da9711c8a01c1732","type":"function","z":"10ece3ab98e93683","name":"Is it time?","func":"const days = 20\nconst mod = (parseInt((msg.date || Date.now()) / 1000 / 60 / 60 / 24) % days)\nif (mod === 0) {\n // today is the day\n msg.payload = \"whatever you want\"\n // @ts-ignore\n node.status({ fill: \"blue\", shape: \"ring\", text: `Next message in ${days} day${days === 1 ? \"\" : \"s\"}` });\n return msg\n} \nconst daysUntil = days - mod\nnode.status({ fill: \"blue\", shape: \"ring\", text: `Next message in ${daysUntil} day${daysUntil == 1 ? \"\" : \"s\"}`});","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2360,"y":1480,"wires":[["fd8ded425df58d62"]]},{"id":"fd8ded425df58d62","type":"debug","z":"10ece3ab98e93683","name":"Send telegram ->","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2390,"y":1420,"wires":[]},{"id":"6865444b45fd511e","type":"inject","z":"10ece3ab98e93683","name":"24th 12:00","props":[{"p":"date","v":"1666609200000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1620,"wires":[["da9711c8a01c1732"]]},{"id":"c3c94c936182ae63","type":"inject","z":"10ece3ab98e93683","name":"25th 12:00","props":[{"p":"date","v":"1666695600000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1660,"wires":[["da9711c8a01c1732"]]},{"id":"bbead8dc41557e95","type":"inject","z":"10ece3ab98e93683","name":"26th 12:00","props":[{"p":"date","v":"1666782000000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1700,"wires":[["da9711c8a01c1732"]]},{"id":"fe0fa5982fb87cb9","type":"inject","z":"10ece3ab98e93683","name":"27th 12:00","props":[{"p":"date","v":"1666868400000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1740,"wires":[["da9711c8a01c1732"]]},{"id":"ccf588da28e12c73","type":"inject","z":"10ece3ab98e93683","name":"28th 12:00","props":[{"p":"date","v":"1666954800000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1780,"wires":[["da9711c8a01c1732"]]},{"id":"555a405b55bda354","type":"inject","z":"10ece3ab98e93683","name":"29th 12:00","props":[{"p":"date","v":"1667041200000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1820,"wires":[["da9711c8a01c1732"]]},{"id":"2d6289cf7c6d6373","type":"inject","z":"10ece3ab98e93683","name":"30th 12:00","props":[{"p":"date","v":"1667131200000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1860,"wires":[["da9711c8a01c1732"]]},{"id":"a6d6017c1f7d2d84","type":"inject","z":"10ece3ab98e93683","name":"31th 12:00","props":[{"p":"date","v":"1667217600000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1900,"wires":[["da9711c8a01c1732"]]},{"id":"0ae2588603d16a5e","type":"inject","z":"10ece3ab98e93683","name":"1st 12:00","props":[{"p":"date","v":"1667304000000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1940,"wires":[["da9711c8a01c1732"]]},{"id":"8b5add061c9879f0","type":"inject","z":"10ece3ab98e93683","name":"2nd 12:00","props":[{"p":"date","v":"1667390400000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":1980,"wires":[["da9711c8a01c1732"]]},{"id":"4811478dfdab60a8","type":"inject","z":"10ece3ab98e93683","name":"3rd 12:00","props":[{"p":"date","v":"1667476800000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":2020,"wires":[["da9711c8a01c1732"]]},{"id":"2e0fb163d60e6c75","type":"inject","z":"10ece3ab98e93683","name":"4th 12:00","props":[{"p":"date","v":"1667563200000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":2060,"wires":[["da9711c8a01c1732"]]},{"id":"9a2abb5d3086ca87","type":"inject","z":"10ece3ab98e93683","name":"5th 12:00","props":[{"p":"date","v":"1667649600000","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2140,"y":2100,"wires":[["da9711c8a01c1732"]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 October 2022 10:55 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454/5 "2022-10-23T10:55:39Z")

</div>

The best solution would be to have the dehumidifier empty automatically, and a level alert and auto shut down if auto empty fails.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [23 October 2022 11:02 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454/6 "2022-10-23T11:02:48Z")

</div>

haha - thats true! I guess I should have said "the better solution, given current requirements" 😉

---

<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:** [6 November 2022 11:03 UTC](https://discourse.nodered.org/t/running-a-flow-every-20-days/69454/7 "2022-11-06T11:03:04Z")

</div>

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