# Problems with conditional function

**URL:** <https://discourse.nodered.org/t/problems-with-conditional-function/30498>\
**Category:** General\
**Created:** [21 July 2020 17:57 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498 "2020-07-21T17:57:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 17:57 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/1 "2020-07-21T17:57:56Z")

</div>

Hi I'm having problems with this function I really don't know what is happening.

```auto

var valueread = global.get("read");
var minvalue = global.get("minvalue");
var maxvalue = global.get("maxvalue");

    if (valueread <= minvalue) 
	    {
		msg.payload = "open";
		return msg;
	}
	else if (((maxvalue - minvalue)/2) <= (maxvalue - valueread)) 
	 {
		msg.payload = "1/3";
		return msg;
	}
	else if (((maxvalue - minvalue)/4) <= (maxvalue - valueread)) 
	 {
		msg.payload = "2/3";
		return msg;
	}
	else if (valueread >= maxvalue) 
	 {
		msg.payload = "close";
		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:** [21 July 2020 17:59 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/2 "2020-07-21T17:59:40Z")

</div>

> [@lalo](#):
>
> I really don't know what is happening

You dont understand this code?

Or its not doing what you expect?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 18:01 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/3 "2020-07-21T18:01:16Z")

</div>

It's not doing what I want it only send the first condition after that it not sends anything

This is the only condition working good

```auto
if (valueread <= minvalue) 
	    {
		msg.payload = "open";
		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:** [21 July 2020 18:09 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/4 "2020-07-21T18:09:04Z")

</div>

And you couldnt say this in your initial post?

What do you mean "it only send the first condition"

That would suggest `valueread` really IS `<= minvalue`

  

Are you expecting three messages OR are the conditions not evaluating as you expect?

Put this at the top of your function JUST AFTER `var maxvalue = ...`

```auto

node.warn([
  "valueread", valueread,
  "minvalue", minvalue,
  "maxvalue", maxvalue,
])

```

See what it says in the debug window.

PS - also, post a screen shot of your sidebar with Context Viewer selected (refresh the globals)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/471e7a66dd9f6756b8f0d89d889e178376f17c4d.png)

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 19:50 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/5 "2020-07-21T19:50:11Z")

</div>

the conditions are not evaluating as I expect I will my conditions easier. The way it works is weird. I did what you tell me but the values are ok. I really don't know what is happening what is the problem

---

<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:** [21 July 2020 19:51 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/6 "2020-07-21T19:51:28Z")

</div>

I can't help if you don't show me

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 19:53 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/7 "2020-07-21T19:53:17Z")

</div>

Just give me to tomorrow to do things with the code that doesn't include this conditions

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [21 July 2020 20:04 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/8 "2020-07-21T20:04:42Z")

</div>

I think perhaps you need to look at the conditions again, as it seems there will be input values that don't meet any of them. Possibly a final "else" to cover the case when none of them are valid would help identify the problem.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 20:39 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/9 "2020-07-21T20:39:20Z")

</div>

can you show me the code please?

```auto
var porcentajehumedad=msg.payload;

global.set('humedad',porcentajehumedad);

var condicionhumedad = global.get("humedad");
var humedadminima = global.get("humedadmin");
var humedadmaxima = global.get("humedadmax");

    if (condicionhumedad < humedadminima) 
	    {
		msg.payload = "ABRIR";
		return msg;
	}
	else if (condicionhumedad == humedadmaxima) 
	 {
		msg.payload = "CERRAR";
		return msg;
	} else {
   return [msg, null];
}

```

As I know because I don't know to much JS

---

<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:** [21 July 2020 20:51 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/10 "2020-07-21T20:51:29Z")

</div>

You still have not explained what you expect to see out of the node and what is happening. Tell us an example set of values going in, and what you expect to see out.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 21:17 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/11 "2020-07-21T21:17:35Z")

</div>

I will explain it with a easier function.

```auto

var valuereadfromarduino = global.get("arduinoread");
var setmaxvalue= global.get("maxvalue");
var setminvalue = global.get("minvalue");

    if (valuereadfromarduino < setminvalue) 
	    {
		msg.payload = "OPEN";
		return msg;
	}
	else if (valuereadfromarduino == setmaxvalue) 
	 {
		msg.payload = "CLOSE";
		return msg;
	}
	

```

So basically from my Arduino I'm reciving my first reads from a sensor close to zero, then in a

