Help with javascript in a function node

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;

As far as I can see, your function doesnt actually do anything. if msg.payload is "on", "off" or even "hello", it will return the same unmodified msg and same payload - everytime.

See here...

Just looking at it quickly the logic is not working as you expect since you are always returning the msg including the msg.payload that might be "off" but should not. If you change the last bit of your code like below it will only send out "off" when all 3 relays are "off".

This change will still send "on" if any relay is "on" but I think that is correct

Hey Steve, looking at the same :wink:

krambriw,

Both you and Steve-Mcl are right and thank you both for the feedback. There isn't that much wrong with my actual code, other than I struggle to get syntax right; it's just the logic that's up the Swanee!

Thanks again,

Tim

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.