IF Node Logic Not Correct

I have setup an IF node to perform a simple check and the logic is not behaving as expected. I am simply comparing 2 values to decide whether or not to execute downstream nodes. If the input value is 0 then the “true” branch should qualify. If the input value is any other value than 0 then the “false” branch should qualify. However, upon inspection of the execution this morning, the input value was “0” but get the node still resolved to “false”. The workflow completed successfully but it sent out a notification to a user telling them they had 0 actions to take. I want to prevent notifications from going out when nothing needs to be done.

What am I missing?

Information on your n8n setup

  • n8n version:0.234.0
  • Database (default: SQLite): SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: Linux Umbuntu 20.04

Hi @chadgriff1, I don’t have access to the data you are referencing in your workflow, but I suspect what’s happening here is this that the number and order of items between your “Adjustment Counts” node and your “Chk Os Bldg Adj Cnt” node changes.

Now your expression {{ $node["Adjustment Counts"].json["osBldgCnt"] }} would

  • for the first item on the “Chk Os Bldg Adj Cnt” node look up the first item on the “Adjustment Counts” node
  • for the second item on the “Chk Os Bldg Adj Cnt” node look up the second item on the “Adjustment Counts” node
  • etc.

While the first comparison might still work, the second one would fail if your SQL query only returns only a single row.

In such cases you would need another expression accounting for the different data structure. Looking at Built in methods and variables reference | n8n Docs a suitable one in this case could be {{ $("Adjustment Counts").first().json["osBldgCnt"] }}. This would always read the first (and only) item from the “Adjustment Counts” node.

1 Like

@MutedJam Thanks for your response. I can now see I should have included more detail in my post.

The query in the “Adjustment Counts” node will always return 2 records as follows:

OSBLDGCNT 14
NONOSBLDGCNT 0

The upper IF node in my workflow compares the OSBLDGCNT record against a static value of 0 (zero) while the lower IF node compares the NONOSBLDGCNT records against a static value of 0 (zero). In the execution screenshot I provided of the “Chk NonOS Bldg Adj Cnt” you can see where the value of 0 = 0 is resolving to false. Attached is a copy of the execution of the “Adjustment Counts” node.

1 Like

Thanks for sharing this example @chadgriff1 :slight_smile:

While I understand these are two different values, it’s still a single item in the n8n world:

So I think the example provided {{ $("Adjustment Counts").first().json["osBldgCnt"] }} should work fine. Did you have a chance to test this approach in your workflow?

1 Like

I just tested using the method you proposed and it still behaved in the same manner.

The expected behavior is that the upper path for “osbldg” should have sent out a message by following the “false” branch of the IF node (which it did). However, the lower “nonosbldg” branch sent out messages as well by following the “false” branch vs. resolving to the “true” branch by comparing 0 = 0.

Here is how I had both IF nodes configured…

image

In the nonosbldg IF node, I tested with both Combine options (i.e. ALL and ANY) and it resolved to “false” in both configurations.

image

Do I have to do anything special with “Value 2” in the IF node by chance?

Tom,

Disregard…I see what was amiss…I setup the condition within the IF node as a String vs a Number. I rebuilt the IF node and it works as expected now.

Thanks for your help as I probably would have bumped up against the single “item” issue regardless.

3 Likes

Glad to hear you figured it out :slight_smile:

Thanks so much for confirming!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.