# Beginner questions on IF expressions and wildcard usage in function node

**URL:** <https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999>\
**Category:** General\
**Created:** [3 September 2023 11:43 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999 "2023-09-03T11:43:38Z")\
**Posts on this page:** 20\
**Page:** 5

<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:** [6 September 2023 06:14 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/81 "2023-09-06T06:14:55Z")

</div>

> [@AleXSR700](#):
>
> Is there a way I can connect a debug node to any of the outputs just to check if the flow variables were initialized?

That is not necessary. Just view your context variables in the context viewer on the RH sidebar

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 06:45 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/82 "2023-09-06T06:45:08Z")

</div>

I never even saw that.

Detail question:  
I do not want to connect the function initializing the variables to the mqtt in. Otherwise they will be initialized on every message.

If I initialize them once everytime I make a change, will they remain available forever? Or will I need to initialize again after e.g. a Node-RED reboot?

I now use an inject node set to "once after 0.1 s", but I am not sure if this will only be "everytime I manually deploy" or "everytime the flow is started"=mqtt in receives a message or "everytime Node-RED is started" aka on reboot/restart of Node-RED which should also be kind of a deployment, I hope.

EDIT:  
And where are flow variables and context variables stored? In RAM or written to drive?

---

<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:** [6 September 2023 07:38 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/84 "2023-09-06T07:38:14Z")

</div>

> [@AleXSR700](#):
>
> And where are flow variables and context variables stored? In RAM or written to drive?

It depends on what you set in settings.js. You should read the documentation here:  
[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 11:37 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/85 "2023-09-06T11:37:15Z")

</div>

Somehow, and I mean no offense, I have so far not found the information I was looking for in the docs.  
Maybe it is the colorful nature of the docs or the idea that visual flow nodes should be used in a basic way and everything more complex does not belong in the general docs.  
Plus I usually don't search for anything that is already in the node docs.

But when I searched for variables I landed on the environment variables page. Which was not helpful. Google then found me the flow etc. variables but I did not realize that Node-RED distinguishes between variables (environment) and context (what I would consider variables).

It is learning in progress, I guess 🙂

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 September 2023 12:06 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/86 "2023-09-06T12:06:24Z")

</div>

The devs are always looking (hoping?!) for people to contribute to the docs. 😉

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 September 2023 12:35 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/87 "2023-09-06T12:35:57Z")

</div>

