由于我家属于电采暖的用户,每年分为两个计价周期,每个一二三阶。在论坛翻阅了一波都是以月份为周期的,我写了一个以某月某天为周期的,供大家参阅,借花献佛,应该是拿来可以直接用的。参考了dscao的文章:https://bbs.hassbian.com/forum.php?mod=viewthread&tid=13796&highlight=%E5%AE%9E%E6%97%B6%E7%94%B5%E4%BB%B7
- platform: template
sensors:
dang_qian_jie_ti:
value_template:
{% set now_dt = now() %}
{% set warm_season_start = now_dt.replace(month=3, day=15, hour=0, minute=0, second=0, microsecond=0) %}
{% set warm_season_end = now_dt.replace(month=11, day=15, hour=0, minute=0, second=0, microsecond=0) %}
{% set is_warm_season = (now_dt >= warm_season_start and now_dt < warm_season_end) %}
{% set total_energy = states("sensor.dian_biao_energy") | float(0) %}
{% if is_warm_season %}
{% if total_energy <= 1440 %}
1
{% elif total_energy <= 2080 %}
2
{% else %}
3
{% endif %}
{% else %}
{% if total_energy <= 720 %}
4
{% elif total_energy <= 1040 %}
5
{% else %}
6
{% endif %}
{% endif %}
friendly_name: '当前阶梯'
unit_of_measurement: "L"
month=3, day=15,是你周期开始的月份和天,input_number.chu_shi_dian_jie_xiao_zhun,增加了个首次添加传感器校验前期用电量的数值辅助,可以自己手动在辅助元素里添加一个,更换成自己的。
template:
- sensor:
- name: "家庭实时电价"
unique_id: home_realtime_electricity_price
state: >
{% set now_dt = now() %}
{% set warm_start = now_dt.replace(month=3, day=15) %}
{% set warm_end = now_dt.replace(month=11, day=15) %}
{% set manual_offset = states('input_number.chu_shi_dian_jie_xiao_zhun') | float(0) %}
{% if now_dt >= warm_start and now_dt < warm_end %}
{% set total_energy = (states('sensor.seasonal_energy_warm') | float(0)) + manual_offset %}
{% if total_energy <= 1440 %}
0.56
{% elif total_energy <= 2080 %}
0.61
{% else %}
0.86
{% endif %}
{% else %}
{% set total_energy = (states('sensor.seasonal_energy_cold') | float(0)) + manual_offset %}
{% if total_energy <= 720 %}
0.56
{% elif total_energy <= 1040 %}
0.61
{% else %}
0.86
{% endif %}
{% endif %}
unit_of_measurement: "元/kWh"
device_class: monetary
icon: mdi:currency-cny
|