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

 找回密码
 立即注册
楼主: FrankLv

[进阶教程] 家庭用电量及电费统计模块配置方法

  [复制链接]

0

主题

28

帖子

124

积分

注册会员

Rank: 2

积分
124
金钱
95
HASS币
0
发表于 2021-10-17 23:56:12 | 显示全部楼层
本帖最后由 tane 于 2021-10-18 00:09 编辑

楼主的阶梯电费计算有点问题。现在把我的计算方法共享一下。

  
sensor:  
#本月用电量
  - platform: mqtt
    name: "zong"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{value_json['ENERGY'].Total }}"
    unit_of_measurement: "KWH"
#昨天用电量
  - platform: mqtt
    name: "zuotian"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{value_json['ENERGY'].Yesterday }}"
    unit_of_measurement: "KWH"
#今天用电量
  - platform: mqtt
    name: "jintian"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{value_json['ENERGY'].Today }}"
    unit_of_measurement: "KWH"
#功率
  - platform: mqtt
    name: "gonglv"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{value_json['ENERGY'].Power }}"
    unit_of_measurement: "W"
#功率因数
  - platform: mqtt
    name: "yinshu"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{value_json['ENERGY'].Factor }}"
    unit_of_measurement: "cosΦ"
#电压
  - platform: mqtt
    name: "dianya"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{value_json['ENERGY'].Voltage }}"
    unit_of_measurement: "V"
#电流
  - platform: mqtt
    name: "dianliu"
    state_topic: "tele/sonoff/SENSOR"
    value_template: "{{value_json['ENERGY'].Current }}"
    unit_of_measurement: "A"
    
#本月电费
  - platform: template
    sensors:
      zong_dianfei: 
        value_template: >
          {% set onedianjia=0.588 %}
          {% set twodianjia=0.05 %}
          {% set threedianjia=0.25 %}
          {% set onedianlian=200 %}
          {% set twodianlian1=350 %}
          {% set twodianlian2=450 %}
          {% set two1month=[4,5,6,10,11,12] %}
          {% if now().month in two1month %}
          {% set twodianlian=twodianlian1 %}
          {% else %}
          {% set twodianlian=twodianlian2 %}
          {% endif %}
          {% if states("sensor.zong") | float > twodianlian %}
          {% set threedianfei= (states("sensor.zong") | float - twodianlian) * threedianjia %}
          {% else %}
          {% set threedianfei=0 %}
          {% endif %}
          {% if states("sensor.zong") | float > onedianlian %}
          {% set twodianfei= (states("sensor.zong") | float - onedianlian) * twodianjia %}
          {% else %}
          {% set twodianfei=0 %}
          {% endif %}
          {{ (states("sensor.zong") | float * onedianjia  + twodianfei + threedianfei) | round(2) }}
          
        friendly_name: 'zong_dianfei' 
        unit_of_measurement: "RMB"
    
#今日电费,本月电费-本月今天以前的电费
  - platform: template
    sensors:
      jintian_dianfei: 
        value_template: >
          {% set onedianjia=0.588 %}
          {% set twodianjia=0.05 %}
          {% set threedianjia=0.25 %}
          {% set onedianlian=200 %}
          {% set twodianlian1=350 %}
          {% set twodianlian2=450 %}
          {% set two1month=[4,5,6,10,11,12] %}
          {% if now().month in two1month %}
          {% set twodianlian=twodianlian1 %}
          {% else %}
          {% set twodianlian=twodianlian2 %}
          {% endif %}
          {% if states("sensor.zong") | float > twodianlian %}
          {% set threedianfei= (states("sensor.zong") | float - twodianlian) * threedianjia %}
          {% else %}
          {% set threedianfei=0 %}
          {% endif %}
          {% if states("sensor.zong") | float > onedianlian %}
          {% set twodianfei= (states("sensor.zong") | float - onedianlian) * twodianjia %}
          {% else %}
          {% set twodianfei=0 %}
          {% endif %}
          {% set zongdianfei= states("sensor.zong") | float * onedianjia  + twodianfei + threedianfei %}
          {% set before_dianliang= states("sensor.zong") | float - states("sensor.jintian") | float %}
          {% if before_dianliang > twodianlian %}
          {% set threedianfei= (before_dianliang - twodianlian) * threedianjia %}
          {% else %}
          {% set threedianfei=0 %}
          {% endif %}
          {% if before_dianliang > onedianlian %}
          {% set twodianfei= (before_dianliang - onedianlian) * twodianjia %}
          {% else %}
          {% set twodianfei=0 %}
          {% endif %}
          {% set before_dianfei= before_dianliang * onedianjia  + twodianfei + threedianfei %}
          {{ (zongdianfei - before_dianfei) | round(2) }}
          
        friendly_name: 'jintian_dianfei' 
        unit_of_measurement: "RMB"
    
