# Having trouble with JSONata expression in switch node

**URL:** <https://discourse.nodered.org/t/having-trouble-with-jsonata-expression-in-switch-node/57956>\
**Category:** General\
**Created:** [7 February 2022 21:29 UTC](https://discourse.nodered.org/t/having-trouble-with-jsonata-expression-in-switch-node/57956 "2022-02-07T21:29:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielbrunt57](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danielbrunt57/32/42815_2.png) [@danielbrunt57](https://discourse.nodered.org/u/danielbrunt57)\
**Post date:** [7 February 2022 21:29 UTC](https://discourse.nodered.org/t/having-trouble-with-jsonata-expression-in-switch-node/57956/1 "2022-02-07T21:29:17Z")

</div>

The error is: `Invalid JSONata expression: Invalid context key`  
Here is my node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7c547ee8c122b3ca4998da2885858b5b900d2d92.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58f6ea5c1bf738e38051973de07e710d221ae65f.png)

```auto
[{"id":"1e0a4c9d9b44c848","type":"switch","z":"3a2942f23b8df6dc","name":"Bedroom?","property":"payload.cover","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"payload.cover != \"cover.bedroom_blind\" or\t (payload.cover = \"cover.bedroom_blind\" and \t $globalContext(homeassistant.homeAssistant.states[\"person.daniel_brunt\"].state) = \"home\" and \t $globalContext(homeassistant.homeAssistant.states[\"input_boolean.sleep_mode\"].state) = \"off\")\t","vt":"jsonata"}],"checkall":"false","repair":false,"outputs":1,"x":619,"y":1120,"wires":[["3aabdce8ca72cedb","dad60146b95672f9"]]}]

```

---

<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 February 2022 21:49 UTC](https://discourse.nodered.org/t/having-trouble-with-jsonata-expression-in-switch-node/57956/2 "2022-02-07T21:49:53Z")

</div>

Is `homeassistant.homeAssistant.states["person.daniel_brunt"].state` a message property or a string.  
What happens if you use a debug set to JSONata and set it to

```auto
$globalContext(homeassistant.homeAssistant.states["person.daniel_brunt"].state)

```

, is it undefined or thdoes it throw an error

---

<div class="post-metadata">

**Author:** ![danielbrunt57](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danielbrunt57/32/42815_2.png) [@danielbrunt57](https://discourse.nodered.org/u/danielbrunt57)\
**Post date:** [7 February 2022 22:05 UTC](https://discourse.nodered.org/t/having-trouble-with-jsonata-expression-in-switch-node/57956/3 "2022-02-07T22:05:53Z")

</div>

Thanks for opening my eyes to another use of the Debug node!  
It should be a string! This works:  
`$globalContext('homeassistant.homeAssistant.states["person.daniel_brunt"].state')`

Thanks!!

---

<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:** [21 February 2022 22:06 UTC](https://discourse.nodered.org/t/having-trouble-with-jsonata-expression-in-switch-node/57956/4 "2022-02-21T22:06:20Z")

</div>

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