Watson IoT Platform - サンプルフロー - シミュレーターの値を閾値で判断

このサンプルのフローでは、シミュレーターからの情報を閾値で判断してアクションへ繋げます。
1. Watson IoTレシピのシミュレータを使用します。
2. 温度を取得し、閾値で判断します。
3. 同じイベントを複数回検知するのを防ぐため、前回の発生から10秒以内であれば無視します。
4. イベントが検知されたらメールで通知します。

#センサータグからの値を閾値で判断するフロー
- [Node-REDのフローをコピーする方法](http://qiita.com/egplnt/items/84b244a50d8bdaced324)を参照して、下記のフローをコピペします。

```lang:シミュレーターからの値を判断
[{"id":"2119c7db.eb32c8","type":"template","z":"283e6bff.404e3c","name":"htmlを返す","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<!DOCTYPE HTML>\n<html>\n\t<head>\n\t\t<title>IoT Quick Dashboard</title>\n\t\t<meta charset=\"utf-8\" />\n\t</head>\n\t<body>\n\n<div id=\"a-box\" style=\"background-color: #96e0a9; padding: 20px;\">\n\n\t<h1>IoT Quick Dashboard</h1>\n\t<br />\n\t<p>\n\t\tセンサー/デバイスから取得されたIoTデータを表示します<br />\n\t</p>\n\t<form method=\"get\" action=\"http://<このIoTアプリの名前>.mybluemix.net/test7\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://<このIoTアプリの名前>.mybluemix.net/test8\">\n\t\t<p><input type=\"submit\" value=\"カウンターをリセットする\"></p>\n\t</form>\n</div>\n\t\t\n<div id=\"b-box\" style=\"background-color: #ecf2fe; padding: 20px; \">\n\n\t<h2>回数\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.count }}</span>回\n\t<br /><br />\n\t\n\t<h2>現在の温度\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.temp }}</span>℃\n\t\t\n\t\t\n</div>\n\n<div id=\"c-box\" style=\"background-color: #f0e8fa; padding: 20px; \">\n\t<br />\n\t<div class=\"category\"><h3>関連リンク</h3></div>\n\t\t<ul>\n\t\t\t<li><a href=\"http://qiita.com/egplnt/items/af867711a7191923b2ff\">Qiita: Watson IoT Platformを使ってみる</a></li>\n\t\t\t<li><a href=\"https://internetofthings.ibmcloud.com/#/\">IBM Watson IoT Platform</a></li>\n\t\t\t<li><a href=\"https://bluemix.net\">Bluemix</a></li>\n\t\t\t<li><a href=\"https://developer.ibm.com/recipes/tutorials/use-the-simulated-device-to-experience-the-iot-foundation/\">Recipe - Simulator</a></li>\n\t\t</ul>\n\t<div class=\"category\"><h3>資料ページ</h3></div>\n\t\t<ul>\n\t\t\t<li><a href=\"http://ibm.com/iot\">IBM IoT</a></li>\n\t\t\t<li><a href=\"https://docs.internetofthings.ibmcloud.com/ja/\">Watson IoT Platform資料ページ</a></li>\n\t\t\t<li><a href=\"https://developer.ibm.com/recipes/\">Recipe</a></li>\n\t\t</ul>\n\t<!---\n\t\t<p>テキスト　（省略）</p>\n\t\t<h3>h3 見出し</h3>\n\t\t<p>テキスト　（省略）</p>\n\t\t<h3>h3 見出し</h3>\n\t\t<p>テキスト　（省略）</p>\n\t--->\n</div>\n\n<div id=\"d-box\" style=\"clear: both; background-color: #e0ce96; font-size: 18px; padding: 10px; text-align: center;  padding: 20px;\">\n\t\tcopyright (c) abc.com All right reserved. \n</div>\n<!---</div> --->\n\t</body>\n</html>\n\n","x":550,"y":380,"wires":[["e4ba2840.9179c8"]]},{"id":"9ffcd620.82ec88","type":"function","z":"283e6bff.404e3c","name":"前回からの間隔を確認","func":"var prev_date = global.get('prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":820,"y":140,"wires":[["b8c01fb.213536","22549763.bc9738"]]},{"id":"765f0a7e.ab7214","type":"http in","z":"283e6bff.404e3c","name":"","url":"/test7","method":"get","swaggerDoc":"","x":180,"y":380,"wires":[["3fc0f8ee.15f12"]]},{"id":"e4ba2840.9179c8","type":"http response","z":"283e6bff.404e3c","name":"","x":710,"y":380,"wires":[]},{"id":"ae2d1ed3.c4a6d","type":"ibmiot in","z":"283e6bff.404e3c","authentication":"quickstart","apiKey":"","inputType":"evt","deviceId":"","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"IBM IoT","service":"quickstart","allDevices":"","allApplications":"","allDeviceTypes":true,"allEvents":true,"allCommands":"","allFormats":"","x":76,"y":140,"wires":[["e5be2001.51a3a","f49c8d43.36f318"]]},{"id":"29392f09.27cae8","type":"template","z":"283e6bff.404e3c","name":"警報発報","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"大きな温度{{payload.d.temp}}が観測されました！","x":480,"y":280,"wires":[["9dfe7095.3e48d8","698ad41.b64c8ac"]]},{"id":"740abd5c.a416ac","type":"function","z":"283e6bff.404e3c","name":"TEMP値をセット","func":"var temp = global.get('temp')||0;\ntemp = msg.payload.d.temp;\nglobal.set('temp',temp);\n\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":140,"wires":[["341d2921.5ed9fe"]]},{"id":"3fc0f8ee.15f12","type":"function","z":"283e6bff.404e3c","name":"値を取得","func":"msg.payload.temp = global.get(\"temp\");\nmsg.payload.count = global.get(\"count\");\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":380,"wires":[["2119c7db.eb32c8"]]},{"id":"9dfe7095.3e48d8","type":"debug","z":"283e6bff.404e3c","name":"","active":true,"console":"false","complete":"false","x":710,"y":320,"wires":[]},{"id":"e5be2001.51a3a","type":"function","z":"283e6bff.404e3c","name":"日付時刻の追加","func":"var date = parseInt(new Date() / 1000);\nvar dt = new Date();\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours()+9;\nvar minute = dt.getMinutes();\nvar second = dt.getSeconds();\nif (!msg.payload) {\n    msg.payload = { \"initial\": \"value\" };\n}\nmsg.payload.date = {\n        \"date\": date,\n        \"year\": year,\n        \"month\": month,\n        \"day\": day,\n        \"hour\": hour,\n        \"minute\": minute,\n        \"second\": second\n}\nreturn msg;","outputs":1,"noerr":0,"x":240,"y":140,"wires":[["740abd5c.a416ac"]]},{"id":"b8c01fb.213536","type":"debug","z":"283e6bff.404e3c","name":"","active":false,"console":"false","complete":"payload.delta","x":990,"y":180,"wires":[]},{"id":"22549763.bc9738","type":"switch","z":"283e6bff.404e3c","name":"前回からの間隔の閾値判定","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"str"}],"checkall":"true","outputs":1,"x":260,"y":260,"wires":[["29392f09.27cae8","a1372b15.975b5"]]},{"id":"13d13256.3a81e6","type":"function","z":"283e6bff.404e3c","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"count\", zero);\nglobal.set(\"temp\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":420,"wires":[["2119c7db.eb32c8"]]},{"id":"9ff40e3e.30438","type":"http in","z":"283e6bff.404e3c","name":"","url":"/test8","method":"get","swaggerDoc":"","x":180,"y":420,"wires":[["13d13256.3a81e6"]]},{"id":"a1372b15.975b5","type":"function","z":"283e6bff.404e3c","name":"カウンターを＋１","func":"var count = global.get('count')||0;\ncount += 1;\nglobal.set('count',count);\n\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":240,"wires":[[]]},{"id":"1fee1742.898439","type":"comment","z":"283e6bff.404e3c","name":"ストリームから取得した値で判定","info":"","x":170,"y":60,"wires":[]},{"id":"f49c8d43.36f318","type":"debug","z":"283e6bff.404e3c","name":"","active":false,"console":"false","complete":"false","x":230,"y":180,"wires":[]},{"id":"341d2921.5ed9fe","type":"switch","z":"283e6bff.404e3c","name":"TEMPで判定","property":"payload.d.temp","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"}],"checkall":"true","outputs":1,"x":610,"y":140,"wires":[["9ffcd620.82ec88","24dbba9e.99c3b6"]]},{"id":"698ad41.b64c8ac","type":"e-mail","z":"283e6bff.404e3c","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":690,"y":280,"wires":[]},{"id":"24dbba9e.99c3b6","type":"debug","z":"283e6bff.404e3c","name":"","active":false,"console":"false","complete":"false","x":790,"y":180,"wires":[]}]

```

![スクリーンショット 2016-06-27 23.36.13.png](https://qiita-image-store.s3.amazonaws.com/0/125842/cf3b732b-f7da-9335-e95b-4ecf290a335e.png)


- [温湿度計シミュレーターを用意します](http://qiita.com/egplnt/items/e1cf075c56ae83addbd9)および[温湿度計シミュレーターをフローに繋いでみる](http://qiita.com/egplnt/items/0e63ee3d5c64ae41713a)を参照して温湿度シミュレーターを用意し、デバイスIDを冒頭の青い「IBM IoT」ノードの「デバイスID」欄に指定します。
- 薄い緑色の「email」ノードにメール宛て先とメール発信のためのユーザーID+アプリパスワードを指定します。このメール発信にGmailを使用する場合にはユーザーID＋アプリパスワードは[Gmailヘルプ - アプリパスワード](https://support.google.com/mail/answer/185833?hl=ja)から取得します。
- 二カ所のオレンジ色の「htmlを返す」ノードを開き、`<form method="get" action="http://<このIoTアプリの名前>.mybluemix.net/test10">`のアプリ名部分を更新します。
- ブラウザの新しいタブを開き、`<IoTアプリ名>.bluemix.net/test7`へアクセスすると下記のような画面が表示されます。

![スクリーンショット 2016-06-27 18.54.49.png](https://qiita-image-store.s3.amazonaws.com/0/125842/60f20198-7476-c9a3-7b8f-f1407a89c296.png)