本帖最后由 telanx 于 2026-1-6 22:53 编辑
aj11891 发表于 2024-5-21 17:42
感谢楼主的分享,已近把这个新程序更新上去了,只是用的esp8266,就是不知芯片能不能刚住,这么多实体的更新 ...
大佬,请问下8266运行起来稳吗?
我之前培正+8266连的,用的peacefair来记录的,看起来ESPhome的比较好啊,原生支持,不需要插件了,想刷成ESPHome的。。。
另外,大佬能否分享下您8266的yaml文件呢?谢谢。
问了下AI,要修改的地方还不少,比如8266 framework: type: arduino,删除BLE,修改UART的TX RX阵脚代码,删除web server和improv_serial。
用AI糊了一个8266的版本,硬件还是沿用Involute大神的PeaceFair的接线方式,直接ESPHome OTA升级,丝滑接入HA,以后再也不需要插件了,哈哈哈
北京城区没有峰谷电价区别,阶梯电价的限值也不一样,就都一并改了,虽然记录峰谷用电量和费用,但是电价是一样的。
substitutions:
name: power-meter
friendly_name: Power Meter
esphome:
name: ${name}
friendly_name: ${friendly_name}
name_add_mac_suffix: false
project:
name: esp8266-pzem004Tv3.power-meter
version: "1.0.0"
esp8266:
board: nodemcuv2
api:
encryption:
key: !secret api_encryption_key
reboot_timeout: 30min
ota:
- platform: esphome
password: !secret ota_password
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
min_auth_mode: WPA2
# Optional manual IP
manual_ip:
static_ip: 192.168.1.246
gateway: 192.168.1.1
subnet: 255.255.255.0
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Pzem-004T-V3"
password: !secret ap_password
logger:
uart:
- id: main_uart
tx_pin: D6
rx_pin: D7
baud_rate: 9600
debug:
direction: BOTH
modbus:
- id: mod_bus_pzemac
send_wait_time: 5ms
uart_id: main_uart
button:
- platform: template
name: Meter Reset
id: reset_button
icon: "mdi:emoticon-outline"
on_press:
- pzemac.reset_energy: pzemac_main
- platform: template
name: Clear Memory
id: clear_memory
entity_category: diagnostic
on_press:
- lambda: |-
id(global_today_peak_usage) = 0.000f;
id(global_today_valley_usage) = 0.000f;
id(global_today_peak_bill) = 0.000f;
id(global_today_valley_bill) = 0.000f;
id(global_month_peak_usage) = 0.000f;
id(global_month_valley_usage) = 0.000f;
id(global_month_peak_bill) = 0.000f;
id(global_month_valley_bill)= 0.000f;
- platform: safe_mode
name: Safe Mode Boot
entity_category: diagnostic
- platform: restart
name: Restart
entity_category: diagnostic
globals:
# flash保存表复位(清0)时的年度用电量
- id: global_reset_annual_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存1号00:00:00的用电度数(上月用电度数)
- id: global_last_month_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的抄表数(昨日抄表数)
- id: global_yesterday_energy
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日用电量
- id: global_yesterday_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日峰电量
- id: global_yesterday_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日谷电量
- id: global_yesterday_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日峰电费
- id: global_yesterday_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存00:00:00的昨日谷电费
- id: global_yesterday_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月峰电量
- id: global_last_month_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月谷电量
- id: global_last_month_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月峰电费
- id: global_last_month_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月谷电费
- id: global_last_month_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日峰电量
- id: global_today_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日谷电量
- id: global_today_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日峰电费
- id: global_today_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日谷电费
- id: global_today_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月峰电量(昨日为止)
- id: global_month_peak_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月谷电量(昨日为止)
- id: global_month_valley_usage
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月峰电费(昨日为止)
- id: global_month_peak_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月谷电费(昨日为止)
- id: global_month_valley_bill
type: float
restore_value: yes
initial_value: '0.000'
# 全局变量:当前是峰电价(true,flase)flash不保存,计算所得
- id: global_is_peak_price
type: bool
restore_value: false
initial_value: 'true'
# 全局变量:当前阶梯档位(0,1,2)flash不保存,计算所得
- id: global_step_id
type: int
restore_value: false
initial_value: '0'
# 全局变量:上1分钟抄表数,不保存
- id: global_last_energy
type: float
restore_value: false
initial_value: '-0.001'
number:
# 设置表读数为0(复位)时的年度电量,加上表上读数为实际当年使用度数, 用来计算阶梯电价
- platform: template
id: set_reset_annual_usage
name: Set Reset Annual Usage
mode: box
min_value: 0
max_value: 9999
initial_value: 0
optimistic: true
step: 1
restore_value: false
unit_of_measurement: kwh
on_value_range:
above: 1
below: 9999
then:
- lambda: |-
id(global_reset_annual_usage) = x;
id(reset_annual_usage).publish_state(id(global_reset_annual_usage));
id(actual_annual_usage).publish_state( id(global_reset_annual_usage) + id(main_energy).state );
id(current_price).update();
sensor:
# 培正电表,每月重置
- platform: pzemac
id: pzemac_main
update_interval: 10s
address: 0x01
modbus_id: mod_bus_pzemac
current:
name: "Main Current"
voltage:
name: "Main Voltage"
power:
name: "Main Power"
id: main_power
frequency:
name: "Main Frequency"
power_factor:
name: "Main Factor"
energy:
# 当前读表度数,每月清0,即为当月度数
name: "Main Energy"
id: main_energy
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
accuracy_decimals: 3
filters:
- multiply: 0.001
on_value:
then:
- lambda: |-
id(actual_annual_usage).publish_state( id(global_reset_annual_usage) + id(main_energy).state );
id(today_usage).publish_state( id(main_energy).state - id(global_yesterday_energy));
id(current_price).update();
# 表读数为0(复位)时的年度电量,加上当前读表数为实际年度用量
- platform: template
name: Reset Annual Usage
id: reset_annual_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
id(set_reset_annual_usage).publish_state(0);
return id(global_reset_annual_usage);
# 实际年度用量( 表复位时的年度电量+当前电量(表读数))
- platform: template
name: Actual Annual Usage
id: actual_annual_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_reset_annual_usage) + id(main_energy).state;
# 当前电价
- platform: template
name: Current Price
id: current_price
accuracy_decimals: 3
icon: mdi:home-lightning-bolt
unit_of_measurement: 'CNY/kWh'
update_interval: 10s
lambda: |-
int step_id=0;
float current_price = 0.000f;
float all_prices[3][2] = { {0.488, 0.488}, {0.538, 0.538}, {0.788, 0.788} };
float total_usage = id(actual_annual_usage).state;
if(total_usage <= 2880) step_id=0;
if((total_usage>2880) && (total_usage <= 4800)) step_id=1;
if(total_usage > 4800) step_id=2;
id(global_step_id) = step_id;
int t_now = parse_number<int>(id(time_sntp).now().strftime("%H%M")).value();
if ( t_now>=800 && t_now < 2200 ){
current_price = all_prices[step_id][0];
id(global_is_peak_price) = true;
}else{
current_price = all_prices[step_id][1];
id(global_is_peak_price) = false;
}
return current_price;
# 昨日抄表数
- platform: template
name: Yesterday Energy
id: yesterday_energy
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_yesterday_energy);
# 昨日用电量
- platform: template
name: Yesterday Usage
id: yesterday_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_yesterday_usage);
# 今日用电量
- platform: template
name: Today Usage
id: today_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 10s
lambda:
return id(main_energy).state - id(global_yesterday_energy);
# 上月用电量
- platform: template
name: Last Month Usage
id: last_monthly_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_last_month_usage);
# 昨日、上月、今日、当月谷电量
- platform: template
name: Yesterday Valley Usage
id: yesterday_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_yesterday_valley_usage);
- platform: template
name: Last Month Valley Usage
id: last_month_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_last_month_valley_usage);
- platform: template
name: Today Valley Usage
id: today_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_today_valley_usage);
- platform: template
name: Month Valley Usage
id: month_valley_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_month_valley_usage) + id(global_today_valley_usage);
# 昨日、上月、今日、当月峰电量
- platform: template
name: Yesterday Peak Usage
id: yesterday_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_yesterday_peak_usage);
- platform: template
name: Last Month Peak Usage
id: last_month_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_last_month_peak_usage);
- platform: template
name: Today Peak Usage
id: today_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 15s
lambda:
return id(global_today_peak_usage);
- platform: template
name: Month Peak Usage
id: monthly_peak_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 20s
lambda:
return id(global_month_peak_usage) + id(global_today_peak_usage);
# 昨日、上月、今日、当月电费
- platform: template
name: yesterday_bill
id: yesterday_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_yesterday_valley_bill)+id(global_yesterday_peak_bill);
- platform: template
name: Last Month bill
id: last_month_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_last_month_valley_bill)+id(global_last_month_peak_bill);
- platform: template
name: today_bill
id: today_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_today_valley_bill)+id(global_today_peak_bill);
- platform: template
name: monthly_bill
id: monthly_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_month_valley_bill)+id(global_month_peak_bill) + id(today_bill).state;
# 昨日、上月、今日、当月谷电费
- platform: template
name: yesterday_valley_bill
id: yesterday_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_yesterday_valley_bill);
- platform: template
name: Last Month Valley Bill
id: last_month_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_last_month_valley_bill);
- platform: template
name: Today Valley Bill
id: today_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_today_valley_bill);
- platform: template
name: Month Valley Bill
id: month_valley_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 25s
lambda:
return id(global_month_valley_bill) + id(global_today_valley_bill);
# 昨日、上月、今日、当月峰电费
- platform: template
name: Yesterday Peak Bill
id: yesterday_peak_bill
unit_of_measurement: CNY
accuracy_decimals: 3
update_interval: 15s
lambda:
return id(global_yesterday_peak_bill);
- platform: template
name: Last Month Peak Bill
id: last_month_peak_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_last_month_peak_bill);
- platform: template
name: Today Peak_bill
id: today_peak_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_today_peak_bill);
- platform: template
name: Month Peak Bill
id: month_peak_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_month_peak_bill) + id(global_today_peak_bill);
- platform: wifi_signal
name: "WiFi Signal Sensor"
update_interval: 60s
text_sensor:
- platform: wifi_info
ssid:
name: Connected SSID
icon: "mdi:signal-variant"
ip_address:
name: IP Address
icon: "mdi:ip-network"
mac_address:
name: Mac Address
icon: "mdi:wifi-marker"
time:
- platform: homeassistant
timezone: UTC-8
id: time_ha
- platform: sntp
id: time_sntp
servers: ntp.aliyun.com
timezone: UTC-8
on_time:
# 昨日总电量,昨日抄表数,昨日总电费,今日总电费重置
# 当月峰电量,当月谷电量(昨日为止)
# 当月峰电费,当月谷电费(昨日为止)
# 昨日峰电量,今日峰电量重置
# 昨日谷电量,今日谷电量重置
# 昨日峰电费,今日峰电费重置
# 昨日谷电费,今日谷电费重置
- seconds: 0
minutes: 0
hours: 0
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
id(global_yesterday_usage) = id(main_energy).state - id(global_yesterday_energy);
id(global_yesterday_energy) = id(main_energy).state;
id(yesterday_energy).publish_state( id(global_yesterday_energy) );
id(yesterday_usage).publish_state( id(global_yesterday_usage) );
id(today_bill).publish_state(0.000f);
id(global_month_peak_usage) = id(global_month_peak_usage) + id(global_today_peak_usage);
id(global_month_valley_usage) = id(global_month_valley_usage) + id(global_today_valley_usage);
id(global_month_peak_bill) = id(global_month_peak_bill) + id(global_today_peak_bill);
id(global_month_valley_bill) = id(global_month_valley_bill) + id(global_today_valley_bill);
id(global_yesterday_peak_usage) = id(global_today_peak_usage);
id(global_today_peak_usage) = 0.000f;
id(global_yesterday_valley_usage) = id(global_today_valley_usage);
id(global_today_valley_usage) = 0.000f;
id(global_yesterday_peak_bill) = id(global_today_peak_bill);
id(global_today_peak_bill) = 0.000f;
id(global_yesterday_valley_bill) = id(global_today_valley_bill);
id(global_today_valley_bill) = 0.000f;
id(global_month_peak_usage) = id(global_month_peak_usage) + id(global_today_peak_usage);
id(global_month_valley_usage) = id(global_month_valley_usage) + id(global_today_valley_usage);
# 上月电量, 表复位年度用电量
# 上月峰电量,上月谷电量, 本月峰电量重置,本月谷电量重置
# 上月峰电费,上月谷电费, 本月峰电费重置,本月谷电费重置
- seconds: 00
minutes: 00
hours: 00
days_of_month: 1
then:
- lambda: |-
id(global_last_month_usage) = id(main_energy).state;
id(global_reset_annual_usage) = id(global_reset_annual_usage) + id(global_last_month_usage);
id(last_monthly_usage).publish_state(id(global_last_month_usage));
id(reset_annual_usage).publish_state(id(global_reset_annual_usage));
id(actual_annual_usage).publish_state(id(global_reset_annual_usage));
id(global_last_month_peak_usage) = id(global_month_peak_usage);
id(global_month_peak_usage) = 0.000f;
id(global_last_month_valley_usage) = id(global_month_valley_usage);
id(global_month_valley_usage) = 0.000f;
id(global_last_month_peak_bill) = id(global_month_peak_bill);
id(global_month_peak_bill) = 0.000f;
id(global_last_month_valley_bill) = id(global_month_valley_bill);
id(global_month_valley_bill) = 0.000f;
id(global_last_energy)=0.000f;
- if:
condition:
time.has_time:
then:
- logger.log: "-->publish last monthly electricity."
- pzemac.reset_energy: pzemac_main
- delay: 1s
- pzemac.reset_energy: pzemac_main
- seconds: /5
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
if(id(global_last_energy)==-0.001f) id(global_last_energy) = id(main_energy).state;
# 今日峰电量、峰电费、谷电量、谷电费
- seconds: 59
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
if(id(global_last_energy)==-0.001f) id(global_last_energy) = id(main_energy).state;
id(current_price).update();
float price = id(current_price).state;
if(id(global_is_peak_price)){
id(global_today_peak_usage) = id(global_today_peak_usage) + id(main_energy).state - id(global_last_energy);
id(global_today_peak_bill) = id(global_today_peak_usage) * price;
id(today_peak_usage).publish_state(id(global_today_peak_usage));
id(today_peak_bill).publish_state(id(global_today_peak_bill));
}else{
id(global_today_valley_usage) = id(global_today_valley_usage) + id(main_energy).state - id(global_last_energy);
id(global_today_valley_bill) = id(global_today_valley_usage) * price;
id(today_valley_usage).publish_state(id(global_today_valley_usage));
id(today_valley_bill).publish_state(id(global_today_valley_bill));
}
id(today_bill).publish_state(id(global_today_peak_bill) + id(global_today_valley_bill));
id(global_last_energy) = id(main_energy).state;
|