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