Skip to content

homeassistant接入

Zip Zhang edited this page Aug 14, 2023 · 21 revisions

zTC1支持接入home assistant(以下简称为ha).

zTC1设置

zTC1通过MQTT服务器接入ha.通过MQTT配置使zTC1接入ha连接的MQTT服务器.即可

注意:必须能够用app通过mqtt进行控制,之后的homeassistant接入才能成功,如果app无法通过mqtt控制,请先完成mqtt的相关配置

home assistant设置

注意: 不建议为了此排插入坑hass,使用hass需要很多时间专门来学习相关内容.本人没有精力教授hass相关配置.仅提供配置文件.请自行学习相关配置方式.

home assistant手动配置

建议使用packages文件夹下创建单独文件的方式来管理ha的设备.将以下内容直接保存为一个yaml文件即可.
如果接入多个ztc1,只需要创建多个yaml文件(文件名不同),每个文件替换mac地址即可接入多个ztc1.
packages的使用方式请在hass论坛中搜索学习使用

以下内容中,请将MACMAC替换为你的排插的mac地址,不带冒号,全部小写,如123456789abc

(mac地址可以在app设备设置页面中点击mac地址直接复制)

注意:如果接入多个zTC1,请保证以下 name字段唯一性

20230615:更改了配置内容,适配ha 2022.12.0版本

20230814:更改了配置内容,适配ha 2023.8.2版本(吐槽:ha更新改格式!!! 太频繁了!)

mqtt:
  - switch: 
      name: 'ztc1_1_MACMAC'
      unique_id: ztc1_1_MACMAC
      state_topic: 'device/ztc1/MACMAC/state'
      command_topic: 'device/ztc1/MACMAC/set'
      payload_on: '{"mac":"MACMAC","plug_0":{"on":1}}'
      payload_off: '{"mac":"MACMAC","plug_0":{"on":0}}'
      value_template: '{{ value_json.plug_0.on }}'
      state_on: '1'
      state_off: '0'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
  - switch: 
      name: 'ztc1_2_MACMAC'
      unique_id: ztc1_2_MACMAC
      state_topic: 'device/ztc1/MACMAC/state'
      command_topic: 'device/ztc1/MACMAC/set'
      payload_on: '{"mac":"MACMAC","plug_1":{"on":1}}'
      payload_off: '{"mac":"MACMAC","plug_1":{"on":0}}'
      value_template: '{{ value_json.plug_1.on }}'
      state_on: '1'
      state_off: '0'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
  - switch: 
      name: 'ztc1_3_MACMAC'
      unique_id: ztc1_3_MACMAC
      state_topic: 'device/ztc1/MACMAC/state'
      command_topic: 'device/ztc1/MACMAC/set'
      payload_on: '{"mac":"MACMAC","plug_2":{"on":1}}'
      payload_off: '{"mac":"MACMAC","plug_2":{"on":0}}'
      value_template: '{{ value_json.plug_2.on }}'
      state_on: '1'
      state_off: '0'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
  - switch: 
      name: 'ztc1_4_MACMAC'
      unique_id: ztc1_4_MACMAC
      state_topic: 'device/ztc1/MACMAC/state'
      command_topic: 'device/ztc1/MACMAC/set'
      payload_on: '{"mac":"MACMAC","plug_3":{"on":1}}'
      payload_off: '{"mac":"MACMAC","plug_3":{"on":0}}'
      value_template: '{{ value_json.plug_3.on }}'
      state_on: '1'
      state_off: '0'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
  - switch: 
      name: 'ztc1_5_MACMAC'
      unique_id: ztc1_5_MACMAC
      state_topic: 'device/ztc1/MACMAC/state'
      command_topic: 'device/ztc1/MACMAC/set'
      payload_on: '{"mac":"MACMAC","plug_4":{"on":1}}'
      payload_off: '{"mac":"MACMAC","plug_4":{"on":0}}'
      value_template: '{{ value_json.plug_4.on }}'
      state_on: '1'
      state_off: '0'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
  - switch: 
      name: 'ztc1_6_MACMAC'
      unique_id: ztc1_6_MACMAC
      state_topic: 'device/ztc1/MACMAC/state'
      command_topic: 'device/ztc1/MACMAC/set'
      payload_on: '{"mac":"MACMAC","plug_5":{"on":1}}'
      payload_off: '{"mac":"MACMAC","plug_5":{"on":0}}'
      value_template: '{{ value_json.plug_5.on }}'
      state_on: '1'
      state_off: '0'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
  - sensor: 
      name: 'ztc1_power_MACMAC'
      unique_id: ztc1_power_MACMAC
      state_topic: 'device/ztc1/MACMAC/sensor'
      unit_of_measurement: 'W'
      icon: 'mdi:gauge'
      value_template: '{{ value_json.power }}'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
  - sensor: 
      name: 'ztc1_time_MACMAC'
      unique_id: ztc1_time_MACMAC
      state_topic: 'device/ztc1/MACMAC/sensor'
      #unit_of_measurement: '秒'
      icon: 'mdi:gauge'
      availability_topic: "device/ztc1/MACMAC/availability"
      payload_available: 1
      payload_not_available: 0
      #value_template: '{{ value_json.total_time }}'
      value_template: >-
        {% set time = value_json.total_time %}
        {% set minutes = ((time % 3600) / 60) | int %}
        {% set hours = ((time % 86400) / 3600) | int %}
        {% set days = (time / 86400) | int %}
        {%- if time < 60 -%}
          <1分钟
        {%- else -%}
          {%- if days > 0 -%}
              {{ days }}天
          {%- endif -%}
          {%- if hours > 0 -%}
              {{ hours }}小时
          {%- endif -%}
          {%- if minutes > 0 -%}
              {{ minutes }}分钟
          {%- endif -%}
        {%- endif -%}
    
homeassistant:
  customize:
    switch.ztc1_1_MACMAC:
      friendly_name: zTC1插槽1
    switch.ztc1_2_MACMAC:
      friendly_name: zTC1插槽2
    switch.ztc1_3_MACMAC:
      friendly_name: zTC1插槽3
    switch.ztc1_4_MACMAC:
      friendly_name: zTC1插槽4
    switch.ztc1_5_MACMAC:
      friendly_name: zTC1插槽5
    switch.ztc1_6_MACMAC:
      friendly_name: zTC1插槽6
    sensor.ztc1_power_MACMAC:
      friendly_name: zTC1功率
    sensor.ztc1_time_MACMAC:
      friendly_name: zTC1运行时间