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

 找回密码
 立即注册
查看: 11062|回复: 0

[基础教程] mqtt配置错误原因分析及ha无法自动加载mqtt设备,手动加入方法

[复制链接]

11

主题

53

帖子

325

积分

论坛分享达人

积分
325
金钱
262
HASS币
70
发表于 2020-7-19 12:18:57 | 显示全部楼层 |阅读模式
本帖最后由 zhanghy 于 2020-7-19 12:37 编辑

前提

  1. 安装好bt-mqtt-gateway、mqtt、ziigbee2mqtt
  2. 加入homeassistant

本文探讨如果没有自动识别的情况下,如何手动加入mqtt传感器.昨天我的设备无法自动加入,但是今天通过改配置的方式实现自动加入了,顺便分析一下为什么无法自动加入.

mqtt设备无法自动加入ha的排查方法

  1. 检查蓝牙是否工作,你的cc2531是否工作正常
  2. bt-mqtt-gateway、mqtt、ziigbee2mqtt 服务状态正常
  3. 检查mqtt中是否有相应的topic
  4. 检查以下几个容易出错的配置项
  • ha的配置如下
    
    mqtt:
    discovery: true 开启自动发现
    broker: 'localhost' 必须是字符串
    port: 1883
    username: pi
    password: flyetoak
    discovery_prefix: homeassistant 可以发现的topic的前缀需要满足的条件
    birth_message:
    topic: 'hass/status'
    payload: 'online'
    will_message:
    topic: 'hass/status'
    payload: 'offline'