#昨日电费,本月今天以前的电费-本月昨天以前的电费
  - platform: template
    sensors:
      zuotian_dianfei: 
        value_template: >
          {% set onedianjia=0.588 %}
          {% set twodianjia=0.05 %}
          {% set threedianjia=0.25 %}
          {% set onedianlian=200 %}
          {% set twodianlian1=350 %}
          {% set twodianlian2=450 %}
          {% set two1month=[4,5,6,10,11,12] %}
          {% if now().month in two1month %}
          {% set twodianlian=twodianlian1 %}
          {% else %}
          {% set twodianlian=twodianlian2 %}
          {% endif %}
          {% set before_dianliang= states("sensor.zong") | float - states("sensor.jintian") | float %}
          {% if before_dianliang > twodianlian %}
          {% set threedianfei= (before_dianliang - twodianlian) * threedianjia %}
          {% else %}
          {% set threedianfei=0 %}
          {% endif %}
          {% if before_dianliang > onedianlian %}
          {% set twodianfei= (before_dianliang - onedianlian) * twodianjia %}
          {% else %}
          {% set twodianfei=0 %}
          {% endif %}
          {% set before_dianfei= before_dianliang * onedianjia  + twodianfei + threedianfei %}
          {% set zuotian_before_dianliang= states("sensor.zong") | float - states("sensor.jintian") | float - states("sensor.zuotian") | float %}
          {% if zuotian_before_dianliang > twodianlian %}
          {% set threedianfei= (zuotian_before_dianliang - twodianlian) * threedianjia %}
          {% else %}
          {% set threedianfei=0 %}
          {% endif %}
          {% if zuotian_before_dianliang > onedianlian %}
          {% set twodianfei= (zuotian_before_dianliang - onedianlian) * twodianjia %}
          {% else %}
          {% set twodianfei=0 %}
          {% endif %}
          {% set zuotian_before_dianfei= zuotian_before_dianliang * onedianjia  + twodianfei + threedianfei %}
          {{ (before_dianfei - zuotian_before_dianfei) | round(2) }}
          
        friendly_name: 'zuotian_dianfei' 
        unit_of_measurement: "RMB"
        
        
        
        
        
        
#计量清零
automation:
  - id: 'monitor_clear'
    alias: 月初清零电表
    initial_state: true
    hide_entity: false
    trigger:
      - platform: template
        value_template: '{{ states("sensor.jintian") | round(3) == 0.000 }}'
    condition:
      - condition: template
        value_template: '{{ (now().strftime("%d") | int) == 1 }}'
    action:
      - service: mqtt.publish
        data:
          topic: 'cmnd/electric/EnergyReset2'
          payload: '0'
      - service: mqtt.publish
        data:
          topic: 'cmnd/electric/EnergyReset3'
          payload: '0'


回复

使用道具 举报

0

主题

28

帖子

124

积分

注册会员

Rank: 2

积分
124
金钱
95
HASS币
0
发表于 2021-10-18 00:03:12 | 显示全部楼层
其中0.588为基础电费,0.05二档电价和一档电价的差值,0.25为三档电价和二档电价和差值。
湖南省的二三阶电量分为春秋和夏冬两个不同的时节,4,5,6,10,11,12这几个月为春秋季,二阶电量为200度到350度,三阶电量为350度以上。其它月为夏冬季,二阶电量为200度到450度,三阶电量为450度以上。
回复

使用道具 举报

0

主题

28

帖子

124

积分

注册会员

Rank: 2

