Hi,
I have a Node Red flow that has been working fine for some time, operating solenoid valves for my garden watering system. I have recently replaced the power supply with an old ATX PC type which has a permanent on 5V supply (Standby @ 2A) and a separate lead, which when grounded, wakes up the PSU to provide 12V etc. I have wired the "wake up" lead to ground via relay 4.
What I am trying to achieve in a function node is to turn on the 12V if any of the three solenoid valves (relays 1 to 3) are sent MQTT messages to open and to turn off the 12V only if all three solenoid valves have been sent MQTT messages to close.
The code currently sends out "on" every time any solenoid valve is demanded open (this is OK), but "off" any time any valve is demanded closed (this is not OK).
Can anyone help me to understand what I have to do to make this work? I'm guessing that the way I'm trying to add the values in the context variables is the bit that's failing. I can see the values of 0 and 1 in the Context Data tab and that seems OK.
I've copied the code below, hopefully, my error will be evident to someone?
Thanks,
Tim
On Start Initialisation:
// Code added here will be run once
// whenever the node is started.
if (context.get("relay1") === undefined) {
context.set("relay1", 0)
}
if (context.get("relay2") === undefined) {
context.set("relay2", 0)
}
if (context.get("relay3") === undefined) {
context.set("relay3", 0)
}
On Message:
var topic=msg.topic;
var payload=msg.payload;
var demandstate;
if (payload=="on"){
if (topic=="garden/GPatio/relay1"){
context.set("relay1", 1);
}
else if (topic=="garden/GPatio/relay2"){
context.set("relay2", 1);
}
else if (topic=="garden/GPatio/relay3"){
context.set("relay3", 1);
}
}
else if (payload=="off"){
if (topic=="garden/GPatio/relay1"){
context.set("relay1", 0);
}
else if (topic=="garden/GPatio/relay2"){
context.set("relay2", 0);
}
else if (topic=="garden/GPatio/relay3"){
context.set("relay3", 0);
}
demandstate = (context.get("relay1")+context.get("relay2")+context.get("relay3"));
if (demandstate==0){
msg.payload = "off";
}
}
//
return msg;