---
disqus: ahb0222
GA : G-VF9ZT413CG
---
# Node-Red 搭配 LINE Notify 取得定時平均數據
> [color=#40f1ef][name=LHB阿好伯, 2021/07/16][:earth_africa:](https://www.facebook.com/LHB0222/)
###### tags: `Node-RED` `MQTT` `創客`
[TOC]
之前在[自製室內空品偵測器中提到](https://www.facebook.com/LHB0222/posts/1291948164658562)數據經常是每秒推一筆數據
若是想在Node-red dashboard上顯示歷史折線圖會等待非常非常久🤣
今天分享使用jsin的方式將數據收集求出每數分鐘平均後在畫圖
或是使用LINE Notify這樣也可以避免一堆通知
這樣可以大大提高dashboard畫圖的效率👍
# 安裝LINE Notify節點
https://flows.nodered.org/node/node-red-contrib-line-notify

安裝後應該就會看到下方的節點出現

## 取得LINE Notify 權杖(token)
https://notify-bot.line.me/

登入LINE帳號
選擇個人頁面

移至最底下取得權杖

這邊需要輸入通知機器人的名稱
選擇個人或是群組使用
若是有想要給別人使用就需要選擇群組
之後再將相關人員拉入群組即可

權杖==只會顯示一次==請另外紀錄好

之後手機應該會出現一個LINE Notify的通知

這一段通知非常重要
提醒要將她這個帳號加入通知的群組中
少了這一步就前功盡棄了XD

# 取得平均值


選擇輸出為陣列
選擇是累積一定數量或是定時的合併數據

創立一個簡單的JavaScript平均函數


最後就可以得到陣列的平均值了

修改函數也可以取得平均值到小數點後第一位的數值


## 推送平均值到Line
首先第一次使用需要先新增前面取得的權杖


一樣利用function節點我們可以增加一些說明文字



:::spoiler 完整流程
```json=
[
{
"id": "9a3bea13a76f1767",
"type": "function",
"z": "8b5074cef2b7979f",
"name": "Temperature",
"func": "msg.payload=(parseFloat(msg.payload.Temperature)/10);\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 810,
"y": 140,
"wires": [
[
"2d6d63814d984b1b"
]
]
},
{
"id": "2d6d63814d984b1b",
"type": "join",
"z": "8b5074cef2b7979f",
"name": "合併10秒數據",
"mode": "custom",
"build": "array",
"property": "payload",
"propertyType": "msg",
"key": "topic",
"joiner": "\\n",
"joinerType": "str",
"accumulate": false,
"timeout": "10",
"count": "",
"reduceRight": false,
"reduceExp": "",
"reduceInit": "",
"reduceInitType": "",
"reduceFixup": "",
"x": 1000,
"y": 140,
"wires": [
[
"56f40a891da3b3f8",
"b89608fb8a9f22a9"
]
]
},
{
"id": "56f40a891da3b3f8",
"type": "function",
"z": "8b5074cef2b7979f",
"name": "",
"func": "function average(nums) {\n return nums.reduce((a, b) => a + b) / nums.length;\n}\nmsg.payload = Math.round(average(msg.payload)*10)/10;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1200,
"y": 140,
"wires": [
[
"175ec2176192ab16",
"000a88229e9d9577"
]
],
"icon": "node-red-contrib-calc/calculator.png"
},
{
"id": "175ec2176192ab16",
"type": "debug",
"z": "8b5074cef2b7979f",
"name": "平均",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1370,
"y": 80,
"wires": []
},
{
"id": "db42e7c960aa994f",
"type": "line-notify",
"z": "8b5074cef2b7979f",
"name": "",
"message": "",
"contentType": "message",
"imageThumbnail": "",
"imageUrl": "",
"sticker": "default",
"stickerPackageId": "1",
"stickerId": "1",
"silent": false,
"creds": "",
"x": 1480,
"y": 140,
"wires": [
[]
]
},
{
"id": "000a88229e9d9577",
"type": "function",
"z": "8b5074cef2b7979f",
"name": "註解",
"func": "var message = \"溫度 : \" + msg.payload + \" 度\";\nmsg.payload = message;\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1350,
"y": 140,
"wires": [
[
"db42e7c960aa994f"
]
]
},
{
"id": "b89608fb8a9f22a9",
"type": "debug",
"z": "8b5074cef2b7979f",
"name": "合併",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1160,
"y": 100,
"wires": []
}
]
```
:::
🌟
🌟全文可以至下方連結觀看或是補充
全文分享至
https://www.facebook.com/LHB0222/
https://www.instagram.com/ahb0222/
有疑問想討論的都歡迎於下方留言
喜歡的幫我分享給所有的朋友 \o/
有所錯誤歡迎指教
# [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings)
