What 12-2? Like what am I thinking, I know. If you read my PDU #12 write up and looked at the great comments, you would have noticed that Andy Workshopshed (DAB backed Andy up on this) suggested I do what I did in many functions in one. Well, at first I thought it was not going to work because the source does not come through, but then I got thinking, what if I could. But if the community can help me make my project better I should try.
I looked at the output of the RBE node message, not just the payload, but the message. Here is what I got:
I get a topic that is the GPIO port that is being used. So I can write a function. Now how do I update all of these functions into one. Well, here is what it looks like:
That WS Payload 1 was an old name, now I need to call it "Big Daddy" as it does it all. Notice there are 10 output. Eight of them are for MQTT messages and one for WS and MongoDB update. Both WS and Mongo do JSON messages. One thing I need to look into is that the MQTT messages could be JSON and how to make Home Assistant handle JSON in MQTT for a switch. They have that for lights and I use it for RGB lights, but I have not tried for a switch (outlet) yet.
So what does BIG DADDY look like?
var GPIO = msg.topic;
// State Set
var WSstate = "off";
var DBstate = "off";
var MQTTstate = "OFF"
if (msg.payload == "1") {
WSstate = "on";
DBstate = "on";
MQTTstate = "ON";
}
// Control Set
var control = "";
if (GPIO == "GP45") {
control = "outlet1";
}
if (GPIO == "GP47") {
control = "outlet2";
}
if (GPIO == "GP49") {
control = "outlet3";
}
if (GPIO == "GP15") {
control = "outlet4";
}
if (GPIO == "GP42") {
control = "outlet5";
}
if (GPIO == "GP84") {
control = "outlet6";
}
if (GPIO == "GP41") {
control = "outlet7";
}
if (GPIO == "GP78") {
control = "outlet8";
}
var WSmsg = { payload: [
{
control: control,
state: WSstate
}
]
};
var MQTTmsg = { payload: MQTTstate };
var DBmsg = {
query: {
device: "tower00",
control: control
},
payload: {
device: "tower00",
control: control,
state: DBstate
}
};
// Return values now!
if (GPIO == "GP45") {
return [MQTTmsg,null,null,null,null,null,null,null,WSmsg,DBmsg];
}
if (GPIO == "GP47") {
return [null,MQTTmsg,null,null,null,null,null,null,WSmsg,DBmsg];
}
if (GPIO == "GP49") {
return [null,null,MQTTmsg,null,null,null,null,null,WSmsg,DBmsg];
}
if (GPIO == "GP15") {
return [null,null,null,MQTTmsg,null,null,null,null,WSmsg,DBmsg];
}
if (GPIO == "GP42") {
return [null,null,null,null,MQTTmsg,null,null,null,WSmsg,DBmsg];
}
if (GPIO == "GP84") {
return [null,null,null,null,null,MQTTmsg,null,null,WSmsg,DBmsg];
}
if (GPIO == "GP41") {
return [null,null,null,null,null,null,MQTTmsg,null,WSmsg,DBmsg];
}
if (GPIO == "GP78") {
return [null,null,null,null,null,null,null,MQTTmsg,WSmsg,DBmsg];
}
TODO
- Consolidate other functions like this one in my other flow, if I can. That one would have to take MQTT and WS messages and put them into one function.
- See if Home Assistant will accept JSON MQTT message for a switch.
Conclusion
Community ROCKS! Thank you Andy and DAB for pushing me.


Top Comments