Condition Statement

Hello,

So, I am learning node-red and im having trouble in making this function.

I have values (temperature, humidity, moisture, etc) and I want to build a water supply system and I need to create rules where, for example:

If temperature high AND moisture low OR temperature high AND humidity low OR temperature low AND moisture low then Turn On the water supply

Same thing for the turn off
Basiclly a set of rules for turning ON and OFF.

Any ideas?
Thanks!

Are all these values in 1 msg or separate messages?

Share your flow and describe the issue.

As for how to compare - use the switch nodes in series for an AND and in parallel for an OR

e.g.

Just one message, I am reading the information from a sqlite database through a query which selects the last record of each so I can perform some operations (I need to check for example temperature > 30 so I can determine that is high temperature).

I assume the results are in 1 row?

You can used the "copy path" button in the debug window to help you avoid making mistakes.

click copy path on the item of interest and paste it into the switch input boxes.

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

https://nodered.org/docs/user-guide/messages

You are not really giving enough info, so difficult to say for sure the best method, but here is how you would set the conditionals in a function node.

if((temperature > 30 && (moisture < 50 || humidity < 50)) || 
(temperature < 30 && moisture < 50)){
    msg.payload = "on";
}else{
    msg.payload = "off";
}

what are high/low values what payload do you need to send to turn on/off device?

1 Like

It doesnt go into the last two debugs


a2

dont put quotes in the box

image

it is already a string

also, you cannot (should not) compare strings to numbers - you will get odd results.

In other words, you should change your database schema to store the numbers as INT or FLOAT

This is the method I was looking for. I have tried it but couldnt figure out.
For now, doesnt matter the high/low values. Lets just assume > 50 (high) and <50 (low).
The msg.payload = "on" / "off"; is exactly what I want.

I have 7 conditions for on and 7 conditions for off

If you cannot / will not change your database schema then you will need to coerce your values into numbers before comparing them

e.g...

var temperature = Number(msg.payload[0].TEMPERATURA);
var humidity = Number(msg.payload[0].HUMIDADE);
var moisture = Number(msg.payload[0].HIDRATACAO);
if((temperature > 30 && (moisture < 50 || humidity < 50)) || 
(temperature < 30 && moisture < 50)){
    msg.payload = "on";
}else{
    msg.payload = "off";
}

HOWEVER, to avoid having to do this all the time / all over your flows, i strongly recommend updating your database schema to use INTS and FLOATS (instead of strings)

1 Like

It worked this way! Thank you!

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