Problems with conditional function

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


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;
	}

You dont understand this code?

Or its not doing what you expect?

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

if (valueread <= minvalue) 
	    {
		msg.payload = "open";
		return msg;
	}

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


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

1 Like

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

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

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

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.

can you show me the code please?

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

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.

I will explain it with a easier function.


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.

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.

1 Like

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

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;
	}
1 Like

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

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?

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.

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

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

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:

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

will give you a check for such a range.

1 Like

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

2 Likes