如何获取特定Zigbee设备与网关的最后通信时间?
解决方案:基于ZHA的Zigbee休眠设备快速离线检测
核心思路
利用ZHA收到设备轮询数据包时触发的事件,记录设备最后通讯时间,再通过时间差判断设备是否离线。
方法1:通过ZHA事件监听记录最后通讯时间
1. 创建模板传感器存储最后通讯时间
添加以下配置到configuration.yaml(或通过UI的模板编辑器创建):
template: - sensor: - name: "Aeotec Sensor Last Seen" state: "{{ now().isoformat() }}" device_class: timestamp attributes: ieee_address: "替换为你的设备IEEE地址"
2. 创建自动化触发时间更新
当目标设备发送数据包时,自动更新传感器的时间:
automation: - alias: Update Aeotec Sensor Last Seen trigger: - platform: event event_type: zha_event event_data: device_ieee: "替换为你的设备IEEE地址" action: - service: homeassistant.update_entity target: entity_id: sensor.aeotec_sensor_last_seen
方法2:直接使用ZHA原生属性(若支持)
部分Zigbee设备在ZHA中会原生暴露last_seen属性,可直接在开发者工具的状态面板中查找目标设备实体,检查是否存在该属性。若存在,可跳过方法1,直接基于该属性构建离线判断逻辑。
离线判断自动化
基于记录的最后通讯时间,创建自动化实现1分钟内的离线检测:
automation: - alias: Detect Aeotec Sensor Offline trigger: - platform: template value_template: "{{ as_timestamp(now()) - as_timestamp(states('sensor.aeotec_sensor_last_seen')) > 60 }}" condition: # 可选:添加额外条件,比如设备之前处于在线状态 - condition: state entity_id: binary_sensor.aeotec_sensor_contact state: 'on' action: # 替换为你的通知方式 - service: notify.mobile_app_你的设备 data: message: "Aeotec传感器已离线超过1分钟"
关键注意事项
- 设备IEEE地址可在ZHA的设备详情页获取(通常为16位十六进制字符串)
- 基于设备7秒的长轮询间隔,设置60秒的超时阈值足够覆盖网络波动
- 确保Home Assistant系统时间同步正常,避免时间差判断错误
内容的提问来源于stack exchange,提问作者lelmarir
相关产品推荐
相关产品推荐

