# Working with conditions

**URL:** <https://discourse.nodered.org/t/working-with-conditions/34325>\
**Category:** General\
**Created:** [15 October 2020 23:41 UTC](https://discourse.nodered.org/t/working-with-conditions/34325 "2020-10-15T23:41:27Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [15 October 2020 23:41 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/1 "2020-10-15T23:41:27Z")

</div>

Hello all node-red fans!

I'm working with conditions and tryng to match several ones to have a final result and make an action to happens. So...

I have this nodes so far.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/cef3e58a06ea8c25200314a4041b88c67dd55cb9.png)

The path goes like this:

- check if im stay at home or away
- check if lights are on
- In the switch node i check that previous conditions

All things seems to be easy but the issue comes when i'm home and i turn on the lights the "out home" node starts and turn off the light.

There is something wrong about the condition check and how that is managed in node-red. Obviously im doing something wrong...

Any clue or help?

Regards!

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 October 2020 01:14 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/2 "2020-10-16T01:14:43Z")

</div>

Not at my computer but I suggest you put a separate debug node on the output of every node in the flow so you can see what is going on. And remember the old saying “computers tend to do what you tell them to do which isn’t always what you want them to do” 😄

---

<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:** [16 October 2020 06:58 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/3 "2020-10-16T06:58:40Z")

</div>

Not sure as we can not see the switch node settings,  
The switch node checks all the conditions in order, it will check all unless it is set to stop after finding the first true condition.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [16 October 2020 20:21 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/4 "2020-10-16T20:21:29Z")

</div>

Ok.  
The switch node has this configuration.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/c/5c6ae0956a9e5db9789a33b34ec875451dceb6ad.jpeg)

The first condition checks whether im home or not. The weird thing is that i change this for example from home (being home) to not\_home all works as equal as it is. I dont understand that part because it has to stop all the following process.

Maybe im misunderstanding something here.

Regards!

---

<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:** [16 October 2020 21:20 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/5 "2020-10-16T21:20:27Z")

</div>

It is set to check all rules so, if you only want it to fire one rule if true, then change the checking all rules option.  
As you have it set, if not\_home is true that path fires. but then it Checks other rules.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [16 October 2020 23:17 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/6 "2020-10-16T23:17:47Z")

</div>

Ok...so let me see if i get it.

The logical works weird because i know that all conditions need to be meet in orden to follow with the next node.

Here are the nodes so you can check it out.

```auto
[{"id":"371b5d1c.9c46c2","type":"tab","label":"Presence actions","disabled":false,"info":""},{"id":"b78fb400.7d8bc8","type":"comment","z":"371b5d1c.9c46c2","name":"todo ","info":"Hacer un schedule para vacaciones y que la luz\nse prenda automatica y se apague sola en bandas\nde tiempo, que me mande un mensaje tambien con esto.\nHay un nodo que hace esto, ver cron-plus nodo.\n\nhacer que se detecte la zona de casa, eso me \npermite ejecutar cosas en el rango de casa. \nSi es de noche y estoy fuera de la zona de casa\nno voy a querer que se prendan las luces. De lo \ncontrario si. Hay que ver como se hace con life360\nya que ahi se delimitan las zonas.\n\nEnviar un mensaje a telegram cuando llegue a casa y\ncuando salga de casa\n\na futuro....hay que instalar life 360 en el\ndispositivo de jime porque la politica\nde que realmente no hay nadie en casa y que dejamos\nuna luz prendida no seria posible.\nSi solamente me detecta mi dispositivo y me voy\nlos dejo sin luz a los que estan adentro.\n","x":150,"y":40,"wires":[]},{"id":"1b589a17.437956","type":"comment","z":"371b5d1c.9c46c2","name":"Apaga todas las luces cuando estoy en casa","info":"","x":210,"y":100,"wires":[]},{"id":"67dbe26f.4e82cc","type":"function","z":"371b5d1c.9c46c2","name":"enter home","func":"msg.payload = {}\nmsg.payload.chatId = 587232739;\nmsg.payload.type = 'message';\nmsg.payload.content = 'TAB PRESENCE ACTIONS AVISO: Estas dentro de casa.';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":240,"wires":[[]]},{"id":"505ae856.192da8","type":"trigger-state","z":"371b5d1c.9c46c2","name":"","server":"59ced628.1507a8","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"device_tracker.life360_martin_coria","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"targetType":"this_entity","targetValue":"","propertyType":"current_state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"not_home","propertyValue":"new_state.state"}],"outputs":3,"customoutputs":[{"messageType":"default","messageValue":"home","messageValueType":"str","comparatorPropertyType":"current_state","comparatorType":"is","comparatorValue":"home","comparatorValueDataType":"str","comparatorPropertyValue":"new_state.state"}],"outputinitially":true,"state_type":"str","x":220,"y":280,"wires":[[],["97e430b6.11b36"],["67dbe26f.4e82cc"]]},{"id":"c4a07c80.fd1ce","type":"function","z":"371b5d1c.9c46c2","name":"checking if lights are on","func":"msg.payload = {}\nmsg.payload.chatId = 587232739;\nmsg.payload.type = 'message';\nmsg.payload.content = 'TAB PRESENCE ACTIONS AVISO: Chequeando si la luz de prueba esta encendida...';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":730,"y":400,"wires":[["f856ebfd.4deb38","e5c87795.fea4a8"]]},{"id":"5532d535.d91dec","type":"delay","z":"371b5d1c.9c46c2","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":680,"y":360,"wires":[["c4a07c80.fd1ce"]]},{"id":"2442976c.7ca968","type":"function","z":"371b5d1c.9c46c2","name":"out home","func":"msg.payload = {}\nmsg.payload.chatId = 587232739;\nmsg.payload.type = 'message';\nmsg.payload.content = 'TAB PRESENCE ACTIONS AVISO: Estas fuera de casa.';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":680,"y":320,"wires":[["5532d535.d91dec"]]},{"id":"2a598912.1e5436","type":"http request","z":"371b5d1c.9c46c2","name":"Apagar luz de prueba","method":"use","ret":"txt","paytoqs":"ignore","url":"http://192.168.1.165/cm?cmnd=Power%20off","tls":"","persist":false,"proxy":"","authType":"","x":940,"y":560,"wires":[[]]},{"id":"4a527deb.5836f4","type":"server-state-changed","z":"371b5d1c.9c46c2","name":"","server":"59ced628.1507a8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"switch.luz_prueba_luz_prueba_2","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":220,"y":580,"wires":[["97e430b6.11b36"]]},{"id":"e5c87795.fea4a8","type":"function","z":"371b5d1c.9c46c2","name":"light off","func":"msg.payload = {}\nmsg.payload.chatId = 587232739;\nmsg.payload.type = 'message';\nmsg.payload.content = 'TAB PRESENCE ACTIONS AVISO: La luz estaba apagada, no hay de que preocuparse!...';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":620,"wires":[[]]},{"id":"f856ebfd.4deb38","type":"function","z":"371b5d1c.9c46c2","name":"light on","func":"msg.payload = {}\nmsg.payload.chatId = 587232739;\nmsg.payload.type = 'message';\nmsg.payload.content = 'TAB PRESENCE ACTIONS AVISO: La luz estaba prendida, apagando luz...';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":480,"wires":[["a10c8a09.c88748"]]},{"id":"a10c8a09.c88748","type":"delay","z":"371b5d1c.9c46c2","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":900,"y":520,"wires":[["2a598912.1e5436"]]},{"id":"97e430b6.11b36","type":"switch","z":"371b5d1c.9c46c2","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"not_home","vt":"str"},{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":470,"y":420,"wires":[["2442976c.7ca968"],["2442976c.7ca968"],["2442976c.7ca968"]]},{"id":"59ced628.1507a8","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":false,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false}]

```

