# Fan control with condition

**URL:** <https://discourse.nodered.org/t/fan-control-with-condition/38141>\
**Category:** Developing Nodes\
**Created:** [28 December 2020 15:45 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141 "2020-12-28T15:45:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 15:45 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/1 "2020-12-28T15:45:35Z")

</div>

I try to create a node with 7 condition if all condition it activate my blower, if it not it turn off...  
The blower take hot air in my basement fire place(sensor1), and transfer it in my living-room(sensor2)  
My others condition are slider in my lovelace who let my choose the temp Min(3) end Max(4) of my basement, and the temp we will reach in te living-room (5). I have also had a adjustable tolerance (6) to limit the trigger, and a Automation on/off(7).  
My Input are group in a Joint node and output are set up, but I have trouble to figure how to set up function...  
What I try to do is  
IF switch (7) is ON  
AND my temp (sensor1) is between the preset temp (3) and (4) (+/- tolerance (6))  
AND my temp (sensor2) is lower then the preset temp (5) (+/- tolerance (6))  
Then start my blower

---

<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:** [28 December 2020 16:11 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/2 "2020-12-28T16:11:19Z")

</div>

Hard to say for sure as i dont know your variable names and msg structure, You need to add a output from a debug, after the node that creates your msg.

The structure of such a if statement would be something like below.

```auto
if (switch7 === "ON" && 
  mytempsensor1 > (temp3 - tolerance) &&
  mytempsensor1 < (temp4 + tolerance) &&
  mytempsensor2 < (temp5 - tolerance) ) {
    // do somthing when true
    msg.payload = true;
}else{
    // do something when false
    msg.payload = false;
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 17:31 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/3 "2020-12-28T17:31:45Z")

</div>

I try to adapt to my need, could you see what I do wrong?  
I always have false at the top exit instead true or nothing, and nothing on the bottom exit...

 ![Capture d’écran 2020-12-28 122609](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c3f2646060e917814f32bd481f8c310b9c84c8ae.jpeg) !

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 17:32 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/4 "2020-12-28T17:32:26Z")

</div>

![Capture d’écran 2020-12-28 122625](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/acbbeee657288bb3cdde5f7dc7a60e1c440d2e4f.jpeg)

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 17:33 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/5 "2020-12-28T17:33:03Z")

</div>

![Capture d’écran 2020-12-28 122659](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/0/2073dc42b8432650f148a2e1a334a27eaa63e9a6.jpeg)

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 17:34 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/6 "2020-12-28T17:34:01Z")

</div>

if ("input\_boolean.souffleur\_automatique" === "1" &&  
"multi\_sensor1/sensor/temperature\_coin\_a\_bois/state" \> ("input\_number.temperature\_poele\_min" - "input\_number.temperature\_tolerence") &&  
"multi\_sensor1/sensor/temperature\_coin\_a\_bois/state" \< ("input\_number.temperature\_poele" + "input\_number.temperature\_tolerence") &&  
"cam3/sensor/garage\_temperature/state" \< ("input\_number.temperature\_salon" - "input\_number.temperature\_tolerence") ) {  
// do somthing when true  
msg.payload = true;  
return (msg, null );  
}else{  
// do something when false  
msg.payload = false;  
}  
return (null, msg );

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 17:35 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/7 "2020-12-28T17:35:13Z")

</div>

![Capture d’écran 2020-12-28 123453](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/370ca5767892ab8612d4b28a8a988e73b49eb746.jpeg)

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [28 December 2020 17:38 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/8 "2020-12-28T17:38:00Z")

</div>

> [@Deathsoul](#):
>
> return (null, msg );

Correct will be `return [null, msg]`

---

<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:** [28 December 2020 17:44 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/9 "2020-12-28T17:44:49Z")

</div>

I can not help with images. If you want more help create a test flow, consisting of an inject with object set in msg.payload , a function node with your code and a debug node. Then we will have the info to help.

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 17:57 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/10 "2020-12-28T17:57:31Z")

</div>

> [@E1cid](#):
>
> If you want more help create a test flow, consisting of an inject with object set in msg.payload , a function node with your code and a debug node.

Sorry, I am a new user of node red maybe 6-7 days, what is a test flow?

---

<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:** [28 December 2020 18:03 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/11 "2020-12-28T18:03:26Z")

</div>

A shorter version of your flow with less junk.

an inject containing the debug output from after the join node. In the debug window to the right of the object name , when you hoover mouse you get options , one is to copy value. you can then paste that value in the inject node and select json.

a copy of your function node.

and a debug/s nodes to show values.

select the nodes by holding Ctrl and selecting them with the mouse. Then press Ctrl/e and copy text.

Then paste text here by pressing \</\> aat top of reply window, and pasting the code between the backticks, like below.  
```  
code  
```

---

<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:** [28 December 2020 18:17 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/12 "2020-12-28T18:17:24Z")

</div>

> [@Deathsoul](#):
>
> ![Capture d’écran 2020-12-28 122625](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/acbbeee657288bb3cdde5f7dc7a60e1c440d2e4f.jpeg)

Looking here the values are in msg.payload.  
so add msg.payload to the front of all your variables.

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 18:17 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/13 "2020-12-28T18:17:56Z")

</div>

Input

```auto
input_number.temperature_tolerence : msg.payload : Object
object
multi_sensor1/sensor/temperature_coin_a_bois/state: 46
cam3/sensor/garage_temperature/state: 26
input_number.souffleur_automatique: 1
input_number.temperature_poele: 65
input_number.temperature_poele_min: 24.5
input_number.temperature_salon: 25
input_number.temperature_tolerence: 1

```

function

```auto
if ("input_boolean.souffleur_automatique" === "1" && 
  "multi_sensor1/sensor/temperature_coin_a_bois/state" > ("input_number.temperature_poele_min" - "input_number.temperature_tolerence") &&
  "multi_sensor1/sensor/temperature_coin_a_bois/state" < ("input_number.temperature_poele" + "input_number.temperature_tolerence") &&
  "cam3/sensor/garage_temperature/state" < ("input_number.temperature_salon" - "input_number.temperature_tolerence") ) {
    // do somthing when true
    msg.payload = true;
return (msg, null);
}else{
    // do something when false
    msg.payload = false;
}
return (null, msg);

```

---

<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:** [28 December 2020 18:21 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/14 "2020-12-28T18:21:56Z")

</div>

I still do not have a copy of the object, that i can just copy and paste. If you copy value correctly using copy value icon to right of msg.payload, then i can work with it, but what you have pasted is not a valid json.

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 18:31 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/15 "2020-12-28T18:31:05Z")

</div>

```auto
[{"id":"c66fb972.1c42f8","type":"server-state-changed","z":"756107df.057b38","name":"Switch Automation","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.souffleur_automatique","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":true,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":90,"y":400,"wires":[["6fc0025a.1ef98c"]]},{"id":"b5554eb2.fbcce","type":"server-state-changed","z":"756107df.057b38","name":"Temp_max poèle","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.temperature_poele","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":80,"y":40,"wires":[["6fc0025a.1ef98c"]]},{"id":"eec2f33c.01b7a","type":"server-state-changed","z":"756107df.057b38","name":"Temp_min poèle","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.temperature_poele_min","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":80,"y":100,"wires":[["6fc0025a.1ef98c"]]},{"id":"b122bc0a.d2af6","type":"server-state-changed","z":"756107df.057b38","name":"Temp_salon","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.temperature_salon","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":70,"y":280,"wires":[["6fc0025a.1ef98c"]]},{"id":"c9bb1e44.a69c2","type":"server-state-changed","z":"756107df.057b38","name":"tolérence temp","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.temperature_tolerence","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":80,"y":220,"wires":[["6fc0025a.1ef98c"]]},{"id":"c9f5b654.4a0198","type":"mqtt in","z":"756107df.057b38","name":"Temp","topic":"multi_sensor1/sensor/temperature_coin_a_bois/state","qos":"0","datatype":"json","broker":"f31ab881.110a68","x":50,"y":160,"wires":[["6fc0025a.1ef98c"]]},{"id":"eaeb8177.228ef","type":"mqtt in","z":"756107df.057b38","name":"Temp","topic":"cam3/sensor/garage_temperature/state","qos":"0","datatype":"json","broker":"f31ab881.110a68","x":50,"y":340,"wires":[["6fc0025a.1ef98c"]]},{"id":"f31ab881.110a68","type":"mqtt-broker","z":"","name":"","broker":"192.168.1.26","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 18:33 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/16 "2020-12-28T18:33:08Z")

</div>

> [@E1cid](#):
>
> but what you have pasted is not a valid json.

Ok and what is wrong?

---

<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:** [28 December 2020 18:41 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/17 "2020-12-28T18:41:06Z")

</div>

This is not a test flow and does not contain any functon or inject node  
This is an example of a correctly formated json notice the { } and the " " and the :

```auto
{"topic":"Mypi/2/KW","payload":{"KW_Total":434},"qos":0,"retain":false,"_msgid":"ef0287b6.34ca08"}

```

if you look at image you can see my cursor over the copy value icon.

 ![Screenshot_20201228-184027](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cba3bb2214b524c3484c6560793e771d0b94baaa.png)

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [28 December 2020 18:53 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/18 "2020-12-28T18:53:51Z")

</div>

```auto
{"multi_sensor1/sensor/temperature_coin_a_bois/state":42.4,"cam3/sensor/garage_temperature/state":25.8,"input_number.souffleur_automatique":1,"input_number.temperature_poele":65,"input_number.temperature_poele_min":24.5,"input_number.temperature_salon":25,"input_number.temperature_tolerence":1}

```

---

<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:** [28 December 2020 19:13 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/19 "2020-12-28T19:13:04Z")

</div>

Here is a test flow

```auto
[{"id":"b95ccd5.63ef1b","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"multi_sensor1/sensor/temperature_coin_a_bois/state\":42.4,\"cam3/sensor/garage_temperature/state\":25.8,\"input_number.souffleur_automatique\":1,\"input_number.temperature_poele\":65,\"input_number.temperature_poele_min\":24.5,\"input_number.temperature_salon\":25,\"input_number.temperature_tolerence\":1}","payloadType":"json","x":140,"y":1940,"wires":[["31a96c2c.4f8c9c"]]},{"id":"31a96c2c.4f8c9c","type":"function","z":"8d22ae29.7df6d","name":"","func":"if (msg.payload[\"input_number.souffleur_automatique\"] === 1 && \n msg.payload[\"multi_sensor1/sensor/temperature_coin_a_bois/state\"] > (msg.payload[\"input_number.temperature_poele_min\"] - msg.payload[\"input_number.temperature_tolerence\"]) &&\n msg.payload[\"multi_sensor1/sensor/temperature_coin_a_bois/state\"] < (msg.payload[\"input_number.temperature_poele\"] + msg.payload[\"input_number.temperature_tolerence\"]) &&\n msg.payload[\"cam3/sensor/garage_temperature/state\"] < (msg.payload[\"input_number.temperature_salon\"] - msg.payload[\"input_number.temperature_tolerence\"]) ) {\n // do somthing when true\n msg.payload = true;\nreturn [msg, null];\n}else{\n // do something when false\n msg.payload = false;\n return [null, msg];\n\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","x":330,"y":1960,"wires":[["be48273.628c958"],["be48273.628c958"]]},{"id":"be48273.628c958","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":1940,"wires":[]},{"id":"5f8cb3d4.47db5c","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"multi_sensor1/sensor/temperature_coin_a_bois/state\":42.4,\"cam3/sensor/garage_temperature/state\":23.8,\"input_number.souffleur_automatique\":1,\"input_number.temperature_poele\":65,\"input_number.temperature_poele_min\":24.5,\"input_number.temperature_salon\":25,\"input_number.temperature_tolerence\":1}","payloadType":"json","x":130,"y":2000,"wires":[["31a96c2c.4f8c9c"]]}]

```

you will see i put the json in the inject node for testing

if you create a json that should return true in a seperate inject node you can the test the function.

edit/ changed === "1" to === 1

---

<div class="post-metadata">

**Author:** ![Deathsoul](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Deathsoul](https://discourse.nodered.org/u/Deathsoul)\
**Post date:** [29 December 2020 20:58 UTC](https://discourse.nodered.org/t/fan-control-with-condition/38141/20 "2020-12-29T20:58:51Z")

</div>

> [@E1cid](#):
>
> edit/ changed === "1" to === 1

Before that change, always false at te bottom output  
After remove " " I have nothing at the output...

I saw your input are set to string, mine are set to number, it is made a difference?

[Next page](https://discourse.nodered.org/t/fan-control-with-condition/38141.md?page=2)
