zTC1支持接入Home Assistant

前言

zTC1通过MQTT服务器接入HA,通过MQTT配置使zTC1接入HA连接的MQTT服务器。
必须能够用app通过mqtt进行控制,之后的homeassistant接入才能成功,如果app无法通过mqtt控制,请先完成mqtt的相关配置。

Home Assistant配置

configuration.yaml配置

使用Packages文件夹下创建单独文件的方式来管理HA的设备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes

# Text to speech
tts:
- platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
packages: !include_dir_named packages

Packages文件夹配置

新建一个packages的文件夹。
如果接入多个ztc1,只需要创建多个yaml文件(文件名不同),每个文件替换mac地址即可接入多个ztc1。

以下内容中,请将MACMAC替换为你的排插的mac地址,不带冒号,全部小写,如123456789abc
mac地址可以在app设备设置页面中点击mac地址直接复制。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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'

- 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'

- 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'

- 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'

- 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'

- 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'

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 }}'

- name: 'ztc1_time_MACMAC'
unique_id: ztc1_time_MACMAC
state_topic: 'device/ztc1/MACMAC/sensor'
#unit_of_measurement: '秒'
icon: 'mdi:gauge'
#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运行时间

zTC1支持接入Home Assistant
https://blog.5772447.xyz/posts/1f2b6c4/
作者
Maskbugzero
发布于
2023年4月1日
许可协议