I have made a function node with following code.
var v_time = new Date();
var v_currentTime = (('00' + v_time.getHours()).slice(-2) + ':' + ('00' + v_time.getMinutes()).slice(-2));
var v_state;
// Constant definitions
const c_topic = "Morning1 ON";
const c_payload = "on";
// Time limits
const c_minTimeWeek = '05:30';
const c_maxTimeWeek = '09:00';
// output decleration
msg1={};
msg2={};
if (v_currentTime < c_minTimeWeek) { //min time
context.set("n_statusNo", 1);
v_state = 1;
context.set("n_statusText", "Min time");
}
else if (v_currentTime > c_maxTimeWeek) { //max time
context.set("n_statusNo", 2);
v_state = 2;
context.set("n_statusText", "Max time");
}
else { //in time window
context.set("n_statusNo", 3);
v_state = 3;
context.set("n_statusText", "In time");
}
const c_different = context.get("n_statusNoPrev") !== context.get("n_statusNo");
context.set("n_statusNoPrev", context.get("n_statusNo"));
if (c_different) { //value changed!
msg2.topic = c_topic; //print notification
msg2.payload = context.get("n_statusText");
//test
msg2.state = v_state;
msg2.prev = context.get("n_statusNoPrev");
msg2.diff = c_different;
return [null, msg2];
}
else {
if ((v_currentTime > c_minTimeWeek) && (v_currentTime < c_maxTimeWeek)) { //in time window
msg1.payload = c_payload;
//test
msg1.state = v_state;
msg1.prev = context.get("n_statusNoPrev");
return [msg1, null];
}
}
I try to only send notification when there is a state change.
But it is not fully working.
Sometimes I get fauls triggers with no payload.
Is there anybody that knows how to solve this problem.
The fauls trig offen comes the first time the input is set.