DS18B20 smoothing and PID tuning

Hi, one of the good things about Node-Red is that it offers capabilities (core nodes) that lessen the amount of programming (JavaScript ) required to perform the task at hand.

In your case, we can take advantage of the node JOIN that has a reduce capability built in. This is how the configuration of this node looks like (the help in the node explains exactly this very same use case):

s-03

What we have to do before sending messages to the JOIN node is to slipt the flow in such a way that the reading for each sensor takes a different path (assuming there are several sensors active), therefore the use of a switch node.

This is how the flow looks like (for 3 sensors):

s-02

Another assumption for this flow to work properly is that the temperature reading is in the form:

msg.topic -> has the sensor ID
msg.payload -_ temperature reading

s-01

I used in the flow a function node with the only purpose of generating random temperature readings.

The batch node groups the temperature readings each 5 seconds (configurable as you wish).

Flow to import and test:

[{"id":"17ddefb0.b444c","type":"tab","label":"DS18B20 temperature probes","disabled":false,"info":""},{"id":"cb2c2667.60a828","type":"inject","z":"17ddefb0.b444c","name":"","topic":"sensor1","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":158.0999984741211,"y":85.00000095367432,"wires":[["37f436ae.42a18a"]]},{"id":"37f436ae.42a18a","type":"function","z":"17ddefb0.b444c","name":"Format sensor output","func":"let value = msg.payload%100;\nmsg.payload =  value;\nnode.status({text:value});\nreturn msg;","outputs":1,"noerr":0,"x":416.1000747680664,"y":128.00000190734863,"wires":[["15017a37.21a4f6","7bcc6e87.b6812"]]},{"id":"3c31e369.eb1a1c","type":"inject","z":"17ddefb0.b444c","name":"","topic":"sensor2","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":161.99999237060547,"y":130.9999885559082,"wires":[["37f436ae.42a18a"]]},{"id":"e0dc5d43.2940d","type":"inject","z":"17ddefb0.b444c","name":"","topic":"sensor3","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":157.99999237060547,"y":181.00000190734863,"wires":[["37f436ae.42a18a"]]},{"id":"7234c85c.daea08","type":"debug","z":"17ddefb0.b444c","name":"Average Sensor 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":882.0000534057617,"y":241.00000476837158,"wires":[]},{"id":"15017a37.21a4f6","type":"switch","z":"17ddefb0.b444c","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"sensor1","vt":"str"},{"t":"cont","v":"sensor2","vt":"str"},{"t":"cont","v":"sensor3","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":400.10010147094727,"y":263.9999952316284,"wires":[["9292c17b.430b7"],["17a50.34fa45b0d"],["8531688e.337048"]]},{"id":"9292c17b.430b7","type":"batch","z":"17ddefb0.b444c","name":"","mode":"interval","count":10,"overlap":0,"interval":"5","allowEmptySequence":false,"topics":[],"x":584.1001014709473,"y":243.00000476837158,"wires":[["bd5ae1a.a41ad2"]]},{"id":"bd5ae1a.a41ad2","type":"join","z":"17ddefb0.b444c","name":"","mode":"reduce","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"$A+payload","reduceInit":"0","reduceInitType":"num","reduceFixup":"$A/$N","x":712.1000556945801,"y":241.0000057220459,"wires":[["7234c85c.daea08"]]},{"id":"c28b755d.76d118","type":"debug","z":"17ddefb0.b444c","name":"Average Sensor 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":880.0000419616699,"y":292.99999141693115,"wires":[]},{"id":"17a50.34fa45b0d","type":"batch","z":"17ddefb0.b444c","name":"","mode":"interval","count":10,"overlap":0,"interval":"5","allowEmptySequence":false,"topics":[],"x":582.1000900268555,"y":294.99999141693115,"wires":[["6df2759a.4df60c"]]},{"id":"6df2759a.4df60c","type":"join","z":"17ddefb0.b444c","name":"","mode":"reduce","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"$A+payload","reduceInit":"0","reduceInitType":"num","reduceFixup":"$A/$N","x":710.1000442504883,"y":292.99999237060547,"wires":[["c28b755d.76d118"]]},{"id":"a868298b.c152c8","type":"debug","z":"17ddefb0.b444c","name":"Average Sensor 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":880.0000419616699,"y":343.99999141693115,"wires":[]},{"id":"8531688e.337048","type":"batch","z":"17ddefb0.b444c","name":"","mode":"interval","count":10,"overlap":0,"interval":"5","allowEmptySequence":false,"topics":[],"x":582.1000900268555,"y":345.99999141693115,"wires":[["d7342bfb.7fc4e8"]]},{"id":"d7342bfb.7fc4e8","type":"join","z":"17ddefb0.b444c","name":"","mode":"reduce","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"$A+payload","reduceInit":"0","reduceInitType":"num","reduceFixup":"$A/$N","x":710.1000442504883,"y":343.99999237060547,"wires":[["a868298b.c152c8"]]},{"id":"7bcc6e87.b6812","type":"debug","z":"17ddefb0.b444c","name":"Sensor Reading","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":663.0000076293945,"y":127.00000190734863,"wires":[]}]

Testing four readings with sensor2. The average is 44.25

s-04

1 Like