In the world of automation, not all paths are straight. Sometimes, your workflow needs to make a decision based on incoming data: “If this, then do that; otherwise, do something else.” This is where conditional logic comes into play, and in n8n, the hero for this task is the Condition Node.
This guide will walk you through everything you need to know about the n8n Condition Node, from basic checks to advanced expressions, empowering you to build smarter, more dynamic workflows. Let’s dive in! 🚀
What is the n8n Condition Node?
At its core, the n8n Condition Node is your workflow’s decision-maker or traffic controller. It evaluates a specific condition (or set of conditions) you define. Based on whether that condition evaluates to true
or false
, the workflow will then follow one of two distinct paths:
- True Branch (top output): Items that satisfy the condition will proceed through this path.
- False Branch (bottom output): Items that do not satisfy the condition will proceed through this path.
Think of it like a fork in the road, but instead of choosing left or right randomly, your workflow intelligently decides which way to go based on the data it carries. 🛣️
Why Use the Condition Node in Your Workflows?
The power of conditional logic opens up a multitude of possibilities for more sophisticated automations. Here are some common and impactful use cases:
- ✅ Filter Data: Process only relevant items.
- Example: Only send an email if a customer’s order total is above a certain amount.
- 📧 Send Conditional Notifications: Tailor alerts based on urgency or status.
- Example: Notify the “Urgent Issues” Slack channel if a support ticket’s priority is “High,” otherwise send it to the “General Support” channel.
- 📋 Handle Diverse Inputs: Respond differently to various forms of incoming data.
- Example: If a webhook receives “payment_successful,” update a database; if it receives “payment_failed,” send a notification to the sales team.
- 🛡️ Implement Robust Error Handling: Create alternative paths for unexpected scenarios.
- Example: If an API call fails, retry a few times, and if it still fails, log the error to a file instead of stopping the workflow.
- 📈 A/B Testing (Simple): Direct a small percentage of users down a different path.
- Example: Send 10% of new sign-ups to a different onboarding flow for testing purposes (though this often involves a Function node for random selection before the Condition node).
How to Use the Condition Node: A Practical Guide with Examples
The Condition Node offers three modes for defining your logic: Basic, Advanced, and Expression. Let’s explore each one.
1. Basic Mode: Simple Comparisons 📏
This is the easiest mode for straightforward checks. You define two values (Value1
and Value2
) and an Operation
to compare them.
Common Operations:
- Equal / Not Equal: Checks if values are identical or different.
- Greater Than / Less Than: Compares numerical values.
- Contains / Not Contains: Checks if a string includes a specific substring.
- Starts With / Ends With: Checks string beginnings/endings.
- IsEmpty / IsNotEmpty: Checks if a value is empty (e.g.,
null
,undefined
, empty string, empty array).
Example Scenario: Filtering High-Value Orders 💰
Let’s say you receive order data from an e-commerce platform, and you only want to process orders above $100 for special handling.
- Add a Condition Node: Drag and drop the “Condition” node onto your canvas.
-
Configure in Basic Mode:
- Value1: Click the “gear” icon or “Add Expression” button and select the path to your order total, e.g.,
{{ $json.orderTotal }}
. - Operation: Choose
Greater Than
. - Value2: Enter
100
(the threshold). - Data Type: Ensure the data type for
Value2
matchesValue1
(e.g.,Number
).
Visual Representation (mental image):
┌─────────────┐ │ Previous │ │ Node │ └─────────────┘ │ ▼ ┌───────────────┐ │ Condition │ │ Value1: {{ $json.orderTotal }} │ │ Operation: Greater Than │ │ Value2: 100 │ └───────────────┘ ╱ ╲ ╱ ╲ ▼ ▼ ┌───────────┐ ┌───────────┐ │ True │ │ False │ │ (>= $100) │ │ (< $100) │ └───────────┘ └───────────┘
- Value1: Click the “gear” icon or “Add Expression” button and select the path to your order total, e.g.,
Now, any order with orderTotal
greater than 100 will go down the True path, while others go down the False path.
2. Advanced Mode: Combining Multiple Conditions (AND/OR) 🧩
When your decision involves more than one factor, the Advanced mode is your friend. You can add multiple conditions and combine them using AND
or OR
logic.
- AND: All conditions must be true for the overall condition to be true. (e.g., “Must be
priority: high
ANDstatus: open
“) - OR: At least one condition must be true for the overall condition to be true. (e.g., “Must be
region: EU
ORregion: NA
“)
You can even group conditions for more complex logic (e.g., (A AND B) OR C
).
Example Scenario: Urgent European Support Tickets 🌍🚨
You want to escalate support tickets that are either “High” priority OR are located in “Europe” AND have a status of “Open”.
- Add a Condition Node.
- Change Mode: Select “Advanced” in the “Mode” dropdown.
- Configure Conditions:
- Condition Group 1 (starts with
AND
orOR
– default isAND
):- Condition 1.1:
- Value1:
{{ $json.priority }}
- Operation:
Equal
- Value2:
High
- Value1:
- Condition 1.1:
- Add Group (using
OR
): Click “Add Group” and selectOR
.- Condition Group 2:
- Condition 2.1:
- Value1:
{{ $json.region }}
- Operation:
Equal
- Value2:
Europe
- Value1:
- Add Condition (using
AND
):- Condition 2.2:
- Value1:
{{ $json.status }}
- Operation:
Equal
- Value2:
Open
- Value1:
- Condition 2.2:
- Condition 2.1:
- Condition Group 2:
- Condition Group 1 (starts with
This setup means: (priority == 'High') OR (region == 'Europe' AND status == 'Open')
.
3. Expression Mode: Unleashing JavaScript Power 🚀
For the most complex and flexible conditional logic, the Expression mode allows you to write direct JavaScript expressions. This gives you full control over data manipulation and logical checks using n8n's robust expression syntax ({{ }}
).
When to Use Expression Mode:
- When
Basic
orAdvanced
modes aren't sufficient. - Performing string manipulations (e.g., checking if a string contains a specific word, converting to lowercase).
- Working with dates and times.
- Checking array properties (e.g., if an array is empty, if it contains a certain item).
- Any logic that requires custom JavaScript functions.
Example 1: Checking for “Urgent” Keywords in a Message 💬
You receive messages and want to flag any that contain the word “urgent” (case-insensitive) or “ASAP”.
- Add a Condition Node.
- Change Mode: Select “Expression”.
- Enter Expression:
{{ $json.message.toLowerCase().includes('urgent') || $json.message.toLowerCase().includes('asap') }}
$json.message
: Accesses themessage
property from the incoming JSON data..toLowerCase()
: Converts the message to lowercase for case-insensitive matching..includes('urgent')
: Checks if the string contains “urgent”.||
: The logical OR operator.
Example 2: Combining Date and Status Checks 📅✅
You want to process tasks that are either “pending” and past their dueDate
, OR have a priority
of “critical”.
- Add a Condition Node.
- Change Mode: Select “Expression”.
- Enter Expression:
{{ ($json.status === 'pending' && new Date($json.dueDate) < new Date()) || $json.priority === 'critical' }}
$json.status === 'pending'
: Checks if status is 'pending'.new Date($json.dueDate)
: Converts thedueDate
string (assuming it's a valid date string) into a Date object.new Date()
: Creates a Date object for the current time.- ` 0 }}
$json.items
: Checks if theitems
property exists.&&
: Logical AND.$json.items.length > 0
: Checks if the array has more than 0 elements. This is a common pattern to safely check for non-empty arrays.
Best Practices & Tips for Using the Condition Node ✨
- 🔍 Clarity is King: Keep your conditions as clear and concise as possible. If an expression gets too long or complex, consider breaking it down into multiple Condition nodes or using a “Set” node to pre-process data into a simpler boolean flag.
- 🧪 Test Thoroughly: Always test your Condition node with various inputs (true, false, edge cases, invalid data) in the n8n editor. The “Execute Workflow” and “Test Workflow” features are your best friends!
- 🏷️ Name Your Nodes: Give your Condition nodes descriptive names (e.g., “Is Order High Value?”, “Check Priority and Region”). This makes your workflow easier to understand and debug later.
- 🔄 Handle All Paths: Always consider what should happen for both the True and False branches. Leaving one path unconnected might lead to unintended behavior or skipped data.
- 🔗 Chain Conditions Wisely: For very complex decision trees, you might need to chain multiple Condition nodes together. However, balance this with using Advanced or Expression mode to avoid overly convoluted workflows.
- ✨ Use Set Nodes for Pre-processing: If you need to manipulate data before a condition (e.g., extract a specific part of a string, calculate a derived value), use a “Set” node immediately before the Condition node. This makes your Condition node cleaner.
- Example: Calculate
totalRevenue
in aSet
node, then use{{ $json.totalRevenue }}
in your Condition node.
- Example: Calculate
- 🚦 Consider the Switch Node: If you have many distinct paths based on a single value (e.g., “if status is ‘A’ do X, if ‘B’ do Y, if ‘C’ do Z”), the Switch Node might be a better choice than a series of Condition nodes. The Condition node is ideal for binary (true/false) branching.
Conclusion
The n8n Condition Node is an indispensable tool for building dynamic, intelligent, and robust automation workflows. By mastering its various modes – Basic for simple checks, Advanced for combined logic, and Expression for ultimate flexibility – you can ensure your workflows react precisely to your data, handling diverse scenarios with elegance and efficiency.
Start experimenting with the Condition Node in your n8n instances today, and unlock a new level of sophistication in your automations! Happy automating! 🎉 G