# Comparing two constants with ds18b20 in function node

**URL:** <https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972>\
**Category:** General\
**Created:** [6 September 2025 15:20 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972 "2025-09-06T15:20:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [6 September 2025 15:20 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/1 "2025-09-06T15:20:35Z")

</div>

In Arduino, things look like this:

```auto
if ((temp > -20) && (temp < 50))
      {
        client.publish("bathtub_temp", String(temp, 1).c_str());
      }
      else
      {
        client.publish("bathtub_temp", String(temp, 1).c_str());
        digitalWrite(relay_heater, LOW);
        client.publish("bathtub_heater", "stopped");
        client.publish("bathtub_temp_malfunction", "sensor malfunction");

```

How do I describe it in a Node red function?

If sensor is out of condition, heater os off.

---

<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:** [6 September 2025 15:44 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/2 "2025-09-06T15:44:59Z")

</div>

What do mean by `client.publish` and `digitalWrite` when running in node-red?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [6 September 2025 15:52 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/3 "2025-09-06T15:52:51Z")

</div>

Hi @devifast

I am not really a hardware guy - but I'll give it a go - plenty here, who are better qualified.

To read the sensor, you can install a Node that supports it, here is one from the Nodes site - but do your own research

Node : [node-red-contrib-ds18b20-sensor (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-ds18b20-sensor)  
Search : [Library - Node-RED](https://flows.nodered.org/search?term=ds18b20)

You will also need to install : [node-red-node-pi-gpio (node) - Node-RED](https://flows.nodered.org/node/node-red-node-pi-gpio)  
(to write to the GPIO on the pi)

Then in a `function` node (connected to output of `ds18b20`), connect **ITS** output to the GPIO Node

```auto
if ((msg.payload > -20) && (msg.payload < 50)) {
    // Do something
} else {

    // achieves digital write (using the GPIO Node)
    msg.payload = 0 // Digital State
    msg.topic = "4" // Target GPIO
    return msg
}

```

I may have the above wrong - but trying to understand hardware a little more.

**EDIT:**  
And as @Colin states, what is `client`? - I have omitted it in my example

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [6 September 2025 16:04 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/4 "2025-09-06T16:04:06Z")

</div>

I guess that 'client.publish' indicates that you are publishing to MQTT.  
If you use the Node-red db18b20 node (which I never used), do you still need to publish to these MQTT topics?

As for the sanity test, you don't actually need a function node. A switch node can do this very easily.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/a/1adec37e6c8ecc2caae353d64f6ad7d1145422e9.png)

So the flow might look something like this

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

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [6 September 2025 16:19 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/5 "2025-09-06T16:19:44Z")

</div>

I did a copy-paste, my mistake. Otherwise the function should look like this.

```auto
if ((msg.payload.temp > -20) && (msg.payload.temp < 50)) {
        msg.payload=true;
}
        else {
        msg.payload=false;
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [6 September 2025 17:43 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/6 "2025-09-06T17:43:35Z")

</div>

Or rather like this:

```auto
var hightemp = 50;
var lowtemp = -20;
var setpoint = msg.payload.triger_heater;
var hysteresis = 1;
var heater = msg.payload.temp;

if (heater >= (setpoint + hysteresis)) {
    msg.payload = "off";
}
else if (heater <= (setpoint - hysteresis)) {
    msg.payload = "on";
}
else if (heater >= hightemp) {
    msg.payload = "malfunction";
}
else if (heater <= lowtemp) {
    msg.payload = "malfunction";
}
else {
    msg.payload = "standby";
}
msg.topic = "setheater";
return msg;

```

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [10 September 2025 21:07 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/8 "2025-09-10T21:07:10Z")

</div>

The problem with DS18b20 sensors is that in the absence of a sensor it reads a temperature of -127 degrees Celsius. In the presence of a sensor, and under certain conditions it reads 85 degrees Celsius. In order to avoid problems with temperature reading, it is necessary to write a function like this. With this function we determine the measurement parameters. If they do not match the specified values, we consider the sensor to be damaged.

```auto
[{"id":"7a0924e596359f55","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"0d03423efbf42c01","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"10","payloadType":"num","x":240,"y":240,"wires":[["745499cecfc990c6"]]},{"id":"e490a503cb240606","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"45","payloadType":"num","x":240,"y":280,"wires":[["745499cecfc990c6"]]},{"id":"e861de94ec407232","type":"function","z":"7a0924e596359f55","name":"hitemp","func":"var setpoint = msg.payload.triger_heater;\nvar hysteresis = 1;\nvar heater = msg.payload.temp;\n\nif (heater >= (setpoint + 1)) {\n msg.payload = \"off\";\n}\nelse if (heater <= (setpoint - 1)) {\n msg.payload = \"on\";\n}\nelse {\n msg.payload = \"standby\";\n}\nif (heater >= 40) {\n msg.payload = \"malfunction\";\n}\nelse if (heater <= 0) {\n msg.payload = \"malfunction\";\n}\nmsg.topic = \"setheater\";\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":220,"wires":[["bbd45494c186fc33"]]},{"id":"bbd45494c186fc33","type":"debug","z":"7a0924e596359f55","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":220,"wires":[]},{"id":"69b8065125d3cd3c","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"20","payloadType":"num","x":240,"y":160,"wires":[["745499cecfc990c6"]]},{"id":"745499cecfc990c6","type":"join","z":"7a0924e596359f55","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":430,"y":220,"wires":[["e861de94ec407232","7a43ac85403a4f08"]]},{"id":"563afa0eed47fa38","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"triger_heater","payload":"25","payloadType":"num","x":230,"y":110,"wires":[["745499cecfc990c6"]]},{"id":"7a43ac85403a4f08","type":"debug","z":"7a0924e596359f55","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":180,"wires":[]},{"id":"40add49f827cf26a","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"24.5","payloadType":"num","x":240,"y":200,"wires":[["745499cecfc990c6"]]},{"id":"e33fe96aa98ce240","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"27","payloadType":"num","x":250,"y":320,"wires":[["745499cecfc990c6"]]},{"id":"b1ede71efcede278","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"23","payloadType":"num","x":250,"y":360,"wires":[["745499cecfc990c6"]]},{"id":"57f58b6631cb18c4","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"-10","payloadType":"num","x":250,"y":400,"wires":[["745499cecfc990c6"]]},{"id":"3537986c4fc763aa","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"-20","payloadType":"num","x":250,"y":440,"wires":[["745499cecfc990c6"]]}]

```

---

<div class="post-metadata">

**Author:** ![devifast](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@devifast](https://discourse.nodered.org/u/devifast)\
**Post date:** [18 September 2025 18:09 UTC](https://discourse.nodered.org/t/comparing-two-constants-with-ds18b20-in-function-node/98972/9 "2025-09-18T18:09:50Z")

</div>

@Colin  
Hello, I want to apologize for the misleading topic I created. The idea was how to create a function that would output an error in case of a damaged ds18b20 sensor (if the sensor shows a temperature less than 0 degrees Celsius or higher than 40 degrees Celsius). I had to include (undefined) in the function which means that there is no sensor at all. This was new to me because I am not that good at syntax.

```auto
[{"id":"40add49f827cf26a","type":"inject","z":"7a0924e596359f55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"temp","payload":"24.5","payloadType":"num","x":240,"y":200,"wires":[["745499cecfc990c6"]]}]

```

You decide whether I did it successfully. Best regards.
