HA+mysensor zigbee网关+nodered,替代小米网关

背景

HA + 小米网关 ,不稳定,时常HA上显示此网关下的子设备为“不可用”。拿到H大的mysensor zigbee网关,期望能代替小米网关。

环境

  • Homeasssistant (Hass.io安装 0.98)(软件)
  • nodered(软件) + node-red-contrib-zigbee2mqtt
  • mysensor zigbee网关(硬件)
  • 小米人体感应器等米家的小设备(硬件)
  • zigbee2mqtt(软件)
  • mqtt(软件)

交互图

关键点

  • zigbee2mqtt 通过 tcp 方式 连接 mysensor zigbee网关
  • zigbee2mqtt 采集到的数据 publish 到 mqtt
  • nodered 的 node-red-contrib-zigbee2mqtt 可以为 mysensor zigbee网关 匹配zigbee子设备

mysensor zigbee网关 联网

给网关插上电--------->拿出手机,打开wifi,找到ESP开头的wifi热点--------->链接后,会提示需要登录,点击一下就到了设置页面,输入你家WiFi信息,保存设置就行了。
参考以下,设置mqtt

zigbee2mqtt 的安装

见:N1 或 树莓派 上 裸机安装zigbee2mqtt(即不是基于docker方式的安装),N1 或 树莓派 上 裸机安装zigbee2mqtt(即不是基于docker方式的安装)

node-red-contrib-zigbee2mqtt 节点的安装

作用

  • 匹配子设备。
  • 改子设备的名称。改了名称后,mqtt的主题里就用这个新的名称了,如:zigbee2mqtt/主人按钮/click

zigbee2mqtt-server节点配置

遇到的问题

重启zigbee2mqtt软件所在的服务器,zigbee2mqtt是启动起来的。但是NR的节点看不到早前的子设备信息,如下

后来才发现,确实需要手动点击一下refresh all data。每次重启服务器,我都要点击一下“refresh all data”,实在不够方便。因为我期望使用它的in 节点,监听子设备的数据变化。这样,没点击“refresh all data”前,我是监听不到各种子设备的数据的。后来我改用mqtt in 节点监听子设备的数据的变化了,如下:

