『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 50412|回复: 35

[经验分享] 妖神zigbee2mqtt网关使用经验分享

  [复制链接]

3

主题

39

帖子

572

积分

高级会员

Rank: 4

积分
572
金钱
528
HASS币
20
发表于 2020-8-5 17:25:14 | 显示全部楼层 |阅读模式
本帖最后由 darkleliel 于 2020-8-6 00:38 编辑

前一阵为了使用硬件修改版aqara人体传感器,以及完全本地控制yeelight吸顶灯(在米家APP中是云端控制,只要断网就废了),开始使用妖神的zigbee2mqtt网关。期间踩了不少坑,终于基本稳定运行,总结点使用体验分享给大家。


1 硬件


妖神的zigbee2mqtt网关,闲鱼很多,100元

zigbee2mqtt网关

zigbee2mqtt网关

zigbee设备,最好能多一些路由设备,增强zigbee信号
买来一般都刷好协调器固件了,具体可以参见原贴https://bbs.iobroker.cn/t/topic/3347
有几点设置建议:
  • 1、AP设置、连接完成后,在路由器设置静态IP,重启网关后通过IP地址进入管理页面,在控制台输入命令

SetOption55 0
回车确认,关闭mDNS
  • 2、在"设置-其他设置"启用MQTT后,在"设置-MQTT设置"中填入broker所在设备IP、broker的端口、broker的用户名密码、主题"zigbee2mqtt"



2 软件


2.1 Mosquitto


MQTT broker,有很多版本很多安装方式,我用的openwrt软件包安装
装好后编辑
/ETC/mosquitto/mosquitto.conf

取消注释以下行
port 1883 # 端口,可以改,但没必要
allow_anonymous false # 需要密码访问,建议
password_file /etc/mosquitto/passwords.txt # 用户名密码存储路径,可以改,但没必要

完成后保存,putty登陆openwrt
mosquitto_passwd -c /etc/mosquitto/passwords.txt admin # admin是用户名
会让你输入两次密码,完成后设置完毕


2.2 zigbee2mqtt


我用的docker方式
拉取镜像
koenkk/zigbee2mqtt:latest
创建容器参数:
docker run --name="zigbee2mqtt" -it -e "TZ=Asia/Shanghai" -v /opt/zigbee2mqtt/data:/app/data --net=host koenkk/zigbee2mqtt:latest
注意挂载目录提前创建,我MQTT broker和zigbee2mqtt在同一设备上,所以网络采用host,其他情况参见
https://www.zigbee2mqtt.io/information/docker.html
打开
/app/data/configuration.yaml

实际路径为
/opt/zigbee2mqtt/data/configuration.yaml

添加
homeassistant: true # HASS MQTT集成,MQTT discovery功能必需
permit_join: true # 默认允许添加zigbee设备,添加完成后改为false
mqtt:
  base_topic: zigbee2mqtt
  server: 'mqtt://192.168.101.254' # MQTT broker所在设备IP
  user: admin # MQTT broker用户名
  password: password # MQTT broker密码
serial:
  port: 'tcp://192.168.XXX.YYY:8880' # zigbee2mqtt网关IP,端口默认8880
advanced:
  channel: 20 # zigbee信道,不设置默认11

zigbee信道尽量避开2.4G WIFI和其他zigbee网关的信道(比如小米),例如我家几个米家网关、空调伴侣都是11、25信道(zigbee),各种2.4G WIFI在1、6、11信道都有,但8、9信道之间相对干扰较少,对应zigbee信道是20,具体参考
https://blog.csdn.net/lxh_860624/article/details/80671825
另外,添加zigbee设备成功后,可以在configuration.yaml对每个设备单独设置。
例如硬件改造过的aqara人体传感器(5秒检测一次,改造前为60s,90秒未检测到移动状态改变),为了实现例如10秒返回"10秒无人移动"、20秒返回"20秒无人移动"、30秒状态改变,在configuration.yaml中设备对应的"0x00XXXXXXXXXX"下面添加
no_occupancy_since: [10, 20]
occupancy_timeout: 30

