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:
jin_ri_fen_dianliang:
value_template: >
{{(states("sensor.daily_energy_peak") | float + states("sensor.daily_energy_peak2l") | float + states("sensor.daily_energy_peak3l") | float )| round(3)}}
friendly_name: '今日峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jin_ri_gu_dianliang:
value_template: >
{{(states("sensor.daily_energy_offpeak") | float + states("sensor.daily_energy_offpeak2l") | float + states("sensor.daily_energy_offpeak3l") | float )| round(3)}}
friendly_name: '今日谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
ben_yue_fen_dianliang:
value_template: >
{{(states("sensor.monthly_energy_peak") | float + states("sensor.monthly_energy_peak2l") | float + states("sensor.monthly_energy_peak3l") | float )| round(3)}}
friendly_name: '本月峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
ben_yue_gu_dianliang:
value_template: >
{{(states("sensor.monthly_energy_offpeak") | float + states("sensor.monthly_energy_offpeak2l") | float + states("sensor.monthly_energy_offpeak3l") | float )| round(3)}}
friendly_name: '本月谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jin_nian_fen_dianliang:
value_template: >
{{(states("sensor.yearly_energy_peak") | float + states("sensor.yearly_energy_peak2l") | float + states("sensor.yearly_energy_peak3l") | float )| round(3)}}
friendly_name: '今年峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
jin_nian_gu_dianliang:
value_template: >
{{(states("sensor.yearly_energy_offpeak") | float + states("sensor.yearly_energy_offpeak2l") | float + states("sensor.yearly_energy_offpeak3l") | float )| round(3)}}
friendly_name: '今年谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
zuo_ri_fen_dianliang:
value_template: >
{{(state_attr("sensor.daily_energy_peak","last_period") | float + state_attr("sensor.daily_energy_peak2l","last_period") | float + state_attr("sensor.daily_energy_peak3l","last_period") | float )| round(3)}}
friendly_name: '昨日峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
zuo_ri_gu_dianliang:
value_template: >
{{(state_attr("sensor.daily_energy_offpeak","last_period") | float + state_attr("sensor.daily_energy_offpeak2l","last_period") | float + state_attr("sensor.daily_energy_offpeak3l","last_period") | float )| round(3)}}
friendly_name: '昨日谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
shan_yue_fen_dianliang:
value_template: >
{{(state_attr("sensor.monthly_energy_peak","last_period") | float + state_attr("sensor.monthly_energy_peak2l","last_period") | float + state_attr("sensor.monthly_energy_peak3l","last_period") | float )| round(3)}}
friendly_name: '上月峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
shan_yue_gu_dianliang:
value_template: >
{{(state_attr("sensor.monthly_energy_offpeak","last_period") | float + state_attr("sensor.monthly_energy_offpeak2l","last_period") | float + state_attr("sensor.monthly_energy_offpeak3l","last_period") | float )| round(3)}}
friendly_name: '上月谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
qu_nian_fen_dianliang:
value_template: >
{{(state_attr("sensor.yearly_energy_peak","last_period") | float + state_attr("sensor.yearly_energy_peak2l","last_period") | float + state_attr("sensor.yearly_energy_peak3l","last_period") | float )| round(3)}}
friendly_name: '去年峰电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
qu_nian_gu_dianliang:
value_template: >
{{(state_attr("sensor.yearly_energy_offpeak","last_period") | float + state_attr("sensor.yearly_energy_offpeak2l","last_period") | float + state_attr("sensor.yearly_energy_offpeak3l","last_period") | float )| round(3)}}
friendly_name: '去年谷电量'
unit_of_measurement: "kWh"
icon_template: mdi:finance
- platform: template
sensors:
zuo_ri_fen_dian_fei:
value_template: >
{{(state_attr("sensor.daily_energy_peak","last_period") | float * 0.5953 + state_attr("sensor.daily_energy_peak2l","last_period") | float * 0.6453 + state_attr("sensor.daily_energy_peak3l","last_period") | float * 0.8953 )| round(2)}}
friendly_name: '昨日峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
- platform: template
sensors:
zuo_ri_gu_dian_fei:
value_template: >
{{(state_attr("sensor.daily_energy_offpeak","last_period") | float * 0.3153 + state_attr("sensor.daily_energy_offpeak2l","last_period") | float * 0.3653 + state_attr("sensor.daily_energy_offpeak3l","last_period") | float * 0.6153 )| round(2)}}
friendly_name: '昨日谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
- platform: template
sensors:
zuo_ri_zong_dian_fei:
value_template: >
{{(states("sensor.zuo_ri_fen_dian_fei")|float + states("sensor.zuo_ri_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '昨日总电费'
icon_template: mdi:currency-cny
- platform: template
sensors:
shan_yue_fen_dian_fei:
value_template: >
{{(state_attr("sensor.monthly_energy_peak","last_period") | float * 0.5953 + state_attr("sensor.monthly_energy_peak2l","last_period") | float * 0.6453 + state_attr("sensor.monthly_energy_peak3l","last_period") | float * 0.8953 )| round(2)}}
friendly_name: '上月峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
- platform: template
sensors:
shan_yue_gu_dian_fei:
value_template: >
{{(state_attr("sensor.monthly_energy_offpeak","last_period") | float * 0.3153 + state_attr("sensor.monthly_energy_offpeak2l","last_period") | float * 0.3653 + state_attr("sensor.monthly_energy_offpeak3l","last_period") | float * 0.6153 )| round(2)}}
friendly_name: '上月谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
- platform: template
sensors:
shan_yue_zong_dian_fei:
value_template: >
{{(states("sensor.shan_yue_fen_dian_fei")|float + states("sensor.shan_yue_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '上月总电费'
icon_template: mdi:currency-cny
- platform: template
sensors:
qu_nian_fen_dian_fei:
value_template: >
{{(state_attr("sensor.yearly_energy_peak","last_period") | float * 0.5953 + state_attr("sensor.yearly_energy_peak2l","last_period") | float * 0.6453 + state_attr("sensor.yearly_energy_peak3l","last_period") | float * 0.8953 )| round(2)}}
friendly_name: '去年峰电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
- platform: template
sensors:
qu_nian_gu_dian_fei:
value_template: >
{{(state_attr("sensor.yearly_energy_offpeak","last_period") | float * 0.3153 + state_attr("sensor.yearly_energy_offpeak2l","last_period") | float * 0.3653 + state_attr("sensor.yearly_energy_offpeak3l","last_period") | float * 0.6153 )| round(2)}}
friendly_name: '去年谷电费'
unit_of_measurement: "RMB"
icon_template: mdi:currency-cny
- platform: template
sensors:
qu_nian_zong_dian_fei:
value_template: >
{{(states("sensor.qu_nian_fen_dian_fei")|float + states("sensor.qu_nian_gu_dian_fei")|float) | round(2)}}
unit_of_measurement: "RMB"
friendly_name: '去年总电费'
icon_template: mdi:currency-cny
#今日峰电费
- 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.3153) + (states("sensor.daily_energy_offpeak2l") | float * 0.3653) + (states("sensor.daily_energy_offpeak2l") | float * 0.6153)) | 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.3153) + (states("sensor.monthly_energy_offpeak2l") | float * 0.3653) + (states("sensor.monthly_energy_offpeak2l") | float * 0.6153)) | 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.3153) + (states("sensor.yearly_energy_offpeak2l") | float * 0.3653) + (states("sensor.yearly_energy_offpeak2l") | float * 0.6153)) | 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
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: []
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
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