# Condition Statement

**URL:** <https://discourse.nodered.org/t/condition-statement/46274>\
**Category:** General\
**Created:** [26 May 2021 11:24 UTC](https://discourse.nodered.org/t/condition-statement/46274 "2021-05-26T11:24:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eorkz](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Eorkz](https://discourse.nodered.org/u/Eorkz)\
**Post date:** [26 May 2021 11:24 UTC](https://discourse.nodered.org/t/condition-statement/46274/1 "2021-05-26T11:24:34Z")

</div>

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!

---

<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:** [26 May 2021 11:27 UTC](https://discourse.nodered.org/t/condition-statement/46274/2 "2021-05-26T11:27:42Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf3f95fb126e6d556ef529b2545f994bdcd112b6.png)

---

<div class="post-metadata">

**Author:** ![Eorkz](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Eorkz](https://discourse.nodered.org/u/Eorkz)\
**Post date:** [26 May 2021 11:37 UTC](https://discourse.nodered.org/t/condition-statement/46274/4 "2021-05-26T11:37:57Z")

</div>

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

---

<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:** [26 May 2021 12:06 UTC](https://discourse.nodered.org/t/condition-statement/46274/5 "2021-05-26T12:06:25Z")

</div>

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://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [26 May 2021 12:19 UTC](https://discourse.nodered.org/t/condition-statement/46274/6 "2021-05-26T12:19:40Z")

</div>

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.

```auto
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?

---

<div class="post-metadata">

**Author:** ![Eorkz](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Eorkz](https://discourse.nodered.org/u/Eorkz)\
**Post date:** [26 May 2021 12:32 UTC](https://discourse.nodered.org/t/condition-statement/46274/7 "2021-05-26T12:32:17Z")

</div>

It doesnt go into the last two debugs

 ![a1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e59d3bd12600153c80d88b4dd3d2e0023d05c874.png)  
 ![a2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8aff115e23ccc19e195dd9e6f77e96d9bff0e310.png)

---

<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:** [26 May 2021 12:33 UTC](https://discourse.nodered.org/t/condition-statement/46274/8 "2021-05-26T12:33:10Z")

</div>

dont put quotes in the box

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d4ee3c39e79bf85296f784cf7ad9376da7f52f2.png)

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

---

<div class="post-metadata">

**Author:** ![Eorkz](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Eorkz](https://discourse.nodered.org/u/Eorkz)\
**Post date:** [26 May 2021 12:35 UTC](https://discourse.nodered.org/t/condition-statement/46274/9 "2021-05-26T12:35:05Z")

</div>

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

---

<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:** [26 May 2021 12:37 UTC](https://discourse.nodered.org/t/condition-statement/46274/10 "2021-05-26T12:37:56Z")

</div>

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

e.g...

```auto
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)

---

<div class="post-metadata">

**Author:** ![Eorkz](https://avatars.discourse-cdn.com/v4/letter/e/f14d63/32.png) [@Eorkz](https://discourse.nodered.org/u/Eorkz)\
**Post date:** [26 May 2021 13:00 UTC](https://discourse.nodered.org/t/condition-statement/46274/11 "2021-05-26T13:00:24Z")

</div>

It worked this way! Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 June 2021 13:01 UTC](https://discourse.nodered.org/t/condition-statement/46274/12 "2021-06-09T13:01:22Z")

</div>

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