# Switch not working with global dictionary

**URL:** <https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281>\
**Category:** General\
**Tags:** function-node\
**Created:** [6 May 2022 09:42 UTC](https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281 "2022-05-06T09:42:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will.Freitag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/will.freitag/32/61170_2.png) [@Will.Freitag](https://discourse.nodered.org/u/Will.Freitag)\
**Post date:** [6 May 2022 09:42 UTC](https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281/1 "2022-05-06T09:42:26Z")

</div>

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):

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

```

Other nodes then set conditions like:

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

watchdog_status['cond1'] = false;

global.set('watchdog_status', watchdog_status);

```

or

```auto
...

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:

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

```

The Switch-Node then looks like:

 ![Screenshot 2022-05-06 at 11-23-14 Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32ef7de7ec90d3f228a7e31cc215040b7cae992e.png)

While "testing" in the Expression Editor works:  
 ![Screenshot 2022-05-06 at 11-25-56 Node-RED](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/c/8c8ab453cfddc623ef592b8383d2ddf53902e6d7.png)  
...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!

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [7 May 2022 21:38 UTC](https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281/2 "2022-05-07T21:38:56Z")

</div>

Try `cond1 and cond2 = true`

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [7 May 2022 23:14 UTC](https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281/3 "2022-05-07T23:14:27Z")

</div>

I tested with this **payload.cond1 and payload.cond2** and it works for me 😉

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [7 May 2022 23:24 UTC](https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281/4 "2022-05-07T23:24:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Will.Freitag](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/will.freitag/32/61170_2.png) [@Will.Freitag](https://discourse.nodered.org/u/Will.Freitag)\
**Post date:** [9 May 2022 06:47 UTC](https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281/5 "2022-05-09T06:47:10Z")

</div>

Many thanks everybody for all your suggestions.  
@Buckskin, @Sean-McG : 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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 May 2022 06:47 UTC](https://discourse.nodered.org/t/switch-not-working-with-global-dictionary/62281/6 "2022-05-23T06:47:24Z")

</div>

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