# IF Statement for two DS18B20 Sensors

**URL:** <https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280>\
**Category:** General\
**Created:** [13 June 2020 14:36 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280 "2020-06-13T14:36:12Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![Lurey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lurey/32/6991_2.png) [@Lurey](https://discourse.nodered.org/u/Lurey)\
**Post date:** [20 June 2020 08:00 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/21 "2020-06-20T08:00:56Z")

</div>

I love context variables and use these in combination with the change node to assign values to the context variables.  
This solves a lot of issues without using function nodes.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2020 08:54 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/22 "2020-06-20T08:54:16Z")

</div>

> [@Lurey](#):
>
> I love context variables

I am of the exact opposite opinion and virtually never use them, but I believe in the freedom of others to programme using the paradigm they prefer, provided I am not asked to approve 🙂

Notice the solution with the Join node uses one less node than the alternative suggested (by @machadotiago) , as well as using no context variables and no function node, so I fail to see the advantage of using context, at least in this case.

---

<div class="post-metadata">

**Author:** ![Lurey](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lurey/32/6991_2.png) [@Lurey](https://discourse.nodered.org/u/Lurey)\
**Post date:** [20 June 2020 13:47 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/23 "2020-06-20T13:47:58Z")

</div>

Still share the opinion of machadotiago that using 5 basic nodes to get things done is ok. Perhaps it requires more nodes but enhances readability and ease of use.  
PS : Join uses context internally (key/value) (and upstream specification of the topic key).

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [20 June 2020 14:42 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/24 "2020-06-20T14:42:44Z")

</div>

> [@Colin](#):
>
> but I believe in the freedom of others to programme using the paradigm they prefer,

O fully agree with you, but I always encourage people, that are just starting at Node-RED, to try using “change” and “switch” before going to function, and trust me, this is based on science 🙃.  
We have been training companies to use Node-RED since almost 3 years, and we have changed the training materials a lot during this period. We have also many employees that were also trained this way, and today I know people in the manufacturing industry that program in Node-RED every single day and have never touched a single function node because they are “afraid” of writing code. That’s why I’m here trying to spread my message

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2020 14:44 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/25 "2020-06-20T14:44:42Z")

</div>

> [@Lurey](#):
>
> Join uses context internally (key/value)

Of course, there is nothing wrong with node context, which is entirely within one node and is not accessible by another node. When using flow/global context data hops from one node to another with no wire between or in a manner that bypasses the normal message passing paradigm, so the data flow is hidden. That is not the case when using node context.

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [20 June 2020 14:47 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/26 "2020-06-20T14:47:17Z")

</div>

Again, I agree with you, I have nothing against the “join” node actually, but I usually prefer using context variables because of the fact you can easily see them in the context menu.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [20 June 2020 15:28 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/27 "2020-06-20T15:28:27Z")

</div>

> [@machadotiago](#):
>
> I know people in the manufacturing industry that program in Node-RED every single day and have never touched a single function node because they are “afraid” of writing code.

Isn't that an argument for encouraging people to learn how to do it? Sometimes it is the best way. No node-red programmer should be afraid of writing functions.  
But in this case I am not suggesting using a function node (I did initially but was converted to Join and Switch).

---

<div class="post-metadata">

**Author:** ![Los\_Andros](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@Los\_Andros](https://discourse.nodered.org/u/Los_Andros)\
**Post date:** [22 June 2020 15:05 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/28 "2020-06-22T15:05:22Z")

</div>

I see both points in using a function node, as well as a switch node.

I am still not sure, if I replace the debug node with a LED light which format to use...

 ![Node Red Flow 3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b7ba199f8f62be10ce209ace6b2b3c25a94db178.png)

Is there a possibility to use a function node to change the output of the GPIO.Pin from 0 to 1 when reaching 50 degrees?

I looked through the whole Node Red forum and only found examples using a seperate on/off switch (so no automation).

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 June 2020 15:22 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/29 "2020-06-22T15:22:00Z")

</div>

In node red you have to think in smaller blocks of logic. Split the problem into parts, detecting \> 50 (which you now have multiple possibilities to choose from) and switching the GPIO output.  
Do you know how to switch the output using an Inject node?

---

<div class="post-metadata">

**Author:** ![Los\_Andros](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@Los\_Andros](https://discourse.nodered.org/u/Los_Andros)\
**Post date:** [22 June 2020 15:31 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/30 "2020-06-22T15:31:31Z")

</div>

I personally never worked with an inject node but I would use either the number option (1 or 0) or the boolean option to controll the led.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 June 2020 16:32 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/31 "2020-06-22T16:32:09Z")

</div>

Well you need to experiment with the Inject node, that is the best way to test your flows as you are developing them.

So all you have to do is to modify the function node to send 0 or 1 in the payload.  
If you can't work out how to do that post what you have so far, in the function node.

---

<div class="post-metadata">

**Author:** ![Los\_Andros](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@Los\_Andros](https://discourse.nodered.org/u/Los_Andros)\
**Post date:** [22 June 2020 16:52 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/32 "2020-06-22T16:52:52Z")

</div>

Hey Colin,

I used the following formula:

if (msg.payload['Temperatursensor 1'] \> 50 && msg.payload['Temperatursensor 2'] \> 50) {

msg.payload= 1;  
}  
else  
{  
msg.payload= 0;  
}  
return msg;

and I'm able to switch the status of my LED. Thanks a lot for your help!!

---

<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:** [6 July 2020 16:52 UTC](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280/33 "2020-07-06T16:52:55Z")

</div>

Closed on 6 July 2020 16:52 UTC

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

[Previous page](https://discourse.nodered.org/t/if-statement-for-two-ds18b20-sensors/28280.md?page=1)
