『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 82400|回复: 54

[经验分享] 电表接入后(阶梯+峰谷)电费算法的改进

  [复制链接]

77

主题

1391

帖子

2万

积分

元老级技术达人

积分
24293
金钱
22862
HASS币
290
发表于 2020-8-23 13:38:05 | 显示全部楼层 |阅读模式
本帖最后由 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阶梯内,但强迫症还是让我想完善一下这个阶梯及峰谷的算法。
主要在这个基础上进行了修改:

先查了一下当地的电价政策:
45.JPG
平时(当成峰时)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


49.jpg

效果:
50.PNG

51.JPEG

昨日数据需要用influxdb,查询某一天中最大值的语句不知怎么写。

电表给塞到电箱中了,看起来整齐多了。
48.jpg

41.JPG

42.JPG


附完整版代码,或放到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





评分

参与人数 6金钱 +87 HASS币 +30 收起 理由
sorrypqa + 5 感谢楼主分享!
令狐鸣 + 10 这个完美了,大赞
咸味土豆 + 12 膜拜大神!
Roger + 20 + 20 墙都不扶,就服楼主!必须顶你!!!.
+ 20 + 10 厉害了word楼主!
jyz_0501 + 20 光看这么多接入数据 就知道下了功夫了,不.

查看全部评分

回复

使用道具 举报

7

主题

143

帖子

1707

积分

金牌会员

Rank: 6Rank: 6

积分
1707
金钱
1564
HASS币
0
发表于 2020-8-23 14:35:21 | 显示全部楼层
还可以这么玩,学习了
回复

使用道具 举报

17

主题

184

帖子

1609

积分

金牌会员

Rank: 6Rank: 6

积分
1609
金钱
1425
HASS币
0
发表于 2020-8-23 16:05:29 | 显示全部楼层
你好,我们这里的电价貌似也是白天晚上不同价,可以使用你分享的代码吗
回复

使用道具 举报

75

主题

1976

帖子

8179

积分

元老级技术达人

积分
8179
金钱
6153
HASS币
430

活跃会员教程狂人

发表于 2020-8-23 18:19:50 | 显示全部楼层
楼主用心了,点赞。
所有过往,皆为序章。
回复

使用道具 举报

1

主题

70

帖子

589

积分

高级会员

Rank: 4

积分
589
金钱
519
HASS币
0
发表于 2020-8-24 14:29:21 | 显示全部楼层
这是新家嘛,用电量这么小,我怎么出差每天都要2kwh
回复

使用道具 举报

9

主题

787

帖子

3858

积分

论坛元老

Rank: 8Rank: 8

积分
3858
金钱
3071
HASS币
87
发表于 2020-8-24 15:18:59 | 显示全部楼层
借楼咨询下楼主,你的PZEM统计总电量准吗?
我的目前接入,看着实时用电都挺准.但是每天统计用电量感觉少好多.
基本上实际用电10度左右的话,这个才显示3度左右.
不知道是不还需要设置什么
回复

使用道具 举报

77

主题

1391

帖子

2万

积分

元老级技术达人

积分
24293
金钱
22862
HASS币
290
 楼主| 发表于 2020-8-25 00:24:44 | 显示全部楼层
令狐鸣 发表于 2020-8-24 15:18
借楼咨询下楼主,你的PZEM统计总电量准吗?
我的目前接入,看着实时用电都挺准.但是每天统计用电量感觉少好多. ...

也发现了这个情况了,可能是utility_meter或者更新间隔导致的误差,要再慢慢研究研究。
回复

使用道具 举报

77

主题

1391

帖子

2万

积分

元老级技术达人

积分
24293
金钱
22862
HASS币
290
 楼主| 发表于 2020-8-25 00:25:56 | 显示全部楼层
hang1993 发表于 2020-8-24 14:29
这是新家嘛,用电量这么小,我怎么出差每天都要2kwh

是的,新家,还在装修,暂时就保持了网络及这几台小设备供电。
回复

使用道具 举报

9

主题

787

帖子

3858

积分

论坛元老

Rank: 8Rank: 8

积分
3858
金钱
3071
HASS币
87
发表于 2020-8-25 15:11:22 | 显示全部楼层
dscao 发表于 2020-8-25 00:24
也发现了这个情况了,可能是utility_meter或者更新间隔导致的误差,要再慢慢研究研究。 ...

我的还没试utility_meter
是电表本身出现这个问题.在esp的页面显示也是少
回复

使用道具 举报

77

主题

1391

帖子

2万

积分

元老级技术达人

积分
24293
金钱
22862
HASS币
290
 楼主| 发表于 2020-8-25 23:43:39 | 显示全部楼层
本帖最后由 dscao 于 2020-8-26 10:49 编辑

001.jpg

Sonoff-Tasmota中设置每15秒上传一次

2020-08-25 22:52  
今日电量 2.284kWh (mqtt获取电表值)
今日峰电量 1.815kWh  今日谷电量 0.468kWh,合计:2.283kWh
昨日电量:1.356kwh
昨日峰电量:1.08    昨日谷电量:0.25(influxdb读取前一天中最后一条的值)
昨日电量偏差有点大,也可能是influxdb存取时间点不是在23:59:59是最后一条导致的误差。
误差应该在可接受范围内,24:00后再看一下

002.jpg


昨日:2.313
昨日峰:1.815
昨日谷:0.497
误差在接受范围内。
暂时感觉误差问题不大,但如果经常关闭或重启hass系统可能也会影响准确性。

晚些时间再将电表的数据与国家电网的信息对比一下
003.jpg

8月24日 电表当天电量:1.356kWh,实际电量 1.41kWh,误差3.8%,电表比电网的数据是要小一点,误差在所难免,这个电表本来也只能作个参考的。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-22 03:55 , Processed in 0.142734 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表