Can you use JSONata with global or flow variables?

4,681 views
Skip to first unread message

Zenofmud

unread,
Jul 26, 2017, 6:35:36 AM7/26/17
to node...@googlegroups.com
npm: v3.10.10
node: v6.11.0
node-red: v0.17.4
OS: OSX 10.11.6

While trying to help on another thread, I tried an experiment to use the change node to increment a global (and a flow) variable using JSONata but it doesn’t work. Attached is a flow and image of the flow.

Tests
--------
Step 1 initializes global.counter
Step 2 moves global.counter to msg.payload and dumps it (via debug)
Step 3 attempts a JSONata on the global.counter and moves it to msg.payload and dumps it
Step 4 moves global.counter to msg.payload and does the JSONata to msg.payload
Step 5 just puts a different value in the global to make sure things change.

Conclusion: you can (currently) use JSONata on flow or global variables.

Question: Does JSONata only work with msg.xxxx or this bug?



[{"id":"df8bd2ff.2d4ea8","type":"inject","z":"e3e564e7.9d276","name":"Step (1) initilize Global.counter","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":200,"y":1460,"wires":[["49a6c154.d49498"]]},{"id":"49a6c154.d49498","type":"change","z":"e3e564e7.9d276","name":"set global.counter","rules":[{"t":"set","p":"counter","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":1460,"wires":[[]]},{"id":"834d644a.b5125","type":"inject","z":"e3e564e7.9d276","name":"Step (2) dump global.counter","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":200,"y":1500,"wires":[["7a4cd2f4.51a42c"]]},{"id":"ca4b684e.2d62a","type":"debug","z":"e3e564e7.9d276","name":"Display msg.payload","active":true,"console":"false","complete":"payload","x":800,"y":1500,"wires":[]},{"id":"7a4cd2f4.51a42c","type":"change","z":"e3e564e7.9d276","name":"Set msg.payload = global.counter","rules":[{"t":"set","p":"payload","pt":"msg","to":"counter","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":1500,"wires":[["ca4b684e.2d62a"]]},{"id":"db1f26ec.743d3","type":"change","z":"e3e564e7.9d276","name":"","rules":[{"t":"set","p":"counter","pt":"global","to":"(global.counter +1)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"counter","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":1540,"wires":[["d28da755.c7f88"]]},{"id":"9a9ce4b9.6a984","type":"inject","z":"e3e564e7.9d276","name":"Step (3) attempt jsonata on global","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":220,"y":1540,"wires":[["db1f26ec.743d3"]]},{"id":"d28da755.c7f88","type":"debug","z":"e3e564e7.9d276","name":"Display msg.payload","active":true,"console":"false","complete":"true","x":800,"y":1540,"wires":[]},{"id":"c26ab0ed.b7444","type":"change","z":"e3e564e7.9d276","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"counter","tot":"global"},{"t":"set","p":"payload","pt":"msg","to":"(msg.payload + 1)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":1580,"wires":[["71c0e205.6ee3d4"]]},{"id":"bea39865.1152a","type":"inject","z":"e3e564e7.9d276","name":"Step (4) jsonata on msg.payload","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":210,"y":1580,"wires":[["c26ab0ed.b7444"]]},{"id":"71c0e205.6ee3d4","type":"debug","z":"e3e564e7.9d276","name":"Display msg.payload","active":true,"console":"false","complete":"true","x":800,"y":1580,"wires":[]},{"id":"e3bd860c.3beab","type":"inject","z":"e3e564e7.9d276","name":"Step (5) set Global.counter to 20","topic":"","payload":"20","payloadType":"num","repeat":"","crontab":"","once":false,"x":210,"y":1640,"wires":[["bc8a824e.5aa498"]]},{"id":"bc8a824e.5aa498","type":"change","z":"e3e564e7.9d276","name":"set global.counter","rules":[{"t":"set","p":"counter","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":1640,"wires":[[]]}]

Dave C-J

unread,
Jul 26, 2017, 6:54:35 AM7/26/17
to node...@googlegroups.com
See the blog for some info on using flow and global variables with jsonata 
https://nodered.org/blog/2017/06/28/version-0-17-released
sent from phone

Zenofmud

unread,
Jul 26, 2017, 8:25:10 AM7/26/17
to node...@googlegroups.com
Ok, help…I’d looked at the blog and at the 0.16 (where I started from). In the 0.16 notes there is the example
And I hoped I could use the same format for a global, but that doesn't work (i.e. 
set - global.counter
to - J: (global.counter +1)
but that doesn’t work and at this point I’ve tried so many variations my head hurts.

so my question is how would you use a global in this case or can’t you?

Thanks
Paul

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/CACXWFwKL%3DZFdXvEaN4_kghBth8NrwKQwH8i_zSMBygEZMgiu_Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Nick O'Leary

unread,
Jul 26, 2017, 8:28:39 AM7/26/17
to Node-RED Mailing List
Hi,

the 0.17 blog does mention, albeit briefly and without a full example, that we've introduced the $flowContext and $globalContext functions to access their respective contexts from within a JSONata expression.

So for your example of incrementing global.counter, you would use the expression: $globalContext("counter")+1

N

On 26 July 2017 at 05:25, Zenofmud <zeno...@zenofmud.org> wrote:
Ok, help…I’d looked at the blog and at the 0.16 (where I started from). In the 0.16 notes there is the example
And I hoped I could use the same format for a global, but that doesn't work (i.e. 
set - global.counter
to - J: (global.counter +1)
but that doesn’t work and at this point I’ve tried so many variations my head hurts.

so my question is how would you use a global in this case or can’t you?

Thanks
Paul

On Jul 26, 2017, at 6:54 AM, Dave C-J <dce...@gmail.com> wrote:

See the blog for some info on using flow and global variables with jsonata 
https://nodered.org/blog/2017/06/28/version-0-17-released
sent from phone

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Zenofmud

unread,
Jul 26, 2017, 8:42:14 AM7/26/17
to node...@googlegroups.com
Nick, Thank you!!! I’d tried $globalContect.counter, $globalContect(counter), $globalContect["counter”] but not the correct one.

Now I’m off to help the other thread :>)

Paul
On Jul 26, 2017, at 8:28 AM, Nick O'Leary <nick....@gmail.com> wrote:

Hi,

the 0.17 blog does mention, albeit briefly and without a full example, that we've introduced the $flowContext and $globalContext functions to access their respective contexts from within a JSONata expression.

So for your example of incrementing global.counter, you would use the expression: $globalContext("counter")+1

N
On 26 July 2017 at 05:25, Zenofmud <zeno...@zenofmud.org> wrote:
Ok, help…I’d looked at the blog and at the 0.16 (where I started from). In the 0.16 notes there is the example
<Screen Shot 2017-07-26 at 8.20.06 AM.png>
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages