Date/time based processing

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??

Take a look at this example in the cookbook

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

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

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...

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

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

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.

1 Like

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.

2 Likes

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:

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

image

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

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]; }
}