```
mqtt的配置如下

port 1883
listener 1884
protocol websockets
allow_anonymous false
# 认证配置

password_file /etc/mosquitto/pwfile

zigbee2mqtt

homeassistant: true
permit_join: true
mqtt:
  base_topic: zigbee2mqtt
  server: 'mqtt://localhost'
  user: pi
  password: flyetoak
serial:
  port: /dev/ttyACM0
advanced:
  network_key:
    - 173
    - 237
    - 202
    - 116
    - 94
    - 249
    - 153
    - 164
    - 72
    - 19
    - 75
    - 128
    - 249
    - 238
    - 110
    - 19
devices:
  '0x00158d00026b57aa':
    friendly_name: '0x00158d00026b57aa'
  '0x00158d00033a988c':
    friendly_name: '0x00158d00033a988c'
  '0x00158d0002eac601':
    friendly_name: '0x00158d0002eac601'
  '0x00158d00035c1a91':
    friendly_name: '0x00158d00035c1a91'
  '0x00158d00026e0a29':
    friendly_name: '0x00158d00026e0a29'

bt-mqtt-gateway

mqtt:
  host: localhost
  port: 1883
  username: pi
  password: flyetoak
  topic_prefix: 
  client_id: bt-mqtt-gateway
  availability_topic: lwt_topic
manager:
  sensor_config:
    topic: homeassistant
    retain: true
  topic_subscription:
    update_all:
      topic: homeassistant/status
      payload: online
  command_timeout: 35          
  workers:
    mithermometer:
      args:
        devices:
          living_room: 4c:65:a8:de:8f:9b
        topic_prefix: mithermometer
      update_interval: 300

手动加入方法

实际就是一个解析json的过程,关闭自动发现自己写也很简单.zigbee2mqtt官方有现成的例子,复制进ha的configration.yaml中,(然后为每个设备增加unique_id配置,新版本必须有,例子版本旧了),比如添加小米光线传感器,可以在这直接复制

  1. 安装MQTT.fx,登录mqtt服务器
  2. 用软件查看现在mqtt中的topic
  3. 找到你传感器相关的topic
  4. 在ha的配置文件中书写配置文件,基本上就是通过解析json的数据形式实现,可以大家可以对比我的配置文件和zigbee的json信息得出规律
mqtt:
  discovery: true
  broker: 'localhost'
  port: 1883
  username: pi
  password: flyetoak
  discovery_prefix: homeassistant
  birth_message:
    topic: 'hass/status'
    payload: 'online'
  will_message:
    topic: 'hass/status'
    payload: 'offline'
sensor:
  - platform: mqtt
    name: MiButtonBattery
    state_topic: "zigbee2mqtt/0x00158d00026b57aa"  #替换你自己设备ID,并删掉这句注释。
    value_template: "{{ value_json.battery }}"
    unit_of_measurement: "%"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: battery
    unique_id: "MiButtonBattery"
  - platform: mqtt
    name: MiButtonSignal
    state_topic: "zigbee2mqtt/0x00158d00026b57aa"  #替换你自己设备ID,并删掉这句注释。
    value_template: "{{ value_json.linkquality }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: signal_strength
    unique_id: "MiButtonSignal"
  - platform: mqtt
    name: MiButton
    state_topic: "zigbee2mqtt/0x00158d00026b57aa"
    value_template: "{{ value_json.click }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    unique_id: "MiButton"
  - platform: mqtt
    name: MiDoorBattery
    state_topic: "zigbee2mqtt/0x00158d0002eac601"
    value_template: "{{ value_json.battery }}"
    unit_of_measurement: "%"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: battery
    unique_id: "MiDoorBattery"
  - platform: mqtt
    name: MiDoorSignal
    state_topic: "zigbee2mqtt/0x00158d0002eac601"
    value_template: "{{ value_json.linkquality }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: signal_strength
    unique_id: "MiDoorSignal"
  - platform: mqtt
    name: MiThBattery
    state_topic: "zigbee2mqtt/0x00158d00033a988c"
    value_template: "{{ value_json.battery }}"
    unit_of_measurement: "%"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: battery
    unique_id: "MiThBattery"
  - platform: mqtt
    name: MiThSignal
    state_topic: "zigbee2mqtt/0x00158d00033a988c"
    value_template: "{{ value_json.linkquality }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: signal_strength
    unique_id: "MiThSignal"
  - platform: mqtt
    name: MiTemperature
    state_topic: "zigbee2mqtt/0x00158d00033a988c"
    value_template: "{{ value_json.temperature }}"
    unit_of_measurement: "C"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    unique_id: "MiTemperature"
  - platform: mqtt
    name: MiHumidity
    state_topic: "zigbee2mqtt/0x00158d00033a988c"
    value_template: "{{ value_json.humidity }}"
    unit_of_measurement: "%"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    unique_id: "MiHumidity"
  - platform: mqtt
    name: MiMotionBattery
    state_topic: "zigbee2mqtt/0x00158d00026e0a29"
    value_template: "{{ value_json.battery }}"
    unit_of_measurement: "%"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: battery
    unique_id: "MiMotionBattery"
  - platform: mqtt
    name: MiMotionSignal
    state_topic: "zigbee2mqtt/0x00158d00026e0a29"
    value_template: "{{ value_json.linkquality }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: signal_strength
    unique_id: "MiMotionSignal"
  - platform: "mqtt"
    state_topic: "zigbee2mqtt/0x00158d00035c1a91"
    availability_topic: "zigbee2mqtt/bridge/state"
    unit_of_measurement: "W"
    icon: "mdi:flash"
    value_template: "{{ value_json.power }}"
    unique_id: "MiSwitchPower"
    name: MiSwitchPower
  - platform: "mqtt"
    state_topic: "zigbee2mqtt/0x00158d00035c1a91"
    availability_topic: "zigbee2mqtt/bridge/state"
    unit_of_measurement: "°C"
    device_class: "temperature"
    value_template: "{{ value_json.temperature }}"
    unique_id: "MiSwitchTemperature"
    name: MiSwitchTemperature
  - platform: "mqtt"
    state_topic: "zigbee2mqtt/0x00158d00035c1a91"
    availability_topic: "zigbee2mqtt/bridge/state"
    unit_of_measurement: "kWh"
    value_template: "{{ value_json.consumption }}"
    icon: "mdi:flash"
    unique_id: "MiSwitchConsumption"
    name: MiSwitchConsumption
  - platform: "mqtt"
    state_topic: "zigbee2mqtt/0x00158d00035c1a91"
    availability_topic: "zigbee2mqtt/bridge/state"
    icon: "mdi:signal"
    unit_of_measurement: "lqi"
    value_template: "{{ value_json.linkquality }}"
    unique_id: "MiSwitchSingal"
    name: MiSwitchSingal
  - platform: mqtt
    name: MiBtThBattery
    state_topic: "mithermometer/living_room/battery"
    value_template: "{{ value_json }}"
    unit_of_measurement: "%"
    availability_topic: "mithermometer/living_room/availability"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: battery
    unique_id: MiBtThBattery
  - platform: mqtt
    name: MiBtTemperature
    state_topic: "mithermometer/living_room/temperature"
    value_template: "{{ value_json }}"
    unit_of_measurement: "C"
    availability_topic: "mithermometer/living_room/availability"
    payload_available: "online"
    payload_not_available: "offline"
    unique_id: MiBtTemperature
  - platform: mqtt
    name: MiBtHumidity
    state_topic: "mithermometer/living_room/humidity"
    value_template: "{{ value_json }}"
    unit_of_measurement: "%"
    availability_topic: "mithermometer/living_room/availability"
    payload_available: "online"
    payload_not_available: "offline"
    unique_id: MiBtHumidity
binary_sensor:
  - platform: mqtt
    name: MiDoor
    state_topic: "zigbee2mqtt/0x00158d0002eac601"
    value_template: "{{ value_json.contact }}"
    payload_on: false
    payload_off: true
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: door
    unique_id: "MiDoor"
  - platform: mqtt
    name: MiMotion
    state_topic: "zigbee2mqtt/0x00158d00026e0a29"
    value_template: "{{ value_json.occupancy }}"
    payload_on: true
    payload_off: false
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_available: "online"
    payload_not_available: "offline"
    qos: 0
    device_class: motion
    unique_id: "MiMotion"
  - platform: ffmpeg_motion
    input: http://127.0.0.1:8081
    name: 移动侦测
switch:
  - platform: "mqtt"
    state_topic: "zigbee2mqtt/0x00158d00035c1a91"
    availability_topic: "zigbee2mqtt/bridge/state"
    payload_off: "OFF"
    payload_on: "ON"
    value_template: "{{ value_json.state }}"
    command_topic: "zigbee2mqtt/0x00158d00035c1a91/set"
    unique_id: "MiSwitch"
    name: MiSwitch
camera:
  - platform: generic
    name: 摄像头  
    still_image_url: http://127.0.0.1:8765/picture/1/current/
    stream_source: http://127.0.0.1:8081









待提交.png
doto.png
dotoo.png

评分

参与人数 1金钱 +20 HASS币 +20 收起 理由
+ 20 + 20 厉害了word楼主!

查看全部评分

回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-26 08:26 , Processed in 0.646902 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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