IF Statement for two DS18B20 Sensors

Dear Node Red Community,

as part of an university project did I connect two DS18B20 sensors (via a Rasperry Pi) to Node Red.
My goal is to insert a function flow, which will alert the user if both sensors show a payload higher than 50 degrees. Since I have very little experience coding with Java Script I dont know how to adress each msg.payload to the specific sensor.

The code should look a little bit like this:
if ('Sensor 1'msg.payload>50 && 'Sensor 2'msg.payload>50){
msg.payload="Temp too high!"
return[msg,null];
}
return msg;

I hope my description was helpful and I look forward for your suggestions, big thank you!

You should first read the section in the documentation about working with messages. Once you have read that look at the join node

And in particular the cookbook recipe on Joining Streams.

[{"id":"7ce8dc5c.5ca08c","type":"tab","label":"Temp","disabled":false,"info":""},{"id":"95cae51e.19f358","type":"inject","z":"7ce8dc5c.5ca08c","name":"Emulate Sensor 1","topic":"","payload":"51","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":100,"wires":[["c9454982.d00d5"]]},{"id":"c9ca42cc.090538","type":"inject","z":"7ce8dc5c.5ca08c","name":"Emulate Sensor 2","topic":"","payload":"51","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":180,"wires":[["d83fce4.0c0973"]]},{"id":"c9454982.d00d5","type":"function","z":"7ce8dc5c.5ca08c","name":"From Sensor 1","func":"flow.set('sensor_1', msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":100,"wires":[["64d7c139.033be","ff2ec504.77147"]]},{"id":"d83fce4.0c0973","type":"function","z":"7ce8dc5c.5ca08c","name":"From Sensor 2","func":"flow.set('sensor_2', msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":180,"wires":[["64d7c139.033be","2809fe47.4a8cca"]]},{"id":"64d7c139.033be","type":"function","z":"7ce8dc5c.5ca08c","name":"Both Sensors ?","func":"var sensor_1 = flow.get('sensor_1') || 0;\nvar sensor_2 = flow.get('sensor_2') || 0;\n\nif (sensor_1 > 50 && sensor_2 > 50)\n{\n    msg.payload = 'Alert !!!';\n    return msg\n}    ","outputs":1,"noerr":0,"x":750,"y":140,"wires":[["a4ed887f.39e09"]]},{"id":"a4ed887f.39e09","type":"debug","z":"7ce8dc5c.5ca08c","name":"Alert","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":930,"y":140,"wires":[]},{"id":"7219e70f.00421","type":"inject","z":"7ce8dc5c.5ca08c","name":"Emulate Sensor 2","topic":"","payload":"50","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":220,"wires":[["d83fce4.0c0973"]]},{"id":"ff2ec504.77147","type":"debug","z":"7ce8dc5c.5ca08c","name":"Sensor 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":740,"y":60,"wires":[]},{"id":"2809fe47.4a8cca","type":"debug","z":"7ce8dc5c.5ca08c","name":"Sensor 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":740,"y":220,"wires":[]}]

First of all thank you for the response.

After reading the recommended articels i joined the two flows.

Still I do not know how to adress each sensor and its payload for an IF Statement. So I am missing the msg.payload.(name of the sensor?) So how to adress Sensor 1 and Sensor 2 if they exceed 50 degrees

Just for test, disable one sensor. You will still receive alert.

You need to check if you receive data from each sensor.

Both sensors are working fine. I need to create an IF Statement in order to check if both sensors exceeded 50 degrees. If so the next events (a LED and a message box) will be triggered.
In order to create that statement I need to know how to adress the sensor by using Java Script.

I'm not near a computer to try this out, but I think you could do something like...

if (msg.payload['Temperaturesensor 1'] > 50 && msg.payload['Temperaturesensor 2'] > 50 )  {
   msg.payload = "Temperature is too high";
}
else {
   msg.payload = "Temperature is OK";
}
return msg;
1 Like

