############################################################################################
substitutions:
button_pin: GPIO4
relay_reset_pin: GPIO12
relay_set_pin: GPIO0
relay_latch_pin: GPIO15
led_blue_pin: GPIO16
led_red_pin: GPIO14
led_white_pin: GPIO5
#重要-批量部署到设备上之后,不要去修改node_name和各组件的name(sensor、switch、light等),会导致homeassistant中id变化,影响已有的自动化与脚本。
#下面这三个键值与各组件的name,可以保证homeassistant中id的唯一性。批量删除设备再添加,homeassistant中的id也不会变化。
node_name: "cmcc-powerboard"
node_name_friendly: "CMCC.CMPOWER-W1"
node_name_add_mac_suffix: "true"
#可选填
node_project_name: "CMCC.CMPOWER-W1"
node_project_version: "v3.0.0(espressif-esp12s-sy7t609)"
node_build_min_version: "2023.11.4"
packages:
common: !include 1-common-esp8266-2m.yaml
common-component: !include 1-common-component.yaml
esphome:
on_boot:
- priority: 0
then:
- output.turn_off: id_pin_relay_reset
- output.turn_off: id_pin_relay_set
- lambda: |-
if(nullptr != esphome::captive_portal::global_captive_portal)
{
//配网模式,蓝色led闪烁
if(esphome::captive_portal::global_captive_portal->is_active() && !id(id_wifi).is_connected())
{
id(id_led_blue).turn_off().perform();
id(id_led_blue).turn_on().set_effect("fast").perform();
}
}
esp8266:
restore_from_flash: true
preferences:
flash_write_interval: 1min
web_server:
version: 2
external_components:
####local mode####
#- source:
# type: local
# path: components
# components: [ sy7t609 ]
####git mode######
- source: github://shzlww/esphome_custom_components@20240103
components: [ sy7t609 ]
wifi:
#使用use_address对使用mac地址修饰esphome:name的设备进行ota
#use_address: cmcc-powerboard-26faec.local
on_connect:
- light.turn_on:
id: id_led_blue
effect: "None"
on_disconnect:
- light.turn_off: id_led_blue
logger:
level: INFO
baud_rate: 0
output:
#继电器触发使能引脚
- platform: gpio
id: id_pin_relay_latch
pin:
number: ${relay_latch_pin}
inverted: true
- platform: gpio
id: id_pin_relay_set
pin:
number: ${relay_set_pin}
inverted: true
- platform: gpio
id: id_pin_relay_reset
pin:
number: ${relay_reset_pin}
inverted: true
#白色led
- platform: esp8266_pwm
id: id_pin_led_white
pin:
number: ${led_white_pin}
inverted: true
#蓝色led
- platform: esp8266_pwm
id: id_pin_led_blue
pin:
number: ${led_blue_pin}
inverted: true
#红色led
- platform: esp8266_pwm
id: id_pin_led_red
pin:
number: ${led_red_pin}
inverted: true
- platform: template
id: fake_out
type: float
write_action:
- logger.log: "NoThing"
light:
#白色led(分路继电器的状态)
- platform: monochromatic
name: light-Led White
id: id_led_white
output: id_pin_led_white
restore_mode: ALWAYS_OFF
default_transition_length: 0s
internal: true
#蓝灯常亮-wifi已连接
#蓝灯灯闪烁-配网模式
#红灯闪烁,蓝灯熄灭-wifi未连接
#蓝色led(wifi状态)
- platform: monochromatic
name: light-Led Blue
id: id_led_blue
output: id_pin_led_blue
restore_mode: ALWAYS_OFF
default_transition_length: 0s
internal: true
effects:
#配网模式
- pulse:
name: "fast"
transition_length: 0ms
update_interval: 100ms
#预留(未使用,可用作指示其他状态)
- pulse:
name: "slow"
transition_length: 200ms
update_interval: 500ms
#红色led(wifi状态与配网状态)
- platform: status_led
name: light-Led Red
id: id_led_red
output: id_pin_led_red
internal: true
# 红蓝灯合并(后期实现彩色status_led)
# - platform: cwww
# name: "light-CWWW Status Led "
# cold_white: pin_led_blue
# warm_white: pin_led_red
# cold_white_color_temperature: 6536 K
# warm_white_color_temperature: 2000 K
# constant_brightness: true
# restore_mode: ALWAYS_OFF
# default_transition_length: 0s
# effects:
# - pulse:
# name: "fast"
# transition_length: 0ms
# update_interval: 100ms
button:
#实现一个电平反转,触发继电器动作
- platform: output
#name: button-Relay Trigger
id: id_trigger_relay_latch
output: id_pin_relay_latch
duration: 10ms
internal: true
#重置计量芯片寄存器上的电度量
- platform: template
name: adv-ResetEnergyTotal
id: id_button_reset_energy_total
on_press:
then:
- if:
condition:
switch.is_on: id_switch_operate_confirm
then:
# - globals.set:
# id: id_energy_counter_persist
# value: '0.0'
- sy7t609.reset_energy: id_sensor_sy7t609
- switch.turn_off: id_switch_operate_confirm
#重置esp计算的的每日电度量
- platform: template
name: adv-ResetEnergyDaily
id: id_button_reset_energy_daily
on_press:
then:
- if:
condition:
switch.is_on: id_switch_operate_confirm
then:
- lambda: |-
id(id_total_daily_energy_median).publish_state_and_save(0.0f);
- switch.turn_off: id_switch_operate_confirm
#重置校准寄存器信息
- platform: template
name: adv-ResetCalibration
id: id_button_reset_calibration
on_press:
then:
- if:
condition:
switch.is_on: id_switch_operate_confirm
then:
- sy7t609.reset_calibration: id_sensor_sy7t609
- switch.turn_off: id_switch_operate_confirm
#打印调试信息
- platform: template
name: adv-PrintRegister
id: id_button_debug
on_press:
then:
- if:
condition:
switch.is_on: id_switch_operate_confirm
then:
- sy7t609.debug: id_sensor_sy7t609
- switch.turn_off: id_switch_operate_confirm
switch:
#重置系统(并且所有电度量)
# id(id_energy_counter_persist) = 0.0f;
- platform: template
name: "system-FactoryReset-WithEnergyCounter"
id: id_factoryreset_with_energy_counter
optimistic: true
restore_mode: ALWAYS_OFF
on_turn_on:
then:
- sy7t609.reset_energy: id_sensor_sy7t609
- delay: 20ms #确保uart命令可以执行成功
- lambda: |-
id(id_total_daily_energy_median).publish_state_and_save(0.0f);
- switch.toggle: id_factoryreset
- platform: template
name: adv-Confirm
id: id_switch_operate_confirm
optimistic: true
restore_mode: ALWAYS_OFF
#插排按钮-逻辑开关
- platform: template
id: id_power_key_logic
name: switch-Power Key
optimistic: true
restore_mode: ALWAYS_OFF
on_turn_on:
then:
- light.turn_on: id_led_white
- script.execute: id_power_turn_on_relay
- script.wait: id_power_turn_on_relay
on_turn_off:
then:
- light.turn_off: id_led_white
- script.execute: id_power_turn_off_relay
- script.wait: id_power_turn_off_relay
#插排电源按钮
binary_sensor:
- platform: gpio
#name: button-Power Key Real
id: id_power_key
internal: true
pin:
number: ${button_pin}
inverted: true
mode:
input: true
pullup: true
#插排电源按钮单击、长按功能
on_multi_click:
#长按5s以上,重置为出厂模式,进入配网状态
- timing:
- ON for at least 5s
then:
- logger.log: "Long press."
- switch.turn_on: id_factoryreset_with_energy_counter
invalid_cooldown: 0s
#双击
- timing:
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at least 0.2s
then:
- logger.log: "double click."
invalid_cooldown: 0s
#单击
- timing:
- ON for at most 0.3s
- OFF for at least 0.2s
then:
- logger.log: "single click."
- switch.toggle: id_power_key_logic
invalid_cooldown: 0s
script:
- id: id_power_turn_on_relay
then:
- button.press: id_trigger_relay_latch
- output.turn_on: id_pin_relay_set
- delay: 70ms
- button.press: id_trigger_relay_latch
- output.turn_off: id_pin_relay_set
- id: id_power_turn_off_relay
then:
- button.press: id_trigger_relay_latch
- output.turn_on: id_pin_relay_reset
- delay: 70ms
- button.press: id_trigger_relay_latch
- output.turn_off: id_pin_relay_reset
time:
- platform: homeassistant
id: id_homeassistant_time
timezone: "Asia/Shanghai"
uart:
tx_pin: 1
rx_pin: 3
baud_rate: 9600
stop_bits: 1
data_bits: 8
parity: NONE
sensor:
- platform: sy7t609
id: id_sensor_sy7t609
power_factor:
name: "energy-Power Factor"
id: id_sensor_pf
voltage:
name: "energy-Voltage"
id: id_sensor_voltage
current:
name: "energy-Current"
id: id_sensor_current
power:
name: "energy-Power"
id: id_sensor_power
reactive_power:
name: "energy-Reactive Power"
id: id_sensor_reactive_power
energy:
name: "energy-Energy Counter"
id: id_sensor_energy
frequency:
name: "energy-Frequency"
id: id_sensor_frequency
chip_temperature:
name: "energy-Chip Temperature"
id: id_sensor_chip_temperature
update_interval: 3s
- platform: total_daily_energy
name: "energy-DailyEnergy"
id: id_total_daily_energy_median
power_id: id_energy_median_power
accuracy_decimals: 2
device_class: "energy"
state_class: "total_increasing"
icon: "mdi:counter"
restore: true
unit_of_measurement: Wh
#功率传感器(power)的副本,使用中值滤波器,过滤掉异常抖动的数据,缺点就是需要缓存采集的数据,状态反馈慢。可用作homeassistant中能源统计的功率。
#1.中值滤波是一种非线性数字滤波器技术,经常用于去除图像或者其它信号中的噪声。
#2.检查输入信号中的采样并判断它是否代表了信号,使用奇数个采样组成的观察窗实现这项功能。
#3.观察窗口中的数值进行排序,位于观察窗中间的中值作为输出。
#4.然后丢弃最早的值,取得新的采样,重复上面的计算过程。
- platform: copy
name: energy-MedianPower
id: id_energy_median_power
source_id: id_sensor_power
filters:
- median:
window_size: 5
send_every: 3
send_first_at: 3
#电度量数据持久化存储到esp芯片的flash中,实现断电记忆电度量
#- platform: copy
# name: energy-Energy Counter(Persist)
# id: id_sensor_energy_counter_persist
# source_id: id_sensor_energy
# filters:
# - lambda: |-
# if(x < id(id_energy_counter_persist))
# {
# float delta = x - id(id_energy_counter_lastvalue);
# id(id_energy_counter_persist) += delta;
# id(id_energy_counter_lastvalue) = x;
# }
# else
# {
# id(id_energy_counter_persist) = x;
# }
# return id(id_energy_counter_persist);
#计量芯片sy7t609的电度量计数器是内存存储的,断电会丢失,因此使用esp的全局变量存储电度量
#globals:
# - id: id_energy_counter_persist
# type: float
# restore_value: yes
# initial_value: '0.0'
# - id: id_energy_counter_lastvalue
# type: float
# restore_value: no
# initial_value: '0.0'
大佬,我按上面这个改的,主要是去掉了写入flash的段落
|