本帖最后由 6907367 于 2025-6-30 14:00 编辑
第一步登录路由器192.168.1.1,生成脚本获取温度。 echo -e '#!/bin/sh\necho "Content-type: text/plain"\necho ""\ncat /sys/class/thermal/thermal_zone0/temp' > /www/cgi-bin/temp 第二步给执行权限 chmod +x /www/cgi-bin/temp 第三步nodered生成流,请在mqtt out 节点修改对应的mqtt服务器用户名和密码 第四步,修改configuration.yaml
mqtt:
sensor:
- name: "OpenWrt CPU 温度"
state_topic: "homeassistant/sensor/openwrt_cpu/state" # 必须与 Node-RED 中设置的主题完全一致
unit_of_measurement: "°C"
value_template: "{{ value_json.temperature }}" # 告诉 Home Assistant 从 JSON 中提取 "temperature" 字段
device_class: temperature # 会自动显示温度图标
state_class: measurement # 用于历史记录和图表
unique_id: openwrt_cpu_temp # 推荐添加,用于 UI 编辑和实体管理
[
{
"id": "4e46d03797b743c4",
"type": "tab",
"label": "路由器温度",
"disabled": false,
"info": "",
"env": []
},
{
"id": "e0a2b1c3d4e5f6a7b8c9d0e1f2a3b4c5",
"type": "inject",
"z": "4e46d03797b743c4",
"name": "定时获取温度",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "20",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 300,
"y": 320,
"wires": [
[
"f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6"
]
]
},
{
"id": "f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6",
"type": "http request",
"z": "4e46d03797b743c4",
"name": "获取OpenWrt温度",
"method": "GET",
"ret": "txt",
"paytoqs": "ignore",
"url": "http://192.168.1.1/cgi-bin/temp",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "",
"senderr": false,
"headers": [],
"x": 550,
"y": 240,
"wires": [
[
"a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7"
]
]
},
{
"id": "a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7",
"type": "function",
"z": "4e46d03797b743c4",
"name": "处理温度数据 (除以1000)",
"func": "let rawTemp = parseInt(msg.payload);\n\nif (isNaN(rawTemp)) {\n node.warn(\"接收到的温度数据无效: \" + msg.payload);\n // 如果获取到的不是有效数字,则不向下传递消息\n return null; \n}\n\n// 将原始整数值除以 1000 得到实际温度 (例如 43300 -> 43.3)\nlet actualTemp = rawTemp / 1000;\n\n// 构建 Home Assistant 期望的 JSON 格式\nmsg.payload = {\n \"temperature\": actualTemp,\n \"unit\": \"°C\"\n};\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 690,
"y": 380,
"wires": [
[
"c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9",
"eea95cde404912bd"
]
]
},
{
"id": "c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9",
"type": "mqtt out",
"z": "4e46d03797b743c4",
"name": "发布到 Home Assistant MQTT",
"topic": "homeassistant/sensor/openwrt_cpu/state",
"qos": "1",
"retain": "true",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "16dbc903cbb78b24",
"x": 970,
"y": 280,
"wires": []
},
{
"id": "eea95cde404912bd",
"type": "debug",
"z": "4e46d03797b743c4",
"name": "debug 1",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload.temperature",
"targetType": "jsonata",
"statusVal": "",
"statusType": "auto",
"x": 960,
"y": 380,
"wires": []
},
{
"id": "16dbc903cbb78b24",
"type": "mqtt-broker",
"name": "",
"broker": "192.168.1.13",
"port": 1883,
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": 4,
"keepalive": 60,
"cleansession": true,
"autoUnsubscribe": true,
"birthTopic": "",
"birthQos": "1",
"birthRetain": "true",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
] |