Problem with global json object variable and JSONata switch decision

Dear friends,

i have something strange. I am trying to make a switch decision with an Global JSON Object and JSONata condition. Problem is that in test zone resolves well but at execution time are not working as expected.

Global "presencia" variable is:


Test Flow is:

[{"id":"c82ac7b3.f78e58","type":"tab","label":"TEST_ZONE","disabled":false,"info":""},{"id":"d92b3715.f42e58","type":"comment","z":"c82ac7b3.f78e58","name":"Control Deslumbrar Salón","info":"","x":330,"y":340,"wires":[]},{"id":"3dc69f34.17b28","type":"switch","z":"c82ac7b3.f78e58","name":"Presencia En Casa","property":"presencia","propertyType":"global","rules":[{"t":"jsonata_exp","v":"abraham = \"Casa\" or hellem = \"Casa\"","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":480,"wires":[["61908026.c932a"]]},{"id":"ebd16dfc.39067","type":"inject","z":"c82ac7b3.f78e58","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":480,"wires":[["3dc69f34.17b28"]]},{"id":"61908026.c932a","type":"debug","z":"c82ac7b3.f78e58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":740,"y":500,"wires":[\*]}]

What's wrong?

Best Regards

you need to use the $globalContext to get the global variable and if you use a single equal sigh, you will assign the value to the variable instead of testing it. The result could be true or false depending on what the result is.
$globalContext('abraham') == "Casa"


JSONata doesn't like double equal for comparison into switch component...

Hi @ljpaff

the JSONata expression option in the Switch node is evaluated against the message it receives - regardless of what you set in the top 'property' box. This means you cannot point it at a context value and evaluate an expression against it. That is slightly surprising to me - I would have expected the JSONata expression to be evaluated against whatever you set in the top property box... I'll have a look to see if we can correct that - although would need to be careful not to change behaviour that people may rely on.

For your scenario, it doesn't matter what you put in the property box, but you can use the following expression:

$globalContext('presencia.abraham') = 'Casa' or $globalContext('presencia.hellem') = 'Casa'
  • the single = is correct for a JSONata expression
  • your value is stored under the context name of presencia - so you need to include that in the key passed to $globalContext

You are right, what is surprising to me is that if you use a payload from msg as property, inside a JSONata expression is populated.

This situation confused me because I was trying to use the same approach when I read a global variable

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