Node-red - 需要一个数字值的多输入函数

Node-red - need a multi-input function for a number value

所以我刚刚开始掌握 node-red,我需要创建一个有条件的全局函数。

我有两个单独的 global.payloads 设置为 0 或 1 的数值。

我现在需要做的是,如果 global.payload 等于值 1 则遵循此流程,如果它等于值 0 则遵循此流程。

我只是对函数语句的语法有点困惑。任何帮助感激不尽。

您可以使用 Switch 节点而不是 Function 节点来执行此操作。

由于您尚未接受当前答案,我想我会试一试。 我认为这是处理来自两个独立全局上下文的输入所需要的。我在这里用两个单独的 inject 节点模拟它们来演示:

checkconf inject 节点发出 1 或 0。meshstatus 节点也是如此。将您的真实输入替换为那些注入节点。真正的工作是在函数内部完成的:

var c = context.get('c') || 0;  // initialize variables
var m = context.get('m') || 0;

if (msg.topic == "checkconf")  // update context based on topic of input
{
    c = {payload: msg.payload};
    context.set("c", c);  // save last value in local context
}

if (msg.topic == 'meshstatus') // same here
{
    m = {payload: msg.payload};
    context.set('m', m); // save last value in local context
}

// now do the test to see if both inputs are triggered...
if (m.payload == 1) // check last value of meshstatus first
{
    if (c.payload == 1)  // now check last value of checkconf
        return {topic:'value', payload: "YES"};
}
else
    return {topic:'value', payload: "NO"};

请务必设置您用作输入的任何内容的 "topic" 属性,以便 if 语句可以区分两个输入。祝你好运!