也可以每个设备单独设置qos、retain等,具体参考
https://www.zigbee2mqtt.io/information/configuration.html


2.3 Zigbee2MQTT Admin Panel


因为使用了Node-Red,所以按照zigbee2mqtt官方推荐,使用Zigbee2MQTT Admin Panel进行设备管理
具体参见
https://github.com/ben423423n32j14e/zigbee2mqttadminpanel
照做即可,安装完成后全局设置中,"mqtt-broker"节点,Connection填MQTT broker的IP、端口,SSL我没开(因为内网),Security填MQTT broker用户名、密码,保存后记得检查Zigbee2MQTT Admin Panel安装后添加的每个subflow,确认所有节点的mqtt-broker设置正确,部署完成后用Node-Red IP:1880/ui/就可以进入zigbee2mqtt管理界面了,在这里添加、重命名、删除zigbee设备

3 踩过的坑


3.1 mosquitto无响应


如果你连接zigbee2mqtt网关的设备比较多,mosquitto可能过了几个小时至1天以上会无响应,我也没有仔细研究过具体原因,现在在openwrt计划任务中加了个
7 */2 * * * /etc/init.d/mosquitto restart # 每隔2小时重启mosquitto,例如4:07、6:07
实际只影响1-2秒,解决了这个问题


3.2 重启HASS后MQTT设备状态不显示、无法控制


google后发现遇到这个问题的很多,在Node-Red中一切正常,重启zigbee2mqtt网关后可以恢复正常,我采取了两个措施:
  • 1、所有连接zigbee2mqtt网关的需要控制开关zigbee设备,均通过Node-Red进行控制