积分
124
金钱
95
HASS币
0
发表于 2021-10-18 00:17:01 | 显示全部楼层
本帖最后由 tane 于 2021-10-18 00:25 编辑

再发一个nodeRED的流程,每天早上9点自动发送本月和昨天的用电信息到企业微信


[
    {
        "id": "1d46653.604e69b",
        "type": "tab",
        "label": "流程1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "f893ac1f8aad8c04",
        "type": "inject",
        "z": "1d46653.604e69b",
        "name": "每天9点发送用电信息",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "00 09 * * *",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 180,
        "y": 300,
        "wires": [
            [
                "7591564a577ec462",
                "5deba9b47876cdbc",
                "bccbc0133730c976",
                "97b6d13c343b7466"
            ]
        ]
    },
    {
        "id": "7591564a577ec462",
        "type": "api-current-state",
        "z": "1d46653.604e69b",
        "name": "昨天用电量",
        "server": "e8349c19021c66e6",
        "version": 2,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "num",
        "halt_if_compare": "is",
        "entity_id": "sensor.zuotian",
        "state_type": "num",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload1",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data1",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 410,
        "y": 240,
        "wires": [
            [
                "751e2708c33ced2c"
            ]
        ]
    },
    {
        "id": "c680952d07579b39",
        "type": "function",
        "z": "1d46653.604e69b",
        "name": "",
        "func": "\nvar desc='本月已用电量:'+ msg.payload[2] + 'KWH\\r\\n';\ndesc+='本月已用电费:'+ msg.payload[3] + '元\\r\\n';\ndesc+='昨天用电量:'+ msg.payload[0] + 'KWH\\r\\n';\ndesc+='昨天电费:'+ msg.payload[1] + '元';\n\nwechat = {\n    'touser' : '@all',\n    'msgtype': 'textcard',\n    'textcard': {\n        'title': '家庭用电详情',\n        'description' : desc,\n        'url' : `http://你的hass地址:9080`,\n        'btntxt':'查看详情',\n    },\n}\n\nmsg.payload = {};\nmsg.payload = wechat;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1070,
        "y": 300,
        "wires": [
            [
                "eb48db2366f3d619"
            ]
        ]
    },
    {
        "id": "eb48db2366f3d619",
        "type": "bizwechat-push",
        "z": "1d46653.604e69b",
        "name": "",
        "bizwechat": "c36787cd99e1d359",
        "x": 1200,
        "y": 300,
        "wires": [
            []
        ]
    },
    {
        "id": "5deba9b47876cdbc",
        "type": "api-current-state",
        "z": "1d46653.604e69b",
        "name": "昨天电费",
        "server": "e8349c19021c66e6",
        "version": 2,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "sensor.zuotian_dianfei",
        "state_type": "num",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload2",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data2",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 400,
        "y": 280,
        "wires": [
            [
                "751e2708c33ced2c"
            ]
        ]
    },
    {
        "id": "bccbc0133730c976",
        "type": "api-current-state",
        "z": "1d46653.604e69b",
        "name": "本月已用电量",
        "server": "e8349c19021c66e6",
        "version": 2,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "sensor.zong",
        "state_type": "num",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload3",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data3",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 420,
        "y": 320,
        "wires": [
            [
                "751e2708c33ced2c"
            ]
        ]
    },
    {
        "id": "97b6d13c343b7466",
        "type": "api-current-state",
        "z": "1d46653.604e69b",
        "name": "本月已用电费",
        "server": "e8349c19021c66e6",
        "version": 2,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "sensor.zong_dianfei",
        "state_type": "num",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload4",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data4",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 420,
        "y": 360,
        "wires": [
            [
                "751e2708c33ced2c"
            ]
        ]
    },
    {
        "id": "dbd29a142dd582dd",
        "type": "debug",
        "z": "1d46653.604e69b",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1220,
        "y": 180,
        "wires": []
    },
    {
        "id": "3070750dc82bb153",
        "type": "join",
        "z": "1d46653.604e69b",
        "name": "合并成数组",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "4",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 750,
        "y": 300,
        "wires": [
            [
                "68ea1d77a313e3b3",
                "1ea592885261c0c0"
            ]
        ]
    },
    {
        "id": "751e2708c33ced2c",
        "type": "function",
        "z": "1d46653.604e69b",
        "name": "合并",
        "func": "\nreturn { payload:[msg.payload1, msg.payload2,msg.payload3,msg.payload4]};\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 610,
        "y": 300,
        "wires": [
            [
                "3070750dc82bb153"
            ]
        ]
    },
    {
        "id": "68ea1d77a313e3b3",
        "type": "debug",
        "z": "1d46653.604e69b",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1100,
        "y": 420,
        "wires": []
    },
    {
        "id": "1ea592885261c0c0",
        "type": "function",
        "z": "1d46653.604e69b",
        "name": "合并成一个数组",
        "func": "var redata=[0,0,0,0]\nfor (var x in msg.payload)\n{\n    var data=msg.payload[x];\n    for(var i =0;i<redata.length;i++){\n        if(data.length>=i-1 && data[i] && data[i]>0){\n            redata[i]=data[i];\n        }\n    }\n}\nreturn { payload:redata};\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 920,
        "y": 300,
        "wires": [
            [
                "dbd29a142dd582dd",
                "c680952d07579b39"
            ]
        ]
    },
    {
        "id": "e8349c19021c66e6",
        "type": "server",
        "name": "Home Assistant",
        "version": 1,
        "legacy": false,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true
    },
    {
        "id": "c36787cd99e1d359",
        "type": "bizwechat-configurator",
        "name": "企业微信推送",
        "port": "3001",
        "corpid": "",
        "agentid": "",
        "corpsecret": "",
        "url": "",
        "token": "",
        "aeskey": "",
        "client_id": "",
        "client_secret": ""
    }
]




