【问题标题】:check if an event has happened before performing an action in NodeRed在 NodeRed 中执行操作之前检查事件是否发生
【发布时间】:2022-11-15 19:40:35
【问题描述】:

我有一个门传感器,可以检测它是打开还是关闭。我还有一个摄像头,它可以检测运动并在运动停止时发送一条 mqtt 消息。

如果门打开,摄像头会检测到移动,稍后,我会收到 mqtt 消息。但是,如果我走到门附近,摄像头也会检测到移动并发送 mqtt 消息。

我在 NodeRed 中尝试做的是在收到相机运动消息时执行一个操作,但前提是门是打开的。

为此,我已将门传感器连接到 change 节点以创建表示“门已打开”的消息。但是,当我收到 mqtt 消息时,我不知道如何检查门是否打开以继续流程

基本上,我的问题可以概括为如何检查是否发生了某些事情以继续或停止流程

【问题讨论】:

  • 在文档中查找“上下文”
  • 我试图避免全局样式变量。来自 C++ 背景,我觉得使用它们不舒服。此外,它隐藏了节点之间的依赖关系,不是吗?
  • 你必须在某处存储状态,上下文是你存储状态的地方。 (我建议你存储最后一个门事件的时间戳,这样你就可以决定将这两个事件关联的时间段)
  • 我懂了。我实际上是在检查 join 节点以尝试使用 door_open + 摄像头消息创建消息。不过还没有成功,我想我会看看上下文方法
  • 确保在连接节点的 2 条输入消息上设置不同的主题,这会有所帮助。您也许可以使用触发节点来添加超时,但我仍然会先尝试上下文

标签: node-red


【解决方案1】:

给我你的红色节点源。我可以给你修。
您可以使用“功能”和“延迟”节点。
你能上传你的节点红色流吗?

先试试这个。

[
{
    "id": "82b48005d8d8eb1a",
    "type": "tab",
    "label": "stackoverflow",
    "disabled": false,
    "info": "",
    "env": []
},
{
    "id": "0247562ae8b3c9ed",
    "type": "inject",
    "z": "82b48005d8d8eb1a",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "door open",
    "payloadType": "str",
    "x": 140,
    "y": 140,
    "wires": [
        [
            "380af527db692157"
        ]
    ]
},
{
    "id": "380af527db692157",
    "type": "function",
    "z": "82b48005d8d8eb1a",
    "name": "door open",
    "func": "global.set("DoorOpen",1);
return msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 300,
    "y": 140,
    "wires": [
        []
    ]
},
{
    "id": "8848430d64746121",
    "type": "inject",
    "z": "82b48005d8d8eb1a",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "door close",
    "payloadType": "str",
    "x": 140,
    "y": 180,
    "wires": [
        [
            "8f6ce1d3d43cdf9d"
        ]
    ]
},
{
    "id": "8f6ce1d3d43cdf9d",
    "type": "function",
    "z": "82b48005d8d8eb1a",
    "name": "door close",
    "func": "global.set("DoorOpen", 0);
return msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 310,
    "y": 180,
    "wires": [
        []
    ]
},
{
    "id": "7166a60ba150e1e9",
    "type": "comment",
    "z": "82b48005d8d8eb1a",
    "name": "Used Inject node Instead of 'Door sensor'",
    "info": "",
    "x": 240,
    "y": 100,
    "wires": []
},
{
    "id": "5c4e6ebc4448deaf",
    "type": "comment",
    "z": "82b48005d8d8eb1a",
    "name": "Used inject node instead of 'Motion sensor'",
    "info": "",
    "x": 240,
    "y": 260,
    "wires": []
},
{
    "id": "64a58da41010748c",
    "type": "inject",
    "z": "82b48005d8d8eb1a",
    "name": "",
    "props": [
        {
            "p": "payload"
        },
        {
            "p": "topic",
            "vt": "str"
        }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "",
    "payload": "",
    "payloadType": "date",
    "x": 140,
    "y": 300,
    "wires": [
        [
            "7cf32612d4e3cb63"
        ]
    ]
},
{
    "id": "7cf32612d4e3cb63",
    "type": "function",
    "z": "82b48005d8d8eb1a",
    "name": "motion sensor",
    "func": "var door = global.get("DoorOpen");

if(door==1){
 msg.payload="door opened. You can add your function here";
}
else if(door==0){
 msg.payload = "door closed. You can add your function here";
}

return msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 320,
    "y": 300,
    "wires": [
        [
            "0b9d0ad34f867991"
        ]
    ]
},
{
    "id": "67b509b7f84f4cc2",
    "type": "comment",
    "z": "82b48005d8d8eb1a",
    "name": "Stackoverflow link",
    "info": "https://stackoverflow.com/questions/74340554/check-if-an-event-has-happened-before-performing-an-action-in-nodered/74397929#74397929",
    "x": 170,
    "y": 40,
    "wires": []
},
{
    "id": "0b9d0ad34f867991",
    "type": "debug",
    "z": "82b48005d8d8eb1a",
    "name": "debug 19",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "false",
    "statusVal": "",
    "statusType": "auto",
    "x": 480,
    "y": 300,
    "wires": []
}

]

【讨论】:

    猜你喜欢
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 2013-05-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多