#Function with multiple messges
1 messages · Page 1 of 1 (latest)
I'm trying to write a function that takes the messages from 2 switches and does some Boolean logic on it
3 outputs :"SW1 on", "SW2 on" and "Both off"
If SW1 is on and SW2 is off then O1 sends a message
If SW1 is on and SW2 is on then O1 sends a message (SW1 overrides)
If SW1 is off and SW2 is on then O2 sends a message
If SW1 is off and SW2 is off then O3 sends a message
But I can't get it to work
Any ideas?
This is the flow (I hope, haven't exported before)
and this is the function "on message" part
var statsw1 = "off";
var statsw2 = "off";
var tt = "tt";
if (RED.util.getMessageProperty(msg, "data.entity_id") == "input_boolean.testsw1")
{
statsw1 = context.get(RED.util.getMessageProperty(msg, "data.state")) || "off";
}
else if (RED.util.getMessageProperty(msg, "data.entity_id") == "input_boolean.testsw2")
{
statsw2 = context.get(RED.util.getMessageProperty(msg, "data.state")) || "off";
}
tt = statsw1 + "/" + statsw2;
if (statsw1 == "on" && statsw2 == "on") {
return [{ topic: "sw1", payload: tt }, null, null];
}
else if (statsw1 == "on" && statsw2 == "off") {
return [{ topic: "sw1", payload: tt }, null, null];
}
else if (statsw1 == "off" && statsw2 == "on") {
return [null, { topic: "sw2", payload: tt }, null];
}
else if (statsw1 == "off" && statsw2 == "off") {
return [null, null, { topic: "stat", payload: tt }];
}
context.set('statsw1', statsw1);
context.set('statsw2', statsw2);
what's not working with it?