本帖最后由 zy302688381 于 2024-9-21 12:51 编辑
esphome当前版本 2024.8.3
esphome:
name: cs-s-b
friendly_name: CS-S-B
esp32:
board: esp32-s3-devkitc-1
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "+aB7U1UB5Z3W+cNM3ReX7gtRGpe10O4H+bmDeq8UWWI="
ota:
- platform: esphome
password: "7e3136e98d11f564899e87005bd8668f"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Cs-S-B Fallback Hotspot"
password: "K5p5tCDdWIVZ"
captive_portal:
web_server: ##web控制,添加了这2行,就可以在网页端打开设备ip
port: 80
time: #时间
- platform: sntp
id: my_time
servers: 120.25.108.11
update_interval: 24h
# 定义二进制传感器
binary_sensor:
- platform: gpio
pin: GPIO33 # 替换为实际连接的GPIO引脚
id: water_sensor
on_press:
then:
- lambda: |-
// 每次触发时增加水量计数器
if (id(water_meter).state < 1.2) {
id(water_meter).state += 0.01;
}
// 如果达到1.2立方米,则关闭阀门
if (id(water_meter).state >= 1.2) {
id(close_valve).turn_on();
}
# 定义水表传感器,用于记录累积水量
sensor:
- platform: template
id: water_meter
name: "Water Meter"
unit_of_measurement: "m³"
accuracy_decimals: 2
lambda: return id(water_sensor).state;
# 定义开关
switch:
- platform: gpio
pin: GPIO20 # 替换为实际连接的GPIO引脚
id: open_valve
name: "开阀门"
- platform: gpio
pin: GPIO21 # 替换为实际连接的GPIO引脚
id: close_valve
name: "关阀门"
# 每天中午12点重置水量计数器
# 定时任务,每天中午12点执行重置脚本
# String time
automation:
- id: reset_counter_at_noon
trigger:
platform: time
at: '12:00:00'
action:
- lambda: |-
id(water_meter).state = 0.0;
|