Can not figure how to do a node-red math funtion - maybe someone could help

hi there, i was working on ds18b20 arduino.ino that transmits the temperature data of multiple sensors via Mqtt. if you plug one in it transmit the info of that one using it mac ID as the mqtt topic. if you plug 2 , 3 … etc it will continue to publish the temps from all the sensors using each individual macs as the topic automatically … i got that working fine. my problem is it publishes in raw format. which is not a problem in freeboard that easily corrected in the java script editor

my issue is with dashboard ui-- I can not figure out . how to convert the raw data to say Celsius . which basically is the raw data divided by 16.
I can not figure out what sort of math format it wants to use and where. i assume it is the function option. if someone would be so kind to point me in the right direction it would be greatly appreciated

Please could you post an example of what your trying to do. The DS18B20 can return temperature in deg C

There are a few places where I consume raw numeric data and I found I had to create a “Force Number” function that simply does this:

msg.payload = Number(msg.payload);
return msg;

Then stuff started working.

1 Like

thank you for your replies.
glyn do you want the ino for the esp- I will post it in a bit once I finish cleaning up a bit more… i think it is very useful ino for the esp. if i just need the one ds18b20 or several it automatically self configure to what ever number of sensors I plug into…

thanks peter that did the trick.

msg.payload = Number(msg.payload);
msg.payload = (msg.payload)/16;
return msg;

now it lists the raw data and converts it to Celsius and later on if I want Fahrenheit or Kelvin in my Dashboard I just convert the raw data directly in node-red

Okay as promised the “cleaned up” code…
it handles multiple DS18B20 from 1 sensor and up automatically.
when I built my little box to hold my esp i just mounted a bunch of mini 3.5 female jacks and then male plugs to the ds18b20. I only tested up to 4 but it should be able to do more.

it simple to use - just modify the wifi and mqtt setting to match your own. and that’s pretty much it.
once it connects to wifi and publishes to it, you can easily grab the topic name for each individual ds1820 by simply subscribing to the topic /ID/ from there it will publish the topic of the temperature sensor 20 times then not publish it any more until the device restarts.

I also changed the publishing of raw data and it now publishes in Celsius

ie:

/temp/28ff03121165c9
/temp/28ff2dc57016572

if you have a lot of temperature sensors, then just change the /temp/ to say, /spa/ or /garage/ and then it will publish the topic like this.

/spa/28ff03121165c9
or
/garage/28ff2dc57016572

if a sensor dies, or more are added, restart the device to publish the updated sensorID for the new ds18b20s
mqtt_esp_1_wt.ino (4.8 KB)

okay. good luck, have fun.

1 Like