> [@AleXSR700](#):
>
> I did not realize that Node-RED distinguishes between variables (environment) and context (what I would consider variables).

I have to admit that the name "context" for what most other languages would call a variable seems bizarre.  
Then there is "global context" - available globally, "flow context" - available througout a flow and just plain "context" - available to a single node. What? Maybe "node context" has some other meaning.

I have seen a lot of mentions of environment variables too, yet they seem almost totally useless since they are **not variable** (!) but fixed at Node-red startup and have to be defined in settings.js (I'm running NR as a service on Linux)  
What I understand by "environment variables" eg $PATH _are_ accessible in the editor but only via an exec node `echo $PATH`

Every programming language comes with it's own names: pointers, dictionaries, tuples, objects.  
It is frustrating though when the designers overload a well understood word with a new meaning.

---

<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:** [6 September 2023 12:39 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/88 "2023-09-06T12:39:20Z")

</div>

> [@jbudd](#):
>
> What I understand by "environment variables" eg $PATH _are_ accessible in the editor but only via an exec node `echo $PATH`

PATH should be available in exactly the same way as any other environment variable. Try this in a function node

```auto
msg.payload = env.get("PATH")
return msg;

```

---

<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:** [6 September 2023 12:42 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/89 "2023-09-06T12:42:58Z")

</div>

> [@AleXSR700](#):
>
> I did not realize that Node-RED distinguishes between variables (environment) and context (what I would consider variables).

Variables is a generic term. There are environment variables (which are OS level things), global, flow and node context variables, variables in functions declared by, for example,  
`let myVar = 1`  
and probably also others I can't immediately bring to mind.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 September 2023 12:50 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/90 "2023-09-06T12:50:06Z")

</div>

> [@Colin](#):
>
> ```auto
> msg.payload = env.get("PATH")
> return msg;
> 
> ```

Yes you are right Colin! 😀

What does this mean then?

> Running as a service
> 
> When Node-RED is running as a service having been installed using the provided [script](https://nodered.org/docs/getting-started/raspberrypi), it will not have access to environment variables that are defined only in the calling process. ...

There is no env.set()  
I'm not sure if I would expect one given the shells within shells nature of Linux

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 September 2023 12:57 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/91 "2023-09-06T12:57:47Z")

</div>

Environment variables are actually available in multiple ways in Node-RED:

[Using environment variables : Node-RED (nodered.org)](https://nodered.org/docs/user-guide/environment-variables)

They are variable but only in the context of the OS environment. Except where they are not! Since Node-RED has now introduced several places where you can set what it calls environment variables that really aren't 🙂 Those being in groups, flows and sub-flows.

> [@jbudd](#):
>
> There is no env.set()

That is correct. Because the whole point of them is that they are set in the OS environment (or context if you prefer that term). At the OS level, env variables are defined BEFORE a service starts. Changing them from within a service therefore makes no sense.

Their use in this way is a tradition I suppose but that's the way everyone now expects them to work.

This is why you can use them to control things like Node-RED's port, userDir and other settings global to Node-RED.

They are also often used to dynamically set id's and passcodes when starting a service. However, that really should not be done since it means that the information is in memory - unencrypted - and so available to malware.

> [@jbudd](#):
>
> Every programming language comes with it's own names: pointers, dictionaries, tuples, objects.  
> It is frustrating though when the designers overload a well understood word with a new meaning.

General purpose computing is a complex topic of course so not surprising that terminology is also complex.

All the names you've mentioned could be included in variables but also in constants. They are examples of STRUCTURES. Well, maybe not pointers but hopefully they are fairly obvious. You could think of them as an alias to some actual data.

---

<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:** [6 September 2023 13:02 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/92 "2023-09-06T13:02:03Z")

</div>

> [@jbudd](#):
>
> What does this mean then?
> 
> > Running as a service
> > 
> > When Node-RED is running as a service having been installed using the provided [script](https://nodered.org/docs/getting-started/raspberrypi), it will not have access to environment variables that are defined only in the calling process. ...

When you run a command window in Linux the OS sets up a number of environment variables (such as PATH) in order to make life easier. I think that what the above is trying to say (arguably unsuccessfully) is that when it is run as a service node-red will only have access to env vars that are automatically made available to all services, plus any setup in the node red service script.  
So for example, if you run `echo $PATH` and compare that with the output of the function node which shows the PATH value in node-red you will see they are different.  
That is why, when running a command via an exec node it is often necessary to provide the full path to the command, as PATH may not include that folder.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 September 2023 13:15 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/93 "2023-09-06T13:15:16Z")

</div>

> [@Colin](#):
>
> So for example, if you run `echo $PATH` and compare that with the output of the function node which shows the PATH value in node-red you will see they are different.  
> That is why, when running a command via an exec node it is often necessary to provide the full path to the command, as PATH may not include that folder.

Yes, that is an excellent point, well made.

Specifically, if you run Node-RED manually (from a terminal command line), you are logged in with a specific user and Node-RED will inherit the USERS env variables that are set by the users profile script.

Whereas, when you run Node-RED as a service via systemd, you specify the user id in the systemd script and unless you specify some env variables, you don't get any unless they are set by the global OS startup. Specifically, you don't get a path, etc. You can adjust the systemd script to include env variables. Indeed, in my alt installer, I set up a local env file that the systemd script will automatically incorporate which makes it easier to manage them.

---

<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:** [6 September 2023 13:54 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/94 "2023-09-06T13:54:53Z")

</div>

> [@TotallyInformation](#):
>
> Specifically, you don't get a path,

I think you do get a basic path. On my Ubuntu system I see  
`/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin`.

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 14:02 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/95 "2023-09-06T14:02:56Z")

</div>

I am thoroughly enjoying reading along 🙂

---

<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:** [6 September 2023 14:44 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/96 "2023-09-06T14:44:35Z")

</div>

Sorry, we seem to have strayed off topic rather.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 September 2023 14:54 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/97 "2023-09-06T14:54:04Z")

</div>

> [@Colin](#):
>
> I think you do get a basic path.

Fair point, it may depend on the OS.

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 14:57 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/98 "2023-09-06T14:57:34Z")

</div>

Actually, this thread is not for a specific topic anymore.

I long linked the answers to the original questions in the first post.  
Everything since then is education and follow up.

And I find this discussion quite interesting. I don't have a use case for system variables (or env var in Node-RED), but hey, I might some day.

I will try to write compare function node next (since the filter does not have the functionality I need).  
I am sure this will cause some new questions to pop up also.

---

<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:** [6 September 2023 15:17 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/99 "2023-09-06T15:17:55Z")

</div>

> [@AleXSR700](#):
>
> Here is what I once used in a simple tasmota approach (different but easily understandable syntax).
> 
> ```auto
> IF ((%value%>=var1) AND (var11!=var2)) powerdelta1 %var2%
> ELSEIF ((%value%>=var3) AND (var11!=var4)) powerdelta1 %var4%
> ELSEIF ((%value%>=var5) AND (var11!=var6)) powerdelta1 %var6%
> ELSEIF ((%value%>=var7) AND (var11!=var8)) powerdelta1 %var8%
> ELSEIF ((%value%<var7) AND (var11!=var10)) powerdelta1 %var10% ENDIF;
> IF ((%value%>=var7) AND (var14!=600)) teleperiod 600
> ELSEIF ((%value%<var7) AND (var14!=3600)) teleperiod 3600 ENDIF ENDON
> 
> ```
> 
> %value% is the current sensor value, var11 is the previous value of the last reading, uneven numbered vars are the thresholds (e.g. if value \> 100 W) and even numbered vars are the power delta (if value change \> 3 W).
> 
> So the delta is dependent on the absolute value. I do not see this supported in the filter (RBE) node.

Can you explain in words what you are trying to achieve?

---

<div class="post-metadata">

**Author:** ![AleXSR700](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexsr700/32/41161_2.png) [@AleXSR700](https://discourse.nodered.org/u/AleXSR700)\
**Post date:** [6 September 2023 16:30 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/100 "2023-09-06T16:30:22Z")

</div>

I will hopefully show you in Node-RED code soon 😃

I am essentially doing

```auto
IF power > 1000 W and value has changed since last time Then publish whenever power changes by 50 W
ELSEIF power > 100 W and value has changed since last time Then publish whenever power changes by 10 W
.
.

```

So depending on what the absolute power value is, change the threshold.

EDIT: Solved my own problem. An uninitialized context is not considered 0 but undefined. So it will not be usable without initalizing it first.

P.S.: Where do you enable word wrap in the code editor?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 September 2023 16:56 UTC](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999/101 "2023-09-06T16:56:06Z")

</div>

> [@AleXSR700](#):
>
> An uninitialized context is not considered 0 but undefined. So it will not be usable without initalizing it first.

That's true.

Syntax to provide a default value:

```auto
let something = context.get("something") ?? 0
msg.payload = something + 42
return msg

```

[Previous page](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999.md?page=4)

[Next page](https://discourse.nodered.org/t/beginner-questions-on-if-expressions-and-wildcard-usage-in-function-node/80999.md?page=6)
