Node-Red 利用米家智能插座实现充满电自动关闭电源


以下方法经本人验证通过,环境如下:

Home Assistant 0.91.4

Node-RED V0.20.5

node-red-contrib-home-assistant-websocket 0.13.1

米家智能插座(Zigbee版),已接入Home Assistant

小度蓝牙音箱,已接入 Home Assistant

node-red-contrib-light-scheduler 0.0.15


前言:我经常在给相机充电的时候会搞忘记,往往会好几天才想起来去拔插头,正好有一个闲置的米家智能插座(Zigbee版),因此打算使用Node-Red来实现给设备充满电后自动关闭电源,并通过蓝牙音箱给出提示。

第一步:拖入节点

按下图所示,拖入poll state , swtch , delay,current state , call service,LS Filter, 及另一个 call service 节点,并依次将他们连接起来

双击 poll state 节点,如下图所示进行编辑

  • Entity ID ,米家智能插座的id
  • Update Interval 填写60秒,即每分钟刷新一次感应器的状态,这个可以根据你需要的精度自己设置
  • Output on Changed 可以不用勾

双击 switch 节点,如下图所示进行编辑

  • 属性填写 data.attributes.load_power ,意思是获取当前功率
  • 条件处增加条件,小于2(瓦)既认为是充电已结束,向下传递消息

current state 节点与 poll state 节点 类似,用于在delay后重新获取状态

将上述switch 节点复制一份,放在delay后,将delay设置为延时5分钟。这个设置是防止打开插座的那一段时间,未能及时获取当前功率而导致刚打开就自动关闭(智能变智障)

LS Filter 我设置为早上7点至晚上22点,即在此范围内才进行提示(睡觉时就不要吓人了),改节点的使用方法可参见 Node-Red 利用节点进行时间调度及实现定时任务

最后一个call service 作用是调用小度音箱发出 “充电结束” 提示。蓝牙音箱的接入和tts可参见 将小度音箱添加到Home Assistant并使用百度TTS实现播音

导出的流程如下:

[{"id":"213261c3.8746ee","type":"tab","label":"测试区","disabled":false,"info":""},{"id":"57838391.b6d9fc","type":"poll-state","z":"213261c3.8746ee","name":"插座是否打开","server":"81a8d3c5.017a1","version":1,"updateinterval":"60","updateIntervalUnits":"seconds","outputinitially":true,"outputonchanged":true,"entity_id":"switch.plug_158d000363c139","state_type":"str","halt_if":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"x":155,"y":216,"wires":[["399389df.025256"],[]]},{"id":"399389df.025256","type":"switch","z":"213261c3.8746ee","name":"判断是否在充电","property":"data.attributes.load_power","propertyType":"msg","rules":[{"t":"lt","v":"2","vt":"num"}],"checkall":"false","repair":false,"outputs":1,"x":354,"y":212,"wires":[["15552891.4b46b7"]]},{"id":"15552891.4b46b7","type":"delay","z":"213261c3.8746ee","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":550.5,"y":210,"wires":[["663583ae.db0edc"]]},{"id":"663583ae.db0edc","type":"api-current-state","z":"213261c3.8746ee","name":"插座是否打开","server":"81a8d3c5.017a1","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"switch.plug_158d000363c139","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":146.5,"y":334,"wires":[["57f6eb2b.aeb924"],[]]},{"id":"57f6eb2b.aeb924","type":"switch","z":"213261c3.8746ee","name":"判断是否在充电","property":"data.attributes.load_power","propertyType":"msg","rules":[{"t":"lt","v":"2","vt":"num"}],"checkall":"false","repair":false,"outputs":1,"x":367,"y":331,"wires":[["364d39d0.869586"]]},{"id":"364d39d0.869586","type":"api-call-service","z":"213261c3.8746ee","name":"插座关闭","server":"81a8d3c5.017a1","service_domain":"switch","service":"turn_off","data":"{\"entity_id\":\"switch.plug_158d000363c139\"}","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":362.5,"y":440,"wires":[["687ae9b1.3c0b28"]]},{"id":"687ae9b1.3c0b28","type":"light-scheduler-filter","z":"213261c3.8746ee","settings":"24f4af25.b315d","events":"[{\"start\":{\"dow\":1,\"mod\":420},\"end\":{\"dow\":1,\"mod\":1380}},{\"start\":{\"dow\":2,\"mod\":420},\"end\":{\"dow\":2,\"mod\":1380}},{\"start\":{\"dow\":3,\"mod\":420},\"end\":{\"dow\":3,\"mod\":1380}},{\"start\":{\"dow\":4,\"mod\":420},\"end\":{\"dow\":4,\"mod\":1380}},{\"start\":{\"dow\":5,\"mod\":420},\"end\":{\"dow\":5,\"mod\":1380}},{\"start\":{\"dow\":6,\"mod\":420},\"end\":{\"dow\":6,\"mod\":1380}},{\"start\":{\"dow\":0,\"mod\":420},\"end\":{\"dow\":0,\"mod\":1380}}]","name":"控制计划","onlyWhenDark":false,"scheduleRndMax":0,"sunElevationThreshold":"0","sunShowElevationInStatus":false,"x":555,"y":434,"wires":[["90418f9d.5ca03"],[]]},{"id":"90418f9d.5ca03","type":"api-call-service","z":"213261c3.8746ee","name":"提醒","server":"81a8d3c5.017a1","service_domain":"tts","service":"baidu_say","data":"{\"entity_id\":\"media_player.xiao_du_zhi_neng_yin_xiang_2397_4\",\"message\":\"充电已完成\",\"cache\":\"false\",\"options\":{\"person\":\"0\",\"speed\":\"5\",\"volume\":\"5\"}}","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":667,"y":333,"wires":[[]]},{"id":"81a8d3c5.017a1","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true},{"id":"24f4af25.b315d","type":"light-scheduler-settings","z":"","name":"home","latitude":"30.495840","longitude":"104.039738"}]

第二步: 部署,即可完成设置

  • load_power 这个属性在小米智能插座里面是有的,如果你使用的是其他的智能插座,请根据实际情况填写(例如博联的sp mini3 是 current_power_w )
  • load_power 的值, 这个建议根据自己使用的实际情况进行调整,可以参考米家app的功率曲线进行分析得出
  • 查看更多关于Node-Red自动化的文章可点击此处: https://www.feeus.com/tag/node-red/

如果您喜欢这篇文章,或者它给您带来了帮助,您可以请我们喝一杯咖啡,我们将非常感谢您的支持!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

5 − 3 =