首页 > 解决方案 > Node-red temperature sensor with function node and relay

问题描述

I want to use a DS18b20 probe connected to Raspberri Pi to control by aircon. I get the relay to switch on and off at the correct temperatures I want. The next step is where I am struggling... My requirements: Switch aircon off <= 20 (this works) Switch aircon on >= 25 (this works) when the aircon switches on above 25...keep the aircon on until 20 is reached, then switch off (this I can't get working) when the aircon switches off, wait for 10min {to protect the motor} (This I can't get working)

I have tried using a function node with the following code:

image of code

Any help here it be greatly appreciated...I have tried doing the same with switch and change nodes, but I can;t get the delay portion working then or get it to stay on to cool down till 20.

标签: javascriptnode-redhome-automation

解决方案


因此,为了使其正常工作,您需要流上下文变量来存储状态。我为您的问题创建了以下示例流程,该流程使用了以下 2 个上下文变量:

  • aircon_is_on:是一个布尔值,指示空调当前是打开还是关闭。
  • aircon_last_switch_tms:是上次打开或关闭空调的时间戳(以自 1970 年以来的毫秒数表示)。

在此处输入图像描述

您可以在下面找到流程的详细信息。

[{"id":"cc99810d.fdcb8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"16703345.888dcd","type":"inject","z":"cc99810d.fdcb8","name":"initialisation at startup","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":180,"y":80,"wires":[["97135670.acdd88"]]},{"id":"97135670.acdd88","type":"change","z":"cc99810d.fdcb8","name":"initialize flow variables","rules":[{"t":"set","p":"aircon_is_on","pt":"flow","to":"false","tot":"bool"},{"t":"set","p":"aircon_last_switch_tms","pt":"flow","to":"0","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":80,"wires":[[]]},{"id":"3f26e3af.245a1c","type":"inject","z":"cc99810d.fdcb8","name":"","topic":"","payload":"19","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":200,"wires":[["cd981b11.6bac88"]]},{"id":"d7ffb888.3cd648","type":"comment","z":"cc99810d.fdcb8","name":"inject nodes simulating the DS18b20 sensor output","info":"","x":210,"y":140,"wires":[]},{"id":"d0544032.848e","type":"inject","z":"cc99810d.fdcb8","name":"","topic":"","payload":"20","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":240,"wires":[["cd981b11.6bac88"]]},{"id":"995c2849.57d9f8","type":"inject","z":"cc99810d.fdcb8","name":"","topic":"","payload":"21","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":280,"wires":[["cd981b11.6bac88"]]},{"id":"f6cee931.a62e78","type":"inject","z":"cc99810d.fdcb8","name":"","topic":"","payload":"24","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":320,"wires":[["cd981b11.6bac88"]]},{"id":"3dcf7adb.db0d66","type":"inject","z":"cc99810d.fdcb8","name":"","topic":"","payload":"25","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":360,"wires":[["cd981b11.6bac88"]]},{"id":"76d055f9.3878dc","type":"inject","z":"cc99810d.fdcb8","name":"","topic":"","payload":"26","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":400,"wires":[["cd981b11.6bac88"]]},{"id":"7698406b.1ca73","type":"change","z":"cc99810d.fdcb8","name":"set flow context and payload to switch off aircon","rules":[{"t":"set","p":"aircon_is_on","pt":"flow","to":"false","tot":"bool"},{"t":"set","p":"aircon_last_switch_tms","pt":"flow","to":"","tot":"date"},{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":220,"wires":[["7d3fbf47.1bc7a"]]},{"id":"cd981b11.6bac88","type":"switch","z":"cc99810d.fdcb8","name":"aircon is on ?","property":"aircon_is_on","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":300,"wires":[["ea9a6fa9.67ae5"],["fc9c4632.967d78"]]},{"id":"ea9a6fa9.67ae5","type":"switch","z":"cc99810d.fdcb8","name":"temp <= 20 ?","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"20","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":220,"wires":[["7698406b.1ca73"]]},{"id":"7d3fbf47.1bc7a","type":"debug","z":"cc99810d.fdcb8","name":"aircon command to switch it on / off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1400,"y":280,"wires":[]},{"id":"fc9c4632.967d78","type":"switch","z":"cc99810d.fdcb8","name":"temp >= 25 ?","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"25","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":320,"wires":[["78c00f1f.7f943"]]},{"id":"78c00f1f.7f943","type":"switch","z":"cc99810d.fdcb8","name":"switch on / off more than 10 minutes ago ?","property":"($toMillis($now())-$flowContext(\"aircon_last_switch_tms\")) > (10*60*1000)","propertyType":"jsonata","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":700,"y":380,"wires":[["c5c075a6.a039b8"]]},{"id":"c5c075a6.a039b8","type":"change","z":"cc99810d.fdcb8","name":"set flow context and payload to switch off aircon","rules":[{"t":"set","p":"aircon_is_on","pt":"flow","to":"true","tot":"bool"},{"t":"set","p":"aircon_last_switch_tms","pt":"flow","to":"","tot":"date"},{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":380,"wires":[["7d3fbf47.1bc7a"]]},{"id":"a8acfa78.442cd8","type":"debug","z":"cc99810d.fdcb8","name":"flow.aircon_last_switch_tms  (in milli seconds since 1970)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":730,"y":540,"wires":[]},{"id":"e6dec5ed.6e6d78","type":"inject","z":"cc99810d.fdcb8","name":"flow.aircon_last_switch_tms","topic":"","payload":"aircon_last_switch_tms","payloadType":"flow","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":540,"wires":[["a8acfa78.442cd8","b8c55cf8.7f1fe"]]},{"id":"2a2df95.069e206","type":"debug","z":"cc99810d.fdcb8","name":"flow.aircon_last_switch_tms","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":640,"y":600,"wires":[]},{"id":"b8c55cf8.7f1fe","type":"change","z":"cc99810d.fdcb8","name":"$fromMillis(payload)","rules":[{"t":"set","p":"payload","pt":"msg","to":"$fromMillis(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":600,"wires":[["2a2df95.069e206"]]},{"id":"ff677399.2c529","type":"comment","z":"cc99810d.fdcb8","name":"test nodes to check the aircon flow context variables","info":"","x":227,"y":462,"wires":[]},{"id":"4a49b01b.1db3","type":"inject","z":"cc99810d.fdcb8","name":"","topic":"","payload":"aircon_is_on","payloadType":"flow","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":169,"y":499,"wires":[["2497d1be.1155ce"]]},{"id":"2497d1be.1155ce","type":"debug","z":"cc99810d.fdcb8","name":"flow.aircon_is_on","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":500,"wires":[]},{"id":"14796044.a8c56","type":"comment","z":"cc99810d.fdcb8","name":"output to switch on /off aircon","info":"","x":1360,"y":100,"wires":[]},{"id":"3411ff23.37aa1","type":"comment","z":"cc99810d.fdcb8","name":"aircon flow context variables","info":"This flow makes use of 2 flow context variables:\n1. *aircon_is_on* : is a boolean indicating if the aircon is currently switched on or off.\n2. *aircon_last_switch_tms* : is a timestamp (expressed in number of milli seconds since 1970) of the last time the aircon has been switch on or off.","x":453,"y":26,"wires":[]}]

韩日


推荐阅读