一直苦于国网的不稳定性,又想对家里电量电费进行一个统计,于是终于在上个月跟着论坛教程装上了培正!
装了之后问题又来了,由于湖南电价方案与论坛之前的都不一样,没有峰谷的阶梯电价,而且只是根据春夏季节简单地调整了阶梯,所以要对esphome代码进行调整!
参考了jjcs和hzcoolwind两位前辈的方案,感谢两位大佬的付出!
【终极版】培正Esphome电量统计,谷峰计算,阶梯电价
https://bbs.hassbian.com/thread-24441-1-1.html
带实时年度阶梯电价和峰谷电统计的培正ESPHOME
https://bbs.hassbian.com/thread-24535-1-1.html
同时借助chatgpt,对论坛之前的代码进行了一些微调,最终形成了湖南电价版本!
由于湖南电价阶梯是按月电量计算的,所以在原来基础上增加了月度电量重置,同时增加了年度电费和月度电费重置,以便中途修正数据!
目前用下来发现电量与国网数据差0.2~0.3的样子,感觉还可以!
但是也存在一个问题,就是每隔一段时间设备会变成不再可用,然后又自动恢复,由于没有影响到统计我也就没管它了,希望有知道的大佬指点一下应该怎么调整,感谢!
substitutions:
name: power_meter
friendly_name: Power Meter
esphome:
name: ${name}
friendly_name: ${friendly_name}
name_add_mac_suffix: false
project:
name: hzcoolwind.power-meter
version: "1.1.0"
esp32:
board: esp32dev
framework:
type: esp-idf
api:
ota:
password: "12345678"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Bluetoothproxy Fallback Hotspot"
password: !secret ap_password
web_server:
port: 80
logger:
improv_serial:
uart:
- id: main_uart
rx_pin:
number: GPIO16
inverted: false
mode:
input: true
pullup: true
tx_pin:
number: GPIO17
inverted: false
mode:
output: true
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: Reset Memory
id: reset_memory
entity_category: diagnostic
on_press:
- lambda: |-
id(global_today_usage) = 0.000f;
id(global_today_bill) = 0.000f;
id(global_yesterday_usage) = 0.000f;
id(global_yesterday_bill) = 0.000f;
id(global_month_usage) = 0.000f;
id(global_month_bill) = 0.000f;
id(global_annual_usage) = 0.000f;
id(global_annual_bill) = 0.000f;
id(global_yesterday_energy) = 0.000f;
- platform: safe_mode
name: Safe Mode Boot
entity_category: diagnostic
- platform: restart
name: Restart
entity_category: diagnostic
globals:
# flash保存昨日抄表数(每天0点更新保存)
- id: global_yesterday_energy
type: float
restore_value: yes
initial_value: '0.000'
# flash保存今日电量电费
- id: global_today_usage
type: float
restore_value: yes
initial_value: '0.000'
- id: global_today_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash昨日用电量电费(每天0点更新保存)
- id: global_yesterday_usage
type: float
restore_value: yes
initial_value: '0.000'
- id: global_yesterday_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存本月电量电费(不含当天)
- id: global_month_usage
type: float
restore_value: yes
initial_value: '0.000'
- id: global_month_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存年度用电量电费(不含当月)
- id: global_annual_usage
type: float
restore_value: yes
initial_value: '0.000'
- id: global_annual_bill
type: float
restore_value: yes
initial_value: '0.000'
# flash保存上月电量电费(1号0点更新保存)
- id: global_last_month_usage
type: float
restore_value: yes
initial_value: '0.000'
- id: global_last_month_bill
type: float
restore_value: yes
initial_value: '0.000'
# 全局变量:当前阶梯档位(0,1,2)flash不保存,计算所得
- id: global_step_id
type: int
restore_value: false
initial_value: '0'
number:
# 复位年度电量电费,因为国网只能查询上月以前的数据,加上表上读数为实际当年使用度数
- 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: 0.01
restore_value: false
unit_of_measurement: kwh
on_value:
then:
- lambda: |-
id(global_annual_usage) = x;
id(annual_usage).publish_state( id(global_annual_usage) + id(month_usage).state);
- platform: template
id: set_reset_annual_bill
name: Set Reset Annual bill
mode: box
min_value: 0
max_value: 9999
initial_value: 0
optimistic: true
step: 0.01
restore_value: false
unit_of_measurement: CNY
on_value:
then:
- lambda: |-
id(global_annual_bill) = x;
id(annual_bill).publish_state( id(global_annual_bill) + id(month_bill).state);
# 复位当月电量电费,因为国网只能查询昨天以前的数据,加上昨天以及今天的数据才是当月当前的数据
- platform: template
id: set_reset_month_usage
name: Set Reset Month Usage
mode: box
min_value: 0
max_value: 9999
initial_value: 0
optimistic: true
step: 0.01
restore_value: false
unit_of_measurement: kwh
on_value:
then:
- lambda: |-
id(global_month_usage) = x ;
id(month_usage).publish_state( id(global_month_usage) + id(global_today_usage) );
id(current_price).update();
- platform: template
id: set_reset_month_bill
name: Set Reset Month bill
mode: box
min_value: 0
max_value: 9999
initial_value: 0
optimistic: true
step: 0.01
restore_value: false
unit_of_measurement: CNY
on_value:
then:
- lambda: |-
id(global_month_bill) = x ;
id(month_bill).publish_state( id(global_month_bill) + id(global_today_bill) );
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:
name: "Main Energy"
id: main_energy
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
accuracy_decimals: 3
filters:
- multiply: 0.001
# 昨日抄表数
- 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: 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(global_today_usage);
- platform: template
name: Today_bill
id: today_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 10s
lambda:
return id(global_today_bill);
# 昨日用电量、电费
- 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: 60s
lambda:
return id(global_yesterday_usage);
- platform: template
name: Yesterday_bill
id: yesterday_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 60s
lambda:
return id(global_yesterday_bill);
# 当月用电量、电费
- platform: template
name: Month Usage
id: month_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_usage) + id(today_usage).state;
- platform: template
name: Month_bill
id: month_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_month_bill) + id(today_bill).state;
# 上月用电量、电费
- platform: template
name: Last Month Usage
id: last_month_usage
accuracy_decimals: 3
device_class: "energy"
state_class: "total_increasing"
icon: mdi:lightning-bolt
unit_of_measurement: kWh
update_interval: 60s
lambda:
return id(global_last_month_usage);
- platform: template
name: Last Month bill
id: last_month_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 60s
lambda:
return id(global_last_month_bill);
# 年度用电量、电费
- platform: template
name: Annual Usage
id: 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_annual_usage) + id(month_usage).state;
- platform: template
name: Annual Bill
id: annual_bill
accuracy_decimals: 3
unit_of_measurement: CNY
update_interval: 15s
lambda:
return id(global_annual_bill) + id(month_bill).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[] = {0.588, 0.638, 0.888};
int total_usage = id(month_usage).state;
int current_month = 1;
current_month = parse_number<int>(id(time_sntp).now().strftime("%m")).value();
if (total_usage <= 200) {
step_id = 0;
} else {
if ((current_month >= 3 && current_month <= 5) || (current_month >= 9 && current_month <= 11)) {
if (total_usage <= 350) {
step_id = 1;
} else {
step_id = 2;
}
} else {
if (total_usage <= 450) {
step_id = 1;
} else {
step_id = 2;
}
}
}
id(global_step_id) = step_id;
current_price = all_prices[step_id];
return current_price;
- platform: wifi_signal
name: "WiFi Signal Sensor"
update_interval: 60s
text_sensor:
- platform: wifi_info
ip_address:
name: IP Address
icon: "mdi:ip-network"
time:
- platform: homeassistant
timezone: UTC-8
id: time_ha
- platform: sntp
id: time_sntp
servers: ntp.aliyun.com
timezone: UTC-8
on_time:
# 每隔1分钟更新保存今日电量电费
- seconds: 59
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
id(current_price).update();
float price = id(current_price).state;
id(global_today_bill) = id(global_today_bill) + (id(main_energy).state - id(global_yesterday_energy)-id(global_today_usage)) * price;
id(global_today_usage) = id(main_energy).state - id(global_yesterday_energy);
# 每天0点更新保存当月电量电费、昨日电量电费以及昨日抄表数
- seconds: 0
minutes: 0
hours: 0
then:
- if:
condition:
time.has_time:
then:
- lambda: |-
id(global_month_usage) = id(global_month_usage) + id(global_today_usage);
id(global_month_bill) = id(global_month_bill) + id(global_today_bill);
id(global_yesterday_usage) = id(global_today_usage);
id(global_today_usage) = 0.000f;
id(global_yesterday_bill) = id(global_today_bill);
id(global_today_bill) = 0.000f;
id(global_yesterday_energy) = id(main_energy).state;
# 每月1日0点更新保存年度电量电费、上月电量电费, 并重置电表电量
- seconds: 00
minutes: 00
hours: 00
days_of_month: 1
then:
- lambda: |-
id(global_annual_usage) = id(global_annual_usage) + id(global_month_usage);
id(global_annual_bill) = id(global_annual_bill) + id(global_month_bill);
id(global_last_month_usage) = id(global_month_usage);
id(global_month_usage) = 0.000f;
id(global_last_month_bill) = id(global_month_bill);
id(global_month_bill) = 0.000f;
id(global_yesterday_energy) = 0.000f;
- if:
condition:
time.has_time:
then:
- logger.log: "-->publish last month electricity."
- pzemac.reset_energy: pzemac_main
- delay: 1s
- pzemac.reset_energy: pzemac_main
HNpower.zip
(2.82 KB, 下载次数: 12)
|