# Access to context Stores in JSONata expressions

**URL:** https://discourse.nodered.org/t/access-to-context-stores-in-jsonata-expressions/8738
**Category:** General
**Created:** [8 March 2019 12:01 UTC](https://discourse.nodered.org/t/access-to-context-stores-in-jsonata-expressions/8738 "2019-03-08T12:01:38Z")
**Posts on this page:** 1
**Showing post:** 8

<div class="post-metadata">

### Author: ![IanH](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianh/32/95002_2.png) [@IanH](https://discourse.nodered.org/u/IanH)
#### Post date: [9 March 2019 09:02 UTC](https://discourse.nodered.org/t/access-to-context-stores-in-jsonata-expressions/8738/8 "2019-03-09T09:02:11Z")

</div>

OK, got this working with a Change Node (but that was just for debug), but refuses to work on a switch node (in my case JSONata is in the property field). The JSONata evaluates to an integer.  
The attached flow has a Change Node and Switch node fed by the same message.

The Change node {name: "Read file:Zones elements"} produces a valid output JSON as follows

```auto
{"_msgid":"96befb43.0300d8","topic":"","payload":{"ontime":"16:54","total":2,"Today":17964,"DayLastWatered":17960,"Period":2},"thisZone":0,"Today":17964}

```

Note the msg.payload.total evaluates to 2, this is what I expect in the switch node property.

The Switch node {name:"Watering Due"} reports an error as follow

> Error: Invalid JSONata expression: Cannot read property 'Zone' of undefined

This is a snippet of the flow, just needs the node {name:"All Zones Done"} fed with msg.thisZone=0 and msg.Today=17964.

```auto
[{"id":"721d778f.e33648","type":"change","z":"ea51febe.392e5","name":"Next thisZone","rules":[{"t":"set","p":"thisZone","pt":"msg","to":"msg.thisZone+1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":400,"wires":[["543b1a39.2a9ff4","eeb1e93b.25dca8"]]},{"id":"543b1a39.2a9ff4","type":"switch","z":"ea51febe.392e5","name":"All Zones Done","property":"thisZone","propertyType":"msg","rules":[{"t":"gte","v":"#:(file)::Zones.ZonesQty","vt":"global"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":140,"y":400,"wires":[["7a418753.e9a628"],["a6c43cb6.64ee4","2a2e0f5c.ceea1"]],"outputLabels":["finished","Check next Zone"]},{"id":"a6c43cb6.64ee4","type":"switch","z":"ea51febe.392e5","name":"Watering Due","property":"msg.Today - (\t $globalContext(\t \"Zones.Zone[\" & $string(msg.thisZone) & \"].DayLastWatered\",\t \"file\"\t ) \t + $globalContext(\t \"Zones.Zone[\" & $string(msg.thisZone) & \"].Fixed.Period\" ,\t \"file\" \t ) \t)","propertyType":"jsonata","rules":[{"t":"gte","v":"0","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":380,"y":400,"wires":[["f452e453.78e958","721d778f.e33648"],["721d778f.e33648"]],"outputLabels":["Water","Do Not water"]},{"id":"f452e453.78e958","type":"debug","z":"ea51febe.392e5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":630,"y":480,"wires":[]},{"id":"7a418753.e9a628","type":"debug","z":"ea51febe.392e5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":350,"y":360,"wires":[]},{"id":"eeb1e93b.25dca8","type":"debug","z":"ea51febe.392e5","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":860,"y":400,"wires":[]},{"id":"2a2e0f5c.ceea1","type":"change","z":"ea51febe.392e5","name":"Read file:Zones elements","rules":[{"t":"set","p":"payload.total","pt":"msg","to":"msg.Today - (\t $globalContext(\t \"Zones.Zone[\" & $string(msg.thisZone) & \"].DayLastWatered\",\t \"file\"\t ) \t + $globalContext(\t \"Zones.Zone[\" & $string(msg.thisZone) & \"].Fixed.Period\" ,\t \"file\" \t ) \t)","tot":"jsonata"},{"t":"set","p":"payload.Today","pt":"msg","to":"msg.Today","tot":"jsonata"},{"t":"set","p":"payload.DayLastWatered","pt":"msg","to":"$globalContext(\t \"Zones.Zone[\" & $string(msg.thisZone) & \"].DayLastWatered\",\t \"file\"\t\t\t) ","tot":"jsonata"},{"t":"set","p":"payload.Period","pt":"msg","to":"$globalContext(\t \"Zones.Zone[\" & $string(msg.thisZone) & \"].Fixed.Period\",\t \"file\"\t\t) ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":480,"wires":[["7909d6b1.5ef2b8"]]},{"id":"7909d6b1.5ef2b8","type":"debug","z":"ea51febe.392e5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":710,"y":520,"wires":[]}]

```

The file:Zones Object contains

```auto
{"PumpMaxDel":50,"DailyStart":"16:54","ZonesQty":8,"Zone":[{"Number":0,"Name":"Spare","Description":"Spare outlet on Vege Patch Solinoids for solinoid switched hose","FlowRate":20,"Enable":"Off ","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17960},{"Number":1,"Name":"Bed 1","Description":"Inside Vege bed nearest to Skillion Shed","FlowRate":6,"Enable":"Fixed","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17962},{"Number":2,"Name":"Bed 2","Description":"Inside Vege bed second nearest to Skillion Shed","FlowRate":3.55,"Enable":"Fixed","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17961},{"Number":3,"Name":"Bed 3","Description":"Inside central Vege bed ","FlowRate":6,"Enable":"Fixed","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17960},{"Number":4,"Name":"Bed 4","Description":"Inside Vege bed second nearest to Creek","FlowRate":6,"Enable":"Fixed","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17964},{"Number":5,"Name":"Bed 5","Description":"Inside Vege bed nearest to Creek","FlowRate":6,"Enable":"Fixed","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17963},{"Number":6,"Name":"East Bed","Description":"Outside Vege bed nearest to Skillion","FlowRate":10,"Enable":"Fixed","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17962},{"Number":7,"Name":"West Bed","Description":"Outside Vege bed nearest to Creek","FlowRate":10,"Enable":"Fixed","Fixed":{"Duration":60,"Period":2},"Evap":{"Defined ":false},"DayLastWatered":17961}]}

```

---

_[View the full topic](https://discourse.nodered.org/t/access-to-context-stores-in-jsonata-expressions/8738)._