例如aqara单火双键开关+aqara双键无线开关组成的一组双控开关,在Node-Red中记录开关状态实现toggle单火开关的动作
[{"id":"2ad1859c.241e9a","type":"mqtt in","z":"81ef9759.a0b558","name":"","topic":"zigbee2mqtt/EntranceWireless","qos":"2","datatype":"json","broker":"2f2d4274.d04c7e","x":120,"y":960,"wires":[["4cb7c012.d1ad4"]]},{"id":"4cb7c012.d1ad4","type":"switch","z":"81ef9759.a0b558","name":"Left/Right","property":"payload.click","propertyType":"msg","rules":[{"t":"eq","v":"left","vt":"str"},{"t":"eq","v":"right","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":340,"y":960,"wires":[["46ee82b3.5b4f1c"],["908c8fd6.99004"]]},{"id":"7ab54ef4.cd19d","type":"comment","z":"81ef9759.a0b558","name":"Toggle Living Room Switch","info":"","x":110,"y":860,"wires":[]},{"id":"21bfd089.c9e1c","type":"change","z":"81ef9759.a0b558","name":"Switch Off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{"state_left":"OFF"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":960,"wires":[["6bfad626.bc5cb8"]]},{"id":"3e00433f.623fac","type":"change","z":"81ef9759.a0b558","name":"Switch On","rules":[{"t":"set","p":"payload","pt":"msg","to":"{"state_left":"ON"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":900,"wires":[["6bfad626.bc5cb8"]]},{"id":"46ee82b3.5b4f1c","type":"switch","z":"81ef9759.a0b558","name":"Off/On","property":"living_room_switch_left","propertyType":"flow","rules":[{"t":"neq","v":"ON","vt":"str"},{"t":"eq","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":490,"y":960,"wires":[["3e00433f.623fac"],["21bfd089.c9e1c"]]},{"id":"6bfad626.bc5cb8","type":"mqtt out","z":"81ef9759.a0b558","name":"","topic":"zigbee2mqtt/LivingRoomSwitch/set","qos":"2","retain":"false","broker":"2f2d4274.d04c7e","x":900,"y":960,"wires":[]},{"id":"ff63418a.756dd","type":"change","z":"81ef9759.a0b558","name":"","rules":[{"t":"set","p":"living_room_switch_left","pt":"flow","to":"payload.state_left","tot":"msg"},{"t":"set","p":"living_room_switch_right","pt":"flow","to":"payload.state_right","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1140,"y":1020,"wires":[[]]},{"id":"e4b97005.1cd58","type":"mqtt in","z":"81ef9759.a0b558","name":"","topic":"zigbee2mqtt/LivingRoomSwitch","qos":"2","datatype":"json","broker":"2f2d4274.d04c7e","x":890,"y":1020,"wires":[["ff63418a.756dd"]]},{"id":"a516ab7d.f41798","type":"change","z":"81ef9759.a0b558","name":"Switch Off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{"state_right":"OFF"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":1080,"wires":[["6bfad626.bc5cb8"]]},{"id":"de8edf3e.63ba9","type":"change","z":"81ef9759.a0b558","name":"Switch On","rules":[{"t":"set","p":"payload","pt":"msg","to":"{"state_right":"ON"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":1020,"wires":[["6bfad626.bc5cb8"]]},{"id":"908c8fd6.99004","type":"switch","z":"81ef9759.a0b558","name":"Off/On","property":"living_room_switch_right","propertyType":"flow","rules":[{"t":"neq","v":"ON","vt":"str"},{"t":"eq","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":490,"y":1020,"wires":[["de8edf3e.63ba9"],["a516ab7d.f41798"]]},{"id":"2f2d4274.d04c7e","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.1.1","port":"1883","tls":"3e133b20.e7e384","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"2","birthPayload":"","closeTopic":"","closeQos":"2","closePayload":"","willTopic":"","willQos":"2","willPayload":""},{"id":"3e133b20.e7e384","type":"tls-config","z":"","name":"","cert":"","key":"","ca":"","certname":"m2mqtt_srv.crt","keyname":"m2mqtt_srv.key","caname":"m2mqtt_ca.crt","servername":"","verifyservercert":false}]
  • 2、HASS重启后自动重启zigbee2mqtt网关
我的HASS自动重启只会发生在每周一次定时重启软路由时发生,所以使用Node-red在软路由重启几分钟后通过MQTT命令重启zigbee2mqtt网关
[{"id":"a9957a50.0c78e8","type":"mqtt out","z":"81ef9759.a0b558","name":"","topic":"cmnd/zigbee2mqtt/Restart","qos":"","retain":"","broker":"2f2d4274.d04c7e","x":350,"y":2320,"wires":[]},{"id":"f8ff85a7.70a988","type":"inject","z":"81ef9759.a0b558","name":"Tuesday 5:10","props":[{"p":"payload"}],"repeat":"","crontab":"10 05 * * 2","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":120,"y":2320,"wires":[["a9957a50.0c78e8"]]},{"id":"2f2d4274.d04c7e","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.1.1","port":"1883","tls":"3e133b20.e7e384","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"2","birthPayload":"","closeTopic":"","closeQos":"2","closePayload":"","willTopic":"","willQos":"2","willPayload":""},{"id":"3e133b20.e7e384","type":"tls-config","z":"","name":"","cert":"","key":"","ca":"","certname":"m2mqtt_srv.crt","keyname":"m2mqtt_srv.key","caname":"m2mqtt_ca.crt","servername":"","verifyservercert":false}]

3.3 连接zigbee2mqtt设备电量显示

连接zigbee2mqtt的设备电池电量显示基本是废的,我的目前全是100%。看github好像大概意思是固件会认为3V纽扣电池只要电压高于3V就认为是满电的……
查了一下实际上3V纽扣电池满电电压大概在3.2-3.3V,电压低于2.8-2.9V设备基本就不工作了。所以我在Node-Red里简单设置了一下,判断电压2.85V-3.2V为电量0-100%,每天8:00检查电量,过低时通知。
[{"id":"4b5e3115.9b022","type":"mqtt in","z":"eaf3c639.244698","name":"","topic":"zigbee2mqtt/EntranceMotion","qos":"2","datatype":"json","broker":"2f2d4274.d04c7e","x":120,"y":60,"wires":[["26d02e32.2212c2"]]},{"id":"c03de345.1c5d4","type":"range","z":"eaf3c639.244698","minin":"2850","maxin":"3200","minout":"1","maxout":"100","action":"clamp","round":true,"property":"payload.battery_pct","name":"Battery Percentage","x":550,"y":60,"wires":[["c1f60e65.7c0da"]]},{"id":"2c68fcb1.04a634","type":"ha-entity","z":"eaf3c639.244698","name":"Entrance Motion Battery","server":"1f21f301.8eaead","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"Entrance Motion Battery"},{"property":"device_class","value":"battery"},{"property":"icon","value":""},{"property":"unit_of_measurement","value":"%"}],"state":"payload.battery_pct","stateType":"msg","attributes":[{"property":"voltage","value":"payload.voltage","valueType":"msg"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":950,"y":60,"wires":[[]]},{"id":"df018a8e.6a2f28","type":"comment","z":"eaf3c639.244698","name":"Entrance Motion Battery","info":"","x":100,"y":20,"wires":[]},{"id":"c1f60e65.7c0da","type":"range","z":"eaf3c639.244698","minin":"0","maxin":"3500","minout":"0","maxout":"3.5","action":"clamp","round":false,"property":"payload.voltage","name":"Voltage","x":740,"y":60,"wires":[["2c68fcb1.04a634"]]},{"id":"26d02e32.2212c2","type":"change","z":"eaf3c639.244698","name":"Set Battery","rules":[{"t":"set","p":"payload.battery_pct","pt":"msg","to":"payload.voltage","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":60,"wires":[["c03de345.1c5d4"]]},{"id":"2f2d4274.d04c7e","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.1.1","port":"1883","tls":"3e133b20.e7e384","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"2","birthPayload":"","closeTopic":"","closeQos":"2","closePayload":"","willTopic":"","willQos":"2","willPayload":""},{"id":"1f21f301.8eaead","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"3e133b20.e7e384","type":"tls-config","z":"","name":"","cert":"","key":"","ca":"","certname":"m2mqtt_srv.crt","keyname":"m2mqtt_srv.key","caname":"m2mqtt_ca.crt","servername":"","verifyservercert":false}]
[{"id":"41404c4a.f89924","type":"api-current-state","z":"eaf3c639.244698","name":"Entrance Motion Battery","server":"1f21f301.8eaead","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.entrance_motion_battery","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":330,"y":2420,"wires":[["7aef1d05.0ee4b4"]]},{"id":"7aef1d05.0ee4b4","type":"switch","z":"eaf3c639.244698","name":"Voltage <= 2.9V","property":"data.attributes.voltage","propertyType":"msg","rules":[{"t":"lte","v":"2.9","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":2420,"wires":[["f7174b99.78f6e8"]]},{"id":"f7174b99.78f6e8","type":"api-call-service","z":"eaf3c639.244698","name":"Notification","server":"1f21f301.8eaead","version":1,"debugenabled":false,"service_domain":"notify","service":"notify","entityId":"","data":"{"message": "Entrance Motion Battery Low (CR2450)"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":750,"y":2420,"wires":[[]]},{"id":"e7e740d4.56b9d","type":"inject","z":"eaf3c639.244698","name":"Daily 8:00","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 08 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":2420,"wires":[["41404c4a.f89924"]]},{"id":"1f21f301.8eaead","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

希望能对大家有所帮助,欢迎交流





评分

参与人数 2金钱 +15 收起 理由
z741554038 + 5 感谢分享踩过的坑 学习了
chinyaolin + 10 學習了~

查看全部评分

回复

使用道具 举报

4

主题

294

帖子

3982

积分

论坛元老

Rank: 8Rank: 8

积分
3982
金钱
3688
HASS币
0
发表于 2020-8-5 19:23:08 | 显示全部楼层
建议用node-red-contrib-zigbee2mqtt这个节点,更好用。Zigbee2MQTT Admin Panel有点太复杂了
回复

使用道具 举报

3

主题

39

帖子

572

积分

高级会员

Rank: 4

积分
572
金钱
528
HASS币
20
 楼主| 发表于 2020-8-5 21:36:57 | 显示全部楼层
honry 发表于 2020-8-5 19:23
建议用node-red-contrib-zigbee2mqtt这个节点,更好用。Zigbee2MQTT Admin Panel有点太复杂了 ...

设置好后都不用动了,后面重命名、添加删除都挺方便的
回复

使用道具 举报

4

主题

294

帖子

3982

积分

论坛元老

Rank: 8Rank: 8

积分
3982
金钱
3688
HASS币
0
发表于 2020-8-6 09:10:48 | 显示全部楼层
darkleliel 发表于 2020-8-5 21:36
设置好后都不用动了,后面重命名、添加删除都挺方便的

如果多个多话,这个很难用,你可以试试node-red-contrib-zigbee2mqtt
而且node-red-contrib-zigbee2mqtt会让你省很多数据转换多问题 iShot2020-08-06上午09.10.05.png

回复

使用道具 举报

3

主题

39

帖子

572

积分

高级会员

Rank: 4

积分
572
金钱
528
HASS币
20
 楼主| 发表于 2020-8-6 12:48:04 | 显示全部楼层
honry 发表于 2020-8-6 09:10
如果多个多话,这个很难用,你可以试试node-red-contrib-zigbee2mqtt
而且node-red-contrib-zigbee2mqtt ...

还有这等好事
我研究研究
回复

使用道具 举报

2

主题

128

帖子

2424

积分

金牌会员

Rank: 6Rank: 6

积分
2424
金钱
2296
HASS币
0
发表于 2020-8-10 08:33:01 | 显示全部楼层
壳子不错,求链接.
回复

使用道具 举报

3

主题

39

帖子

572

积分

高级会员

Rank: 4

积分
572
金钱
528
HASS币
20
 楼主| 发表于 2020-8-10 11:10:12 | 显示全部楼层
noahzh89 发表于 2020-8-10 08:33
壳子不错,求链接.

发你个2.0版本的吧,上盖设计成铝的,可以给芯片散热(尽管热量很低),这版没实际做过,尺寸自己检查
链接: https://pan.baidu.com/s/1XkqOl4BMHSnET26XJaAJPQ 提取码: yvut
需要:
M4*10螺母柱X4(直径小于3.5mm)
M4沉头螺丝X8(长度大于6mm,小于9mm)
导热垫X2(空隙1-1.5mm)
回复

使用道具 举报

2

主题

128

帖子

2424

积分

金牌会员

Rank: 6Rank: 6

积分
2424
金钱
2296
HASS币
0
发表于 2020-8-10 12:03:58 | 显示全部楼层
darkleliel 发表于 2020-8-10 11:10
发你个2.0版本的吧,上盖设计成铝的,可以给芯片散热(尽管热量很低),这版没实际做过,尺寸自己检查
链 ...

十分感谢
回复

使用道具 举报

12

主题

145

帖子

1183

积分

金牌会员

Rank: 6Rank: 6

积分
1183
金钱
1038
HASS币
0
发表于 2020-8-10 17:00:09 | 显示全部楼层
darkleliel 发表于 2020-8-10 11:10
发你个2.0版本的吧,上盖设计成铝的,可以给芯片散热(尽管热量很低),这版没实际做过,尺寸自己检查
链 ...

感谢分享
回复

使用道具 举报

2

主题

128

帖子

2424

积分

金牌会员

Rank: 6Rank: 6

积分
2424
金钱
2296
HASS币
0
发表于 2020-8-10 21:47:20 | 显示全部楼层
darkleliel 发表于 2020-8-10 11:10
发你个2.0版本的吧,上盖设计成铝的,可以给芯片散热(尽管热量很低),这版没实际做过,尺寸自己检查
链 ...

1.0有现成的商家可以推荐吗,2.0现在报价都太高了.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-25 00:56 , Processed in 0.252084 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表