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;