本帖最后由 zxyny1989 于 2021-5-1 22:10 编辑
培正电表接入很久了,一直没有搞电费计算。今天心血来潮,就搞了一下。
代码参照的论坛帖子,我的是郑州的阶梯电价
#今日电费
- 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.tasmota_energy_today") | float + (states("sensor.tasmota_energy_yesterday") | float * (date + 1))) | int > 3120) or (date == 0 and states("sensor.electric") | int > 3120) %}
{{(states("sensor.tasmota_energy_today") | float * 0.86) | round(2)}}
{% elif (date != 0 and (states("sensor.electric") | float - states("sensor.tasmota_energy_today") | float + (states("sensor.tasmota_energy_yesterday") | float * (date + 1))) | int > 2160) or (date == 0 and states("sensor.electric") | int > 2160) %}
{{(states("sensor.tasmota_energy_today") | float * 0.61) | round(2)}}
{% else %}
{{(states("sensor.tasmota_energy_today") | float * 0.56) | round(2)}}
{% endif %}
friendly_name: '今日电费'
unit_of_measurement: "RMB"
#昨日电费
- 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.tasmota_energy_today") | float + (states("sensor.tasmota_energy_yesterday") | float * (date + 1))) | int > 3120) or (date == 0 and states("sensor.electric") | int > 3120) %}
{{(states("sensor.tasmota_energy_yesterday") | float * 0.86) | round(2)}}
{% elif (date != 0 and (states("sensor.electric") | float - states("sensor.tasmota_energy_today") | float + (states("sensor.tasmota_energy_yesterday") | float * (date + 1))) | int > 2160) or (date == 0 and states("sensor.electric") | int > 2160) %}
{{(states("sensor.tasmota_energy_yesterday") | float * 0.61) | round(2)}}
{% else %}
{{(states("sensor.tasmota_energy_yesterday") | float * 0.56) | round(2)}}
{% endif %}
friendly_name: '昨日电费'
unit_of_measurement: "RMB"
#总电费
- platform: template
sensors:
zongdianfei:
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.tasmota_energy_today") | float + (states("sensor.tasmota_energy_yesterday") | float * (date + 1))) | int > 3120) or (date == 0 and states("sensor.electric") | int > 3120) %}
{{(states("sensor.tasmota_energy_total") | float * 0.86) | round(2)}}
{% elif (date != 0 and (states("sensor.electric") | float - states("sensor.tasmota_energy_today") | float + (states("sensor.tasmota_energy_yesterday") | float * (date + 1))) | int > 2160) or (date == 0 and states("sensor.electric") | int > 2160) %}
{{(states("sensor.tasmota_energy_total") | float * 0.61) | round(2)}}
{% else %}
{{(states("sensor.tasmota_energy_total") | float * 0.56) | round(2)}}
{% endif %}
friendly_name: '总电费'
unit_of_measurement: "RMB"
|