Comparing two constants with ds18b20 in function node

In Arduino, things look like this:

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.

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

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
Search : Library - Node-RED

You will also need to install : node-red-node-pi-gpio (node) - Node-RED
(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

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

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.

So the flow might look something like this

2 Likes

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

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

Or rather like this:

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;

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.

[{"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"]]}]

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

[{"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.