HomeAssistant和Node-Red结合教程

说下背景:
node-red做自动化,协议解析,采集,转发,函数,都是很好用的。因为它有着非常丰富的第三控件。但是奈何dashboard太丑了,没法满足要求。另一方面Hass的lovelace就很惊艳,还有着非常丰富的UI组件,权限控制。两者结合下就非常好了,就像前后端分离。
前期准备:
1.linux基本操作
2.docker
开始搞事
第一部分 node-red
1.node red 安装:

docker run -d -p 1880:1880 -p 10502:10502 --restart=always -e TZ="Asia/Shanghai" -v myNodeREDdata:/data --name mynodered nodered/node-red

注释1:1880 就是你访问 node-red页面的端口
注释2:为啥要多 -p 出来个10502呢?因为我是想用node-red做个modbus server的转发*
2.关于node-red的一些注意事项:

https://blog.csdn.net/qq_36393991/article/details/115692049

在这里我写了一点
3.node-red的使用
注释1:
基本控件的使用,dashboard ,S7,modbus等等的看教程
注释2:
S7的使用 注意两点,
一是 西门子200,200smart这类型号的配置,红框里的要注意,必须是这样
在这里插入图片描述
二是 V区的变量配置: V区 V直接换成X ,如果是V VD这种去掉V
在这里插入图片描述
**注释3:**modbus
我贴个代码吧,可以导入看看

[{"id":"6ee4efcd.713c","type":"tab","label":"modbus 收 分 发 转","disabled":true,"info":""},{"id":"c862bafc.693f68","type":"modbus-read","z":"6ee4efcd.713c","name":"readtest","topic":"","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"","dataType":"HoldingRegister","adr":"0","quantity":"40","rate":"1000","rateUnit":"ms","delayOnStart":false,"startDelayTime":"","server":"10d01abc.038e85","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":700,"y":40,"wires":[["21c15648.f3c7ca"],[]]},{"id":"21c15648.f3c7ca","type":"debug","z":"6ee4efcd.713c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":850,"y":40,"wires":[]},{"id":"ed67fd66.960db","type":"modbus-read","z":"6ee4efcd.713c","name":"test","topic":"test","showStatusActivities":false,"logIOActivities":false,"showErrors":false,"unitid":"1","dataType":"HoldingRegister","adr":"0","quantity":"40","rate":"1000","rateUnit":"ms","delayOnStart":false,"startDelayTime":"","server":"9ccfb0bc.4b34b","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"x":90,"y":120,"wires":[["b06affd6.08e8b","dec62b51.4e90e8","72b7aa0e.1932f4"],[]]},{"id":"b06affd6.08e8b","type":"modbus-write","z":"6ee4efcd.713c","name":"","showStatusActivities":false,"showErrors":false,"unitid":"","dataType":"MHoldingRegisters","adr":"0","quantity":"40","server":"10d01abc.038e85","emptyMsgOnFail":false,"keepMsgProperties":false,"x":280,"y":120,"wires":[[],["5d5be7ef.0cf4f8"]]},{"id":"5d5be7ef.0cf4f8","type":"modbus-server","z":"6ee4efcd.713c","name":"","logEnabled":false,"hostname":"0.0.0.0","serverPort":10502,"responseDelay":100,"delayUnit":"ms","coilsBufferSize":10000,"holdingBufferSize":10000,"inputBufferSize":10000,"discreteBufferSize":10000,"showErrors":false,"x":500,"y":120,"wires":[[],[],[],[],[]]},{"id":"dec62b51.4e90e8","type":"debug","z":"6ee4efcd.713c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":270,"y":40,"wires":[]},{"id":"72b7aa0e.1932f4","type":"function","z":"6ee4efcd.713c","name":"","func":"var newDate =  new Date()\nvar ts=newDate.toLocaleString( )\nvar msg3 = {data:msg.payload[3],ts:ts};\nvar msg5 = {data:msg.payload[5],ts:ts };\nvar msg10 = {data:msg.payload[10],ts:ts };\nreturn [msg3,msg5,msg10];","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":200,"wires":[["99d42b25.71e748"],[],[]]},{"id":"99d42b25.71e748","type":"debug","z":"6ee4efcd.713c","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"ts","targetType":"msg","statusVal":"","statusType":"auto","x":440,"y":320,"wires":[]},{"id":"10d01abc.038e85","type":"modbus-client","name":"","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"0.0.0.0","tcpPort":"10502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":1,"commandDelay":1,"clientTimeout":1000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true},{"id":"9ccfb0bc.4b34b","type":"modbus-client","name":"modbus104","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"10.86.3.104","tcpPort":"503","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

4.node-red使用 Hass
暂时省略,不过需要注意的是,只是node-red单独导入node-red-contrib-home-assistant-websocket 并且配置了hass 也是没法使用的,必须在hass里面也安装node-red的插件才可以
5.node-red使用邮箱
暂时省略
第二部分 HASS
1.HASS安装

docker run --init -d  --Restart=always --name="home-assistant" -e "TZ=Asia/Shanghai" -v /opt/homeassistant:/config --net=host homeassistant/home-assistant:latest

端口号 8123 启动
2.下载HACS 压缩包
HACS可以简单理解 就是HASS的应用商店,可以在这边安装node-red的插件,想到哪说到哪吧,就是在这边安装也不现实 ,因为是翻墙去github下载,基本不可能安装成功,所以我是自己电脑挂VPN,下载好文件后,放在custom_components文件夹里,(custom_components一般是没有的,需要手动创建,创建的位置就是你 挂在出来的文件夹里 例:/opt/homeassistant/custom_components/),要自己下载文件但是怎么知道插件地址呢?这时候你可以在HACS中搜索
在这里插入图片描述
红框点击 就可以去到该插件的 github主页了,要哪些文件,放在哪,怎么用github上写的都比较清楚
在这里插入图片描述
弄好之后,重启docker,在配置–集成里就可以搜索到并且添加进来,之后你在node-red里面通过 entity控件添加的实体 都可以显示出来
entity控件:这里有3个 一个是开关控制node-red输出 两个node-red输入到hass
在这里插入图片描述
hass能显示出3个实体
在这里插入图片描述
最后就可以配置lovelace就可以了
在这里插入图片描述
点开关可以 看到在node-red 控制台里的输出
最后 更高深的在研究吧

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