Node-Red 设置变量控制设备开启次数


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

Home Assistant 0.91.4

Node-RED V0.20.5

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

相关设备已接入Home Assistant


前言:我有一株漂亮的茉莉花,某一次出远门,为了照顾它,我用闲置的鱼缸、一个小水泵和一截水管组成了一个简易的灌溉系统。同时,将水泵连接在一个智能插座上,这样我就可以使用手机远程的控制它完成浇水。但为了浇透,我需要远程打开水泵运行1分钟,然后关闭约5分钟,待水向下完全渗透后再次打开水泵运行1分钟。是的,这很麻烦,所以有这这篇文章

第一步:实现自动关闭功能

首先我们要实现的是当发现智能插座(水泵)打开后,等待1分钟,自动关闭它

按下图所示,拖入events: state , delay及 call service节点,并依次将他们连接起来

  • events: state :设置智能插座(水泵)的 Entity ID,状态为on 时候触发消息。 Output only on state change 和 Output on Connect 都勾选上
  • delay :设置延时1分钟
  • call service :调用switch的turn_off服务,关闭智能插座

这个步骤很简单,就不再对每个节点做详细描述了。部署后即可实现水泵开启1分钟后自动关闭的功能

第二步: 增加控制开启次数

继续拖入function(2个),switch, delay及 call service节点,并按图所示将他们连接起来

双击第一个 function 节点,如下图所示进行编辑

  • 这个节点的作用是设置一个变量count,记录本次浇花水泵已经运行了几次,并将结果传给后面的节点

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

  • 判断count的值,如果小于2就等待5分钟后使用 call service 节点再次打开智能插座(水泵)
  • 如果大于或等于2,表示本次浇花已经完成,重置count的值,结束流程

双击第二个 function 节点,如下图所示进行编辑

  • 浇花结束后,需重置count的值,否则下次浇花的时候count值会不正确

第三步: 再次部署,即可实现远程或手动开启该智能插座后,水泵按运转1分钟,停顿5分钟,再次运转1分钟,然后结束的预设完成浇花工作

以下为本流程的导出数据,请根据实际情况修改使用

[{"id":"2c0af694.e2068a","type":"server-state-changed","z":"213261c3.8746ee","name":"浇花打开","server":"81a8d3c5.017a1","version":1,"entityidfilter":"switch.spmini3","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":192,"y":462,"wires":[["923993ea.be71f"],[]]},{"id":"923993ea.be71f","type":"delay","z":"213261c3.8746ee","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"minutes","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":397.888916015625,"y":462.33331298828125,"wires":[["1009b396.5787fc"]]},{"id":"1009b396.5787fc","type":"api-call-service","z":"213261c3.8746ee","name":"关闭","server":"81a8d3c5.017a1","service_domain":"switch","service":"turn_off","data":"{\"entity_id\":\"switch.spmini3\"}","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":607.5556030273438,"y":459.7777404785156,"wires":[["e5d8957a.bf45e8"]]},{"id":"e5d8957a.bf45e8","type":"function","z":"213261c3.8746ee","name":"计数","func":"var count = flow.get('count')||0;\ncount += 1;\nflow.set('count',count);\nmsg.payload = count;\nreturn msg;","outputs":1,"noerr":0,"x":802.4444580078125,"y":471.6666564941406,"wires":[["4e62aa36.83c4a4"]]},{"id":"4e62aa36.83c4a4","type":"switch","z":"213261c3.8746ee","name":"判断","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"2","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":976.8888549804688,"y":473.111083984375,"wires":[["bf398280.5169e"],["3a31af97.9bcd9"]]},{"id":"bf398280.5169e","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":1146.4444580078125,"y":473.88897705078125,"wires":[["36e65368.a5939c"]]},{"id":"3a31af97.9bcd9","type":"function","z":"213261c3.8746ee","name":"归零","func":"flow.set('count',0);\n\nreturn msg;","outputs":1,"noerr":0,"x":1149.5555419921875,"y":577,"wires":[[]]},{"id":"36e65368.a5939c","type":"api-call-service","z":"213261c3.8746ee","name":"打开","server":"81a8d3c5.017a1","service_domain":"switch","service":"turn_on","data":"{\"entity_id\":\"switch.spmini3\"}","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1305.444580078125,"y":472.22216796875,"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}]
  • 通过这个小案例,可以了解到 flow.get 和 flow.set 的使用
  • 更进一步的设计是:将花花草草检测仪接入到Home Assistant,然后自动获取湿度,当湿度低于某个设定值时,执行浇花流程,当湿度高于设定值时,停止浇花。后面有时间了来写
  • 查看更多关于Node-Red自动化的文章可点击此处: https://www.feeus.com/tag/node-red/

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

发表评论

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

5 + 19 =