#センサーからの最新値を取得する
- センサーから非同期に送付されてくる値について、最新(最後)に送付された値を定期的に取り出す方法です。
- Node-REDのフローをコピーする方法を参照して、下記のフローをコピペします。
センサーからの最新値を取得する
[{"id":"950d6b21.50386","type":"inject","z":"b82825b4.1760d","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":190,"y":180,"wires":[["e2d55676.90d288"]]},{"id":"e2d55676.90d288","type":"function","z":"b82825b4.1760d","name":"cacheから取り出し","func":"msg.payload = context.global.lastcache;\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":180,"wires":[["fe2b7eff.5636d"]]},{"id":"fe2b7eff.5636d","type":"debug","z":"b82825b4.1760d","name":"","active":true,"console":"false","complete":"payload","x":570,"y":180,"wires":[]},{"id":"5d5b341b.e1cf4c","type":"function","z":"b82825b4.1760d","name":"cacheに待避","func":"context.global.lastcache = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":120,"wires":[["3617eb0d.5c68bc"]]},{"id":"11345bf2.842c1c","type":"function","z":"b82825b4.1760d","name":"日付を追加","func":"var date = parseInt(new Date / 1000);\nvar dt = new Date();\nvar year = dt.getFullYear();\nvar month = dt.getMonth()+1;\nvar day = dt.getDate();\nvar hour = dt.getHours()+9;\nvar minute = dt.getMinutes();\nvar second = dt.getSeconds();\n//if (!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":350,"y":120,"wires":[["5d5b341b.e1cf4c"]]},{"id":"3617eb0d.5c68bc","type":"debug","z":"b82825b4.1760d","name":"","active":false,"console":"false","complete":"payload","x":750,"y":120,"wires":[]},{"id":"9f2518c1.82099","type":"ibmiot in","z":"b82825b4.1760d","authentication":"quickstart","apiKey":"","inputType":"evt","deviceId":"324255ecdf55","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"IBM IoT","service":"quickstart","allDevices":"","allApplications":"","allDeviceTypes":true,"allEvents":true,"allCommands":"","allFormats":"","x":170,"y":80,"wires":[["11345bf2.842c1c"]]}]
- センサーからの情報を
context.global.cache
へ待避します。深さ1のスタックのイメージです。 - Injectノードの「Interval」を必要な間隔(例:5分毎、など)に設定し、そのタイミングでキャッシュ(
context.global.cache
)に待避されている情報を取り出すことで、常に最新(最後)のセンサー値を入手することができます。