# Date/time based processing

**URL:** <https://discourse.nodered.org/t/date-time-based-processing/13241>\
**Category:** General\
**Created:** [13 July 2019 13:55 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241 "2019-07-13T13:55:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinschki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martinschki/32/737_2.png) [@martinschki](https://discourse.nodered.org/u/martinschki)\
**Post date:** [13 July 2019 13:55 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/1 "2019-07-13T13:55:40Z")

</div>

I'm looking for a way to enable or disable processing or flows based on time of day or dates or vars.

Example:

- I want audio feedback only during daytime and not while I'm sleeping.

- I want to turn certain task off when not home via a general home/away flag.

- I don't want to run certain flows based on what day of the week it is.

Is there an elegant way of doing this? Like a simple IF - THEN or SWITCH - CASE??

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [13 July 2019 14:12 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/2 "2019-07-13T14:12:23Z")

</div>

Take a look at this example in the cookbook

[https://cookbook.nodered.org/basic/route-on-context](https://cookbook.nodered.org/basic/route-on-context)

eg Set a context variable in daytime true  
and then use a switch to only route if the context value is true

---

<div class="post-metadata">

**Author:** ![x375993](https://avatars.discourse-cdn.com/v4/letter/x/9de053/32.png) [@x375993](https://discourse.nodered.org/u/x375993)\
**Post date:** [13 July 2019 14:51 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/3 "2019-07-13T14:51:08Z")

</div>

If you have a lot of time-rules you should have a look at: [https://tech.scargill.net/big-timer/](https://tech.scargill.net/big-timer/)

---

<div class="post-metadata">

**Author:** ![martinschki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martinschki/32/737_2.png) [@martinschki](https://discourse.nodered.org/u/martinschki)\
**Post date:** [13 July 2019 19:38 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/4 "2019-07-13T19:38:41Z")

</div>

it is not so much about how to use big-timer or schedex, it is more about how to manage that in flows

do you guys activate/deactivate flows based on context or whole tabs?  
What's a good practice here...

---

<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:** [13 July 2019 19:43 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/5 "2019-07-13T19:43:03Z")

</div>

I use Global Variables and set them based on over ride actions i wish to implement - it is easy then to put them as the first step in which flow you are interested in and test them - then switch flow based on the result

Obviously they can also be easily access via any function nodes also

Craig

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [13 July 2019 19:45 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/6 "2019-07-13T19:45:34Z")

</div>

I set flow variables (boolean) based on incoming MQTT actions for the more variable actions (enabling guest mode when I have visitors to stop my smart speaker from asking me questions verbally and instead send notifications to my phone), and the rest of the flow based on repeated actions, often at set times, and only allow those to go on (switch: flow.boolean is true/false) when the correct condition is met else do nothing. So basically the messages are dropped and only an inject node keeps executing

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 July 2019 21:05 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/7 "2019-07-13T21:05:26Z")

</div>

> [@martinschki](#):
>
> do you guys activate/deactivate flows based on context or whole tabs?  
> What's a good practice here...

The technique is generally not to disable a flow in some way, but just to block whatever message triggers it. The link posted earlier by @ukmoose shows one way to do that.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 July 2019 06:23 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/8 "2019-07-14T06:23:45Z")

</div>

Personally I prefer to use something like node-red-contrib-simple-gate to block messages dependent on some other aspect of the system. That avoids the use of global/flow context.

---

<div class="post-metadata">

**Author:** ![depaljas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/depaljas/32/38279_2.png) [@depaljas](https://discourse.nodered.org/u/depaljas)\
**Post date:** [5 March 2021 12:13 UTC](https://discourse.nodered.org/t/date-time-based-processing/13241/9 "2021-03-05T12:13:40Z")

</div>

Hi, using state/flow is an option, but could be tricky to debug because you are dependent on something that happened in the past (is the state reliable). For robustness I prefer to rely as least as possible on context / derived states. I created a simple set up using only default nodes:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/909204fb328fb4f0536fb1a7cfc8d259437eeabd.png)

You set the time range in the time\_range change node. Midnight crossing (from \> to) is taken into account:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae59d47c5a8178f2616a495e970adf67450fa715.png)

Set the number of outputs of the function node to 2.  
Code for the function node:

```auto
var from, to, now;

from = new Date("2000-01-01 " + msg.from);
to = new Date("2000-01-01 " + msg.to);
now = new Date();
now.setFullYear(2000, 0, 1);

now = now.getTime();
from = from.getTime();
to = to.getTime();

if (from < to) {
    if (from <= now && now < to) { return [msg,null]; }
    else { return [null,msg]; }
} else {
    if (to <= now && now < from) { return [null,msg]; }
    else { return [msg,null]; }
}

```
