Calculations for dew point

Hello everybody,
Is there a way to make calculations witch sensors-data in HA?

I have temperature sensors and humidity sensors outdoor and indoor.
Now I would like to calculate from these values the dew point and then turn on a fan in the basement.

But I have to perform calculations to calculate the dew point. Similar to this Arduino script:

//int runtime;
float tautemp;
float tau, maxtau, mintau;

//Temp & Humid
float a = 17.271; // für Taupunkt Formel
float b = 237.7; // für Taupunkt Formel

float temperature;
float temp, maxtemp, mintemp;
float h,h1, maxh, minh;
float t,t1;

void taupunkt()
  tautemp = (a * t1) / (b + temp) + log(h1 / 100);
  tau = (b * tautemp) / (a - tautemp); 

oid minmax()
if (temp > maxtemp) maxtemp = temp;
if (temp < mintemp) mintemp = temp;
if (h > maxh) maxh = h;
if (h < minh) minh = h;
if (tau > maxtau) maxtau = tau;
if (tau < mintau) mintau = tau; */

the only easy way i can think off is to use appdeamon and make a py code out of this.

Did you try with a template sensor?

It uses Jinja2 templating engine and can do some stuff like that (might be hard to render in yaml but you can try :slight_smile: )

Another way could be to create custom component that is doing the calculation like the Mold sensor.

1 Like


Simplistic Templates Sensor based on this calculation:

Put this in sensor.yaml (with correct indentations):

  • platform: template
    value_template: ‘{{ (states.sensor.oregoncar_temperature.state | float * 1) - ((100 - (states.sensor.oregoncar_humidity.state | float * 1)) / 5) }}’

I reformatted your example.

  platform: template
        value_template: "{{ ((states('oregoncar_temperature') | float * 1) - 
          (100 - (states('oregoncar_humidity') | float * 1) / 5)) | round(2) }}"

Is it possible to calculate this for two sensors without duplicating the code?

For me your formula calculates wrong DP, I´ve modifed it a bit - 1 more pair of brackets :

{{ ((states.sensor.temp_2.state | float - ((100 - states.sensor.humidity.state | float) /5)))}}

Now it is correct :slight_smile: I guess :sunglasses:


Is it possible to calculate this for two sensors without duplicating the code?
With AppDaemon you can. I don’t think you can with regular Home Assistant sensors.


I added this to my configuration.yaml:
  platform: template
        value_template: "{{ ((sensor.hmwds40thi2_meq1651011_temperature | float - ((100 - sensor.hmwds40thi2_meq1651011_humidity | float) /5)))}}"

But I get this error message:

rror loading /home/homeassistent/.homeassistant/configuration.yaml: mapping values are not allowed here
  in "/home/homeassistent/.homeassistant/configuration.yaml", line 76, column 12

What am I doing wrong?

I could eliminate the errors. Now it looks like this and seems to work:

  - platform: template
        friendly_name: "Taupunkt Keller"
        value_template: "{{ ((state.sensor.hmwds40thi2_meq1651011_temperature | float - ((100 - state.sensor.hmwds40thi2_meq1651011_humidity | float) /5)))}}"
        entity_id: sensor.hmwds40thi2_meq1651011_temperature
1 Like

But notice this from the documentation:


The next two statements result in same value if state exists. The second one will result in an error if state does not exist.

{{ states('device_tracker.paulus') }}
{{ states.device_tracker.paulus.state }}

If state is unknown for instance during startup of hass it will generate an error.

Thanks, I will change that :slight_smile:

But actually I am confused how it calculates --> heavily wrong!

With the given values of the sensors it calculates dew point = -20 !
The formula seems right for me. I calculated “by hand” and got another value: 7,6 WTF!

It gets even more strange, when I replace the sensor Id with the actual values of the sensors, it gets another value:

"{{ ((states.sensor.hmwds40thi2_meq1651011_temperature | float) - ((100 - states.sensor.hmwds40thi2_meq1651011_humidity | int) /5)) }}"

–> -20

"{{ ((14.2) - ((100 - 67) /5)) }}"

–> 7,6 (same I calculated manually)

Perhaps I am too tired now, but I don not understand this.

After I made the change according your hint, it calculates right:

{{ ((states('sensor.hmwds40thi2_meq1651011_temperature') | float) - ((100 - states('sensor.hmwds40thi2_meq1651011_humidity') | int) /5)) }}"

–> 7,6

So the problem is solved, but not understood :wink:


again :wink: from the documentation:

  • states.sensor.temperature returns the state object for sensor.temperature .
  • states('device_tracker.paulus') will return the state string (not the object) of the given entity or unknown if it doesn’t exist

I realize the thread is older, but it really helped me! Here are some sensors with a fancier dew point calculation, in case it’s of use to someone else.

  - platform: template
        friendly_name: "Living Room Dew Point Input"
        value_template: >-
          {{ log( states('sensor.living_room_humidity')|int / 100 ) + 18.678 * states('sensor.living_room_temperature')|float / (257.14 + states('sensor.living_room_temperature')|float )  }}  
  - platform: template          
        friendly_name: "Living Room Dew Point"
        unit_of_measurement: '°C'
        value_template: >-
          {{ (257.14 * states('sensor.lr_dewpt_gamma')|float / (18.678 -  states('sensor.lr_dewpt_gamma')|float ))|round(1) }} 



Why do you use lr_dewpt_gamma and lr_dewpt? Could you please explain the logic and maths behind that? TIA

Sorry, I should have included a reference to source documentation. I borrowed the first relationship listed on the Wikipedia page for calculating dew point, here:

The ‘gamma’ sensor is just a intermediate step that isn’t very meaningful. Its used twice in the actual dewpoint calculation (the ‘lr_dewpt’ sensor), it just saves me from writing the math out twice. I hide the gamma portion in my setup. You could also copy the math into the final sensor and only have one sensor (that’s very long) since no one will see the math…

I’m pretty sure the whole thing is just an empirical data fit of the steam tables anyways.

Hope that helps,

1 Like

Started here, ended up making a custom component.
Hope it is also useful to you.


It’s great thanks! I had to make a small change to work for me because my default unit is Fahrenheit. I think it will work for both with the change.

@@ -10,7 +10,7 @@

 import voluptuous as vol

+from homeassistant.util.temperature import convert
 from homeassistant.core import callback
 from homeassistant.const import (
@@ -108,8 +108,7 @@
         unit = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)

-            return self.hass.config.units.temperature(
-                float(state.state), unit)
+            return convert(float(state.state), unit, TEMP_CELSIUS)
         except ValueError as ex:
             _LOGGER.error('Unable to read temperature from sensor: %s', ex)

@@ -135,4 +134,4 @@
             import psychrolib
             TDewPoint = psychrolib.GetTDewPointFromRelHum(dry_temp, rel_hum)
-            self._state = round(TDewPoint, 2)
\ No newline at end of file
+            self._state = round(TDewPoint, 1)

Thank you, updated!