Trigger

What is the Trigger Node in Node-RED?

The Trigger node in Node-RED facilitates the initiation and repetition of messages at customizable intervals, offering precise control over when messages are sent, their recurrence frequency, and optional delays. This functionality is valuable for automating tasks and efficiently managing communication flow within Node-RED flows.

Inject Node Vs Trigger Node

The Inject node lets you send messages at specific intervals but it starts immediately and continues indefinitely unless manually configured to stop. With the Trigger node, you have control over when the node starts and stops sending messages. Nonetheless, both nodes possess distinct use cases and limitations.

Configuring the Trigger Node

  • Send: Message to be passed to subsequent nodes.

  • Then:

    • Wait For: Allows sending a message when triggered and then optionally a second message. You can also set it to send nothing when triggered or for the second message.
      • Extend Delay if New Message Arrives: Enabling this option will extend the delay time if a new message is received.
      • Then Send: Allows setting the second message to be sent after a specific delay, or you can set it to send nothing.
      • Send Second Message to Separate Output: Enabling this option will add a second output to receive the second message from the trigger node.
    • Resend it Every: Allows resending a message at specific intervals of time.
    • Wait to be Reset: Selecting this option will send a message once when triggered and will wait until it is reset. If not reset, it will not send any message with the same property specified in handling config property. If all messages are selected, it will not send any message if not reset.
  • Reset the Trigger if: Allows setting msg.payload that, when received, will reset the trigger node. Alternatively, sending a message containing a reset property will reset the node (which is the default behavior).

  • Override Delay with msg.delay: Enabling this option will allow sending the delay time dynamically with the msg.delay. The value must be provided in milliseconds.

  • Handling: Allows configuring the node to treat messages as separate streams, using a msg property to identify each stream. Selecting "All Messages" will handle all types of messages separately.

Trigger node Use cases:

  • Repetitive Tasks: If you have tasks that need to be repeated at regular intervals, such as data polling or device status checks when triggered, the Trigger node can handle this by configuring it to resend messages at specified time intervals.

  • Timeout Handling: You can utilize the Trigger node to manage timeouts within your flow. For example, you could trigger an action if a response is not received within a certain time frame, or set up a timeout mechanism for user interactions.

  • Resource Conservation: The Trigger node can conserve energy or system resources by automatically initiating actions, such as turning off lights or closing valves, after a predefined period of inactivity or completion of a task

Examples

  1. In the example flow below, we've simulated a door lock system. We employ an inject node to input a password, which is then verified against a specified password in a switch node. If the input password is correct, a trigger node sends a payload to open the door. After 4 seconds, a second message is sent to close the door. This can also be utilized for scenarios involving turning an LED on and off.
  1. In the example flow below, we have a trigger node polling data continuously from an API. It polls data at specific interval when a message is received and stops if a message is received with the property of 'reset'. This can also be used in scenarios where you want to read sensor data with custom control.

Node Documentation

When triggered, can send a message, and then optionally a second message, unless extended or reset.

Inputs

delay number
Sets the delay, in milliseconds, to be applied to the message. This option only applies if the node is configured to allow the message to override the configured default delay interval.
reset
If a message is received with this property, any timeout or repeat currently in progress will be cleared and no message triggered.

Details

This node can be used to create a timeout within a flow. By default, when it receives a message, it sends on a message with a payload of 1. It then waits 250ms before sending a second message with a payload of 0. This could be used, for example, to blink an LED attached to a Raspberry Pi GPIO pin.

The payloads of each message sent can be configured to a variety of values, including the option to not send anything. For example, setting the initial message to nothing and selecting the option to extend the timer with each received message, the node will act as a watchdog timer; only sending a message if nothing is received within the set interval.

If set to a string type, the node supports the mustache template syntax.

The delay between sending messages can be overridden by msg.delay if that option is enabled in the node. The value must be provided in milliseconds.

If the node receives a message with a reset property, or a payload that matches that configured in the node, any timeout or repeat currently in progress will be cleared and no message triggered.

The node can be configured to resend a message at a regular interval until it is reset by a received message.

Optionally, the node can be configured to treat messages as if they are separate streams, using a msg property to identify each stream. Default msg.topic.

The status indicates the node is currently active. If multiple streams are used the status indicates the number of streams being held.