回复

使用道具 举报

0

主题

4

帖子

72

积分

注册会员

Rank: 2

积分
72
金钱
68
HASS币
0
发表于 2022-2-2 08:57:36 来自手机 | 显示全部楼层
不知道和国网电表比准确性能不能达到90%  众所周知国网的表都快5%-8
回复

使用道具 举报

0

主题

10

帖子

42

积分

新手上路

Rank: 1

积分
42
金钱
31
HASS币
0
发表于 2022-2-19 19:32:36 | 显示全部楼层
tane 发表于 2021-10-17 23:56
楼主的阶梯电费计算有点问题。现在把我的计算方法共享一下。

老哥,按您的代码来,出现这个错误是什么意思?
FireShot Capture 001 - File editor - Home Assistant - 192.168.99.196.png
回复

使用道具 举报

0

主题

14

帖子

112

积分

注册会员

Rank: 2

积分
112
金钱
98
HASS币
0
发表于 2022-2-22 10:43:09 | 显示全部楼层
真好,学习学习
回复

使用道具 举报

0

主题

14

帖子

112

积分

注册会员

Rank: 2

积分
112
金钱
98
HASS币
0
发表于 2022-2-22 10:45:46 | 显示全部楼层
怎么才能联系到白老板?我也想搞一套
回复

使用道具 举报

0

主题

14

帖子

112

积分

注册会员

Rank: 2

积分
112
金钱
98
HASS币
0
发表于 2022-2-22 10:58:42 | 显示全部楼层
现在还可以联系到白老板买一套吗?
回复

使用道具 举报

0

主题

23

帖子

232

积分

中级会员

Rank: 3Rank: 3

积分
232
金钱
209
HASS币
0
发表于 2022-9-6 21:53:50 | 显示全部楼层
学习一下
回复

使用道具 举报

0

主题

562

帖子

2187

积分

金牌会员

Rank: 6Rank: 6

积分
2187
金钱
1625
HASS币
0
发表于 2022-9-26 13:46:01 | 显示全部楼层
tane 发表于 2021-10-18 00:03
其中0.588为基础电费,0.05二档电价和一档电价的差值,0.25为三档电价和二档电价和差值。
湖南省的二三阶电 ...

你好兄弟,我这边不分春夏秋冬,也就是一年都是一个价,怎么把春秋季和夏冬季删掉呢?另外还想要个今月电量和上月电量。今月电费和上月电费能帮忙写下嘛,小白一个
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-3 10:41 , Processed in 0.101322 second(s), 32 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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