`ui_text_input` node I set my min an max values so basically if my first read is 5 I will set my minvalue as 15 and my max value in 25. With this I'm expecting to have a msg.payload OPEN/CLOSE depending on my conditions.

---

<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:** [21 July 2020 21:29 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/12 "2020-07-21T21:29:56Z")

</div>

The code says the if the value read is less than the set min then return with payload "OPEN". Otherwise if the value read is _exactly equal to_ the set max value then return with payload "CLOSE".  
If the value read is \>= the set min value and is not exactly the set max then neither test will succeed, so no return statement is executed and no message will be sent.

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [21 July 2020 21:48 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/13 "2020-07-21T21:48:50Z")

</div>

You should cover all possibilities in your conditional tests. Perhaps something like :

```auto
var valuereadfromarduino = global.get("arduinoread");
var setmaxvalue= global.get("maxvalue");
var setminvalue = global.get("minvalue");

	if (valuereadfromarduino < setminvalue) 
	{
		// value too low - open valve
		msg.payload = "OPEN";
		return msg;
	}
	else if (valuereadfromarduino > setmaxvalue) 
	{
		// value too high - close valve
		msg.payload = "CLOSE";
		return msg;
	}
	else 
	{
		// value between min and max - set valve to half
		msg.payload = "HALF";
		return msg;
	}

```

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 22:12 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/14 "2020-07-21T22:12:30Z")

</div>

exactly because in that only two cases it will work. And I don't need something else.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [21 July 2020 22:16 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/15 "2020-07-21T22:16:40Z")

</div>

> [@molesworth](#):
>
> ```auto
> var valuereadfromarduino = global.get("arduinoread");
> var setmaxvalue= global.get("maxvalue");
> var setminvalue = global.get("minvalue");
> 
> if (valuereadfromarduino < setminvalue) 
> {
> // value too low - open valve
> msg.payload = "OPEN";
> return msg;
> }
> else if (valuereadfromarduino > setmaxvalue) 
> {
> // value too high - close valve
> msg.payload = "CLOSE";
> return msg;
> }
> else 
> {
> // value between min and max - set valve to half
> msg.payload = "HALF";
> return msg;
> }
> 
> ```

That is cool but in the case that I have my max value as 50 and my min value as 25 and I want to have like a range before 50 to set "HALF" we could say 20% of the value before 50 it will be like 50-25 = 25 , 25 \* 0.2= 5 so I want to set something like that and in 45 it will set the valve in half open how it will be?

---

<div class="post-metadata">

**Author:** ![molesworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/molesworth/32/11748_2.png) [@molesworth](https://discourse.nodered.org/u/molesworth)\
**Post date:** [21 July 2020 22:36 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/16 "2020-07-21T22:36:58Z")

</div>

> [@lalo](#):
>
> I have my max value as 50 and my min value as 25 and I want to have like a range before 50 to set "HALF" we could say 20% of the value before 50 it will be like 50-25 = 25 , 25 \* 0.2= 5 so I want to set something like that and in 45 it will set the valve in half open how it will be?

I'm not really following what you're trying to do here. You can add as many "else if" sections as you need, with different conditions checking the input value against each range.

Alternatively you might be able to analyse the values to define an equation which you could use to calculate the best valve setting/percentage for any given input value.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [22 July 2020 03:10 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/17 "2020-07-22T03:10:03Z")

</div>

yeah but how can I do to set a between 25 to 50 in a fuction?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [22 July 2020 03:25 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/18 "2020-07-22T03:25:58Z")

</div>

Actually I'm trying to make a PID control so when the read value is close to my set point I want to stop before that set point to slow it down

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [22 July 2020 05:20 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/19 "2020-07-22T05:20:05Z")

</div>

You can use one or **more** conditions in a javascript if statement. You need to connect them with either the logical and `&&` or the logical or `||`.  
So something like:

```auto
if (readvalue > 10 && readvalue < 20)...

```

will give you a check for such a range.

---

<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:** [22 July 2020 05:46 UTC](https://discourse.nodered.org/t/problems-with-conditional-function/30498/20 "2020-07-22T05:46:36Z")

</div>

> [@lalo](#):
>
> Actually I'm trying to make a PID control

In that case you can use node-red-contrib-pid

[Next page](https://discourse.nodered.org/t/problems-with-conditional-function/30498.md?page=2)
