# Code question from a dopey newbe\[solved\]

**URL:** <https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742>\
**Category:** General\
**Created:** [22 July 2018 15:42 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742 "2018-07-22T15:42:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonHeron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonheron/32/6261_2.png) [@JonHeron](https://discourse.nodered.org/u/JonHeron)\
**Post date:** [22 July 2018 15:42 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/1 "2018-07-22T15:42:24Z")

</div>

Hello,  
I am setting up a flow for automating my greenhouse using a pi 3.  
I have some prior experience coding in Qt c++ but it has been a few years...  
I want to return 'true' or 'false' to operate a switch but due to my lack of coding skills 'result' ends up 'undefined' and msg.payload returns 'undefined'.  
I hope someone can set me straight here on setting the variables in functions...  
Best regards,  
Jon

```auto
    //gets the temp and humidity reading from the DHT11
var hum = msg.humidity;
var tmp = msg.payload;
var result;
setInterval(function(){
    var hour = new Date().getHours();
    if (hour >= 8 && hour < 18) {
        controlMister(); 
    }
} , 1000*60);
function controlMister(){
if(hum < 60 && tmp > 10)
{
    result = "false";
}
    else
{
        result = "true";
}
//node.warn(result);
}
node.warn(result);
msg.payload = result;
return msg;

```

---

<div class="post-metadata">

**Author:** ![JonHeron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonheron/32/6261_2.png) [@JonHeron](https://discourse.nodered.org/u/JonHeron)\
**Post date:** [22 July 2018 15:43 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/2 "2018-07-22T15:43:02Z")

</div>

How do you use code tags on this forum?  
Jon

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 July 2018 15:50 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/3 "2018-07-22T15:50:24Z")

</div>

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![JonHeron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonheron/32/6261_2.png) [@JonHeron](https://discourse.nodered.org/u/JonHeron)\
**Post date:** [22 July 2018 15:56 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/4 "2018-07-22T15:56:38Z")

</div>

Thank you!  
Cheers,  
Jon

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [22 July 2018 16:08 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/5 "2018-07-22T16:08:13Z")

</div>

Try:

```auto
//gets the temp and humidity reading from the DHT11
var hum = msg.humidity;
var tmp = msg.payload;
var result;

setInterval(function(){
    var hour = new Date().getHours();
    if (hour >= 8 && hour < 18) {
        controlMister();
    }
}, 1000*60);

function controlMister(){
    if(hum < 60 && tmp > 10) {
        result = "false";
    } else {
        result = "true";
    }
    //node.warn(result);
    msg.payload=result;
    node.send(msg);
}
//node.warn(result);
//msg.payload = result;
return null;

```

to test it now you should change this line

```auto
    if (hour >= 8 && hour < 20) {

```

---

<div class="post-metadata">

**Author:** ![JonHeron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonheron/32/6261_2.png) [@JonHeron](https://discourse.nodered.org/u/JonHeron)\
**Post date:** [22 July 2018 16:19 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/6 "2018-07-22T16:19:42Z")

</div>

Thank you!  
Works as intended!  
Cheers,  
Jon

---

<div class="post-metadata">

**Author:** ![JonHeron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonheron/32/6261_2.png) [@JonHeron](https://discourse.nodered.org/u/JonHeron)\
**Post date:** [22 July 2018 16:21 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/7 "2018-07-22T16:21:34Z")

</div>

Why is 'result' (which I assumed was a global since I added it outside the function) data not maintained/exposed outside of the function?

---

<div class="post-metadata">

**Author:** ![cflurin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cflurin/32/29_2.png) [@cflurin](https://discourse.nodered.org/u/cflurin)\
**Post date:** [22 July 2018 16:24 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/8 "2018-07-22T16:24:18Z")

</div>

`result` is a node local var, if you need a global, you have to use global.get() and global.set().

[https://nodered.org/docs/writing-functions#storing-data](https://nodered.org/docs/writing-functions#storing-data)

... and in your case here when leaving the function node, result isn't set.

---

<div class="post-metadata">

**Author:** ![JonHeron](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonheron/32/6261_2.png) [@JonHeron](https://discourse.nodered.org/u/JonHeron)\
**Post date:** [22 July 2018 16:50 UTC](https://discourse.nodered.org/t/code-question-from-a-dopey-newbe-solved/1742/9 "2018-07-22T16:50:55Z")

</div>

AHA!  
Thank you very much.  
Cheers,  
Jon
