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

 找回密码
 立即注册
查看: 7086|回复: 6

[流程系列] 分享一个小米插座计算日月电量的方法

[复制链接]

43

主题

412

帖子

3820

积分

元老级技术达人

积分
3820
金钱
3403
HASS币
136
发表于 2021-1-4 23:40:46 | 显示全部楼层 |阅读模式
本帖最后由 hunterfox 于 2021-1-6 17:22 编辑

[{"id":"f9c98469.a771e8","type":"api-current-state","z":"a54dc4ab.4344e8","name":"小米zigbee插座","server":"2b887588.e131fa","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"switch.plug_158d00039beb7f","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":340,"y":340,"wires":[["86e23958.5f5d48"]]},{"id":"d4d8b443.fe0948","type":"inject","z":"a54dc4ab.4344e8","name":"每日零点","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"01 00 * * *","once":false,"onceDelay":0.1,"x":150,"y":340,"wires":[["f9c98469.a771e8"]]},{"id":"86e23958.5f5d48","type":"function","z":"a54dc4ab.4344e8","name":"数据整定","func":"var time=new Date();\nvar day = time.getDate()\n//月零点冻结\n\n//当前电量\nvar nowpowr=msg.data.attributes.power_consumed;\n\nif (day==1)\n{global.set('monthpowr',nowpowr);}\n\n\n//昨日冻结数\nvar lastpower=global.get('lingdianpower');  \n//赋值 昨日电量=当前电量-昨日零点电量\nvar yespor=Number(nowpowr)-Number(lastpower);\nglobal.set('poweryestoday',yespor);\n//将今日数据写入零点数据。\nglobal.set('lingdianpower',nowpowr);\n\n//  昨日电量 poweryestoday,今日零点示数:lingdianpower\nvar msg1={};\nmsg1.payload={};\n\nmsg1.payload.poweryestoday=global.get('poweryestoday');\nmsg1.payload.lingdianpower=global.get('lingdianpower');\nmsg1.payload.monthpowr=global.get('monthpowr');\nreturn msg1;","outputs":1,"noerr":0,"x":500,"y":340,"wires":[[]]},{"id":"fe09ff50.d6902","type":"api-current-state","z":"a54dc4ab.4344e8","name":"小米zigbee插座","server":"2b887588.e131fa","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"switch.plug_158d00039beb7f","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":280,"y":400,"wires":[["5084a563.4f113c"]]},{"id":"5084a563.4f113c","type":"function","z":"a54dc4ab.4344e8","name":"数据整定","func":"\n//当日电量\nvar nowpowr=msg.data.attributes.power_consumed-global.get('lingdianpower');\n//当月电量\nvar nowmonthpowr=msg.data.attributes.power_consumed-global.get('monthpowr');\n//昨日电量\n\nvar msg1={};  //当日\nmsg1.payload=nowpowr/1000;\nmsg1.topic=\"a/sensor/xiaomi_kwh_today/home/state\";\nvar msg2={};  //当月\nmsg2.payload=nowmonthpowr/1000;\nmsg2.topic=\"a/sensor/xiaomi_kwh_month/home/state\";\nvar msg3={};   //昨日\nvar ss=Number(global.get('poweryestoday'));\nmsg3.payload=ss/1000;\nmsg3.topic=\"a/sensor/xiaomi_kwh/home/state\";\nreturn [msg1,msg2,msg3];","outputs":3,"noerr":0,"x":440,"y":400,"wires":[["a56ac618.2a2c18"],["a56ac618.2a2c18"],["a56ac618.2a2c18"]]},{"id":"a56ac618.2a2c18","type":"mqtt out","z":"a54dc4ab.4344e8","name":"","topic":"","qos":"","retain":"","broker":"37541fbf.c8804","x":600,"y":400,"wires":[]},{"id":"e14b6a29.699b98","type":"inject","z":"a54dc4ab.4344e8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":420,"wires":[["fe09ff50.d6902"]]},{"id":"2b887588.e131fa","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"37541fbf.c8804","type":"mqtt-broker","z":"","name":"MY_MQTT","broker":"192.168.1.188","port":"1883","clientid":"Node_RED","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
3.jpg

评分

参与人数 2金钱 +30 收起 理由
kgt09382000 + 10 厉害了word楼主!
neroxps + 20 厉害了word楼主!

查看全部评分

回复

使用道具 举报

2

主题

291

帖子

1530

积分

论坛积极会员

积分
1530
金钱
1239
HASS币
0
发表于 2021-1-5 20:19:23 | 显示全部楼层
好东西,学习一下。。。
回复

使用道具 举报

65

主题

853

帖子

3044

积分

论坛元老

Rank: 8Rank: 8

积分
3044
金钱
2184
HASS币
40
发表于 2021-1-6 09:24:32 | 显示全部楼层
有错误呢
SyntaxError: Unexpected token a in JSON at position 2382
msg1.topic="a/sensor/xia
回复

使用道具 举报

43

主题

412

帖子

3820

积分

元老级技术达人

积分
3820
金钱
3403
HASS币
136
 楼主| 发表于 2021-1-6 17:24:23 | 显示全部楼层

我重新传了
回复

使用道具 举报

65

主题

853

帖子

3044

积分

论坛元老

Rank: 8Rank: 8

积分
3044
金钱
2184
HASS币
40
发表于 2021-1-7 08:56:24 | 显示全部楼层

已经没有问题了
回复

使用道具 举报

5

主题

90

帖子

1542

积分

金牌会员

Rank: 6Rank: 6

积分
1542
金钱
1452
HASS币
0
发表于 2022-8-24 20:23:21 | 显示全部楼层
大佬牛X,但是有一个问题,你这个是一个,如果主前二十个,这个数据量是不是有点大
回复

使用道具 举报

22

主题

319

帖子

3981

积分

论坛元老

Rank: 8Rank: 8

积分
3981
金钱
3657
HASS币
30
发表于 2022-8-27 08:00:45 | 显示全部楼层
多谢楼主分享
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-23 03:08 , Processed in 0.289993 second(s), 32 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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