Page 1 of 1

DS18B20 temperature sensore calibration

Posted: Sunday 28 May 2023 12:11
by bitzy
hello.
i have a bunch of ds18b20 sensors attached to my raspberry that read various temperatures on my hydronic system and the room temperature ...
the problem i that when put together, to read the same temperature, the deviation of readings is high from 1 sensor to another. i try to do a calibration like in this site https://www.instructables.com/Calibrati ... duino-UNO/.
the only way i could do this was to create a virtual temperature sensor that will display the calculated temperature from this script:

Code: Select all

return {
    on = {
        devices = { 'EXTERIOR_raw' } -- the physical temperature sensore
    },
    execute = function(dz, device)
        -- Calibration values
        local rawHigh = 97.3 -- Raw temperature read at the boiling point of water
        local rawLow = -0.5 -- Raw temperature read in the ice bath
        local rawRange = rawHigh - rawLow

        local referenceHigh = 99.2 -- Temperature for the boiling point of water at atmospheric pressure
        local referenceLow = 0 -- Temperature for the Triple-point bath
        local referenceRange = referenceHigh - referenceLow

        -- Function to perform temperature correction and rounding
        local function correctTemperature(inTemp)
            local corrected = (((inTemp - rawLow) * referenceRange) / rawRange) + referenceLow
            return math.floor(corrected * 10 + 0.5) / 10 -- Round to 1 decimal place
        end
        
        -- Updating the virtual sensor with the calculated value
        if (device.name == 'EXTERIOR_raw') then
            local inTemp = device.temperature
            local correctedTemp = correctTemperature(inTemp)
            dz.devices('EXTERIOR').updateTemperature(correctedTemp)
        end
    end
}



the problem is i need to do this for every sensor i have. and are couple of them (7 for the moment and more are to come) witch lead to a lot of scripts and a lot of new sensors to display.
is there a simpler way to use this type of calibration? some type of advanced calibration directly on the physical sensor?

Re: DS18B20 temperature sensore calibration

Posted: Sunday 28 May 2023 12:56
by bitzy
with the help of my new best friend (ChatGPT) 1 simplification down ;)
1 script to rule them all

Code: Select all

-- Define the mapping between physical and virtual sensors
local sensorMapping = {
  ['EXTERIOR_raw'] = 'EXTERIOR',
  ['Cam_Centrala_raw'] = 'Cam_Centrala',
  ['Vana_Amestec_raw'] = 'Vana_Amestec',
  ['Tur_raw'] = 'Tur',
  ['Retur_raw'] = 'Retur',
  ['Baie_raw'] = 'Baie',
  ['Puffer_raw'] = 'Puffer',
  -- Add more mappings as needed
}

