# Help with javascript in a function node

**URL:** <https://discourse.nodered.org/t/help-with-javascript-in-a-function-node/48490>\
**Category:** General\
**Created:** [16 July 2021 12:59 UTC](https://discourse.nodered.org/t/help-with-javascript-in-a-function-node/48490 "2021-07-16T12:59:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [16 July 2021 12:59 UTC](https://discourse.nodered.org/t/help-with-javascript-in-a-function-node/48490/1 "2021-07-16T12:59:56Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [16 July 2021 13:51 UTC](https://discourse.nodered.org/t/help-with-javascript-in-a-function-node/48490/2 "2021-07-16T13:51:37Z")

</div>

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...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3dcacf7a1428d89ed0797a322ec00e356d02bbcc.png)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [16 July 2021 13:52 UTC](https://discourse.nodered.org/t/help-with-javascript-in-a-function-node/48490/3 "2021-07-16T13:52:48Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/54b5d0be054e28080b1432b527a76776025564cd.png)

Hey Steve, looking at the same 😉

---

<div class="post-metadata">

**Author:** ![Bodad](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bodad/32/22612_2.png) [@Bodad](https://discourse.nodered.org/u/Bodad)\
**Post date:** [16 July 2021 14:22 UTC](https://discourse.nodered.org/t/help-with-javascript-in-a-function-node/48490/4 "2021-07-16T14:22:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 September 2021 14:22 UTC](https://discourse.nodered.org/t/help-with-javascript-in-a-function-node/48490/5 "2021-09-14T14:22:57Z")

</div>

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