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

 找回密码
 立即注册
查看: 2268|回复: 4

[技术探讨] 求助PZEM-004电表计算电费模板新的写法

[复制链接]

9

主题

180

帖子

940

积分

论坛积极会员

积分
940
金钱
760
HASS币
0
发表于 2022-3-2 22:35:26 | 显示全部楼层 |阅读模式

我用的是PZEM-004刷的tasmota固件。当时按照论坛的教程配置的hass,当时一切正常,通过电费模板可以按照用电量自动核算阶梯电费。
后来hass不断的升级新版本,忘了是升级哪个hass版本后,发现计算电费的的模板始终是按照最高的阶梯电价来计算电费的,而且看了的系统日志,也有这方面的警告提示,但无奈水平有限,不知道怎么解决。寻求坛友帮助

提示错误信息的日志:

WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unknown' when rendering 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.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 250) or (date == 0 and states("sensor.dianbiaoelectric") | int > 250) %} {{(states("sensor.dianbiaoelectric") | float * 0.82) | round(2)}} {% elif (date != 0 and (states("sensor.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 170) or (date == 0 and states("sensor.dianbiaoelectric") | int > 170) %} {{(states("sensor.dianbiaoelectric") | float * 0.57) | round(2)}} {% else %} {{(states("sensor.dianbiaoelectric") | float * 0.52) | round(2)}} {% endif %}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

现在配置的代码:

sensor:
####电表电量####
  #电压传感器
  - platform: mqtt
    name: "dianbiaodianya"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "V"
    value_template: "{{ value_json['ENERGY'].Voltage }}"
 
  #电流传感器
  - platform: mqtt
    name: "dianbiaodianliu"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "A"
    value_template: "{{ value_json['ENERGY'].Current }}"
 
  #功率传感器
  - platform: mqtt
    name: "dianbiaogonglv"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "W"
    value_template: "{{ value_json['ENERGY'].Power }}"
 
  #功率因数传感器
  - platform: mqtt
    name: "dianbiaogonglvyinshu"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "cosΦ"
    value_template: "{{ value_json['ENERGY'].Factor }}"
    
  #电表箱温度
  - platform: mqtt
    name: "dianbiaowendu"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "℃"
    value_template: "{{ value_json['AHT1X-0x38'].Temperature }}"

  #电表箱湿度
  - platform: mqtt
    name: "dianbiaoshidu"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "%"
    value_template: "{{ value_json['AHT1X-0x38'].Humidity }}"

  #今天用电量
  - platform: mqtt
    name: "dianbiaojinri_dianliang"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "kWh"
    value_template: "{{ value_json['ENERGY'].Today }}"
 
  #今日电费
  - platform: template
    sensors:
      dianbiaojinri_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.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 250) or (date == 0 and states("sensor.dianbiaoelectric") | int > 250) %}
          {{(states("sensor.dianbiaojinri_dianliang") | float * 0.82) | round(2)}}
          {% elif (date != 0 and (states("sensor.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 170) or (date == 0 and states("sensor.dianbiaoelectric") | int > 170) %}
          {{(states("sensor.dianbiaojinri_dianliang") | float * 0.57) | round(2)}}
          {% else %}
          {{(states("sensor.dianbiaojinri_dianliang") | float * 0.52) | round(2)}}
          {% endif %}
        unit_of_measurement: "RMB"
 
  #昨天用电量
  - platform: mqtt
    name: "dianbiaozuori_dianliang"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "kWh"
    value_template: "{{ value_json['ENERGY'].Yesterday }}"
 
  #昨日电费
  - platform: template
    sensors:
      dianbiaozuori_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.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 250) or (date == 0 and states("sensor.dianbiaoelectric") | int > 250) %}
          {{(states("sensor.dianbiaozuori_dianliang") | float * 0.82) | round(2)}}
          {% elif (date != 0 and (states("sensor.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 170) or (date == 0 and states("sensor.dianbiaoelectric") | int > 170) %}
          {{(states("sensor.dianbiaozuori_dianliang") | float * 0.57) | round(2)}}
          {% else %}
          {{(states("sensor.dianbiaozuori_dianliang") | float * 0.52) | round(2)}}
          {% endif %}
        unit_of_measurement: "RMB"
 
  #总用电量 
  - platform: mqtt
    name: "dianbiaoelectric"
    state_topic: "tele/dianliang/SENSOR"
    qos: 1
    unit_of_measurement: "kWh"
    value_template: "{{ value_json['ENERGY'].Total }}"
    state_class: 'total'
    device_class: 'energy'
 
  #总电费
  - platform: template
    sensors:
      dianbiaozongdianfei:
        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.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 250) or (date == 0 and states("sensor.dianbiaoelectric") | int > 250) %}
          {{(states("sensor.dianbiaoelectric") | float * 0.82) | round(2)}}
          {% elif (date != 0 and (states("sensor.dianbiaoelectric") | float - states("sensor.dianbiaojinri_dianliang") | float + (states("sensor.dianbiaozuori_dianliang") | float * (date + 1))) | int > 170) or (date == 0 and states("sensor.dianbiaoelectric") | int > 170) %}
          {{(states("sensor.dianbiaoelectric") | float * 0.57) | round(2)}}
          {% else %}
          {{(states("sensor.dianbiaoelectric") | float * 0.52) | round(2)}}
          {% endif %}
        unit_of_measurement: "RMB"


回复

使用道具 举报

77

主题

1391

帖子

2万

积分

元老级技术达人

积分
24363
金钱
22932
HASS币
290
发表于 2022-3-3 14:40:01 | 显示全部楼层
本帖最后由 dscao 于 2022-3-3 14:41 编辑

现在的“能源”面板不香吗?设置完整了,随便哪天的电费还是哪月的电费都能查呀。
回复

使用道具 举报

9

主题

180

帖子

940

积分

论坛积极会员

积分
940
金钱
760
HASS币
0
 楼主| 发表于 2022-3-3 19:22:09 | 显示全部楼层
dscao 发表于 2022-3-3 14:40
现在的“能源”面板不香吗?设置完整了,随便哪天的电费还是哪月的电费都能查呀。 ...

是挺香的,我也同时设置着能源面板,但是在能源里无法显示实时功率、电压、电流等信息
回复

使用道具 举报

1

主题

53

帖子

825

积分

高级会员

Rank: 4

积分
825
金钱
772
HASS币
0
发表于 2022-3-6 12:21:01 来自手机 | 显示全部楼层
dscao 发表于 2022-3-3 14:40
现在的“能源”面板不香吗?设置完整了,随便哪天的电费还是哪月的电费都能查呀。 ...

能源面板怎么设置?大佬有相关教程吗

论坛助手,iPhone
回复

使用道具 举报

10

主题

147

帖子

462

积分

中级会员

Rank: 3Rank: 3

积分
462
金钱
315
HASS币
0
发表于 2022-3-14 12:55:45 | 显示全部楼层
dscao 发表于 2022-3-3 14:40
现在的“能源”面板不香吗?设置完整了,随便哪天的电费还是哪月的电费都能查呀。 ...

能源数据怎么显示电费呀?麻烦有空指点下
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-28 09:39 , Processed in 0.054385 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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