使用Node-Red创建你的第一个自动化

在这里我们用一个最常用的应用场景来说明 Node-Red 是如何工作的

人体传感器感应到人就开灯

为了实现以上场景,我们需要用到两个基本节点,如下:

首先拖一个 单条件触发节点 出来放置于编辑区的任意位置,并双击该节点进行设置:

不要忘记设置完成后点击右上角的部署按钮进行部署

然后再拖一个操作节点出来放在编辑区的任意位置,并双击该节点进行设置:

这样两个节点就都设置好了,现在我们把两个节点连起来并点击部署让这个流生效:

这里需要说明的是这个连线为什么要从第一个点连出去,把鼠标指上去悬停就可以看到如下:

当触发节点设置触发条件后,该节点会产生两个输出点(可以连线的点),第一个代表满足你设置的条件,那么第二个代表不满足,用户可根据自己的需要进行连线

流5

小技巧:

当需要创建多个类似流的时候不需要一遍一遍重复拖放设置的步骤,可以直接用鼠标框选住已经创建好的流,按 Ctrl + C 复制,然后 Ctrl + V 粘贴即可

在这里附上这个流,可以自行导入后并修改节点里的设置,然后部署看看效果

[{"id":"43a61c9d.8cee84","type":"eh-events-state","z":"cedf2837.70ed18","name":"人体","server":"26788cd2.5f8c3f","category":"传感器","devid":"5564","conditions":[{"val":"%7B%22name%22%3A%22%E4%BA%BA%E4%BD%93%22%2C%22keys%22%3A%5B%22motion%22%5D%2C%22type%22%3A%22custom%22%2C%22inputs%22%3A%5B%7B%22type%22%3A%22select%22%2C%22option%22%3A%5B%7B%22name%22%3A%22%E6%9C%89%E4%BA%BA%22%2C%22value%22%3A%22on%22%7D%2C%7B%22name%22%3A%22%E6%B2%A1%E4%BA%BA%22%2C%22value%22%3A%22off%22%7D%5D%7D%5D%2C%22JSONata%22%3A%22data.motion%3Dparams%5B0%5D%22%7D","JSONata":"data.motion=params[0]","params":["on"]}],"delay":0,"delay-units":"m","onconnect":false,"onchange":true,"outputs":2,"x":250,"y":200,"wires":[["bcf6771b.088468"],[]]},{"id":"bcf6771b.088468","type":"eh-set","z":"cedf2837.70ed18","name":"开灯","server":"26788cd2.5f8c3f","category":"灯","devid":"group-cg5e86","action":[{"power":"on"}],"action_config":[{"action_type":"电源","电源":"{\"power\":\"on\"}"}],"ignoredevid":false,"x":410,"y":200,"wires":[[]]},{"id":"26788cd2.5f8c3f","type":"eh-server","name":"localhost","host":"localhost","port":"33217","token":"J9lglcuQOecUfVNLbgL9Bagrijf2ty5hdcrMatQ0WQf5Fxu9zMevmmgAvOPVGb1k7LsBuV1LHPs89Cm1HFylkehCQOHCIlgUe7atyjpUn5acvd3ORSUiopnN418AAL2V"}]
1 个赞