【问题标题】:Node-red temperature sensor with function node and relay带功能节点和继电器的节点-红色温度传感器
【发布时间】:2018-05-09 13:15:39
【问题描述】:

我想使用连接到 Raspberri Pi 的 DS18b20 探头通过空调进行控制。 我让继电器在我想要的正确温度下打开和关闭。 下一步是我挣扎的地方...... 我的要求: 关闭空调 = 25(这有效) 当空调在 25 度以上打开时...保持空调开到 20 度,然后关闭(这我无法工作) 当空调关闭时,等待10分钟{保护电机}(这个我无法工作)

我已经尝试使用具有以下代码的功能节点:

非常感谢您提供任何帮助...我已经尝试对切换和更改节点执行相同的操作,但我无法让延迟部分工作,或者让它一直冷却到 20 点。

【问题讨论】:

  • 你能提供不起作用的代码吗?图像根本不是很有用。此外,您提供的图片似乎没有包含回答您的问题所需的所有代码 - 而且它还存在语法错误。
  • 如果图像中的代码就是您所拥有的,那么请考虑每个条件。前三个条件应该是逻辑连接(&&),而不是析取(||)。 while 条件在语法上不正确。 (...)(...) 应该写成(... && ...)
  • 您可能需要在上下文中保存状态才能使其正常工作,但您还应该查看触发节点。

标签: javascript node-red home-automation


【解决方案1】:

因此,为了使这项工作正常运行,您需要流上下文变量来存储状态。我为您的问题创建了以下示例流程,它使用了以下 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":[]}]

韩币 一月

【讨论】:

  • 我在其中一个流程名称中(不在流程代码中)做了一点错字。如您所见,有 2 个名为 “设置流上下文和有效负载以关闭空调”的流。这是不正确的。较低的流应该得到名称 "set flow context and payload to switch on aircon"
  • 您好 Jan,非常感谢您。你向我展示了我有很多东西要学;)
  • { code: "T1006", stack: "Error↵ at applyInner (/usr/...", position: 11, token: "toMillis", message: "Attempted to invoke a non-func... " }red/node_modules/jsonata/jsonata.js:1580:27)\n at next (native)","position":11,"token":"toMillis","message":"尝试调用非功能”} ....这是在“开关超过10分钟前?”开关节点
  • 显然我的 jsonata 表达式中使用的 jsonata 函数 toMillis() 在您的 node-red 安装中是未知的。因此,我认为您使用的 node-red 版本比我要旧一些。 1/ 你可以尝试升级 node-red 并检查这是否修复了错误? 2/ 触发底部流flow.aircon_last_switch_tms 在调试窗口中打印flow.aircon_last_switch_tms 时,您是否也遇到错误? 3/ 当然不是使用 jsonata 表达式来检查开关是否发生在 10 分钟前_,您也可以在 javascript 中的函数节点中执行此操作。
  • 非常感谢!更新后就可以使用了
猜你喜欢
  • 2017-08-03
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多