# 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 )

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

1 Like

Bump…

Simplistic Templates Sensor based on this calculation: https://en.wikipedia.org/wiki/Dew_point#Simple_approximation

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

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

Thanks!
I reformatted your example.

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

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 I guess

2 Likes

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.

Hmmm,

``````I added this to my configuration.yaml:
sensor:
platform: template
sensors:
taupunkt_keller:
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:

``````sensor:
- platform: template
sensors:
taupunkt_keller:
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

Nice!
But notice this from the documentation:

STATES

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

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

2 Likes

again 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
2 Likes

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
sensors:
lr_dewpt_gamma:
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
sensors:
lr_dewpt:
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) }}
``````

Scott

8 Likes

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: https://en.m.wikipedia.org/wiki/Dew_point.

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,
Scott

1 Like

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

4 Likes

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.

``````--- sensor.py.bak
+++ sensor.py
@@ -10,7 +10,7 @@

import voluptuous as vol

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

try:
-            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
psychrolib.SetUnitSystem(psychrolib.SI)
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!