Switch not working with global dictionary

I need a simple switch with 2 outputs. One for "all conditions are true" (logically AND'ed) and one "otherwise".
For this to end, I create an empty dictionary on start (global scope):

let watchdog_status = global.get('watchdog_status') || {};

Other nodes then set conditions like:

let watchdog_status = global.get('watchdog_status') || {};

watchdog_status['cond1'] = false;

global.set('watchdog_status', watchdog_status);

or

...

watchdog_status['cond2'] = true;

...

A debug-output right before the switch (via node.warn(JSON.stringify(global.get('watchdog_status'))); in a function node) looks like:

{
    "cond1": false,
    "cond2": true
}

The Switch-Node then looks like:

While "testing" in the Expression Editor works:
Screenshot 2022-05-06 at 11-25-56 Node-RED
...the switch never fires on the expression, but always on "otherwise".

What am I doing wrong? (or is this an issue?)

My current workaround is to have a function node, that evaluates ALL conditions and stores the result in a single (global) value. That single boolean then can be checked in a switch (global.var "is true" -> output1; "otherwise" -> output2).
It's working but looks really dumb!

Try cond1 and cond2 = true

I tested with this payload.cond1 and payload.cond2 and it works for me :wink:

Try using the JSONata in the property.
$count($globalContext("watchdog_status").*[$=true]) and == 2 in rule
Or
false in $globalContext("watchdog_status").*
and is false in rule.

The JSONata test window is using the route msg scope, as is the JSONata in the rules.

2 Likes

Many thanks everybody for all your suggestions.
@Buckskin, @smcgann99 : I tried that already (more like (cond1 and cond2) = true or even things like cond1 = true and cond2 = true. Nothing worked for me (again: it works in the test-section of the editor while the switch never fires).
It seems, the trick is to not use the dictionary as the property but an expression instead like @E1cid suggested!

For some reasons (idk), false in $globalContext("watchdog_status").* as property and is false in rule does not work.
But counting all booleans work! I now use this with a reverse logic so I don't have to care about the switch in case the number of booleans to check increase in the future - the switch failes as long as at least ONE (or more) of N booleans is false:
Property: $count($globalContext("watchdog_status").*[$=false]) and Rule: == 0 -> everything is fine! Otherwise: FAIL!
Btw: I use this for health checks in a multi flow (therefore the global context) container in kubernetes where the connected status to ttn is one condition beside others.

Again: Thanks to everybody!

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