[{"id":"f1f95c96.fe3df","type":"tab","label":"012:zigbee网关匹配子设备","disabled":false,"info":""},{"id":"f7e3bb28.e8c398","type":"zigbee2mqtt-in","z":"f1f95c96.fe3df","name":"","server":"3d827146.163b3e","friendly_name":"主人按钮","device_id":"0x00158d0003cff637","state":"0","outputAtStartup":true,"x":108.10000801086426,"y":77.50000190734863,"wires":[["bb47a69c.1b7f58"]]},{"id":"a1e4efd9.f15c3","type":"zigbee2mqtt-bridge","z":"f1f95c96.fe3df","name":"","server":"3d827146.163b3e","topic":null,"x":137.35000610351562,"y":163.50000381469727,"wires":[["48b0fe2d.5915f"]]},{"id":"bb47a69c.1b7f58","type":"debug","z":"f1f95c96.fe3df","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":306.00000190734863,"y":78.50000190734863,"wires":[]},{"id":"8a566310.c291c","type":"zigbee2mqtt-in","z":"f1f95c96.fe3df","name":"","server":"3d827146.163b3e","friendly_name":"门口人体","device_id":"0x00158d0002b43e16","state":"0","outputAtStartup":true,"x":106.50000381469727,"y":242.00000381469727,"wires":[["8a33356a.21ea28"]]},{"id":"8a33356a.21ea28","type":"debug","z":"f1f95c96.fe3df","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":301.00000762939453,"y":241.75000381469727,"wires":[]},{"id":"48b0fe2d.5915f","type":"debug","z":"f1f95c96.fe3df","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":374.25,"y":163.50000381469727,"wires":[]},{"id":"1bc0a858.77ebd8","type":"mqtt in","z":"f1f95c96.fe3df","name":"","topic":"zigbee2mqtt/主人按钮/click","qos":"2","datatype":"auto","broker":"b9a68001.50c6a","x":154.25000381469727,"y":317.7500066757202,"wires":[["77408610.4dac38"]]},{"id":"77408610.4dac38","type":"debug","z":"f1f95c96.fe3df","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":387.75000381469727,"y":319.7500066757202,"wires":[]},{"id":"2077d1f3.ffc27e","type":"mqtt in","z":"f1f95c96.fe3df","name":"","topic":"zigbee2mqtt/门口人体","qos":"2","datatype":"json","broker":"b9a68001.50c6a","x":143.75000381469727,"y":397.7500066757202,"wires":[["55cffeb4.a9915","3dea1973.d6d706"]]},{"id":"55cffeb4.a9915","type":"rbe","z":"f1f95c96.fe3df","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.occupancy","x":335.25000381469727,"y":401.7500066757202,"wires":[["f77833e4.d8009"]]},{"id":"f77833e4.d8009","type":"switch","z":"f1f95c96.fe3df","name":"","property":"payload.occupancy","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":472.75000381469727,"y":399.7500066757202,"wires":[["642a2495.78736c"],["f5dad5fd.671368"]]},{"id":"418377e7.a768b8","type":"debug","z":"f1f95c96.fe3df","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":794.7500038146973,"y":369.7500066757202,"wires":[]},{"id":"642a2495.78736c","type":"function","z":"f1f95c96.fe3df","name":"有人移动","func":"msg.payload = '有人移动';\n\nreturn msg;","outputs":1,"noerr":0,"x":644.7500038146973,"y":381.7500066757202,"wires":[["418377e7.a768b8"]]},{"id":"f5dad5fd.671368","type":"function","z":"f1f95c96.fe3df","name":"无人移动","func":"msg.payload = '没人';\n\nreturn msg;","outputs":1,"noerr":0,"x":641.7500038146973,"y":424.7500066757202,"wires":[["fa646cdd.3dfc"]]},{"id":"fa646cdd.3dfc","type":"debug","z":"f1f95c96.fe3df","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":794.2500038146973,"y":423.7500066757202,"wires":[]},{"id":"1c0cf944.2bae27","type":"comment","z":"f1f95c96.fe3df","name":"rb的作用:无人->有人(触发),有人->无人(触发),有人->有人(不触发)","info":"有人->有人,最短1分钟内触发。有人触发后,其中如果一直有人,至少也要等一分钟才触发第二次。\n有人->无人,2分钟才触发。有人触发后,至少要等2分钟才触发无人。","x":549.2500038146973,"y":476.7500066757202,"wires":[]},{"id":"3dea1973.d6d706","type":"debug","z":"f1f95c96.fe3df","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":334.75000381469727,"y":362.7500066757202,"wires":[]},{"id":"3d827146.163b3e","type":"zigbee2mqtt-server","z":"","name":"108","host":"192.168.1.108","mqtt_port":"1883","mqtt_username":"mqtt","mqtt_password":"mqtt","base_topic":"zigbee2mqtt"},{"id":"b9a68001.50c6a","type":"mqtt-broker","z":"","name":"","broker":"192.168.1.108","port":"1883","clientid":"red2","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

小米子设备的匹配

先在小米网关删除这些子设备(例如开关按钮、人体感应器)。开关按钮刺这里,3秒,闪烁。


等一下,这里会提示 匹配中,然后显示 匹配成功。

进去看,这里会显示新发现的设备

HA 通过 mqtt 自动发现 的方式接入这些子设备

config文件

加入discovery:true 和 discovery_prefix: homeassistant (这个前缀不要调整成其他)

mqtt:
  broker: 192.168.1.108
  port: 1883
  username: mqtt
  password: mqtt
  discovery: true
  discovery_prefix: homeassistant

zigbee2mqtt 的配置成支持ha的自动发现

修改zigbee2mqtt的配置,homeassistant: true

Open the configuration file:

nano /opt/zigbee2mqtt/data/configuration.yaml

# Home Assistant integration (MQTT discovery)
homeassistant: true

# allow new devices to join
permit_join: true

# MQTT settings
mqtt:
  # MQTT base topic for zigbee2mqtt MQTT messages
  base_topic: zigbee2mqtt
  # MQTT server URL
  server: 'mqtt://192.168.1.108'
  # MQTT server authentication, uncomment if required:
  user: mqtt
  password: mqtt

# Serial settings
serial:
  # Location of CC2531 USB sniffer
  port: 'tcp://192.168.1.120:1024'

集成页面可以看到子设备

后话

接入的小米的子设备,目前运行稳定。

棒棒的,参考楼主的教程已经配置成功。

另home assistant 自动发现好像配置写错了,直接在 configuration.yaml 根级写上如下配置即可

hassio 的addons 食用起来更佳。半个小时我就部署好了。(算上hassio 部署的时间)

# Required: Home Assistant integration (MQTT discovery) (default: false)
homeassistant: false