It is better not to put spaces (or other special characters) into the topic. If they had underscores instead you could do
if (msg.payload.Temperaturesensor_1 > 50 && msg.payload.Temperaturesensor_2 > 50
Which saves strain on the fingers and the brain.

2 Likes

Thank you for definement of the sensor! It really helped.
Unfortunately even when both sensors are above 50 degrees they still dont show the message "Temperature is too high"... Could this be that the formula does not recognize the value 50? Does it need to be further definded? Something like >msg.payload=50?

Have you checked that you spelt Temperaturesensor (or possibly Temaratursensor) correctly? I notice that the suggestions posted have spelt it differently to your debug output.

Yes I did. I tried to do the if statement with only one sensor:

if (msg.payload.Temperatursensor_1 >20)
{
msg.payload ="Temperature is too high";
}
else
{
msg.payload="temp is ok"
}
return msg;

It still shows me temp is ok, eventhough it is above 20 degrees (changed the number to 20 just to get the "temp is too high" result). So the if statement is not recognizing the number or the sensor.

I added a join node on top of the suggestion you gave me and it worked!

Thank you so much!

The suggestions were made knowing that you already had the Join node in the flow.

I've already said that a couple of times here in this forum but, just a quick hint: for such a functionality it is not required to use the "function" node. As an exercise try to always avoid the usage of a "function" node and use the "switch", "change", "join" and "split" nodes instead.
After you get used to it you will see that "function" is only necessary for extreme situations :slight_smile:

3 Likes

Whilst that is entirely true, where the choice is simple javascript versus non-trivial use of a Change node with jsonata then, for a beginner, learning some javascript may well be a more useful choice than leaping straight into jsonata. However, certainly there is no harm in posting the equivalent solution using, in this case, presumably, a Change node.

Here is the possible solution using only change and switch nodes (JSONata is not required).

[{"id":"339ae978.6528a6","type":"inject","z":"d8490dda.1a1e5","name":"Sensor 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"payload":"60","payloadType":"num","x":180,"y":260,"wires":[["9b609f90.f2f78"]]},{"id":"adca7f1b.fcfde","type":"inject","z":"d8490dda.1a1e5","name":"Sensor 2","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"payload":"60","payloadType":"num","x":180,"y":380,"wires":[["79e8845f.5fbafc"]]},{"id":"9b609f90.f2f78","type":"change","z":"d8490dda.1a1e5","name":"","rules":[{"t":"set","p":"sensor1","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":260,"wires":[["42a9c86e.9efd88"]]},{"id":"79e8845f.5fbafc","type":"change","z":"d8490dda.1a1e5","name":"","rules":[{"t":"set","p":"sensor2","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":380,"wires":[["42a9c86e.9efd88"]]},{"id":"42a9c86e.9efd88","type":"switch","z":"d8490dda.1a1e5","name":"Sensor 1 > 50","property":"sensor1","propertyType":"flow","rules":[{"t":"gt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":660,"y":320,"wires":[["edca8fa6.76025"]]},{"id":"edca8fa6.76025","type":"switch","z":"d8490dda.1a1e5","name":"Sensor 2 > 50","property":"sensor2","propertyType":"flow","rules":[{"t":"gt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":860,"y":320,"wires":[["d6254350.6f88"]]},{"id":"d6254350.6f88","type":"debug","z":"d8490dda.1a1e5","name":"ALERT!!!","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1040,"y":320,"wires":[]}]
2 Likes

What do you see is the advantage of getting rid of one Join node and replacing it with two Change nodes?

It is not an advantage in terms of performance. What I try to encourage is to make people using switch nodes instead of function + "IF", because I see very often, people that are not used to JS, to abandon Node-RED complaining that "it's syntax is too complicated". That's why I always recommend to everybody to learn how to use the standard node-red nodes, and avoid as much as possible the function node (at least in the beginning).

What I meant was what is wrong with using a Join node instead of two Change nodes (plus having to use flow context, which should generally only be done when there is a good reason)?

[{"id":"b50ddd83.4bce98","type":"inject","z":"8c99ec05.74e658","name":"Sensor 1 40","topic":"Temperatursensor_1","payload":"40","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":104,"y":873,"wires":[["ba231650.e5c3a8"]]},{"id":"87099bf.67aa968","type":"inject","z":"8c99ec05.74e658","name":"Sensor 2 40","topic":"Temperatursensor_2","payload":"40","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":104,"y":1000,"wires":[["ba231650.e5c3a8"]]},{"id":"8f1d45b4.ac8d1","type":"switch","z":"8c99ec05.74e658","name":"Sensor 1 > 50?","property":"payload.Temperatursensor_1","propertyType":"msg","rules":[{"t":"gt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":425,"y":932,"wires":[["a8826527.31e6d8"]]},{"id":"a8826527.31e6d8","type":"switch","z":"8c99ec05.74e658","name":"Sensor 2 > 50?","property":"payload.Temperatursensor_2","propertyType":"msg","rules":[{"t":"gt","v":"50","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":932,"wires":[["fcf750af.81b83"]]},{"id":"fcf750af.81b83","type":"debug","z":"8c99ec05.74e658","name":"ALERT!!!","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":554,"y":1015,"wires":[]},{"id":"ba231650.e5c3a8","type":"join","z":"8c99ec05.74e658","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":271,"y":932,"wires":[["8f1d45b4.ac8d1"]]},{"id":"183fdae0.e8457d","type":"inject","z":"8c99ec05.74e658","name":"Sensor 1 60","topic":"Temperatursensor_1","payload":"60","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":107,"y":910,"wires":[["ba231650.e5c3a8"]]},{"id":"5b068159.d3724","type":"inject","z":"8c99ec05.74e658","name":"Sensor 2 60","topic":"Temperatursensor_2","payload":"60","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":102,"y":1041,"wires":[["ba231650.e5c3a8"]]}]