# Using IF and env variables

**URL:** https://discourse.nodered.org/t/using-if-and-env-variables/58480
**Category:** General
**Created:** [16 February 2022 17:40 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480 "2022-02-16T17:40:50Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)
#### Post date: [16 February 2022 17:40 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/1 "2022-02-16T17:40:50Z")

</div>

I'm trying to use a env variable in an if statement, from a sub flow Command1  
as a string as below  
Command1=payload[0].Device==off  
var Command=env.get("Command1")  
if(Command1===true){  
return msg};  
This doesn't work. Mark

---

<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 February 2022 18:36 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/2 "2022-02-16T18:36:40Z")

</div>

> [@Mark\_Ellis](#):
>
> Command1=payload[0].Device==off  
> var Command=env.get("Command1")  
> if(Command1===true){  
> return msg};  
> This doesn't work

Hi, firstly, you should surround code with backticks  
`````  
`like this`  
`````

e.g...

```auto
Command1=payload[0].Device==off
var Command=env.get("Command1")
if(Command1===true){
return msg};

```

> [@Mark\_Ellis](#):
>
> This doesn't work

It is most likely this doesnt work because `off` is not defined and therefore `Command1` will be `false` and the `if` will always evaluate to `false` - never returning a `msg`  
PS in a function, `payload` should be `msg.payload`

You might want to enable the monaco editor in node-red settings.js - it will show you more of your issues...

![chrome_spvzCX4fzc](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/4/e42f04446aa15d68bfad1af3620dc9384436d194.gif)

---

<div class="post-metadata">

### Author: ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)
#### Post date: [16 February 2022 22:47 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/3 "2022-02-16T22:47:27Z")

</div>

The command below (Line 1 of code) after = is a string which is set in the subflow properties page Command1 is the name of the env variable which stores the string

Command1 =payload[0].Device==off  
var Command=env.get("Command1")  
if(Command===true){  
return msg};

This doesn't work. Mark

---

<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: [16 February 2022 22:54 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/4 "2022-02-16T22:54:45Z")

</div>

Hi @Mark_Ellis

please take on board what @Steve-Mcl about how to format code in the forum. That makes it much more readable.

So are you saying the first line of code isn't actually part of your Function node code - you're telling us that you have defined `Command1` in the Subflow properties to have the value `"payload[0].Device==off"` ?

And that your Function code is actually:

```auto
var Command=env.get("Command1")
if(Command===true){
return msg};

```

?

Assuming I have understood what you are saying, then in your code, `Command` will have the value of the env var - the literal string `"payload[0].Device==off"`. There's nothing there to evaluate that string as a piece of code.

At this point it might be more helpful if you describe what you want this code to actually do.

---

<div class="post-metadata">

### Author: ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)
#### Post date: [16 February 2022 23:28 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/5 "2022-02-16T23:28:50Z")

</div>

Yes that is correct, this is how i would like it to work,  
if(msg.payload[0].Device=="Off"){  
but like this  
if(Command){  
Command holds the msg.payload[0].Device==Off from the env variable from subflow.

---

<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: [16 February 2022 23:36 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/6 "2022-02-16T23:36:47Z")

</div>

So the following might work...

```auto
const Command=env.get("Command1")
const result = eval(Command)
if (result) {
   return msg
}

```

In general, `eval` should be avoided unless you're in complete control over what Strings it evaluates - as it could allow arbitrary code to be executed.

But if it's only going to be run on strings you provide via the env vars, then it'll probably be okay.

---

<div class="post-metadata">

### Author: ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)
#### Post date: [17 February 2022 09:08 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/7 "2022-02-17T09:08:11Z")

</div>

Hi, Thanks that works, 1 more question for you. why does this not work  
switch(result||env.get("Command1").includes("Hazel")===true)  
switch(result||env.get("Command1").includes("Hazel"))  
if(result||env.get("Command1").includes("Hazel")===true)  
if(result||env.get("Command1").includes("Hazel"))  
get error: "TypeError: Cannot read property '0' of undefined"  
this does though  
if(result)  
switch(result)  
Thanks for you help, couldn't of done it without you.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [17 February 2022 11:10 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/8 "2022-02-17T11:10:19Z")

</div>

Is that supposed to be javascript?

---

<div class="post-metadata">

### Author: ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)
#### Post date: [17 February 2022 11:25 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/9 "2022-02-17T11:25:23Z")

</div>

Yes, just shortened it for quickness, sorry if i confused you, just added to already code you sent me, which i copied to function node. Mark

---

<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: [17 February 2022 11:30 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/10 "2022-02-17T11:30:04Z")

</div>

@Mark_Ellis **please** format your code properly.

Use the `</>` button in the toobar.

> [@Mark\_Ellis](#):
>
> Yes, just shortened it for quickness, sorry if i confused you

You have shared a fragments of code that don't make much sense by itself and is invalid js syntax.  
That makes it hard to know what you intent is with the code, so it is impossible to say what might be going wrong.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [17 February 2022 11:42 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/11 "2022-02-17T11:42:45Z")

</div>

> [@Mark\_Ellis](#):
>
> shortened it for quickness,

When you are asking why a section of code does not work it is essential to post the exact code you are using, since very often bugs are caused by typos. Ideally post the full function node.  
To debug your code you can add `node.warn()` statements at appropriate points in order to follow what happens.

---

<div class="post-metadata">

### Author: ![Mark\_Ellis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mark_ellis/32/79062_2.png) [@Mark\_Ellis](https://discourse.nodered.org/u/Mark_Ellis)
#### Post date: [17 February 2022 12:04 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/12 "2022-02-17T12:04:17Z")

</div>

```auto
//if(env.get("Command1").includes("Hazel")){
//return msg}

const Command=env.get("Command1")
const result = eval(Command)

switch(result||env.get("Command1").includes("Hazel")){
case true:
   return msg;
case false:

}

```

I would like to incorporate the first if(after //) into the switch statement as an OR, pated directly from function node.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [17 February 2022 12:39 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/13 "2022-02-17T12:39:13Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [17 February 2022 16:20 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/14 "2022-02-17T16:20:52Z")

</div>

You have been pointed to the method to be used for posting flows three times now. If you don't understand what is meant by those instructions then please ask, do not just ignore them.

---

<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: [18 April 2022 16:21 UTC](https://discourse.nodered.org/t/using-if-and-env-variables/58480/15 "2022-04-18T16:21:17Z")

</div>

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