DS18B20 temperature sensore calibration

Moderator: leecollings

Post Reply
bitzy
Posts: 33
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

DS18B20 temperature sensore calibration

Post 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?
bitzy
Posts: 33
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: DS18B20 temperature sensore calibration

Post 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?
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: DS18B20 temperature sensore calibration

Post by Kedi »

Why not ask your 'new best friend' ?
Logic will get you from A to B. Imagination will take you everywhere.
bitzy
Posts: 33
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: DS18B20 temperature sensore calibration

Post 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 ;) )
willemd
Posts: 621
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: DS18B20 temperature sensore calibration

Post by willemd »

My temperature sensors show an "adjustment" field when I click "edit" on the sensor icon. I assume this is a calibration value?
bitzy
Posts: 33
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: DS18B20 temperature sensore calibration

Post 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.
Last edited by bitzy on Thursday 28 March 2024 14:58, edited 1 time in total.
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: DS18B20 temperature sensore calibration

Post 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.
Logic will get you from A to B. Imagination will take you everywhere.
bitzy
Posts: 33
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: DS18B20 temperature sensore calibration

Post 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 ...
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: DS18B20 temperature sensore calibration

Post by Kedi »

Are all ds18b20 set to the same resolution?
Logic will get you from A to B. Imagination will take you everywhere.
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: DS18B20 temperature sensore calibration

Post 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.
Bugs bug me.
kimot
Posts: 104
Joined: Saturday 25 November 2017 17:18
Target OS: Raspberry Pi / ODroid
Domoticz version: v3.8153
Location: Czech Rep.
Contact:

Re: DS18B20 temperature sensore calibration

Post by kimot »

Connect sensors to ESP running ESPeasy and you can calibrate each sensor individually before send data to Domoticz....
RPi2 Domoticz v 4.10717
10 x Sonoff Basic - ESPeasy
1 x Wemos D1 - ESPeasy
1 x Shelly Plus Plug S
1 x Sonoff S26 - ESPeasy
1 x Shelly 1
1 x MySensors HC-SR04
1 x MySenosrs wifi gateway
1 x RFLink
4x Cam IPC-T240H
simat
Posts: 33
Joined: Thursday 04 November 2021 21:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: UK
Contact:

Re: DS18B20 temperature sensore calibration

Post by simat »

Or maybe the DS18B20's are fake, there are loads of non genuine ones on Aliexpress.
https://github.com/cpetrich/counterfeit_DS18B20
Raspberry Pi4, Sunny Boy 4000TL, Victron Multiplus II 10kw ESS, 44kWh LiFEPO4, Batrium BMS, NodeRED on Cerbo GX
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest