Hello all,
I have written a function node which works, but I think there must be a clever way of doing it, rather than individual IF/ELSE statements.
Here is the flow, simply mqtt json data going through the function node, being filtered and sent to appropriate Blynk pin:
[{"id":"764b06c6.78b878","type":"mqtt in","z":"6f68fb89.34bc24","name":"","topic":"BW-1.27_MQTT/message","qos":"1","datatype":"json","broker":"","x":390,"y":780,"wires":[["eb32bac.74dab48"]]},{"id":"eb32bac.74dab48","type":"function","z":"6f68fb89.34bc24","name":"Check incoming and send to appropriate Blynk pin","func":"//POWER\nif (msg.payload.power === true)\n{\nvar msgPower ={\n payload: \"1\",\n pin: \"71\" };\n}\nelse {\n msgPower ={\n payload: \"0\",\n pin: \"71\" };\n}\n\n\n\n//FILTER\nif (msg.payload.filter === true)\n{\nvar msgFilter ={\n payload: \"1\",\n pin: \"74\" };\n}\nelse\n{\n msgFilter ={\n payload: \"0\",\n pin: \"74\" };\n}\n\n\n\n//HEATING OR HEATER\nif ((msg.payload.heating || msg.payload.heater) === true)\n{\nvar msgHeat ={\n payload: \"1\",\n pin: \"75\" };\n}\nelse\n{\n msgHeat ={\n payload: \"0\",\n pin: \"75\" };\n}\n\n\n//CELSIUS/FAHRENHEIT\nif (msg.payload.celsius === true)\n{\nvar msgCelsius ={\n payload: \"1\",\n pin: \"76\" };\n}\nelse\n{\n msgCelsius ={\n payload: \"0\",\n pin: \"76\" };\n}\n\n\n//AIR\nif (msg.payload.air === true)\n{\nvar msgAir ={\n payload: \"1\",\n pin: \"77\" };\n}\nelse\n{\n msgAir ={\n payload: \"0\",\n pin: \"77\" };\n}\n\n\n//LOCKED\nif (msg.payload.locked === true)\n{\nvar msgLocked ={\n payload: \"1\",\n pin: \"79\" };\n}\nelse\n{\n msgLocked ={\n payload: \"0\",\n pin: \"79\" };\n}\n\n\n\nreturn [ [ msgPower, msgFilter, msgHeat, msgCelsius, msgAir, msgLocked ] ];\n","outputs":1,"noerr":0,"x":790,"y":780,"wires":[["e832b721.ad19a8"]]},{"id":"e832b721.ad19a8","type":"blynk-ws-out-write","z":"6f68fb89.34bc24","name":"Blynk (dynamic pin by code)","pin":"0","pinmode":"1","client":"","x":1170,"y":780,"wires":[]}]
Here is the MQTT json payload, I am only need some of the topics as per the function node:
Here is the function node code, I feel there is some way to cut it down in a clever way:
//POWER
if (msg.payload.power === true)
{
var msgPower ={
payload: "1",
pin: "71" };
}
else {
msgPower ={
payload: "0",
pin: "71" };
}
//FILTER
if (msg.payload.filter === true)
{
var msgFilter ={
payload: "1",
pin: "74" };
}
else
{
msgFilter ={
payload: "0",
pin: "74" };
}
//HEATING OR HEATER
if ((msg.payload.heating || msg.payload.heater) === true)
{
var msgHeat ={
payload: "1",
pin: "75" };
}
else
{
msgHeat ={
payload: "0",
pin: "75" };
}
//CELSIUS/FAHRENHEIT
if (msg.payload.celsius === true)
{
var msgCelsius ={
payload: "1",
pin: "76" };
}
else
{
msgCelsius ={
payload: "0",
pin: "76" };
}
//AIR
if (msg.payload.air === true)
{
var msgAir ={
payload: "1",
pin: "77" };
}
else
{
msgAir ={
payload: "0",
pin: "77" };
}
//LOCKED
if (msg.payload.locked === true)
{
var msgLocked ={
payload: "1",
pin: "79" };
}
else
{
msgLocked ={
payload: "0",
pin: "79" };
}
return [ [ msgPower, msgFilter, msgHeat, msgCelsius, msgAir, msgLocked ] ];
Any information is appreciated!