Watson IoT Platform - サンプルフロー - センサーからの値を閾値で判断

初めてWatson IoT Platformを使ってみよう、というかたのためのガイドです。
[Watson IoT Platformを使ってみる](http://qiita.com/egplnt/items/af867711a7191923b2ff)でIoTアプリを準備しました。
このサンプルのフローでは、TIセンサータグからの情報を閾値で判断してアクションへ繋げます。
1. TIセンサータグ、CC2650またはCC2541をスマホで接続して使用します。
2. 三方向の回転（gyro）の絶対値を取得し、閾値で判断します。
3. 同じイベントを複数回検知するのを防ぐため、前回の発生から10秒以内であれば無視します。
4. イベントが検知されたらメールで通知します。

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

- [TIセンサータグを使う](http://qiita.com/egplnt/items/98f1eb14812c1522555c)を参照して入手したTIセンサータグのデバイス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/test10`へアクセスすると下記のような画面が表示されます。

![スクリーンショット 2016-06-27 18.15.16.png](https://qiita-image-store.s3.amazonaws.com/0/125842/0dc2cdc0-4d63-e5e4-f4d6-3d8a84d14029.png)

* * *

####サンプル１
- 角速度(回転)がある値を超えたら警報を発信する、というシナリオです。

```lang:角速度で判定、シンプル版
[{"id":"a501e350.c21ab8","type":"function","z":"d73f03e8.a1d0e","name":"値を取得","func":"msg.payload.gyrox = global.get(\"gyrox\");\nmsg.payload.gyroy = global.get(\"gyroy\");\nmsg.payload.gyroz = global.get(\"gyroz\");\nmsg.payload.count = global.get(\"count\");\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":720,"wires":[["7edd0067.75a3b8"]]},{"id":"e603c581.265e8","type":"debug","z":"d73f03e8.a1d0e","name":"","active":true,"console":"false","complete":"false","x":750,"y":660,"wires":[]},{"id":"ebdc276c.a699b8","type":"function","z":"d73f03e8.a1d0e","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":880,"y":420,"wires":[["c5772268.70a238","1f402cd0.ccf7bb"]]},{"id":"c75384af.c3182","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test10","method":"get","swaggerDoc":"","x":190,"y":720,"wires":[["a501e350.c21ab8"]]},{"id":"b3094917.e4cf98","type":"http response","z":"d73f03e8.a1d0e","name":"","x":710,"y":720,"wires":[]},{"id":"46c17c90.dbf33c","type":"ibmiot in","z":"d73f03e8.a1d0e","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":116,"y":420,"wires":[["95c67239.ef6978","f810fe6d.6915b"]]},{"id":"fe4e8626.654e","type":"switch","z":"d73f03e8.a1d0e","name":"GyroXで判定","property":"payload.d.gyroXabs","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"}],"checkall":"true","outputs":1,"x":670,"y":380,"wires":[["ebdc276c.a699b8","f9b8c20f.5beb18"]]},{"id":"6fe88dac.88377c","type":"template","z":"d73f03e8.a1d0e","name":"警報発報","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"大きな回転（角速度）が観測されました！","x":520,"y":620,"wires":[["e603c581.265e8","c1b85e38.4a7d28"]]},{"id":"7edd0067.75a3b8","type":"template","z":"d73f03e8.a1d0e","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/test10\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test11\">\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>現在のX方向の角速度\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.gyrox }}</span>\n\t\t\n\t<h2>現在のY方向の角速度\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.gyroy }}</span>\n\t\t\n\t<h2>現在のZ方向の角速度\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.gyroz }}</span>\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":720,"wires":[["b3094917.e4cf98"]]},{"id":"38fba2f2.615e9e","type":"function","z":"d73f03e8.a1d0e","name":"GYRO値をセット","func":"var gyrox = global.get('gyrox')||0;\ngyrox = msg.payload.d.gyroX;\nmsg.payload.d.gyroXabs = Math.abs(msg.payload.d.gyroX);\nglobal.set('gyrox',gyrox);\n\nvar gyroy = global.get('gyroy')||0;\ngyroy = msg.payload.d.gyroY;\nmsg.payload.d.gyroYabs = Math.abs(msg.payload.d.gyroY);\nglobal.set('gyroy',gyroy);\n\nvar gyroz = global.get('gyro')||0;\ngyroz = msg.payload.d.gyroZ;\nmsg.payload.d.gyroZabs = Math.abs(msg.payload.d.gyroZ);\nglobal.set('gyroz',gyroz);\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":420,"wires":[["fe4e8626.654e","fa5568c2.3c5498","b88b30e5.fcf19"]]},{"id":"95c67239.ef6978","type":"function","z":"d73f03e8.a1d0e","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":280,"y":420,"wires":[["38fba2f2.615e9e"]]},{"id":"c5772268.70a238","type":"debug","z":"d73f03e8.a1d0e","name":"","active":true,"console":"false","complete":"payload.delta","x":910,"y":480,"wires":[]},{"id":"1f402cd0.ccf7bb","type":"switch","z":"d73f03e8.a1d0e","name":"前回からの間隔の閾値判定","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"str"}],"checkall":"true","outputs":1,"x":300,"y":600,"wires":[["6fe88dac.88377c","36f3279b.635e18"]]},{"id":"ef13a692.06ca18","type":"function","z":"d73f03e8.a1d0e","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"count\", zero);\nglobal.set(\"gyrox\", zero);\nglobal.set(\"gyroy\", zero);\nglobal.set(\"gyroz\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":780,"wires":[["7edd0067.75a3b8"]]},{"id":"7a2e269a.c841c8","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test11","method":"get","swaggerDoc":"","x":190,"y":780,"wires":[["ef13a692.06ca18"]]},{"id":"36f3279b.635e18","type":"function","z":"d73f03e8.a1d0e","name":"カウンターを＋１","func":"var count = global.get('count')||0;\ncount += 1;\nglobal.set('count',count);\n\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":580,"wires":[[]]},{"id":"e985a40f.66c91","type":"comment","z":"d73f03e8.a1d0e","name":"角速度で判定","info":"","x":150,"y":340,"wires":[]},{"id":"f810fe6d.6915b","type":"debug","z":"d73f03e8.a1d0e","name":"","active":false,"console":"false","complete":"false","x":270,"y":460,"wires":[]},{"id":"fa5568c2.3c5498","type":"switch","z":"d73f03e8.a1d0e","name":"GyroYで判定","property":"payload.d.gyroYabs","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"}],"checkall":"true","outputs":1,"x":670,"y":420,"wires":[["ebdc276c.a699b8","f9b8c20f.5beb18"]]},{"id":"b88b30e5.fcf19","type":"switch","z":"d73f03e8.a1d0e","name":"GyroZで判定","property":"payload.d.gyroZabs","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"}],"checkall":"true","outputs":1,"x":670,"y":460,"wires":[["ebdc276c.a699b8","f9b8c20f.5beb18"]]},{"id":"f9b8c20f.5beb18","type":"debug","z":"d73f03e8.a1d0e","name":"","active":false,"console":"false","complete":"false","x":910,"y":380,"wires":[]},{"id":"c1b85e38.4a7d28","type":"e-mail","z":"d73f03e8.a1d0e","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":730,"y":620,"wires":[]}]

```

![スクリーンショット 2016-07-07 18.47.00.png](https://qiita-image-store.s3.amazonaws.com/0/125842/3cf53c10-e4b3-ec81-ba15-8a8620d5b5bc.png)

* * *

####サンプル２
- 角速度（回転)がある値を超えたら警報を発信する、というシナリオです。
- 履歴を表示するようにしました。

```lang:角速度で判定、履歴表示版
[{"id":"3b29b375.68750c","type":"function","z":"d73f03e8.a1d0e","name":"値を取得","func":"var array = global.get('dateArray')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\n\nmsg.payload.gyrox = global.get(\"gyrox\");\nmsg.payload.gyroy = global.get(\"gyroy\");\nmsg.payload.gyroz = global.get(\"gyroz\");\nmsg.payload.count = global.get(\"count\");\nreturn msg;\n\n","outputs":1,"noerr":0,"x":340,"y":760,"wires":[["bbc4e1e5.73e2e8"]]},{"id":"8c8d8970.01e2e8","type":"debug","z":"d73f03e8.a1d0e","name":"","active":true,"console":"false","complete":"false","x":730,"y":680,"wires":[]},{"id":"8ea61fc8.97617","type":"function","z":"d73f03e8.a1d0e","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":860,"y":460,"wires":[["b87aef1e.e8b868","b93caf3e.f550a8"]]},{"id":"4b6aacef.0a52dc","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test12","method":"get","swaggerDoc":"","x":170,"y":760,"wires":[["3b29b375.68750c"]]},{"id":"df8bcf3c.67042","type":"http response","z":"d73f03e8.a1d0e","name":"","x":690,"y":760,"wires":[]},{"id":"1bb436bf.601fa9","type":"ibmiot in","z":"d73f03e8.a1d0e","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":96,"y":460,"wires":[["d4931f52.7d49b","fa4ba4e9.e51be8"]]},{"id":"dc196d9d.bfbd1","type":"switch","z":"d73f03e8.a1d0e","name":"GyroXで判定","property":"payload.d.gyroXabs","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"}],"checkall":"true","outputs":1,"x":650,"y":420,"wires":[["8ea61fc8.97617","76174119.dec1b8"]]},{"id":"2227da7d.dadfb6","type":"template","z":"d73f03e8.a1d0e","name":"警報発報","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"大きな回転（角速度）が観測されました！","x":500,"y":640,"wires":[["8c8d8970.01e2e8","149a3d48.5b2fb3"]]},{"id":"bbc4e1e5.73e2e8","type":"template","z":"d73f03e8.a1d0e","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/test12\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test13\">\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>現在のX方向の角速度\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.gyrox }}</span>\n\t\t\n\t<h2>現在のY方向の角速度\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.gyroy }}</span>\n\t\t\n\t<h2>現在のZ方向の角速度\t</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.gyroz }}</span>\n\t\t\n\t<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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":530,"y":760,"wires":[["df8bcf3c.67042"]]},{"id":"5c20e27e.821ba4","type":"function","z":"d73f03e8.a1d0e","name":"GYRO値をセット","func":"var gyrox = global.get('gyrox')||0;\ngyrox = msg.payload.d.gyroX;\nmsg.payload.d.gyroXabs = Math.abs(msg.payload.d.gyroX);\nglobal.set('gyrox',gyrox);\n\nvar gyroy = global.get('gyroy')||0;\ngyroy = msg.payload.d.gyroY;\nmsg.payload.d.gyroYabs = Math.abs(msg.payload.d.gyroY);\nglobal.set('gyroy',gyroy);\n\nvar gyroz = global.get('gyro')||0;\ngyroz = msg.payload.d.gyroZ;\nmsg.payload.d.gyroZabs = Math.abs(msg.payload.d.gyroZ);\nglobal.set('gyroz',gyroz);\n\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":460,"wires":[["dc196d9d.bfbd1","deee4d93.44c33","6b69d010.1dbf88"]]},{"id":"d4931f52.7d49b","type":"function","z":"d73f03e8.a1d0e","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":260,"y":460,"wires":[["5c20e27e.821ba4"]]},{"id":"b87aef1e.e8b868","type":"debug","z":"d73f03e8.a1d0e","name":"","active":true,"console":"false","complete":"payload.delta","x":890,"y":520,"wires":[]},{"id":"b93caf3e.f550a8","type":"switch","z":"d73f03e8.a1d0e","name":"前回からの間隔の閾値判定","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"str"}],"checkall":"true","outputs":1,"x":280,"y":640,"wires":[["2227da7d.dadfb6","9fd0e9a1.8885c8"]]},{"id":"eebd1175.cacf9","type":"function","z":"d73f03e8.a1d0e","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"count\", zero);\nglobal.set(\"gyrox\", zero);\nglobal.set(\"gyroy\", zero);\nglobal.set(\"gyroz\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":820,"wires":[["bbc4e1e5.73e2e8"]]},{"id":"d2865b00.741df8","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test13","method":"get","swaggerDoc":"","x":170,"y":820,"wires":[["eebd1175.cacf9"]]},{"id":"9fd0e9a1.8885c8","type":"function","z":"d73f03e8.a1d0e","name":"カウンターを＋１し履歴を保存","func":"var count = global.get('count')||0;\ncount += 1;\nglobal.set('count',count);\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\nvar array = global.get('dateArray')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('dateArray', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":570,"y":600,"wires":[[]]},{"id":"4ca2479c.2846c","type":"comment","z":"d73f03e8.a1d0e","name":"角速度で判定、履歴表示版","info":"","x":170,"y":380,"wires":[]},{"id":"fa4ba4e9.e51be8","type":"debug","z":"d73f03e8.a1d0e","name":"","active":true,"console":"false","complete":"false","x":250,"y":500,"wires":[]},{"id":"deee4d93.44c33","type":"switch","z":"d73f03e8.a1d0e","name":"GyroYで判定","property":"payload.d.gyroYabs","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"}],"checkall":"true","outputs":1,"x":650,"y":460,"wires":[["8ea61fc8.97617","76174119.dec1b8"]]},{"id":"6b69d010.1dbf88","type":"switch","z":"d73f03e8.a1d0e","name":"GyroZで判定","property":"payload.d.gyroZabs","propertyType":"msg","rules":[{"t":"gt","v":"20","vt":"str"}],"checkall":"true","outputs":1,"x":650,"y":500,"wires":[["8ea61fc8.97617","76174119.dec1b8"]]},{"id":"76174119.dec1b8","type":"debug","z":"d73f03e8.a1d0e","name":"","active":false,"console":"false","complete":"false","x":890,"y":420,"wires":[]},{"id":"149a3d48.5b2fb3","type":"e-mail","z":"d73f03e8.a1d0e","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":710,"y":640,"wires":[]}]

```

![スクリーンショット 2016-07-07 18.42.57.png](https://qiita-image-store.s3.amazonaws.com/0/125842/d2ae403c-d4f4-0767-fab8-c64225083a25.png)

* * *

####サンプル３

- 明るさがある値を超えたら警報を発信する、というシナリオです。キャビネットの扉にセンサーをつけたら、というような感じです。

```lang:明度で判定、履歴表示版
[{"id":"4ecca7b6.66a728","type":"function","z":"d73f03e8.a1d0e","name":"値を取得","func":"var array = global.get('light_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\nmsg.payload.count = global.get(\"light_count\");\nmsg.payload.light = global.get(\"light\");\n\nreturn msg;\n","outputs":1,"noerr":0,"x":340,"y":620,"wires":[["7fd12b33.e12ce4"]]},{"id":"eea76909.cda1c8","type":"debug","z":"d73f03e8.a1d0e","name":"警報文を表示","active":false,"console":"false","complete":"payload","x":739,"y":540,"wires":[]},{"id":"c3db9ccb.bdc038","type":"function","z":"d73f03e8.a1d0e","name":"前回からの間隔を確認","func":"var prev_date = global.get('light_prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('light_prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":899,"y":320,"wires":[["f11d9028.5a7cb","a352a0f8.0c61b8"]]},{"id":"4a796178.e91198","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test14","method":"get","swaggerDoc":"","x":169,"y":620,"wires":[["4ecca7b6.66a728"]]},{"id":"b13a93c4.f8bd9","type":"http response","z":"d73f03e8.a1d0e","name":"","x":689,"y":620,"wires":[]},{"id":"2b53e238.6d0786","type":"ibmiot in","z":"d73f03e8.a1d0e","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":95,"y":320,"wires":[["457de3eb.249d74","23cb2f93.a61798"]]},{"id":"7fed8791.4a4bc8","type":"switch","z":"d73f03e8.a1d0e","name":"明るさ100で判定","property":"payload.d.light","propertyType":"msg","rules":[{"t":"gt","v":"100","vt":"str"}],"checkall":"true","outputs":1,"x":669,"y":320,"wires":[["c3db9ccb.bdc038","c47ac4d3.95b718"]]},{"id":"37846bd2.ed434c","type":"template","z":"d73f03e8.a1d0e","name":"警報発報","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"大きな明るさが観測されました！","x":519,"y":500,"wires":[["eea76909.cda1c8","eccfaa1d.96d898"]]},{"id":"7fd12b33.e12ce4","type":"template","z":"d73f03e8.a1d0e","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/test14\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test15\">\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.light }}</span> lx\n\t\t\n\t<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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":539,"y":620,"wires":[["b13a93c4.f8bd9"]]},{"id":"5f625826.80b0d","type":"function","z":"d73f03e8.a1d0e","name":"light値をセット","func":"var light = global.get('light')||0;\nlight = msg.payload.d.light;\nglobal.set('light',light);\n\nreturn msg;","outputs":1,"noerr":0,"x":459,"y":320,"wires":[["7fed8791.4a4bc8"]]},{"id":"457de3eb.249d74","type":"function","z":"d73f03e8.a1d0e","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":259,"y":320,"wires":[["5f625826.80b0d"]]},{"id":"f11d9028.5a7cb","type":"debug","z":"d73f03e8.a1d0e","name":"前回からの間隔","active":false,"console":"false","complete":"payload.delta","x":879,"y":420,"wires":[]},{"id":"a352a0f8.0c61b8","type":"switch","z":"d73f03e8.a1d0e","name":"前回から10秒以上だったら検知","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"str"}],"checkall":"true","outputs":1,"x":289,"y":500,"wires":[["37846bd2.ed434c","bf3a9120.725b68"]]},{"id":"c4335faf.fdcd5","type":"function","z":"d73f03e8.a1d0e","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"light_count\", zero);\nglobal.set(\"light\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":359,"y":680,"wires":[["7fd12b33.e12ce4"]]},{"id":"9af98257.faff08","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test15","method":"get","swaggerDoc":"","x":169,"y":680,"wires":[["c4335faf.fdcd5"]]},{"id":"bf3a9120.725b68","type":"function","z":"d73f03e8.a1d0e","name":"カウンターを＋１し履歴を保存","func":"var count = global.get('light_count')||0;\ncount += 1;\nglobal.set('light_count',count);\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\nvar array = global.get('light_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('light_array', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":589,"y":460,"wires":[[]]},{"id":"77627cab.07a76c","type":"comment","z":"d73f03e8.a1d0e","name":"明度で判定","info":"","x":99,"y":260,"wires":[]},{"id":"23cb2f93.a61798","type":"debug","z":"d73f03e8.a1d0e","name":"全センサー値を表示","active":false,"console":"false","complete":"payload","x":279,"y":360,"wires":[]},{"id":"c47ac4d3.95b718","type":"debug","z":"d73f03e8.a1d0e","name":"検知された明度","active":false,"console":"false","complete":"payload.d.light","x":879,"y":280,"wires":[]},{"id":"eccfaa1d.96d898","type":"e-mail","z":"d73f03e8.a1d0e","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":709,"y":500,"wires":[]}]

```

![スクリーンショット 2016-07-06 10.21.37.png](https://qiita-image-store.s3.amazonaws.com/0/125842/4ab52398-a354-8469-0c44-7844a0c24abf.png)

* * *

####サンプル４

- 明るさがある値を超えたら警報を発信する、扉を開ける（＝明度が閾値を超える）たびに、キャビネットに保管してある在庫品が減っていく、といったシナリオです。

```lang:明度で判定、扉の開閉で在庫数が減少、といったシナリオ
[{"id":"41822622.caced","type":"function","z":"d73f03e8.a1d0e","name":"値を取得","func":"var array = global.get('light_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\nmsg.payload.store = global.get(\"store\");\nmsg.payload.light = global.get(\"light\");\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":340,"y":840,"wires":[["3f5e10cf.bfd938"]]},{"id":"bc495d27.a11cc8","type":"debug","z":"d73f03e8.a1d0e","name":"警報文を表示","active":true,"console":"false","complete":"payload","x":800,"y":700,"wires":[]},{"id":"6aa041ad.e94c5","type":"function","z":"d73f03e8.a1d0e","name":"前回からの間隔を確認","func":"var prev_date = global.get('light_prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('light_prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":940,"y":460,"wires":[["a129df62.2a03d8","b48daece.ad2ac8"]]},{"id":"11400d8c.490e22","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test17","method":"get","swaggerDoc":"","x":169,"y":840,"wires":[["41822622.caced"]]},{"id":"7f1b93cc.936ea4","type":"http response","z":"d73f03e8.a1d0e","name":"","x":750,"y":840,"wires":[]},{"id":"a6e7114c.228c28","type":"ibmiot in","z":"d73f03e8.a1d0e","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":136,"y":460,"wires":[["3e861edb.090e42","76672e66.3fe18"]]},{"id":"e43506.8048c2f8","type":"switch","z":"d73f03e8.a1d0e","name":"明るさ100で判定","property":"payload.d.light","propertyType":"msg","rules":[{"t":"gt","v":"100","vt":"num"}],"checkall":"true","outputs":1,"x":710,"y":460,"wires":[["6aa041ad.e94c5","c0491fdc.040a2"]]},{"id":"bff1d9b5.4d4ae8","type":"template","z":"d73f03e8.a1d0e","name":"警報発報","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"在庫数が少なくなりました！","x":620,"y":700,"wires":[["bc495d27.a11cc8"]]},{"id":"3f5e10cf.bfd938","type":"template","z":"d73f03e8.a1d0e","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/test16\">\n\t\t<input type=\"text\" name=\"storeValue\" size=\"30\" maxlength=\"20\">\n\t\t<p><input type=\"submit\" value=\"在庫数をセットする\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test17\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test18\">\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.store }}</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.light }}</span> lx\n\t\t\n\t<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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","x":570,"y":840,"wires":[["7f1b93cc.936ea4"]]},{"id":"16e63130.e5234f","type":"function","z":"d73f03e8.a1d0e","name":"light値をセット","func":"var light = global.get('light')||0;\nlight = msg.payload.d.light;\nglobal.set('light',light);\n\nreturn msg;","outputs":1,"noerr":0,"x":500,"y":460,"wires":[["e43506.8048c2f8"]]},{"id":"3e861edb.090e42","type":"function","z":"d73f03e8.a1d0e","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":300,"y":460,"wires":[["16e63130.e5234f","92ead37f.8fe98"]]},{"id":"a129df62.2a03d8","type":"debug","z":"d73f03e8.a1d0e","name":"前回からの間隔","active":false,"console":"false","complete":"payload.delta","x":920,"y":520,"wires":[]},{"id":"b48daece.ad2ac8","type":"switch","z":"d73f03e8.a1d0e","name":"前回から10秒以上だったら検知","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"str"}],"checkall":"true","outputs":1,"x":350,"y":580,"wires":[["f742f5.f8cfa508"]]},{"id":"3d3ce2e.362b69e","type":"function","z":"d73f03e8.a1d0e","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"store\", zero);\nglobal.set(\"light\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":359,"y":900,"wires":[["3f5e10cf.bfd938"]]},{"id":"f16a521a.946e38","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test18","method":"get","swaggerDoc":"","x":169,"y":900,"wires":[["3d3ce2e.362b69e"]]},{"id":"f742f5.f8cfa508","type":"function","z":"d73f03e8.a1d0e","name":"在庫数をー１し履歴を保存","func":"var store = global.get('store')||0;\nstore -= 1;\nglobal.set('store',store);\nmsg.payload.store = store;\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\nvar array = global.get('light_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('light_array', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":640,"y":580,"wires":[["4afd6ea6.aa6c8","45de82e9.a654a4"]]},{"id":"fb86098f.e2cf6","type":"comment","z":"d73f03e8.a1d0e","name":"明度で判定、扉の開閉で在庫数が減少","info":"","x":230,"y":380,"wires":[]},{"id":"76672e66.3fe18","type":"debug","z":"d73f03e8.a1d0e","name":"全センサー値を表示","active":false,"console":"false","complete":"payload","x":320,"y":420,"wires":[]},{"id":"c0491fdc.040a2","type":"debug","z":"d73f03e8.a1d0e","name":"検知された明度","active":true,"console":"false","complete":"payload.d.light","x":920,"y":420,"wires":[]},{"id":"9b0f1fc0.9e2f7","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test16","method":"get","swaggerDoc":"","x":170,"y":780,"wires":[["c6b56f7e.1518e"]]},{"id":"c6b56f7e.1518e","type":"function","z":"d73f03e8.a1d0e","name":"在庫数をセット","func":"var store = global.get('store')||0;\nstore = msg.payload.storeValue;\nglobal.set('store',store);\nmsg.payload.store = store;\n\nmsg.payload.light = global.get(\"light\");\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":780,"wires":[["3f5e10cf.bfd938"]]},{"id":"4afd6ea6.aa6c8","type":"switch","z":"d73f03e8.a1d0e","name":"在庫数が少なくなったら警報を発信","property":"payload.store","propertyType":"msg","rules":[{"t":"lt","v":"2","vt":"str"}],"checkall":"true","outputs":1,"x":370,"y":700,"wires":[["bff1d9b5.4d4ae8"]]},{"id":"45de82e9.a654a4","type":"template","z":"d73f03e8.a1d0e","name":"開扉通知を発信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"扉が開きました。","x":920,"y":580,"wires":[["4dd96d0b.2f013c"]]},{"id":"4dd96d0b.2f013c","type":"debug","z":"d73f03e8.a1d0e","name":"開扉通知を表示","active":true,"console":"false","complete":"payload","x":920,"y":640,"wires":[]},{"id":"92ead37f.8fe98","type":"debug","z":"d73f03e8.a1d0e","name":"","active":true,"console":"false","complete":"payload.d.light","x":550,"y":420,"wires":[]}]
```

![スクリーンショット 2016-07-07 16.22.14.png](https://qiita-image-store.s3.amazonaws.com/0/125842/2e84077a-f3f2-84eb-77d1-e8bb7297d8db.png)

* * *

####サンプル５

- （キャビネット内の）温度がある値を超えたら警報を発信する、扉を開けるたびに、キャビネットに保管してある在庫品が減っていく、といったシナリオです。

```lang:温度で判定、扉の開閉で在庫数が減少、といったシナリオ
[{"id":"eef26c77.550bb8","type":"function","z":"d73f03e8.a1d0e","name":"値を取得","func":"//var array = global.get('temp_array');\nvar array = global.get('temp_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\n//msg.payload.count = global.get(\"light_count\");\nmsg.payload.store = global.get(\"store\");\nmsg.payload.temp = global.get(\"temp\");\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":340,"y":800,"wires":[["a3761a1c.86b5"]]},{"id":"826dd28b.c72e6","type":"debug","z":"d73f03e8.a1d0e","name":"警報文を表示","active":true,"console":"false","complete":"payload","x":800,"y":660,"wires":[]},{"id":"8f488057.4a52e","type":"function","z":"d73f03e8.a1d0e","name":"前回からの間隔を確認","func":"var prev_date = global.get('temp_prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('temp_prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":940,"y":420,"wires":[["d1766385.6c22f8","39cdf374.3e7654"]]},{"id":"c4057414.47e048","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test20","method":"get","swaggerDoc":"","x":169,"y":800,"wires":[["eef26c77.550bb8"]]},{"id":"80c2a3ee.cb3168","type":"http response","z":"d73f03e8.a1d0e","name":"","x":750,"y":800,"wires":[]},{"id":"b2e38b6f.41a628","type":"ibmiot in","z":"d73f03e8.a1d0e","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":136,"y":420,"wires":[["928a0dc7.e2abd8","c3f75e22.aa353"]]},{"id":"29e633ec.18905c","type":"switch","z":"d73f03e8.a1d0e","name":"温度40で判定","property":"payload.d.ambientTemp","propertyType":"msg","rules":[{"t":"gt","v":"40","vt":"num"}],"checkall":"true","outputs":1,"x":700,"y":420,"wires":[["8f488057.4a52e","9b158fda.27cd9"]]},{"id":"c1489ec5.7d92a8","type":"template","z":"d73f03e8.a1d0e","name":"警報発報","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"在庫数が少なくなりました！","x":620,"y":660,"wires":[["826dd28b.c72e6","3e930b28.338ee4"]]},{"id":"a3761a1c.86b5","type":"template","z":"d73f03e8.a1d0e","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/test19\">\n\t\t<input type=\"text\" name=\"storeValue\" size=\"30\" maxlength=\"20\">\n\t\t<p><input type=\"submit\" value=\"在庫数をセットする\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test20\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test21\">\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.store }}</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<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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\t","x":570,"y":800,"wires":[["80c2a3ee.cb3168"]]},{"id":"4050a8fa.caecc8","type":"function","z":"d73f03e8.a1d0e","name":"温度をセット","func":"var temp = global.get('temp')||0;\ntemp = msg.payload.d.ambientTemp;\nglobal.set('temp',temp);\n\nreturn msg;","outputs":1,"noerr":0,"x":500,"y":420,"wires":[["29e633ec.18905c"]]},{"id":"928a0dc7.e2abd8","type":"function","z":"d73f03e8.a1d0e","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":300,"y":420,"wires":[["4050a8fa.caecc8","54f31882.34e6d"]]},{"id":"d1766385.6c22f8","type":"debug","z":"d73f03e8.a1d0e","name":"前回からの間隔","active":false,"console":"false","complete":"payload.delta","x":920,"y":480,"wires":[]},{"id":"39cdf374.3e7654","type":"switch","z":"d73f03e8.a1d0e","name":"前回から10秒以上だったら検知","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"str"}],"checkall":"true","outputs":1,"x":350,"y":540,"wires":[["8f12eed6.60cc7"]]},{"id":"73f87101.825c8","type":"function","z":"d73f03e8.a1d0e","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"store\", zero);\nglobal.set(\"temp\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":359,"y":860,"wires":[["a3761a1c.86b5"]]},{"id":"841f7254.1771a","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test21","method":"get","swaggerDoc":"","x":169,"y":860,"wires":[["73f87101.825c8"]]},{"id":"8f12eed6.60cc7","type":"function","z":"d73f03e8.a1d0e","name":"在庫数をー１し履歴を保存","func":"var store = global.get('store')||0;\nstore -= 1;\nglobal.set('store',store);\nmsg.payload.store = store;\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\n//var array = global.get('light_array')||new Array(5);\nvar array = global.get('temp_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('temp_array', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":640,"y":540,"wires":[["3bf116b0.757c9a","ad7db3c9.217db"]]},{"id":"bea3accd.9ce8f8","type":"comment","z":"d73f03e8.a1d0e","name":"温度で判定、扉の開閉で在庫数が減少","info":"","x":210,"y":340,"wires":[]},{"id":"c3f75e22.aa353","type":"debug","z":"d73f03e8.a1d0e","name":"全センサー値を表示","active":false,"console":"false","complete":"payload","x":320,"y":380,"wires":[]},{"id":"9b158fda.27cd9","type":"debug","z":"d73f03e8.a1d0e","name":"検知された温度","active":true,"console":"false","complete":"payload.d.ambientTemp","x":920,"y":380,"wires":[]},{"id":"ccb2782f.d19898","type":"http in","z":"d73f03e8.a1d0e","name":"","url":"/test19","method":"get","swaggerDoc":"","x":170,"y":740,"wires":[["211e6ce6.2e1bcc"]]},{"id":"211e6ce6.2e1bcc","type":"function","z":"d73f03e8.a1d0e","name":"在庫数をセット","func":"var store = global.get('store')||0;\nstore = msg.payload.storeValue;\nglobal.set('store',store);\nmsg.payload.store = store;\n\nmsg.payload.temp = global.get(\"temp\");\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":740,"wires":[["a3761a1c.86b5"]]},{"id":"3bf116b0.757c9a","type":"switch","z":"d73f03e8.a1d0e","name":"在庫数が少なくなったら警報を発信","property":"payload.store","propertyType":"msg","rules":[{"t":"lt","v":"2","vt":"str"}],"checkall":"true","outputs":1,"x":370,"y":660,"wires":[["c1489ec5.7d92a8"]]},{"id":"ad7db3c9.217db","type":"template","z":"d73f03e8.a1d0e","name":"温度超過を発信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"温度が閾値を超えました。","x":920,"y":540,"wires":[["192ecffa.b316e"]]},{"id":"192ecffa.b316e","type":"debug","z":"d73f03e8.a1d0e","name":"温度超過通知を表示","active":true,"console":"false","complete":"payload","x":940,"y":600,"wires":[]},{"id":"54f31882.34e6d","type":"debug","z":"d73f03e8.a1d0e","name":"","active":true,"console":"false","complete":"payload.d.ambientTemp","x":630,"y":380,"wires":[]},{"id":"3e930b28.338ee4","type":"e-mail","z":"d73f03e8.a1d0e","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":770,"y":700,"wires":[]}]

```

![スクリーンショット 2016-07-07 18.16.11.png](https://qiita-image-store.s3.amazonaws.com/0/125842/2394dfb8-84bb-1efe-ceb1-730b2eccbfda.png)

* * *

####サンプル６

- （たとえばコーヒーメーカーの）温度がある値を超えたらその回数をカウントする、といったシナリオです。

```lang:温度で判定、回数をカウント
[{"id":"d9fd5d79.ea864","type":"function","z":"c35f7f93.1e69b8","name":"値を取得","func":"var array = global.get('temp_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\nmsg.payload.hot = global.get(\"hot\");\nmsg.payload.temp = global.get(\"temp\");\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":320,"y":420,"wires":[["26327c3a.0561b4"]]},{"id":"c4ad7e69.ef3b98","type":"function","z":"c35f7f93.1e69b8","name":"前回からの間隔を確認","func":"var prev_date = global.get('temp_prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('temp_prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":900,"y":180,"wires":[["a419a3d9.3620a","b724e322.8fc9d"]]},{"id":"a79623e5.3d172","type":"http in","z":"c35f7f93.1e69b8","name":"","url":"/test22","method":"get","swaggerDoc":"","x":149,"y":420,"wires":[["d9fd5d79.ea864"]]},{"id":"398cf0fa.062dd8","type":"http response","z":"c35f7f93.1e69b8","name":"","x":710,"y":440,"wires":[]},{"id":"890615c1.5ea56","type":"ibmiot in","z":"c35f7f93.1e69b8","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":96,"y":180,"wires":[["b5434c6f.292e98","7699f844.57b708"]]},{"id":"cfd3f4c3.3c6c28","type":"switch","z":"c35f7f93.1e69b8","name":"温度40で判定","property":"payload.d.ambientTemp","propertyType":"msg","rules":[{"t":"gt","v":"30","vt":"num"}],"checkall":"true","outputs":1,"x":660,"y":180,"wires":[["c4ad7e69.ef3b98","6cc8dff.7df22a"]]},{"id":"26327c3a.0561b4","type":"template","z":"c35f7f93.1e69b8","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温度が閾値を超えたらカウントします<br />\n\t</p>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test22\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test23\">\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>回数</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.hot }}</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<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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\t","x":550,"y":440,"wires":[["398cf0fa.062dd8"]]},{"id":"f88b57f0.8cc33","type":"function","z":"c35f7f93.1e69b8","name":"温度をセット","func":"var temp = global.get('temp')||0;\ntemp = msg.payload.d.ambientTemp;\nmsg.payload.temp = temp;\nglobal.set('temp',temp);\n\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":180,"wires":[["cfd3f4c3.3c6c28"]]},{"id":"b5434c6f.292e98","type":"function","z":"c35f7f93.1e69b8","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":260,"y":180,"wires":[["f88b57f0.8cc33","690ef8e3.b3815"]]},{"id":"a419a3d9.3620a","type":"debug","z":"c35f7f93.1e69b8","name":"前回からの間隔","active":false,"console":"false","complete":"payload.delta","x":880,"y":240,"wires":[]},{"id":"b724e322.8fc9d","type":"switch","z":"c35f7f93.1e69b8","name":"前回から10秒以上だったら検知","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"10","vt":"str"}],"checkall":"true","outputs":1,"x":310,"y":300,"wires":[["6901e933.cf3e58"]]},{"id":"2395c4dc.f452f4","type":"function","z":"c35f7f93.1e69b8","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"hot\", zero);\nglobal.set(\"temp\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":339,"y":480,"wires":[["26327c3a.0561b4"]]},{"id":"fea673f6.bec7b8","type":"http in","z":"c35f7f93.1e69b8","name":"","url":"/test23","method":"get","swaggerDoc":"","x":149,"y":480,"wires":[["2395c4dc.f452f4"]]},{"id":"6901e933.cf3e58","type":"function","z":"c35f7f93.1e69b8","name":"回数を＋１し履歴を保存","func":"var hot = global.get('hot')||0;\nhot += 1;\nglobal.set('hot',hot);\nmsg.payload.hot = hot;\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\nvar array = global.get('temp_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('temp_array', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":590,"y":300,"wires":[["71378cd6.147f4c"]]},{"id":"67a17e74.b44ac","type":"comment","z":"c35f7f93.1e69b8","name":"温度で判定、回数をカウント","info":"","x":200,"y":100,"wires":[]},{"id":"7699f844.57b708","type":"debug","z":"c35f7f93.1e69b8","name":"全センサー値を表示","active":false,"console":"false","complete":"payload","x":280,"y":140,"wires":[]},{"id":"6cc8dff.7df22a","type":"debug","z":"c35f7f93.1e69b8","name":"検知された温度","active":true,"console":"false","complete":"payload.d.ambientTemp","x":880,"y":140,"wires":[]},{"id":"71378cd6.147f4c","type":"template","z":"c35f7f93.1e69b8","name":"温度超過を発信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"温度が閾値を超えました。","x":880,"y":300,"wires":[["6bcb968f.5de08","45ec6bae.d4f5bc"]]},{"id":"6bcb968f.5de08","type":"debug","z":"c35f7f93.1e69b8","name":"温度超過通知を表示","active":true,"console":"false","complete":"payload","x":900,"y":400,"wires":[]},{"id":"690ef8e3.b3815","type":"debug","z":"c35f7f93.1e69b8","name":"","active":true,"console":"false","complete":"payload.d.ambientTemp","x":570,"y":140,"wires":[]},{"id":"45ec6bae.d4f5bc","type":"e-mail","z":"c35f7f93.1e69b8","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":850,"y":360,"wires":[]}]

```

![スクリーンショット 2016-07-08 13.15.27.png](https://qiita-image-store.s3.amazonaws.com/0/125842/08bfc353-108d-5a8d-c3ef-8ee42e1e7d50.png)

* * *

####サンプル７

- （たとえばコーヒーメーカーのまわりの）湿度がある値を超えたらその回数をカウントする、といったシナリオです。

```lang:湿度で判定、回数をカウント
[{"id":"86a6e909.8ed0d8","type":"function","z":"748bdb8d.64b434","name":"値を取得","func":"var array = global.get('humidity_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\nmsg.payload.humid = global.get(\"humid\");\nmsg.payload.humidity = global.get(\"humidity\");\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":320,"y":400,"wires":[["981dff4d.85549"]]},{"id":"bc8c3571.56199","type":"function","z":"748bdb8d.64b434","name":"前回からの間隔を確認","func":"var prev_date = global.get('humidity_prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('humidity_prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":897,"y":160,"wires":[["fe4914dd.fdf4b","65087f1a.25728"]]},{"id":"ba6f9aa9.354868","type":"http in","z":"748bdb8d.64b434","name":"","url":"/test24","method":"get","swaggerDoc":"","x":146,"y":400,"wires":[["86a6e909.8ed0d8"]]},{"id":"b3fabd28.55f8b8","type":"http response","z":"748bdb8d.64b434","name":"","x":707,"y":420,"wires":[]},{"id":"ead3c7f.536b9b8","type":"ibmiot in","z":"748bdb8d.64b434","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":93,"y":160,"wires":[["ed12b35b.3b56","1d055b4.3d57825"]]},{"id":"e8a52975.26a55","type":"switch","z":"748bdb8d.64b434","name":"湿度80%で判定","property":"payload.d.humidity","propertyType":"msg","rules":[{"t":"gt","v":"95","vt":"num"}],"checkall":"true","outputs":1,"x":657,"y":160,"wires":[["bc8c3571.56199","8fd158a6.c3fce"]]},{"id":"981dff4d.85549","type":"template","z":"748bdb8d.64b434","name":"htmlを返す","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n<!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湿度が閾値を超えたらカウントします<br />\n\t</p>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test24\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test25\">\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>回数</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.humid }}</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.humidity }}</span> ％\n\t\t\n\t<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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\t","x":548,"y":420,"wires":[["b3fabd28.55f8b8"]]},{"id":"e8b1641c.6c9008","type":"function","z":"748bdb8d.64b434","name":"湿度をセット","func":"var humidity = global.get('humidity')||0;\nhumidity = msg.payload.d.humidity;\nmsg.payload.humidity = humidity;\nglobal.set('humidity',humidity);\n\nreturn msg;","outputs":1,"noerr":0,"x":457,"y":160,"wires":[["e8a52975.26a55"]]},{"id":"ed12b35b.3b56","type":"function","z":"748bdb8d.64b434","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":257,"y":160,"wires":[["e8b1641c.6c9008","a24dceff.e3c888"]]},{"id":"fe4914dd.fdf4b","type":"debug","z":"748bdb8d.64b434","name":"前回からの間隔","active":false,"console":"false","complete":"payload.delta","x":877,"y":220,"wires":[]},{"id":"65087f1a.25728","type":"switch","z":"748bdb8d.64b434","name":"前回から10秒以上だったら検知","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"2","vt":"str"}],"checkall":"true","outputs":1,"x":307,"y":280,"wires":[["a289f88c.306fb8"]]},{"id":"17550f45.b7db31","type":"function","z":"748bdb8d.64b434","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"humid\", zero);\nglobal.set(\"humidity\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":336,"y":460,"wires":[["981dff4d.85549"]]},{"id":"21a22acc.609ffe","type":"http in","z":"748bdb8d.64b434","name":"","url":"/test25","method":"get","swaggerDoc":"","x":146,"y":460,"wires":[["17550f45.b7db31"]]},{"id":"a289f88c.306fb8","type":"function","z":"748bdb8d.64b434","name":"回数を＋１し履歴を保存","func":"var humid = global.get('humid')||0;\nhumid += 1;\nglobal.set('humid',humid);\nmsg.payload.humid = humid;\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\nvar array = global.get('humidity_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('humidity_array', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":587,"y":280,"wires":[["b7217eba.710ee"]]},{"id":"67513611.b0954","type":"comment","z":"748bdb8d.64b434","name":"湿度で判定、回数をカウント","info":"","x":197,"y":80,"wires":[]},{"id":"1d055b4.3d57825","type":"debug","z":"748bdb8d.64b434","name":"全センサー値を表示","active":false,"console":"false","complete":"payload","x":277,"y":120,"wires":[]},{"id":"8fd158a6.c3fce","type":"debug","z":"748bdb8d.64b434","name":"検知された湿度","active":true,"console":"false","complete":"payload.d.humidity","x":877,"y":120,"wires":[]},{"id":"b7217eba.710ee","type":"template","z":"748bdb8d.64b434","name":"湿度超過を発信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"湿度が閾値を超えました。","x":877,"y":280,"wires":[["f6b3dbf1.6d4fe8","b40b2409.afce48"]]},{"id":"f6b3dbf1.6d4fe8","type":"debug","z":"748bdb8d.64b434","name":"湿度超過通知を表示","active":true,"console":"false","complete":"payload","x":897,"y":380,"wires":[]},{"id":"a24dceff.e3c888","type":"debug","z":"748bdb8d.64b434","name":"","active":true,"console":"false","complete":"payload.d.humidity","x":558,"y":120,"wires":[]},{"id":"b40b2409.afce48","type":"e-mail","z":"748bdb8d.64b434","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":847,"y":340,"wires":[]}]

```
![スクリーンショット 2016-07-13 13.48.18.png](https://qiita-image-store.s3.amazonaws.com/0/125842/d652d887-0e42-145f-a636-c2bccc2ca604.png)


* * *

####サンプル８

- （たとえばコーヒーメーカーの）温度が５秒前と比較して３度上昇し、その事象が過去３０秒間発生していなければ「コーヒー抽出中」と判定してその回数をカウントする、といったシナリオです。

```lang:５秒前との比較での温度で判定、回数をカウント
[{"id":"cf5fa34b.af5a18","type":"template","z":"fc9a865.e66cdf8","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温度が閾値を超えたらカウントします<br />\n\t</p>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test26\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test27\">\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>回数</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.hot }}</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<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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\t","x":550,"y":400,"wires":[["c02c591b.0553f"]]},{"id":"839bc470.5dbec8","type":"function","z":"fc9a865.e66cdf8","name":"値を取得","func":"var array = global.get('temp_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\nmsg.payload.hot = global.get(\"hot\");\nmsg.payload.temp = global.get(\"temp\");\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":320,"y":380,"wires":[["cf5fa34b.af5a18"]]},{"id":"a72bca5a.e3e128","type":"function","z":"fc9a865.e66cdf8","name":"前回からの間隔を確認","func":"var prev_date = global.get('temp_prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('temp_prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":920,"y":140,"wires":[["5cdce92c.be2ff","f8f0aa14.51c418"]]},{"id":"5bea4fb3.54d4f8","type":"http in","z":"fc9a865.e66cdf8","name":"","url":"/test26","method":"get","swaggerDoc":"","x":149,"y":380,"wires":[["839bc470.5dbec8"]]},{"id":"c02c591b.0553f","type":"http response","z":"fc9a865.e66cdf8","name":"","x":710,"y":400,"wires":[]},{"id":"cf37935.74991f","type":"ibmiot in","z":"fc9a865.e66cdf8","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":96,"y":140,"wires":[["8e8ed42b.ef9d5","62bd4507.9f48e4"]]},{"id":"c695985b.04f5d","type":"switch","z":"fc9a865.e66cdf8","name":"5秒前から３度上昇で検知","property":"payload.tempDelta","propertyType":"msg","rules":[{"t":"gt","v":"3","vt":"num"}],"checkall":"true","outputs":1,"x":690,"y":140,"wires":[["a72bca5a.e3e128","82f4b730.f2f7c8"]]},{"id":"b8e59ef1.24cb1","type":"function","z":"fc9a865.e66cdf8","name":"温度をセット","func":"var temp = global.get('temp')||0;\ntemp = msg.payload.d.ambientTemp;\nmsg.payload.temp = temp;\nglobal.set('temp',temp);\n\nvar array = global.get('hist_array')||[[0],[0],[0],[0],[0]];\nvar shifted = array.shift();\narray.push(temp);\nglobal.set('hist_array', array);\n\nmsg.payload.tempDelta = temp - array[0];\nmsg.histarray = array;\n\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":140,"wires":[["c695985b.04f5d","8cb3938e.dcd3a8"]]},{"id":"8e8ed42b.ef9d5","type":"function","z":"fc9a865.e66cdf8","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":260,"y":140,"wires":[["b8e59ef1.24cb1","5c9d858.a13217c"]]},{"id":"5cdce92c.be2ff","type":"debug","z":"fc9a865.e66cdf8","name":"前回からの間隔","active":true,"console":"false","complete":"payload.delta","x":900,"y":200,"wires":[]},{"id":"f8f0aa14.51c418","type":"switch","z":"fc9a865.e66cdf8","name":"前回から30秒以上だったら検知","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"30","vt":"str"}],"checkall":"true","outputs":1,"x":310,"y":260,"wires":[["8ea64906.7bbd8"]]},{"id":"2cd28e6d.7e6552","type":"function","z":"fc9a865.e66cdf8","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"hot\", zero);\nglobal.set(\"temp\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":339,"y":440,"wires":[["cf5fa34b.af5a18"]]},{"id":"48b6b99a.916538","type":"http in","z":"fc9a865.e66cdf8","name":"","url":"/test27","method":"get","swaggerDoc":"","x":149,"y":440,"wires":[["2cd28e6d.7e6552"]]},{"id":"8ea64906.7bbd8","type":"function","z":"fc9a865.e66cdf8","name":"回数を＋１し履歴を保存","func":"var hot = global.get('hot')||0;\nhot += 1;\nglobal.set('hot',hot);\nmsg.payload.hot = hot;\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\nvar array = global.get('temp_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('temp_array', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":590,"y":260,"wires":[["220a84a3.314fc4"]]},{"id":"76ad8eaf.b5eff8","type":"comment","z":"fc9a865.e66cdf8","name":"５秒前との比較での温度で判定","info":"","x":210,"y":60,"wires":[]},{"id":"62bd4507.9f48e4","type":"debug","z":"fc9a865.e66cdf8","name":"全センサー値","active":false,"console":"false","complete":"payload","x":260,"y":100,"wires":[]},{"id":"82f4b730.f2f7c8","type":"debug","z":"fc9a865.e66cdf8","name":"検知された温度","active":true,"console":"false","complete":"payload.d.ambientTemp","x":900,"y":100,"wires":[]},{"id":"220a84a3.314fc4","type":"template","z":"fc9a865.e66cdf8","name":"温度超過を発信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"温度が閾値を超えました。","x":880,"y":260,"wires":[["78bd7a9f.0658ec","72b24915.50c338"]]},{"id":"78bd7a9f.0658ec","type":"debug","z":"fc9a865.e66cdf8","name":"温度超過通知を表示","active":true,"console":"false","complete":"payload","x":900,"y":340,"wires":[]},{"id":"5c9d858.a13217c","type":"debug","z":"fc9a865.e66cdf8","name":"温度","active":false,"console":"false","complete":"payload.d.ambientTemp","x":430,"y":100,"wires":[]},{"id":"8cb3938e.dcd3a8","type":"debug","z":"fc9a865.e66cdf8","name":"温度差","active":true,"console":"false","complete":"payload.tempDelta","x":630,"y":100,"wires":[]},{"id":"72b24915.50c338","type":"e-mail","z":"fc9a865.e66cdf8","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":850,"y":300,"wires":[]}]

```

![スクリーンショット 2016-07-13 16.26.36.png](https://qiita-image-store.s3.amazonaws.com/0/125842/d4baec77-a99e-8db5-f2d4-6c54abfedb69.png)

* * *

####サンプル９

- （たとえばコーヒーメーカーのまわりの）湿度が５秒前と比較して３ポイント上昇し、その事象が過去３０秒間発生していなければ「コーヒー抽出中」と判定してその回数をカウントする、といったシナリオです。

```lang:５秒前との比較での湿度で判定、回数をカウント
[{"id":"86a6e909.8ed0d8","type":"function","z":"748bdb8d.64b434","name":"値を取得","func":"var array = global.get('humidity_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\n\nmsg.payload.h00 = array[0][0];\nmsg.payload.h01 = array[0][1];\nmsg.payload.h02 = array[0][2];\nmsg.payload.h03 = array[0][3];\nmsg.payload.h04 = array[0][4];\nmsg.payload.h05 = array[0][5];\n\nmsg.payload.h10 = array[1][0];\nmsg.payload.h11 = array[1][1];\nmsg.payload.h12 = array[1][2];\nmsg.payload.h13 = array[1][3];\nmsg.payload.h14 = array[1][4];\nmsg.payload.h15 = array[1][5];\n\nmsg.payload.h20 = array[2][0];\nmsg.payload.h21 = array[2][1];\nmsg.payload.h22 = array[2][2];\nmsg.payload.h23 = array[2][3];\nmsg.payload.h24 = array[2][4];\nmsg.payload.h25 = array[2][5];\n\nmsg.payload.h30 = array[3][0];\nmsg.payload.h31 = array[3][1];\nmsg.payload.h32 = array[3][2];\nmsg.payload.h33 = array[3][3];\nmsg.payload.h34 = array[3][4];\nmsg.payload.h35 = array[3][5];\n\nmsg.payload.h40 = array[4][0];\nmsg.payload.h41 = array[4][1];\nmsg.payload.h42 = array[4][2];\nmsg.payload.h43 = array[4][3];\nmsg.payload.h44 = array[4][4];\nmsg.payload.h45 = array[4][5];\n\nmsg.payload.humid = global.get(\"humid\");\nmsg.payload.humidity = global.get(\"humidity\");\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":320,"y":400,"wires":[["981dff4d.85549"]]},{"id":"bc8c3571.56199","type":"function","z":"748bdb8d.64b434","name":"前回からの間隔を確認","func":"var prev_date = global.get('humidity_prev_date')||0;\nmsg.payload.delta = msg.payload.date.date - prev_date;\nprev_date = msg.payload.date.date;\nglobal.set('humidity_prev_date',prev_date);\n\nreturn msg;","outputs":1,"noerr":0,"x":980,"y":160,"wires":[["fe4914dd.fdf4b","65087f1a.25728"]]},{"id":"ba6f9aa9.354868","type":"http in","z":"748bdb8d.64b434","name":"","url":"/test28","method":"get","swaggerDoc":"","x":146,"y":400,"wires":[["86a6e909.8ed0d8"]]},{"id":"b3fabd28.55f8b8","type":"http response","z":"748bdb8d.64b434","name":"","x":707,"y":420,"wires":[]},{"id":"ead3c7f.536b9b8","type":"ibmiot in","z":"748bdb8d.64b434","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":93,"y":160,"wires":[["ed12b35b.3b56","1d055b4.3d57825"]]},{"id":"e8a52975.26a55","type":"switch","z":"748bdb8d.64b434","name":"５秒前から３ポイント上昇で判定","property":"payload.humidityDelta","propertyType":"msg","rules":[{"t":"gt","v":"3","vt":"num"}],"checkall":"true","outputs":1,"x":717,"y":160,"wires":[["bc8c3571.56199","8fd158a6.c3fce"]]},{"id":"981dff4d.85549","type":"template","z":"748bdb8d.64b434","name":"htmlを返す","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"\n<!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湿度が閾値を超えたらカウントします<br />\n\t</p>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test28\">\n\t\t<p><input type=\"submit\" value=\"再度取得する\"></p>\n\t</form>\n\t<form method=\"get\" action=\"http://このIoTアプリの名前.mybluemix.net/test29\">\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>回数</h2>\n\t\t<span style=\"text-align:left;font-size: 24pt; color: #3b5998;\" >{{ payload.humid }}</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.humidity }}</span> ％\n\t\t\n\t<h2>これまでの履歴\t</h2>\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h40 }}年 {{ payload.h41 }}月 {{ payload.h42 }}日 {{ payload.h43 }}時 {{ payload.h44 }}分 {{ payload.h45 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h31 }}月 {{ payload.h32 }}日 {{ payload.h33 }}時 {{ payload.h34 }}分 {{ payload.h35 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h30 }}年 {{ payload.h21 }}月 {{ payload.h22 }}日 {{ payload.h23 }}時 {{ payload.h24 }}分 {{ payload.h25 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h20 }}年 {{ payload.h11 }}月 {{ payload.h12 }}日 {{ payload.h13 }}時 {{ payload.h14 }}分 {{ payload.h15 }}秒</span><br />\n\t\t<span style=\"text-align:left;font-size: 18pt; color: #3b5998;\" >{{ payload.h10 }}年 {{ payload.h01 }}月 {{ payload.h02 }}日 {{ payload.h03 }}時 {{ payload.h04 }}分 {{ payload.h05 }}秒</span><br />\n\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\t","x":548,"y":420,"wires":[["b3fabd28.55f8b8"]]},{"id":"e8b1641c.6c9008","type":"function","z":"748bdb8d.64b434","name":"湿度をセット","func":"var humidity = global.get('humidity')||0;\nhumidity = msg.payload.d.humidity;\nmsg.payload.humidity = humidity;\nglobal.set('humidity',humidity);\n\nvar array = global.get('hist_array')||[[0],[0],[0],[0],[0]];\nvar shifted = array.shift();\narray.push(humidity);\nglobal.set('hist_array', array);\n\nmsg.payload.humidityDelta = humidity - array[0];\nmsg.histarray = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":457,"y":160,"wires":[["e8a52975.26a55","d45bc39f.c1edd8"]]},{"id":"ed12b35b.3b56","type":"function","z":"748bdb8d.64b434","name":"日付時刻の追加","func":"var d = new Date();\nvar localTime = d.getTime();\nvar localOffset = d.getTimezoneOffset() * 60000;\nvar utc = localTime + localOffset;\nvar offset = -9.0;\nvar result = utc - (3600000 * offset);\nvar date = parseInt(new Date(result) / 1000);\nvar dt = new Date(result);\n\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours();\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":257,"y":160,"wires":[["e8b1641c.6c9008","a24dceff.e3c888"]]},{"id":"fe4914dd.fdf4b","type":"debug","z":"748bdb8d.64b434","name":"前回からの間隔","active":false,"console":"false","complete":"payload.delta","x":960,"y":200,"wires":[]},{"id":"65087f1a.25728","type":"switch","z":"748bdb8d.64b434","name":"前回から30秒以上だったら検知","property":"payload.delta","propertyType":"msg","rules":[{"t":"gt","v":"30","vt":"str"}],"checkall":"true","outputs":1,"x":307,"y":280,"wires":[["a289f88c.306fb8"]]},{"id":"17550f45.b7db31","type":"function","z":"748bdb8d.64b434","name":"値をリセット","func":"var zero = 0;\nglobal.set(\"humid\", zero);\nglobal.set(\"humidity\", zero);\nreturn msg;","outputs":1,"noerr":0,"x":336,"y":460,"wires":[["981dff4d.85549"]]},{"id":"21a22acc.609ffe","type":"http in","z":"748bdb8d.64b434","name":"","url":"/test29","method":"get","swaggerDoc":"","x":146,"y":460,"wires":[["17550f45.b7db31"]]},{"id":"a289f88c.306fb8","type":"function","z":"748bdb8d.64b434","name":"回数を＋１し履歴を保存","func":"var humid = global.get('humid')||0;\nhumid += 1;\nglobal.set('humid',humid);\nmsg.payload.humid = humid;\n\nvar now = new Array(\n    msg.payload.date.year,\n    msg.payload.date.month,\n    msg.payload.date.day,\n    msg.payload.date.hour,\n    msg.payload.date.minute,\n    msg.payload.date.second\n);\nmsg.now = now;\n\nvar array = global.get('humidity_array')||[[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]];\nvar shifted = array.shift();\narray.push(now);\nglobal.set('humidity_array', array);\n\nmsg.array = array;\n\nreturn msg;\n","outputs":1,"noerr":0,"x":587,"y":280,"wires":[["b7217eba.710ee"]]},{"id":"67513611.b0954","type":"comment","z":"748bdb8d.64b434","name":"５秒前の湿度と比較","info":"","x":167,"y":80,"wires":[]},{"id":"1d055b4.3d57825","type":"debug","z":"748bdb8d.64b434","name":"全センサー値を表示","active":false,"console":"false","complete":"payload","x":277,"y":120,"wires":[]},{"id":"8fd158a6.c3fce","type":"debug","z":"748bdb8d.64b434","name":"検知された湿度","active":true,"console":"false","complete":"payload.d.humidity","x":960,"y":120,"wires":[]},{"id":"b7217eba.710ee","type":"template","z":"748bdb8d.64b434","name":"湿度超過を発信","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"湿度が閾値を超えました。","x":877,"y":280,"wires":[["f6b3dbf1.6d4fe8","b40b2409.afce48"]]},{"id":"f6b3dbf1.6d4fe8","type":"debug","z":"748bdb8d.64b434","name":"湿度超過通知を表示","active":true,"console":"false","complete":"payload","x":897,"y":380,"wires":[]},{"id":"a24dceff.e3c888","type":"debug","z":"748bdb8d.64b434","name":"湿度","active":false,"console":"false","complete":"payload.d.humidity","x":498,"y":120,"wires":[]},{"id":"b40b2409.afce48","type":"e-mail","z":"748bdb8d.64b434","server":"smtp.gmail.com","port":"465","name":"","dname":"","x":847,"y":340,"wires":[]},{"id":"d45bc39f.c1edd8","type":"debug","z":"748bdb8d.64b434","name":"","active":false,"console":"false","complete":"histarray","x":690,"y":120,"wires":[]}]

```

![スクリーンショット 2016-07-13 17.04.30.png](https://qiita-image-store.s3.amazonaws.com/0/125842/6775c9db-2c00-9905-4e05-24696560f606.png)