Having things happen at a particular time, or in response to another event is a common requirement in node-red flows.
Examples are:
- Turning on/off central heating at scheduled times.
- Turning on sprinkler system at a particular time.
- Turning on a conveyor belt after an item has been added.
- Sending an email in response to a fault.
Although there are many add-on timing nodes that you can install you might find the built in inject node, delay and trigger nodes are all you really need.
In this tutorial we will look at these nodes and give some example use scenarios as well as looking at the most popular add-on nodes.
The Inject Node
This node node is often used to start a flow. However the built in inject node has very useful timing features that are often overlooked. It includes
- Interval based Injection
- Scheduled injection
Interval based Injection
The Inject node can be configured to inject messages at regular intervals, such as every second, minute, hour, or between specific times. This allows you to trigger actions or events in your flow at predefined intervals.
Screen shots below:
Interval timer at future time.
Scheduled injection
You can use the Inject node to schedule specific tasks or events in your flow by setting the injection time to a future time stamp.
This is useful for triggering actions at specific times, such as sending notifications or performing maintenance tasks.
——-
The Delay Node
This is used for delaying events and also for rate limiting. For the purpose of timing the delay feature is the most useful.
The delay settings are shown below:
Of interest is how long you can delay the message.
Trigger node
This is a very useful node as it will send a message and then send a second message after an interval, and the second message can be suppressed.
It is particularly useful for command response type scenarios which is illustrated in this video. where the trigger node triggers a read every 5 seconds until the read succeeds.
Other Timing Nodes
If you cannot accomplish what you want with the built in code nodes then you can install and use third part nodes.
Many of these nodes used you latitude and longitude to determine dusk and dawn for controlling events.
Common choices are :
- node-red-contrib-light-scheduler
- node-red-contrib-eztimer
- Timerswitch
- node-red-contrib-ui-time-scheduler
- node-red-contrib-bigtimer
node-red-contrib-light-scheduler Is a node to control lights based on a schedule and the sun position.
The settings of this node are controlled in the Admin panel and cannot be set from the dashboard.
The timer day/hour selection allows you to configure multiple timers per day.
It accepts basic inputs which allow you to override the schedule by sending in a on/off message.
node-red-contrib-eztimer -Timer that is also based on sun position and can be set to normal schedule. Schedule settings not as flexible as the light schedule node, but this node can be pro grammatically controlled using a dashboard.
Schedex – Very similar to eztimer as eztimer was based on this node.
Timerswitch
This is an easy to use multi-schedule timer which allows you to add multiple on/off periods for a single output.
It is meant to resemble the functionality of mechanical and digital wall timers.
UI Scheduler
node-red-contrib-ui-time-scheduler– This is timer node from Dashboard extra nodes which allows you to create and manage timers using a dashboard rather than manually editing the node.
There is a video here.
Location -Sunrise and Sunset:
Some advanced light timers and smart lighting systems use location data (GPS coordinates or zip codes) to automatically adjust the on/off schedule according to the local sunrise and sunset times.
The time of sunrise (when the sun first appears above the horizon) and sunset (when the sun fully disappears below the horizon) depends on the location’s latitude, longitude, and time of year. The Earth’s tilt and orbit around the Sun cause these times to change daily.
By knowing the user’s location, the timer can calculate the local sunrise and sunset times using astronomical algorithms.
The timer can then adjust the lights to turn on at sunset and turn off at sunrise, or any custom schedule relative to those times (e.g., 20 minutes after sunset).
This ensures that lights are only on when necessary, providing both energy savings and security (e.g., lights automatically turning on at dusk).
Example Scenarios
Example 1 Turn device on/off after a period.
This could be a sprinkler system that we want to turn on and run for x (seconds,minutes,hours etc) and then turn off.
In this case we use a simple trigger node and set a 1 at start and then after a delay send a 0. The values the trigger nodes sends can be changed to suit the device.
The flow would look like this:
Example 2 -Trigger and Repeat at an interval.
For a lighting system we could get a strobe affect.
We use the same flow as example 1 but the inject node is set to repeat as shown below:
Example 2 -You need to turn a pump on at several times during the day and run for 10 mins
You could probably use an inject inject nodes with a trigger but the timerswitch or the ui scheduler would be better options.
Other Timer Videos
- Which one would you use? Light Scheduler vs BigTimer Node-RED nodes.
- Creating a node-red dashboard for the EzTimer