I have a flow where I have a function node which I would like to only send output on changes.
This how my flow looks like, it is not ready yet.
This is just for test!
That’s why many inputs comes from change nodes.
The code in the “Sauna On/Off” node look like this:
var v_saunaAutoEnable = context.flow.get("f_sauna_auto_enable");
var v_saunaOnOff = context.flow.get("f_sauna_on_off");
var v_saunaAuto = context.flow.get("f_sauna_auto");
var v_output;
msg.topic = "Sauna/OnOff";
if (v_saunaOnOff == "on")
{
if (v_saunaAutoEnable == "on")
{
if (v_saunaAuto == "on")
{
v_output = "on";
//msg.payload = "on";
}
else
{
v_output = "off";
//msg.paylad = "off";
}
}
else
{
v_output = "on";
//msg.payload = "on";
}
}
else
{
v_output = "off";
//msg.payload = "off";
}
//function change()
//{
// msg.payload = v_output;
// return msg;
//}
//return msg;
I would like to only set the msg when the “v_output” has been changed.
Not each time the input is trigged.
Is that possible to do?
In Javascript there is a change function, but I don’t know if it is possible the use that function in Node red and how the syntax should be.
Does anybody know how this is done?
serkank
December 20, 2019, 3:19pm
2
I think you can use RBE node for this.
Okay, but if I want to do it in the function node?
Kermit
December 20, 2019, 5:30pm
4
const different = context.get("previousValue") !== v_output;
context.set("previousValue", v_output);
if (different) {
msg.payload = v_output;
return msg;
}
1 Like
Sorry I was wrong it is not fully working.
I get one message without any payload.
It is always when the first switch changes from ON to OFF or the other way around.
A fault message will be sent.
In this case when only one input (input3) has changed to ON it should not send any output. But it does.
Something is not working correct.
This is my code in the function node:
var v_enable = [];
var v_input = [];
var v_status_cntr = 0;
var v_no_enable = 0;
var v_status = 0;
var v_output;
v_enable[1] = flow.get("f_autoAlvaWin", "file");
v_enable[2] = flow.get("f_autoTv", "file");
v_enable[3] = flow.get("f_autoMejaWin", "file");
v_enable[4] = flow.get("f_autoAlvaMirror", "file");
v_enable[5] = flow.get("f_autoMejaMirror", "file");
v_enable[6] = flow.get("f_autoWallPlug8", "file");
v_enable[7] = flow.get("f_autoWallPlug9", "file");
v_enable[8] = flow.get("f_autoWallPlug10", "file");
v_enable[9] = flow.get("f_autoWallPlug11", "file");
v_enable[10] = flow.get("f_autoWallPlugOut1", "file");
v_input[1] = context.flow.get("f_wallPlug3");
v_input[2] = context.flow.get("f_wallPlug4");
v_input[3] = context.flow.get("f_wallPlug5");
v_input[4] = context.flow.get("f_wallPlug6");
v_input[5] = context.flow.get("f_wallPlug7");
v_input[6] = context.flow.get("f_wallPlug8");
v_input[7] = context.flow.get("f_wallPlug9");
v_input[8] = context.flow.get("f_wallPlug10");
v_input[9] = context.flow.get("f_wallPlug11");
v_input[10] = context.flow.get("f_wallPlugOut1");
for (i=1;i<11;i++) //numbers of switches
{
if(v_enable[i] == "on") //selected enabled
{
v_no_enable++; //Number of enabled siwtches
if(v_input[i] == "on")
{
v_status_cntr++; //ON add conuter
}
else if(v_input[i] == "off")
{
v_status_cntr--; //OFF minus counter
}
}
}
if((v_no_enable > 0) && (v_status_cntr == v_no_enable))
{
v_status = 1; //all ON
}
else if((v_no_enable > 0) && (v_status_cntr == (-v_no_enable)))
{
v_status = 2; //all OFF
}
else
{
v_status = 0; //not all ON or OFF
}
//Test
msg.enable1 = "enable1: " + v_enable[1];
msg.enable2 = "enable2: " + v_enable[2];
msg.enable3 = "enable3: " + v_enable[3];
msg.enable4 = "enable4: " + v_enable[4];
msg.enable5 = "enable5: " + v_enable[5];
msg.enable6 = "enable6: " + v_enable[6];
msg.enable7 = "enable7: " + v_enable[7];
msg.enable8 = "enable8: " + v_enable[8];
msg.enable9 = "enable9: " + v_enable[9];
msg.enable10 = "enable10: " + v_enable[10];
msg.input1 = "input1: " + v_input[1];
msg.input2 = "input2: " + v_input[2];
msg.input3 = "input3: " + v_input[3];
msg.input4 = "input4: " + v_input[4];
msg.input5 = "input5: " + v_input[5];
msg.input6 = "input6: " + v_input[6];
msg.input7 = "input7: " + v_input[7];
msg.input8 = "input8: " + v_input[8];
msg.input9 = "input9: " + v_input[9];
msg.input10 = "input10: " + v_input[10];
msg.status1 = "Status: " + v_status;
msg.topic = "All Wall Plug 433MHz status";
if(v_status == 1)
{
v_output = "on";
}
else if (v_status == 2)
{
v_output = "off";
}
const different = context.get("previousValue") !== v_output;
context.set("previousValue", v_output);
if (different)
{
msg.payload = v_output;
return msg;
}
What could be wrong?