Thanks a lot!

---

<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:** [17 October 2020 06:36 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/7 "2020-10-17T06:36:23Z")

</div>

To help i will need a debug of the two Homme assistant nodes, these are not standard nodes and i have no idea what msg they produce.  
second i would need a clear explanation of what you are attempting to do. As your flow makes no sense to me.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 October 2020 10:36 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/8 "2020-10-17T10:36:59Z")

</div>

Do you realize that your `function` node 'checking if lights are on' is sending a message to **both** the 'light on' `function` node **and** the 'light off' `function` node?

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [17 October 2020 23:11 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/9 "2020-10-17T23:11:10Z")

</div>

Yes i've realized that, thanks in advance.

Ok. I've arrived to a point where i get a little confused or i'm not doing the right thing.

This is what i have so far...

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

1. check whether im or not at home
2. check if the light is on or off
3. I put a "wait until" node because i want to have a precondition on every node before trigger the action.
4. everything follows as it is....

....so here comes the problem...

If the location node says "home" and i turn on the light, it will turn it Off. Here is when it fails, because i want to turn on the light when im home and not to be turned off automaticaly...

Any clues?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 October 2020 06:08 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/10 "2020-10-18T06:08:51Z")

</div>

Can you provide the current flow?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [18 October 2020 08:14 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/12 "2020-10-18T08:14:22Z")

</div>

As I don't have Home assistant, I'll offer some ideas. Put a `debug` node on the output of both `Wait until` nodes and one on the output of the 'enter home' `function` node. This will give an idea of what data is coming thru and when.

As an aside, since you are only building a message, you could replace your `function` nodes with `template` nodes like this

```auto
[{"id":"2540a245.1881de","type":"template","z":"521e528d.9304e4","name":"Enter Home","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\n \"chatId\": 587232739,\n \"type\" : \"message\",\n \"content\" : \"TAB PRESENCE ACTIONS AVISO: Estas dentro de casa.\"\n }\n","output":"json","x":740,"y":180,"wires":[["b5d0bc54.d31738"]]}]

```

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [18 October 2020 23:03 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/13 "2020-10-18T23:03:44Z")

</div>

Ok...here is the debug node at the output of both `Wait Until` nodes. I have "mocked" up the nodes doing the same but if i'm at home, not away, so i can test the functionality and behavour being inside my home.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/277d799131da1cd2433d943a6a68e3df1ba2d5b7.png)

I didn't know about that template node. Thanks for that!.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 October 2020 17:08 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/14 "2020-10-19T17:08:13Z")

</div>

(FYI when adding debug nodes, add a separate one for each place you want to debug AND name them. That way someone else can see exactly what node created each output in the log.)

So you are sending messages out to telegraph. What happens to those messages?  
Also the 'checking if lights on' doesn't check anything but sends a msg directly to the telegraph node. mrsnehile another msg get's sent thru th edelay to the switch.

However since you didn't expand the `data` object in the log, I have no idea what it is doing. Can you expand them and show what they contain.

---

<div class="post-metadata">

**Author:** ![Marty1982](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marty1982/32/20162_2.png) [@Marty1982](https://discourse.nodered.org/u/Marty1982)\
**Post date:** [23 October 2020 20:20 UTC](https://discourse.nodered.org/t/working-with-conditions/34325/15 "2020-10-23T20:20:19Z")

</div>

Ok, i think i found the solution just simplyfing the nodes by now.

Thanks a lot for the help!.
