Selecting a specific value in an array + workflow optimization

Hi,
Just recently came up n8n, and think it’s a really great project - so hats off to you!

Was wondering if someone might be able to tell me the correct expression (or function if necessary) to use for this, as I’ve been breaking my head on it (I admittedly have very limited js experience).
I have an array of data taken from a MySQL db, that looks like this:
image

I’m trying to set up an IF node (really, multiple IF nodes, as described below), that will be based on the value ‘balance’ of a specific ‘exchange’ and ‘symbol’ combination.

Could you tell me what is the expression I could use that would do that? (Or if need be, the function to use for that?)

I’m looking to trigger an action (i.e. send an email), when the balance for a specific symbol (on specific exchange), reaches below a certain level. The plan is to use a cron job to query the db every few seconds, and have a separate if node for each of the symbols/balances being monitored. There’s likely to be ~25-45 exchange/symbol combinations with their own IF node.

It would be great to also have some confirmation if this workflow is optimal, and a suitable use-case for n8n, or if you would either design it differently or utilize different technologies. Diagram below…
Any help would be much appreciated!

Hey @diskin, from looking at your screenshot and based on your description I think the workflow you have shared would to the trick. That’s assuming each IF node checks whether the respective conditions are met (for example something like exchange equals Binance AND symbol equals btc and balance lower than 0.0003).

Here is a simple example workflow containing a single IF node with working expressions:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    exchange: \"Binance\",\n    symbol: \"bch\",\n    balance: \"0\"\n  }\n}, {\n  json: {\n    exchange: \"Binance\",\n    symbol: \"btc\",\n    balance: \"0.00024266\"\n  }\n}, {\n  json: {\n    exchange: \"Binance\",\n    symbol: \"busd\",\n    balance: \"244.36985825\"\n  }\n}]"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"exchange\"]}}",
              "value2": "Binance"
            },
            {
              "value1": "={{$json[\"symbol\"]}}",
              "value2": "btc"
            }
          ],
          "number": [
            {
              "value1": "={{$json[\"balance\"]}}",
              "value2": 0.0003
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Data": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

The expressions used here are rather simple, for example {{$json["exchange"]}}. This would simply refer to the value in the exchange field of each input item. You don’t have to write it but can select this through the tree structure on the left hand side of the expression editor by clicking Current Node > Input Data > JSON > exchange.

So you wouldn’t need to write any JavaScript here. As for optimization, in a first step you could have all true outputs connect to a single Email node. That’s assuming all your items have the same structure and you are using expressions in your emails too (something like Hi, the balance of {{$json["symbol"]}} held at {{$json["exchange"]}} has dropped below the threshold and is now at {{$json["balance"]}}.). The Email node would then run once for each item it receives, so the workflow would still work even if multiple IF conditions are true and multiple items make it through.

Also, the NoOp nodes are optional (though I do tend to use them to describe why no action is required in a certain point in my workflow).

Hope this helps!

@MutedJam, thanks so much - worked perfectly. Not sure why I wasn’t intuitively understanding the use of multiple conditions in the IF node. I had a feeling I was missing something.

It’s beyond the scope of the first question, but perhaps you could weigh on a different problem that I’m looking to solve? If one of the IF nodes resolves true (and successfully sends of a trigger), it will of course, keep triggering with each subsequent check/run (until the balance rises above the threshold) - that’s not ideal, or even possibly problematic.

What would be great, is after the IF node has resolved to true and triggered an action, it would wait until it first resolves back as false (i.e. above the threshold), before triggering again.
I know there is the WAIT node. The two ‘time’ options of the WAIT node wouldn’t really be suitable for this. The webhooks option could work, but seems like a pretty roundabout way.

Does this functionality that I’m describing (waiting to resolve false, before triggering again on true) either already exist, or otherwise easily accomplished?

Again, thanks so much for your help!