本帖最后由 dscao 于 2023-8-8 10:08 编辑
原来的电费计算,写着跨阶梯不准的,最好那几个删除不要用了。电费可以用后面出现的相应峰电费和谷电费相加得出结果。最好用实时电价模板利用能源面板统计电费会更好。
sensor:
- platform: template
sensors:
dang_qian_jie_ti:
value_template: >
这种方式目前还是可以用的,但为保证以后长期可用,建议按官方说明改成:
template:
- sensor:
- name: shishidianjia #实时电价
unique_id: shishidianjia
unit_of_measurement: 元/kWh
state: >
2022.5.16
---------------
电表最近几天已经正式安装了,结果还行,但参考了几个帖子:
https://bbs.hassbian.com/thread-8901-1-1.html
得知可以实现峰谷电费的计算
https://bbs.hassbian.com/thread-9305-1-1.html
这个除了峰谷还有阶梯,但跨阶梯时就不准了,特别对于年的累计更是误差太大。
虽然普通家庭节省一点应该都会保持在第1阶梯内,但强迫症还是让我想完善一下这个阶梯及峰谷的算法。
主要在这个基础上进行了修改:
先查了一下当地的电价政策:
平时(当成峰时)8:00至22:00
谷时:22:00至8:00
原理:
峰谷电价分六个:1阶峰电量,1阶谷电量,2阶峰电量,2阶谷电量,3阶峰电量,3阶谷电量
utility_meter:
energy:
source: sensor.electric
cycle: yearly
daily_energy:
source: sensor.energy
cycle: daily
tariffs:
- peak
- offpeak
- peak2l
- offpeak2l
- peak3l
- offpeak3l
monthly_energy:
source: sensor.energy
cycle: monthly
tariffs:
- peak
- offpeak
- peak2l
- offpeak2l
- peak3l
- offpeak3l
yearly_energy:
source: sensor.energy
cycle: yearly
tariffs:
- peak
- offpeak
- peak2l
- offpeak2l
- peak3l
- offpeak3l
当前阶梯代码不变,修改一下电价阶梯值:
- platform: template
sensors:
dang_qian_jie_ti:
value_template: >
{% if states("sensor.energy") | float <= 2160 %}
1
{% elif states("sensor.energy") | float >2160 and states("sensor.energy") | float <= 4200 %}
2
{% elif states("sensor.energy") | float > 4200 %}
3
{% else %}
4
{% endif %}
friendly_name: '当前阶梯'
unit_of_measurement: "L"
自动化要增加一些判断,根据实现情况切换峰谷:
- alias: fengshi_peak
trigger:
- platform: time
at: '08:00:00'
action:
- choose:
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak3l
default:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak
- alias: fengshi_offpeak
trigger:
- platform: time
at: '22:00:00'
action:
- choose:
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak3l
default:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak
- alias: fengshi_jietishengji
trigger:
- platform: state
entity_id: sensor.dang_qian_jie_ti
from: '1'
to: '2'
- platform: state
entity_id: sensor.dang_qian_jie_ti
from: '2'
to: '3'
action:
- choose:
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
- condition: time
after: '08:00:00'
before: '22:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
- condition: time
after: '08:00:00'
before: '22:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak3l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
- condition: time
after: '22:00:00'
before: '08:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
- condition: time
after: '22:00:00'
before: '08:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak3l
default: []
今日、本月、今年峰谷电量、电费:
- platform: template
sensors:
jinri_fen_dianliang:
value_template: >
{{(states("sensor.daily_energy_peak") | float + states("sensor.daily_energy_peak2l") | float + states("sensor.daily_energy_peak3l") | float )| round(2)}}
friendly_name: '今日峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jinri_gu_dianliang:
value_template: >
{{(states("sensor.daily_energy_offpeak") | float + states("sensor.daily_energy_offpeak2l") | float + states("sensor.daily_energy_offpeak3l") | float )| round(2)}}
friendly_name: '今日谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
benyue_fen_dianliang:
value_template: >
{{(states("sensor.monthly_energy_peak") | float + states("sensor.monthly_energy_peak2l") | float + states("sensor.monthly_energy_peak3l") | float )| round(2)}}
friendly_name: '本月峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
benyue_gu_dianliang:
value_template: >
{{(states("sensor.monthly_energy_offpeak") | float + states("sensor.monthly_energy_offpeak2l") | float + states("sensor.monthly_energy_offpeak3l") | float )| round(2)}}
friendly_name: '本月谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jinnian_fen_dianliang:
value_template: >
{{(states("sensor.yearly_energy_peak") | float + states("sensor.yearly_energy_peak2l") | float + states("sensor.yearly_energy_peak3l") | float )| round(2)}}
friendly_name: '今年峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jinnian_gu_dianliang:
value_template: >
{{(states("sensor.yearly_energy_offpeak") | float + states("sensor.yearly_energy_offpeak2l") | float + states("sensor.yearly_energy_offpeak3l") | float )| round(2)}}
friendly_name: '今年谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
#今日峰电费
- platform: template
sensors:
jin_ri_fen_dian_fei:
value_template: >
{{((states("sensor.daily_energy_peak") | float * 0.5953) + (states("sensor.daily_energy_peak2l") | float * 0.6453) + (states("sensor.daily_energy_peak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今日峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今日谷电费
- platform: template
sensors:
jin_ri_gu_dian_fei:
value_template: >
{{((states("sensor.daily_energy_offpeak") | float * 0.5953) + (states("sensor.daily_energy_offpeak2l") | float * 0.6453) + (states("sensor.daily_energy_offpeak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今日谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今日总电费
- platform: template
sensors:
jin_ri_zong_dian_fei:
value_template: >
{{(states("sensor.jin_ri_fen_dian_fei")|float + states("sensor.jin_ri_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '今日总电费'
icon_template: mdi:currency-cny
#本月峰电费
- platform: template
sensors:
ben_yue_fen_dian_fei:
value_template: >
{{((states("sensor.monthly_energy_peak") | float * 0.5953) + (states("sensor.monthly_energy_peak2l") | float * 0.6453) + (states("sensor.monthly_energy_peak2l") | float * 0.8953)) | round(2)}}
friendly_name: '本月峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#本月谷电费
- platform: template
sensors:
ben_yue_gu_dian_fei:
value_template: >
{{((states("sensor.monthly_energy_offpeak") | float * 0.5953) + (states("sensor.monthly_energy_offpeak2l") | float * 0.6453) + (states("sensor.monthly_energy_offpeak2l") | float * 0.8953)) | round(2)}}
friendly_name: '本月谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#本月总电费
- platform: template
sensors:
ben_yue_zong_dian_fei:
value_template: >
{{(states("sensor.ben_yue_fen_dian_fei")|float + states("sensor.ben_yue_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '本月总电费'
icon_template: mdi:currency-cny
#今年峰电费
- platform: template
sensors:
jin_nian_fen_dian_fei:
value_template: >
{{((states("sensor.yearly_energy_peak") | float * 0.5953) + (states("sensor.yearly_energy_peak2l") | float * 0.6453) + (states("sensor.yearly_energy_peak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今年峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今年谷电费
- platform: template
sensors:
jin_nian_gu_dian_fei:
value_template: >
{{((states("sensor.yearly_energy_offpeak") | float * 0.5953) + (states("sensor.yearly_energy_offpeak2l") | float * 0.6453) + (states("sensor.yearly_energy_offpeak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今年谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今年总电费
- platform: template
sensors:
jin_nian_zong_dian_fei:
value_template: >
{{(states("sensor.jin_nian_fen_dian_fei")|float + states("sensor.jin_nian_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '今年总电费'
icon_template: mdi:currency-cny
效果:
昨日数据需要用influxdb,查询某一天中最大值的语句不知怎么写。
电表给塞到电箱中了,看起来整齐多了。
附完整版代码,或放到packages下的dianbiao.yaml中
sensor:
#系统日期和时间
- platform: time_date
display_options:
- 'time'
- 'date'
- 'date_time'
#电表时间传感器
- platform: mqtt
name: "shijian"
state_topic: "tele/electric/SENSOR"
qos: 1
#unit_of_measurement: "V"
value_template: "{{ value_json['Time'] }}"
#电压传感器
- platform: mqtt
name: "dianya"
state_topic: "tele/electric/SENSOR"
qos: 1
unit_of_measurement: "V"
value_template: "{{ value_json['ENERGY'].Voltage }}"
#value_template: "{{ value_json.Voltage }}"
#电流传感器
- platform: mqtt
name: "dianliu"
state_topic: "tele/electric/SENSOR"
qos: 1
unit_of_measurement: "A"
value_template: "{{ value_json['ENERGY'].Current }}"
#value_template: "{{ value_json.Current }}"
#功率传感器
- platform: mqtt
name: "gonglv"
state_topic: "tele/electric/SENSOR"
qos: 1
unit_of_measurement: "W"
value_template: "{{ value_json['ENERGY'].Power }}"
#value_template: "{{ value_json.Power }}"
#功率因数传感器
- platform: mqtt
name: "gonglvyinshu"
state_topic: "tele/electric/SENSOR"
qos: 1
unit_of_measurement: "cosΦ"
value_template: "{{ value_json['ENERGY'].Factor }}"
#value_template: "{{ value_json.Factor }}"
#今日电量
- platform: mqtt
name: "jinri_dianliang"
state_topic: "tele/electric/SENSOR"
qos: 1
unit_of_measurement: "kWh"
value_template: "{{ value_json['ENERGY'].Today }}"
#value_template: "{{ value_json.Today }}"
#今日电费(不执行峰谷(跨阶梯时不准))
- platform: template
sensors:
jinri_dianfei:
value_template: >
{% if now().month in [1,3,5,7,8,10,12] %}
{% set date = (31 - now().day) %}
{% elif now().month in [4,6,9,11] %}
{% set date = (30 - now().day) %}
{% elif now().month == 2 and ((now().year-2000) % 4 > 0) %}
{% set date = (28 - now().day) %}
{% elif now().month == 2 and ((now().year-2000) % 4 == 0) %}
{% set date = (29 - now().day) %}
{% endif %}
{% if (date != 0 and (states("sensor.electric") | float - states("sensor.jinri_dianliang") | float + (states("sensor.zuori_dianliang") | float * (date + 1))) | int > 4200) or (date == 0 and states("sensor.electric") | int > 4200) %}
{{(states("sensor.jinri_dianliang") | float * 0.8653) | round(2)}}
{% elif (date != 0 and (states("sensor.electric") | float - states("sensor.jinri_dianliang") | float + (states("sensor.zuori_dianliang") | float * (date + 1))) | int > 2160) or (date == 0 and states("sensor.electric") | int > 2160) %}
{{(states("sensor.jinri_dianliang") | float * 0.6153) | round(2)}}
{% else %}
{{(states("sensor.jinri_dianliang") | float * 0.5653) | round(2)}}
{% endif %}
unit_of_measurement: "RMB"
#昨天用电量
- platform: mqtt
name: "zuori_dianliang"
state_topic: "tele/electric/SENSOR"
qos: 1
unit_of_measurement: "kWh"
value_template: "{{ value_json['ENERGY'].Yesterday }}"
#value_template: "{{ value_json.Yesterday }}"
#昨日电费(不执行峰谷(跨阶梯时不准))
- platform: template
sensors:
zuori_dianfei:
value_template: >
{% if now().month in [1,3,5,7,8,10,12] %}
{% set date = (31 - now().day) %}
{% elif now().month in [4,6,9,11] %}
{% set date = (30 - now().day) %}
{% elif now().month == 2 and ((now().year-2000) % 4 > 0) %}
{% set date = (28 - now().day) %}
{% elif now().month == 2 and ((now().year-2000) % 4 == 0) %}
{% set date = (29 - now().day) %}
{% endif %}
{% if (date != 0 and (states("sensor.electric") | float - states("sensor.jinri_dianliang") | float + (states("sensor.zuori_dianliang") | float * (date + 1))) | int > 4200) or (date == 0 and states("sensor.electric") | int > 4200) %}
{{(states("sensor.zuori_dianliang") | float * 0.8653) | round(2)}}
{% elif (date != 0 and (states("sensor.electric") | float - states("sensor.jinri_dianliang") | float + (states("sensor.zuori_dianliang") | float * (date + 1))) | int > 2160) or (date == 0 and states("sensor.electric") | int > 2160) %}
{{(states("sensor.zuori_dianliang") | float * 0.6153) | round(2)}}
{% else %}
{{(states("sensor.zuori_dianliang") | float * 0.5653) | round(2)}}
{% endif %}
unit_of_measurement: "RMB"
#总用电量
- platform: mqtt
name: "electric"
state_topic: "tele/electric/SENSOR"
qos: 1
unit_of_measurement: "kWh"
value_template: "{{ value_json['ENERGY'].Total }}"
#value_template: "{{ value_json.Total }}"
#总用电量统计开始时间
- platform: mqtt
name: "totalstarttime"
state_topic: "tele/electric/SENSOR"
qos: 1
value_template: "{{ value_json['ENERGY'].TotalStartTime }}"
#value_template: "{{ value_json.Total }}"
#总电费(不执行峰谷)
- platform: template
sensors:
zongdianfei:
value_template: >
{% if (states("sensor.electric") | int > 4200) %}
{{(((states("sensor.electric")-4200) | float * 0.8653) + (4200-2160) * 0.6153 + 2160 * 0.5653) | round(2)}}
{% elif (states("sensor.electric") | int > 2160) %}
{{(((states("sensor.electric")-2160) | float * 0.6153) + 2160 * 0.5653) | round(2)}}
{% else %}
{{(states("sensor.electric") | float * 0.5653) | round(2)}}
{% endif %}
unit_of_measurement: "RMB"
- platform: template
sensors:
dang_qian_jie_ti:
value_template: >
{% if states("sensor.energy") | float <= 2160 %}
1
{% elif states("sensor.energy") | float >2160 and states("sensor.energy") | float <= 4200 %}
2
{% elif states("sensor.energy") | float > 4200 %}
3
{% else %}
4
{% endif %}
friendly_name: '当前阶梯'
unit_of_measurement: "L"
- platform: template
sensors:
jinri_fen_dianliang:
value_template: >
{{(states("sensor.daily_energy_peak") | float + states("sensor.daily_energy_peak2l") | float + states("sensor.daily_energy_peak3l") | float )| round(2)}}
friendly_name: '今日峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jinri_gu_dianliang:
value_template: >
{{(states("sensor.daily_energy_offpeak") | float + states("sensor.daily_energy_offpeak2l") | float + states("sensor.daily_energy_offpeak3l") | float )| round(2)}}
friendly_name: '今日谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
benyue_fen_dianliang:
value_template: >
{{(states("sensor.monthly_energy_peak") | float + states("sensor.monthly_energy_peak2l") | float + states("sensor.monthly_energy_peak3l") | float )| round(2)}}
friendly_name: '本月峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
benyue_gu_dianliang:
value_template: >
{{(states("sensor.monthly_energy_offpeak") | float + states("sensor.monthly_energy_offpeak2l") | float + states("sensor.monthly_energy_offpeak3l") | float )| round(2)}}
friendly_name: '本月谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jinnian_fen_dianliang:
value_template: >
{{(states("sensor.yearly_energy_peak") | float + states("sensor.yearly_energy_peak2l") | float + states("sensor.yearly_energy_peak3l") | float )| round(2)}}
friendly_name: '今年峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jinnian_gu_dianliang:
value_template: >
{{(states("sensor.yearly_energy_offpeak") | float + states("sensor.yearly_energy_offpeak2l") | float + states("sensor.yearly_energy_offpeak3l") | float )| round(2)}}
friendly_name: '今年谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
#今日峰电费
- platform: template
sensors:
jin_ri_fen_dian_fei:
value_template: >
{{((states("sensor.daily_energy_peak") | float * 0.5953) + (states("sensor.daily_energy_peak2l") | float * 0.6453) + (states("sensor.daily_energy_peak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今日峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今日谷电费
- platform: template
sensors:
jin_ri_gu_dian_fei:
value_template: >
{{((states("sensor.daily_energy_offpeak") | float * 0.5953) + (states("sensor.daily_energy_offpeak2l") | float * 0.6453) + (states("sensor.daily_energy_offpeak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今日谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今日总电费
- platform: template
sensors:
jin_ri_zong_dian_fei:
value_template: >
{{(states("sensor.jin_ri_fen_dian_fei")|float + states("sensor.jin_ri_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '今日总电费'
icon_template: mdi:currency-cny
#本月峰电费
- platform: template
sensors:
ben_yue_fen_dian_fei:
value_template: >
{{((states("sensor.monthly_energy_peak") | float * 0.5953) + (states("sensor.monthly_energy_peak2l") | float * 0.6453) + (states("sensor.monthly_energy_peak2l") | float * 0.8953)) | round(2)}}
friendly_name: '本月峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#本月谷电费
- platform: template
sensors:
ben_yue_gu_dian_fei:
value_template: >
{{((states("sensor.monthly_energy_offpeak") | float * 0.5953) + (states("sensor.monthly_energy_offpeak2l") | float * 0.6453) + (states("sensor.monthly_energy_offpeak2l") | float * 0.8953)) | round(2)}}
friendly_name: '本月谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#本月总电费
- platform: template
sensors:
ben_yue_zong_dian_fei:
value_template: >
{{(states("sensor.ben_yue_fen_dian_fei")|float + states("sensor.ben_yue_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '本月总电费'
icon_template: mdi:currency-cny
#今年峰电费
- platform: template
sensors:
jin_nian_fen_dian_fei:
value_template: >
{{((states("sensor.yearly_energy_peak") | float * 0.5953) + (states("sensor.yearly_energy_peak2l") | float * 0.6453) + (states("sensor.yearly_energy_peak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今年峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今年谷电费
- platform: template
sensors:
jin_nian_gu_dian_fei:
value_template: >
{{((states("sensor.yearly_energy_offpeak") | float * 0.5953) + (states("sensor.yearly_energy_offpeak2l") | float * 0.6453) + (states("sensor.yearly_energy_offpeak2l") | float * 0.8953)) | round(2)}}
friendly_name: '今年谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
#今年总电费
- platform: template
sensors:
jin_nian_zong_dian_fei:
value_template: >
{{(states("sensor.jin_nian_fen_dian_fei")|float + states("sensor.jin_nian_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '今年总电费'
icon_template: mdi:currency-cny
utility_meter:
energy:
source: sensor.electric
cycle: yearly
daily_energy:
source: sensor.energy
cycle: daily
tariffs:
- peak
- offpeak
- peak2l
- offpeak2l
- peak3l
- offpeak3l
monthly_energy:
source: sensor.energy
cycle: monthly
tariffs:
- peak
- offpeak
- peak2l
- offpeak2l
- peak3l
- offpeak3l
yearly_energy:
source: sensor.energy
cycle: yearly
tariffs:
- peak
- offpeak
- peak2l
- offpeak2l
- peak3l
- offpeak3l
input_boolean:
gong_lv_da:
initial: off
automation:
#元旦清零电表
- id: 'yuan_dan_qing_ling_dian_biao'
alias: 元旦清零电表
trigger:
- platform: template
value_template: '{{ states("sensor.jinri_dianliang") | round(3) == 0.000 }}'
condition:
- condition: template
value_template: '{{ now().strftime("%m-%d") == "01-01" }}'
action:
- data:
payload: '0'
topic: cmnd/electric/EnergyReset2
service: mqtt.publish
- data:
payload: '0'
topic: cmnd/electric/EnergyReset3
service: mqtt.publish
initial_state: false
- alias: fengshi_peak
trigger:
- platform: time
at: '08:00:00'
action:
- choose:
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak3l
default:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak
- alias: fengshi_offpeak
trigger:
- platform: time
at: '22:00:00'
action:
- choose:
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak3l
default:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak
- alias: fengshi_jietishengji
trigger:
- platform: state
entity_id: sensor.dang_qian_jie_ti
from: '1'
to: '2'
- platform: state
entity_id: sensor.dang_qian_jie_ti
from: '2'
to: '3'
action:
- choose:
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
- condition: time
after: '08:00:00'
before: '22:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
- condition: time
after: '08:00:00'
before: '22:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: peak3l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '2'
- condition: time
after: '22:00:00'
before: '08:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak2l
- conditions:
- condition: state
entity_id: sensor.dang_qian_jie_ti
state: '3'
- condition: time
after: '22:00:00'
before: '08:00:00'
sequence:
- service: utility_meter.select_tariff
data:
entity_id:
- utility_meter.daily_energy
- utility_meter.monthly_energy
- utility_meter.yearly_energy
tariff: offpeak3l
default: []
group:
electric:
name: 电力监测
entities:
- sensor.date_time
- sensor.shijian
- sensor.dianya
- sensor.dianliu
- sensor.gonglv
- sensor.gonglvyinshu
- sensor.jinri_dianliang
- sensor.jinri_dianfei
- sensor.zuori_dianliang
- sensor.zuori_dianfei
- sensor.electric
- sensor.zongdianfei
- sensor.dang_qian_jie_ti
- sensor.jin_ri_fen_dian_fei
- sensor.jin_ri_gu_dian_fei
- sensor.jin_ri_zong_dian_fei
- automation.yue_chu_qing_ling_dian_biao
- automation.yuan_dan_qing_ling_dian_biao
homeassistant:
customize:
sensor.date_time:
friendly_name: 系统时间
sensor.shijian:
friendly_name: 电表时间
icon: mdi:timer
sensor.dianya:
friendly_name: 实时电压
icon: mdi:flash
sensor.dianliu:
friendly_name: 实时电流
icon: mdi:flash-auto
sensor.gonglv:
friendly_name: 实时功率
#icon: mdi:wikipedia
icon: mdi:speedometer
sensor.gonglvyinshu:
friendly_name: 功率因数
icon: mdi:flash-red-eye
sensor.jinri_dianliang:
friendly_name: 今日电量
icon: mdi:finance
#icon: 'mdi:chart-bar-stacked'
sensor.jinri_dianfei:
friendly_name: 今日电费(不执行峰谷)
icon: mdi:currency-cny
sensor.zuori_dianliang:
friendly_name: 昨日电量
icon: mdi:finance
#icon: 'mdi:chart-bar-stacked'
sensor.zuori_dianfei:
friendly_name: 昨日电费(不执行峰谷)
icon: mdi:currency-cny
sensor.electric:
friendly_name: 总用电量
#icon: mdi:finance
icon: 'mdi:chart-bar-stacked'
sensor.zongdianfei:
friendly_name: 总电费(不执行峰谷)
icon: mdi:currency-cny
sensor.totalstarttime:
friendly_name: 统计开始时间
icon: mdi:timer
sensor.zuori_dianliang:
friendly_name: 昨日电量
icon: mdi:finance
刚入坑不久,还有太多东西要学习,错漏之处肯定不少,希望大家能继续改进。
-------20200825------
补充一个用influxdb实现昨日峰谷电量、上月峰谷电量,其它的可以参考扩展。刚测试能显示出来,至于正确与否还要再作测试。
#influxdb数据库中取值
- platform: influxdb
queries:
- name: zuori_fen_dianliang
unit_of_measurement: 'kWh'
value_template: '{{ value | round(3) }}'
group_function: last
where: '"entity_id" = ''jinri_fen_dianliang'' and "time" < ''{{ states("sensor.date") }}'' tz(''Asia/Shanghai'')'
unit_of_measurement: 'kWh'
measurement: '"kWh"'
- name: zuori_gu_dianliang
unit_of_measurement: 'kWh'
value_template: '{{ value | round(3) }}'
group_function: last
where: '"entity_id" = ''jinri_gu_dianliang'' and "time" < ''{{ states("sensor.date") }}'' tz(''Asia/Shanghai'')'
unit_of_measurement: 'kWh'
measurement: '"kWh"'
- name: shanyue_fen_dianliang
unit_of_measurement: 'kWh'
value_template: '{{ value | round(3) }}'
group_function: last
where: >
{% if now().month in [10,11,12] %}
'"entity_id" = ''benyue_fen_dianliang'' and "time" < ''{{ now().year }}-{{ now().month }}-01'' tz(''Asia/Shanghai'')'
{% else %}
'"entity_id" = ''benyue_fen_dianliang'' and "time" < ''{{ now().year }}-0{{ now().month }}-01'' tz(''Asia/Shanghai'')'
{% endif %}
unit_of_measurement: 'kWh'
measurement: '"kWh"'
- name: shanyue_gu_dianliang
unit_of_measurement: 'kWh'
value_template: '{{ value | round(3) }}'
group_function: last
where: >
{% if now().month in [10,11,12] %}
'"entity_id" = ''benyue_gu_dianliang'' and "time" < ''{{ now().year }}-{{ now().month }}-01'' tz(''Asia/Shanghai'')'
{% else %}
'"entity_id" = ''benyue_gu_dianliang'' and "time" < ''{{ now().year }}-0{{ now().month }}-01'' tz(''Asia/Shanghai'')'
{% endif %}
unit_of_measurement: 'kWh'
measurement: '"kWh"'
sensor.zuori_dianliang:
friendly_name: 昨日电量
icon: mdi:finance
sensor.zuori_fen_dianliang:
friendly_name: 昨日峰电量
icon: mdi:finance
sensor.zuori_gu_dianliang:
friendly_name: 昨日谷电量
icon: mdi:finance
sensor.shanyue_fen_dianliang:
friendly_name: 上月峰电量
icon: mdi:finance
sensor.shanyue_gu_dianliang:
friendly_name: 上月谷电量
icon: mdi:finance
influxdb:
host: 192.168.X.X #自己数据库IP
port: 8086
database: home_assistant
username: 自己的用户名
password: 自己的密码
max_retries: 3
default_measurement: state
include: #需要传给Influxdb的重要数据实体贴进去,不重要的不传
entities:
- sensor.gonglv
- sensor.electric
- sensor.jinri_fen_dianliang
- sensor.jinri_gu_dianliang
- sensor.benyue_fen_dianliang
- sensor.benyue_gu_dianliang
|