Node-RED - フロー内で共有する変数など

初めてNode-REDを使ってみよう、というかたのためのガイドです。

#フロー内で共有できる変数 - global
- Node-REDのフロー内で値を共有できる変数を`global`で作成する事ができます。
- [Node-REDのフローをコピーする方法](http://qiita.com/egplnt/items/00bbda990c5babaeaa86)を参照して、下記のフローをコピペします。

```lang:global変数の使い方
[{"id":"5106b054.b39f3","type":"inject","z":"94072ee9.c38dc","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":150,"y":160,"wires":[["3fd0f11e.c35986"]]},{"id":"6b4393d4.788cbc","type":"debug","z":"94072ee9.c38dc","name":"","active":true,"console":"false","complete":"sum","x":580,"y":160,"wires":[]},{"id":"3fd0f11e.c35986","type":"function","z":"94072ee9.c38dc","name":"global変数への加算","func":"var s = global.get(\"sum\")||0;\ns += msg.payload;\nglobal.set(\"sum\", s);\nmsg.sum = s;\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":160,"wires":[["6b4393d4.788cbc"]]},{"id":"ed60fe08.fe653","type":"function","z":"94072ee9.c38dc","name":"リセット","func":"var zero = 0\nglobal.set(\"sum\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":280,"wires":[["37330d4d.8c5f42"]]},{"id":"2208c51b.9bacea","type":"inject","z":"94072ee9.c38dc","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":160,"y":280,"wires":[["ed60fe08.fe653"]]},{"id":"37330d4d.8c5f42","type":"debug","z":"94072ee9.c38dc","name":"","active":true,"console":"false","complete":"sum","x":580,"y":280,"wires":[]},{"id":"833a6849.8161f8","type":"debug","z":"94072ee9.c38dc","name":"","active":true,"console":"false","complete":"sum","x":580,"y":220,"wires":[]},{"id":"42ad8aaf.dcb134","type":"inject","z":"94072ee9.c38dc","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":150,"y":220,"wires":[["a757adef.5e9a4"]]},{"id":"a757adef.5e9a4","type":"function","z":"94072ee9.c38dc","name":"global変数への加算","func":"var s = global.get(\"sum\")||0;\ns += msg.payload;\nglobal.set(\"sum\", s);\nmsg.sum = s;\nreturn msg;","outputs":1,"noerr":0,"x":350,"y":220,"wires":[["833a6849.8161f8"]]}]

```

- 一つめのフローの「+1」と書かれたinjectノードのボタンを押して、メッセージが送付されるたび（異なるメッセージ）にglobal変数 `sum`の値が増加する事を確認します。
![スクリーンショット 2016-06-29 11.19.06.png](https://qiita-image-store.s3.amazonaws.com/0/125842/162a2965-e5ab-108b-9778-664ac4361325.png)
![スクリーンショット 2016-06-29 11.19.13.png](https://qiita-image-store.s3.amazonaws.com/0/125842/8badd9dd-39b9-2813-0a1f-129bcf833a36.png)

- 二つめのフローの「+100」と書かれたinjectノードのボタンを押して、別フローからのメッセージの入力でもglobal変数 `sum`の値へ加算される事を確認します。
![スクリーンショット 2016-06-29 11.20.12.png](https://qiita-image-store.s3.amazonaws.com/0/125842/5381143f-21ae-3d70-5833-5e64a29d36da.png)
![スクリーンショット 2016-06-29 11.20.23.png](https://qiita-image-store.s3.amazonaws.com/0/125842/2dd91498-c845-f0d7-71e5-fbed4bd5ea3c.png)

#ノードで共有できる変数 - context
- Node-REDのノードで値を共有できる変数を`context`で作成する事ができます。
- 前節の`global`変数とは異なり、`context`変数ではフロー間で変数を共有する事はできません。同一フロー内で、後続メッセージとの間で変数を共有する（例：カウンターを加算していく、等）際に使用できます。

- 下記のフローをコピペします。

```lang:context変数の使い方
[{"id":"dfafe05f.baa85","type":"function","z":"caa14425.00de","name":"context変数へ追加","func":"var s = context.get(\"sum\")||0;\ns += msg.payload;\ncontext.set(\"sum\",s);\nmsg.sum = s;\nreturn msg;\n","outputs":1,"noerr":0,"x":290,"y":120,"wires":[["91d34062.7e7f1"]]},{"id":"60095533.c40fb4","type":"inject","z":"caa14425.00de","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":90,"y":120,"wires":[["dfafe05f.baa85"]]},{"id":"91d34062.7e7f1","type":"debug","z":"caa14425.00de","name":"","active":true,"console":"false","complete":"sum","x":560,"y":120,"wires":[]},{"id":"b696395c.102698","type":"debug","z":"caa14425.00de","name":"","active":true,"console":"false","complete":"sum","x":560,"y":200,"wires":[]},{"id":"14c839f2.4d3a9e","type":"inject","z":"caa14425.00de","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":90,"y":200,"wires":[["96508a47.9eace"]]},{"id":"96508a47.9eace","type":"function","z":"caa14425.00de","name":"別フローから加算（できない）","func":"var s = context.get(\"sum\")||0;\ns += msg.payload;\ncontext.set(\"sum\",s);\nmsg.sum = s;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":200,"wires":[["b696395c.102698"]]}]

```

- 一つめのフローの「+1」と書かれたinjectノード左側のボタンを押して、メッセージが送付されるたび（異なるメッセージ）にcontext変数 `sum`の値が増加する事を確認します。
![スクリーンショット 2016-05-22 9.05.23.png](https://qiita-image-store.s3.amazonaws.com/0/125842/4e8b225b-0150-3996-258a-41453cb30121.png)
![スクリーンショット 2016-05-22 8.29.26.png](https://qiita-image-store.s3.amazonaws.com/0/125842/7e04df13-65e1-8a99-49c1-9f75c5473f54.png)
![スクリーンショット 2016-05-22 8.29.36.png](https://qiita-image-store.s3.amazonaws.com/0/125842/79c896e1-0732-5a72-427f-98101dee23fe.png)
![スクリーンショット 2016-05-22 8.29.43.png](https://qiita-image-store.s3.amazonaws.com/0/125842/6ca9894c-a808-f506-d5fe-134f88177cc3.png)

- 二つめのフローの「+100」と書かれたinjectノード左側のボタンを押しても、上記のフローとは異なるfunctionノード（＝ノードを共有していない）ため、一つめのフローのcontext変数`sum`へは加算されず、二つめのフローのcontext変数 `sum`（上記とは同名だが異なる変数）へ加算される事を確認します。
![スクリーンショット 2016-06-29 11.28.23.png](https://qiita-image-store.s3.amazonaws.com/0/125842/c157a6f6-867d-f9d1-e823-ba36c7c59eb4.png)




* * *
（以下はdeprecateされています。参照[Node-RED: Writing Functions](http://nodered.org/docs/writing-functions)）
#フロー内で共有できる変数 - context.global
- Node-REDのフロー内で値を共有できる変数を`context.global.xxx`で作成する事ができます。
- 下記のフローをコピペします。

```lang:context.global変数の使い方
[{"id":"563a7a92.68a924","type":"inject","z":"ff1028b2.e29428","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":380,"wires":[["1c71e61b.77669a"]]},{"id":"4d54ab3a.4f00a4","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":540,"y":380,"wires":[]},{"id":"1c71e61b.77669a","type":"function","z":"ff1028b2.e29428","name":"context.global.sumへ加算","func":"context.global.sum = context.global.sum + msg.payload;\nmsg.sum = context.global.sum;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":380,"wires":[["4d54ab3a.4f00a4"]]},{"id":"a1fa5bd7.2b233","type":"function","z":"ff1028b2.e29428","name":"初期化","func":"context.global.sum = 0;\nmsg.sum = context.global.sum;\nreturn msg;","outputs":1,"noerr":0,"x":270,"y":320,"wires":[["3622297d.12c756"]]},{"id":"bc2193de.68c978","type":"inject","z":"ff1028b2.e29428","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":120,"y":320,"wires":[["a1fa5bd7.2b233"]]},{"id":"3622297d.12c756","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":540,"y":320,"wires":[]},{"id":"ef53b486.3e3738","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":540,"y":440,"wires":[]},{"id":"5eb4468e.a7dd98","type":"function","z":"ff1028b2.e29428","name":"context.global.sumへ加算","func":"context.global.sum = context.global.sum + msg.payload;\nmsg.sum = context.global.sum;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":440,"wires":[["ef53b486.3e3738"]]},{"id":"130f18ba.c828df","type":"inject","z":"ff1028b2.e29428","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":110,"y":440,"wires":[["5eb4468e.a7dd98"]]}]

```

- 一つめのフローのinjectノード左側のボタンを押して`context.global.sum`変数を初期化します。
![スクリーンショット 2016-05-22 8.20.33.png](https://qiita-image-store.s3.amazonaws.com/0/125842/08070b5f-6d19-a721-3439-2fbf05eafc44.png)
![スクリーンショット 2016-05-22 8.29.06.png](https://qiita-image-store.s3.amazonaws.com/0/125842/ad4bca6d-50a5-49f5-4ef0-08af1d95121a.png)

- 二つめのフローの「+1」と書かれたinjectノードのボタンを押して、メッセージが送付されるたび（異なるメッセージ）に`context.global.sum`の値が増加する事を確認します。
![スクリーンショット 2016-05-22 8.28.42.png](https://qiita-image-store.s3.amazonaws.com/0/125842/9f031a54-4b36-fc42-33e8-e114d2c3d36d.png)
![スクリーンショット 2016-05-22 8.29.26.png](https://qiita-image-store.s3.amazonaws.com/0/125842/7e04df13-65e1-8a99-49c1-9f75c5473f54.png)
![スクリーンショット 2016-05-22 8.29.36.png](https://qiita-image-store.s3.amazonaws.com/0/125842/79c896e1-0732-5a72-427f-98101dee23fe.png)
![スクリーンショット 2016-05-22 8.29.43.png](https://qiita-image-store.s3.amazonaws.com/0/125842/6ca9894c-a808-f506-d5fe-134f88177cc3.png)

- 三つめのフローの「+100」と書かれたinjectノードのボタンを押して、別フローからのメッセージの入力でも`context.global.sum`の値へ加算される事を確認します。
![スクリーンショット 2016-05-22 8.28.49.png](https://qiita-image-store.s3.amazonaws.com/0/125842/71f1c6be-1a43-f1e4-3d9c-a0abefceb29c.png)
![スクリーンショット 2016-05-22 8.29.53.png](https://qiita-image-store.s3.amazonaws.com/0/125842/08127a54-499c-d27d-f211-cc1116bd6850.png)

#ノードで共有できる変数 - context
- Node-REDのノードで値を共有できる変数を`context.xxx`で作成する事ができます。
- 前節の`context.global.xxx`とは異なり、`context.xxx`ではフロー間で変数を共有する事はできません。同一フロー内で、後続メッセージとの間で変数を共有する（例：カウンターを加算していく、等）際に使用できます。

- 下記のフローをコピペします。

```lang:context.global変数の使い方
[{"id":"ff6100ff.099f88","type":"function","z":"ff1028b2.e29428","name":"context変数へ追加","func":"if (!context.sum) {\n  context.sum = 0;\n}\ncontext.sum = context.sum + msg.payload;\nmsg.sum = context.sum;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":1120,"wires":[["745271a5.6ae3b"]]},{"id":"ee780d9a.5322f","type":"inject","z":"ff1028b2.e29428","name":"+1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":1120,"wires":[["ff6100ff.099f88"]]},{"id":"745271a5.6ae3b","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":600,"y":1120,"wires":[]},{"id":"cf0aa564.303ac","type":"debug","z":"ff1028b2.e29428","name":"","active":true,"console":"false","complete":"sum","x":600,"y":1200,"wires":[]},{"id":"2555f2a4.b9bcbe","type":"function","z":"ff1028b2.e29428","name":"別フローから加算（できない）","func":"context.sum = context.sum + msg.payload;\nmsg.sum = context.sum;\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":1200,"wires":[["cf0aa564.303ac"]]},{"id":"923f735a.f5084","type":"inject","z":"ff1028b2.e29428","name":"+100","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"x":130,"y":1200,"wires":[["2555f2a4.b9bcbe"]]}]

```

- 一つめのフローの「+1」と書かれたinjectノード左側のボタンを押して、メッセージが送付されるたび（異なるメッセージ）に`context.sum`の値が増加する事を確認します。
![スクリーンショット 2016-05-22 9.05.23.png](https://qiita-image-store.s3.amazonaws.com/0/125842/4e8b225b-0150-3996-258a-41453cb30121.png)
![スクリーンショット 2016-05-22 8.29.26.png](https://qiita-image-store.s3.amazonaws.com/0/125842/7e04df13-65e1-8a99-49c1-9f75c5473f54.png)
![スクリーンショット 2016-05-22 8.29.36.png](https://qiita-image-store.s3.amazonaws.com/0/125842/79c896e1-0732-5a72-427f-98101dee23fe.png)
![スクリーンショット 2016-05-22 8.29.43.png](https://qiita-image-store.s3.amazonaws.com/0/125842/6ca9894c-a808-f506-d5fe-134f88177cc3.png)

- 二つめのフローの「+100」と書かれたinjectノード左側のボタンを押しても、上記のフローとは異なるfunctionノード（＝ノードを共有していない）ため、`context.sum`の値へは加算されないことを確認します。Debug出力へ表示される「NaN」は「Not a Number」の略で、この「別フローから加算（できない）」functionノード内の`context.sum`変数が初期化されていないためにエラーとなっていることを表します。
![スクリーンショット 2016-05-22 9.05.36.png](https://qiita-image-store.s3.amazonaws.com/0/125842/b8e855e0-5a5d-3977-f04e-f10e490f8b13.png)
![スクリーンショット 2016-05-22 9.05.42.png](https://qiita-image-store.s3.amazonaws.com/0/125842/83ade6c6-e00f-67ad-9776-a7bce630ba48.png)

#参考情報
[Node RED Programming Guide - Lecture 5](http://noderedguide.com/index.php/2015/11/06/node-red-lecture-5-the-node-red-programming-model/#h.z5610duwm6dk)
[Using Node-RED: Writing Functions](http://nodered.org/docs/writing-functions)