return {
  on = {
    devices = {
      'EXTERIOR_raw',
      'Cam_Centrala_raw',
      'Vana_Amestec_raw',
      'Tur_raw',
      'Retur_raw',
      'Baie_raw',
      'Puffer_raw',
      -- Add more physical sensor devices here
    }
  },
  execute = function(domoticz, device)
    -- Calibration values for each physical sensor
    local calibrationValues = {
      ['EXTERIOR_raw'] = { rawLow = -0.5, rawHigh = 97.3, referenceLow = 0, referenceHigh = 99.2 },
      ['Cam_Centrala_raw'] = { rawLow = -0.3, rawHigh = 98.3, referenceLow = 0, referenceHigh = 99.2 },
      ['Vana_Amestec_raw'] = { rawLow = 0.7, rawHigh = 98.3, referenceLow = 0, referenceHigh = 99.2 },
      ['Tur_raw'] = { rawLow = 0.7, rawHigh = 98.7, referenceLow = 0, referenceHigh = 99.2 },
      ['Retur_raw'] = { rawLow = 0.3, rawHigh = 98.8, referenceLow = 0, referenceHigh = 99.2 },
      ['Baie_raw'] = { rawLow = 0.8, rawHigh = 98.6, referenceLow = 0, referenceHigh = 99.2 },
      ['Puffer_raw'] = { rawLow = 0.9, rawHigh = 98.9, referenceLow = 0, referenceHigh = 99.2 },
      -- Add calibration values for more physical sensors here
    }
    
    -- Get the corresponding virtual sensor
    local virtualSensor = sensorMapping[device.name]
    
    -- Check if the virtual sensor exists in the mapping
    if virtualSensor then
      -- Get the calibration values for the current physical sensor
      local calibration = calibrationValues[device.name]
      local rawLow, rawHigh, referenceLow, referenceHigh = calibration.rawLow, calibration.rawHigh, calibration.referenceLow, calibration.referenceHigh
      
      -- Read raw temperature from the physical sensor
      local rawTemperature = tonumber(device.temperature)
      
      -- Calculate calibration values
      local referenceRange = referenceHigh - referenceLow
      local rawRange = rawHigh - rawLow
      local correctedValue = (((rawTemperature - rawLow) * referenceRange) / rawRange) + referenceLow
      
      -- Round the corrected value to the nearest decimal place
      correctedValue = math.floor(correctedValue * 10 + 0.5) / 10
      
      -- Update the corresponding virtual sensor with the rounded calibrated value
      domoticz.devices(virtualSensor).updateTemperature(correctedValue)
    end
  end
}
i hope that when my new best friend will take over the world, will forget that i made him an idiot !! :( :( :(

now, someone know some more simplification? a way to get rid of all those virtual sensors?

Re: DS18B20 temperature sensore calibration

Posted: Sunday 28 May 2023 13:50
by Kedi
Why not ask your 'new best friend' ?

Re: DS18B20 temperature sensore calibration

Posted: Sunday 28 May 2023 15:29
by bitzy
i already asked and he sent me to the forum cause he do not know (at this point of his live is more an A and less of an I ).

(and u sound a little bit jealous ;) )

Re: DS18B20 temperature sensore calibration

Posted: Monday 05 June 2023 19:42
by willemd
My temperature sensors show an "adjustment" field when I click "edit" on the sensor icon. I assume this is a calibration value?

Re: DS18B20 temperature sensore calibration

Posted: Friday 07 July 2023 15:13
by bitzy
if your sensor need to read a large range of temperatures (for example between -10 and 40 degrees) and u use that calibration button at a certain temperature, your sensor will be way off when go below or above the temperature u calibrated so i think u are better with the factory calibration and not using that.

Re: DS18B20 temperature sensore calibration

Posted: Friday 07 July 2023 19:41
by Kedi
I think that the calibration value is for ALL values that are send to Domoticz, positive and negative values.
It is just a correction of the value send to Domoticz with that amount.

Re: DS18B20 temperature sensore calibration

Posted: Thursday 28 March 2024 15:13
by bitzy
i tryied to calibrate them with the help of that button.i did like this: put all the sensores together in a glass with ice and water and adjusted them all to show the same value. all great.
then i remove them from the water and let them all side by side to read the room temperature and the diferences betweenn them were bigger then without the calibration so ...

Re: DS18B20 temperature sensore calibration

Posted: Thursday 28 March 2024 16:14
by Kedi
Are all ds18b20 set to the same resolution?

Re: DS18B20 temperature sensore calibration

Posted: Friday 29 March 2024 20:48
by HvdW
If you're using dzvents to display the values it's easy.
Adjust the received value in the script.
You can do it either by adding or substracting a value or by adding or substracting a percentage, depending on the deviation under different temperatures.

Re: DS18B20 temperature sensore calibration

Posted: Wednesday 08 May 2024 20:28
by kimot
Connect sensors to ESP running ESPeasy and you can calibrate each sensor individually before send data to Domoticz....

Re: DS18B20 temperature sensore calibration

Posted: Tuesday 14 May 2024 9:35
by simat
Or maybe the DS18B20's are fake, there are loads of non genuine ones on Aliexpress.
https://github.com/cpetrich/counterfeit_DS18B20