Hi,
I have created a flow in Node-RED that shall determind if all my switches are ON or OFF.
Depending on which switches that the automation are enabled for or not.
When all switches are ON or OFF a notification shall be sent.
I use Pushbullet for that.
The flow looks like this
The code in the “Z-wave Notification New” function-node looks like this
let v_enable = [];
let v_input = [];
let v_output;
const c_totNo = 5; // total number of switches that are supervised
v_enable[0] = context.flow.get("f_autoFacade", "file");
v_enable[1] = context.flow.get("f_autoHallwayWin", "file");
v_enable[2] = context.flow.get("f_autoStairsWin", "file");
v_enable[3] = context.flow.get("f_autoKitchenWin", "file");
v_enable[4] = context.flow.get("f_autoDiningWin", "file");
v_input[0] = context.flow.get("f_facadeLight");
v_input[1] = context.flow.get("f_hallwayWindowLight");
v_input[2] = context.flow.get("f_stairsWindowLight");
v_input[3] = context.flow.get("f_kitchenWindowLight");
v_input[4] = context.flow.get("f_diningWindowLight");
// calculate number of enabled switches
function numberOfEnabled(enableNo) {
let v_no_enable = 0;
for (let i=0; i<enableNo.length; i++) { //numbers of switches
if(enableNo[i] === "on") { //selected enabled
v_no_enable++; //Number of enabled siwtches
}
}
return v_no_enable;
}
// calculate number of turned on switches
function statusNoOn(inputNo, enabled) {
let v_status_cntr_on = 0;
for (let j=0; j<enabled; j++) { //numbers of switches
if(inputNo[j] === "on") {
v_status_cntr_on++; //ON, add conuter
}
}
return v_status_cntr_on;
}
// calculate number of turned off switches
function statusNoOff(inputNo, enabled) {
let v_status_cntr_off = 0;
for (let k=0; k<enabled; k++) { //numbers of switches
if(inputNo[k] === "off") {
v_status_cntr_off++; //OFF, minus counter
}
}
return v_status_cntr_off;
}
let v_enabled = numberOfEnabled(v_enable);
let v_statusOn = statusNoOn(v_input, v_enabled);
let v_statusOff = statusNoOff(v_input, v_enabled);
// All ON or OFF
function allOnOff(enabled, statusOn, statusOff) {
if((enabled > 0) && (statusOn === enabled)) return 1; //all ON
if((enabled > 0) && (statusOff === enabled)) return -1; //all OFF
}
// check if status has changed, all On/Off
const c_ZwaveAllPrevValue = context.get("n_ZwaveAllPrevValue_new");
const c_ZwaveAllValue = allOnOff(v_enabled, v_statusOn, v_statusOff);
let v_different = c_ZwaveAllPrevValue !== c_ZwaveAllValue;
// set previous value of status all On/Off
context.set("n_ZwaveAllPrevValue_new", c_ZwaveAllValue);
if(c_ZwaveAllValue > 0) {
v_output = "on";
}
else if (c_ZwaveAllValue < 0) {
v_output = "off";
}
// only set output if it has been changed
if (v_different) {
msg.topic = "All Z-wave node status";
msg.payload = v_output;
//Test
msg.enable1 = "enable1: " + v_enable[0];
msg.enable2 = "enable2: " + v_enable[1];
msg.enable3 = "enable3: " + v_enable[2];
msg.enable4 = "enable4: " + v_enable[3];
msg.enable5 = "enable5: " + v_enable[4];
msg.input1 = "input1: " + v_input[0];
msg.input2 = "input2: " + v_input[1];
msg.input3 = "input3: " + v_input[2];
msg.input4 = "input4: " + v_input[3];
msg.input5 = "input5: " + v_input[4];
msg.enable_no = "enable no: " + v_enabled;
msg.status_On = "status cntr On: " + v_statusOn;
msg.status_Off = "status cntr Off: " + v_statusOff;
msg.zwaveAllValue = "zwaveAllValue: " + c_ZwaveAllValue;
msg.zwaveAllPrevValue = "zwaveAllPrevValue: " + c_ZwaveAllPrevValue;
msg.diff = "Diff: " + v_different;
msg.out = "Out: " + v_output;
return [msg];
}
The problem I have is when the first switch is turned on/off I got a trig on the output with payload = null.
And that will cause a fake notification, see screenshot.
I have now solved the problem with adding a switch-node on the output that only passes through payload with “on” or “off”.
It works but is not a very nice solution!
Is there any otherway to solve this problem inside the function-node?