Calculations for dew point

This is alse reporting dew point:

5 Likes

Hi,

I have added Home Assistant Dewpoint integration to my HA from https://github.com/miguelangel-nubla/home-assistant-dewpoint via HACS.

This is my configuration.yaml file:

sensor:
  - platform: dewpoint
    sensors:
      viking_01_dewpoint:
        temperature: sensor.viking_0203502038_03_01_temperature
        rel_hum: sensor.viking_0203502038_03_01_humidity

When I go to Configuration, Server Control and try to Check configuration, I got configuration error:

Platform error sensor.dewpoint - Integration 'dewpoint' not found.

What Iā€™m I missing here?

Thanks for feedback.

I have no experience with that integration, but I donā€™t think you need it since you have almost everything you need in your yaml.

Here is the code of what I use:
Everything below being under ā€œsensor:ā€ in your case. I have my yaml broken into several files so it will look a little different but it is still usable for you with a little modification.
If you use Celsius then just modify the equation as needed.

- platform: template
  sensors:
    bathroom_master_dewpoint:
      friendly_name: "Bathroom Master Dewpoint"
      value_template: >-
        {% set h = states('sensor.bathmaster_multisensor_humidity_air') | float(default=0) %}
        {% set t = states('sensor.bathmaster_multisensor_temperature_air') | float(default=0) %}
        {{((t-32)*5/9-(100-h)/5)*9/5+32}}
      unit_of_measurement: "F"
    bathroom_kid_dewpoint:
      friendly_name: "Bathroom Kid Dewpoint"
      value_template: >-
        {% set h = states('sensor.bathkid_multisensor_humidity_air') | float(default=0) %}
        {% set t = states('sensor.bathkid_multisensor_temperature_air') | float(default=0) %}
        {{((t-32)*5/9-(100-h)/5)*9/5+32}}
      unit_of_measurement: "F"
    office_dewpoint:
      friendly_name: "Office Dewpoint"
      value_template: >-
        {% set h = states('sensor.office_multisensor_humidity_air') | float(default=0) %}
        {% set t = states('sensor.office_multisensor_temperature_air') | float(default=0) %}
        {{((t-32)*5/9-(100-h)/5)*9/5+32}}
      unit_of_measurement: "F"
    laundry_dewpoint:
      friendly_name: "Laundry Dewpoint"
      value_template: >-
        {% set h = states('sensor.laundry_multisensor_humidity_air') | float(default=0) %}
        {% set t = states('sensor.laundry_multisensor_temperature_air') | float(default=0) %}
        {{((t-32)*5/9-(100-h)/5)*9/5+32}}
      unit_of_measurement: "F"

Old post, but Iā€™m trying to get a Dew sensor and used your code.
At the moment my sensor is 9.2 degree and 50.1 %
And this template sensor in HA tells me that Dew Point atm is at -0,7 degree.

But if I try and put in those data into online calculators I get other degrees.
This site:
https://www.dimensionera.se/fukt/daggpunkt.php
Gives me: -0.62
And this site:

Gives me: -0.6643

I guess its no big dealā€¦ but how come they all differ so much? Is it how exact the calculation is maybe?

If anyone is interested, here is my working Dew Point sensor, if you use Celsius for your temperatures:

- platform: template
  sensors:
    dewpoint:
      friendly_name: 'Dew Point'
      unit_of_measurement: 'Ā°C'
      value_template: >
        {% set h = states('sensor.thermostat_humidity') | float %}
        {% set t = states('sensor.thermostat_temperature') | float %}
        {% set a = 17.625 %}
        {% set b = 243.04 %}
        {{ ((b*(log(h/100)+((a*t)/(b+t)))) / (a-log(h/100)-((a*t)/(b+t)))) |float |round(1) }}
5 Likes

Most of the answers shown are using the simple approximation from here:

If you use the full calculation from the same page, you can achieve greater accuracy:
image

Where a = 6.1121 mbar, b = 18.678, c = 257.14 Ā°C, d = 234.5 Ā°C.

Here is a template using your numbers:

{% set T = 9.2 %}
{% set H = 50.1 %}
{% set b = 18.678 %}
{% set c = 257.14 %}
{% set d = 234.5 %}
{% set gamma = log(H / 100 * e ** ((b - T / d) * (T / (c + T)))) %}
{% set Tdp = ((c * gamma / (b - gamma))) | round(2) %}

The result is -0.65

1 Like

Many thanks for performing that conversion from the formula.
Hereā€™s my complete working example entry under sensor: in case it helps.

  - platform: template
    sensors:
      pir3_dewpoint:
        friendly_name: "PIR3 Attic Dewpoint"
        value_template: >-
          {% set H = states('sensor.multisensor_gen5_humidity_3') | float(default=0) %}
          {% set T = states('sensor.multisensor_gen5_air_temperature_3') | float(default=0) %}
          {% set b = 18.678 %}
          {% set c = 257.14 %}
          {% set d = 234.5 %}
          {% set gamma = log(H / 100 * e ** ((b - T / d) * (T / (c + T)))) %}
          {% set Tdp = ((c * gamma / (b - gamma))) | round(2) %}
          {{ Tdp |float |round(1) }}
        unit_of_measurement: "C"
2 Likes

Updated 21 November 2023 to remove erroneous version previously posted in this post.

After considerable head banging, Iā€™ve got the dew point calculations working in a Fahrenheit configured HA system. And which is not dependent on a HACS integration. It uses the new template syntax format as well. The code that is now working and displays dew point temperature to one decimal place:

template:

  # Calculate the dew point temperature to one decimal from SHT31-D
  # 'b', 'c', and 'd' are constants - search web for background
  # needs F to C and back again conversions
  - sensor:
     name: "outside_dewpoint_temperature"
     unit_of_measurement: "Ā°F"
     state: >
          {% set h = states('sensor.outside_humidity_sht31') | float %}
          {% set tf = states('sensor.outside_temp_sht31') | float %}
          {% set t = ((tf - 32 ) * 5 / 9) %}
          {% set b = 18.678 %}
          {% set c = 257.14 %}
          {% set d = 234.5 %}
          {% set gamma = log(h / 100 * e ** ((b - t / d) * (t / (c + t)))) %}
          {% set tdp = ((c * gamma / (b - gamma))) %}
          {% set tdpf = ((tdp * 1.8) + 32) %}
          {{ tdpf | float | round(1) }}

With the values in the above code snippet (derived from the Dec 2022 post) the results are now within a degree of various on line calculators, and are what the wiki article describes for those constants of that equation for reasonable temperature ranges.

Will watch to see if it continues to track correctly over a wider range of temperatures and humidity values. Based on the template editor tests I did it should be fine.

Since the HA system is configured for Ā°F I needed to do Ā°F to Ā°C conversion and then back again to get the expected dew point values in Ā°F. Also decided that the more complex calculation from the Dec 2022 post offered better overall accuracy. Though given the humidity measurement accuracy tolerance of the SHT31-D sensor, calculating dew point to .1 degree is probably not actually that meaningful.

Hopefully this post will help someone in the Ā°F parts of the world and not wanting to use a HACS approach for calculating dew point.

1 Like

Checked Ronnieā€™s templated calculation against the Python psychrolib implementation. Its bang-on accurate.

1 Like

I came looking to see if there is something that would replace my template for calculating dew point. Looks templates are still the way to go. I do mine a little different and thought I would share it since it is universal and reduces how much you have to enter for each sensor.
I make use of a custom template to reduce duplicating the code and make it very simple to add new dew point sensors by providing only the temperature and humidity sensor names to a custom macro.

Features

  • Custom template, only need to provide entity ids for temperature and humidity
  • Automatically detects if the input sensor is in Fahrenheit and perform the required calculations, otherwise defaults to Celsius
  • Will detect if either input sensors are unavailable and show unavailable as well (instead of making errors in your log)
  • Provides dew_point_entities where you provide entity names, and dew_point where you can provide numbers directly

Setup

Custom template file

Create the file /config/custom_templates/dewpoint.jinja with the following code

{# Degrees F to C #}
{%- macro f_to_c(deg_f) -%}
    {{ (float(deg_f) - 32) * 5 / 9 }}
{%- endmacro -%}


{# Degrees C to F #}
{%- macro c_to_f(deg_c) -%}
    {{ float(deg_c) * 9 / 5 + 32 }}
{%- endmacro -%}

{# Dew Point calculation (degrees C) #}
{%- macro dew_point(t, h) -%}
    {%- set t = float(t) -%}
    {%- set h = float(h) -%}
    {%- set L = log(h / 100) -%}
    {%- set M = 17.27 * t -%}
    {%- set N = 237.3 + t -%}
    {%- set B = (L + (M / N)) / 17.24 -%}
    {%- set dew = 237.3 * B / (1 - B) -%}
    {{ dew | round(1) }}
{%- endmacro -%}

{# Dew Point calculation via entity names #}
{%- macro dew_point_entities(temperature_entity, humidity_entity) -%}
    {%- set t = float(states(temperature_entity), 'unavailable') -%}
    {%- set h = float(states(humidity_entity), 'unavailable') -%}
    {%- if 'unavailable' in [t, h] -%}
        {{ 'unavailable' }}
    {%- elif state_attr(temperature_entity, 'unit_of_measurement') == "Ā°F" -%}
        {{ c_to_f(dew_point(f_to_c(t), h)) | round(1) }}
    {%- else -%}
        {{ dew_point(t, h) }}
    {%- endif -%}
{%- endmacro -%}

Note: My formula may look a little different than others because I reduced it algebraically and probably used different variable names.


Refresh

Call the service homeassistant.reload_custom_templates. (Link will take you directly there)


Test it out

Before creating a sensor, hop on over to the Templates Dev Tool to make sure everything is working.
Paste the below input your template editor and play around with your sensor names to make sure itā€™s working.

{%- from "dewpoint.jinja" import dew_point, dew_point_entities  -%}

Replace the entity ids with you temperature sensor and humidity sensor respectively
{{ dew_point_entities('sensor.temp_outside', 'sensor.humidity_outside') }}

Do a direct calculation with this:
11Ā°C and 64% humidity is a dew point of {{ dew_point(11, 64) }}Ā°C (should be 4.5Ā°C)

You can test Fahrenheit by importing the conversion macros too
{%- from "dewpoint.jinja" import f_to_c, c_to_f -%}
50Ā°F and 48% humidity is a dew point of {{ c_to_f(dew_point(f_to_c(50), 48)) | round(1) }}Ā°F (should be 31.1Ā°F)

If itā€™s not working for you make sure you are using dew_point_entities with entity names or dew_point for numbers.


Add the sensor

yaml Config

To use the custom template you need to import the sensor any where you use it. It isnā€™t available globally. If you are doing it in yaml, it will look like this:

dew_point_outside:
  friendly_name: Outside Dew Point
  unit_of_measurement: "Ā°F"
  value_template: >
    {% from 'dewpoint.jinja' import dew_point_entities %}
    {{ dew_point_entities('sensor.temp_outside', 'sensor.humidity_outside') }}

Template Sensor Helper

I have started using helpers for this sort of thing which makes it nice to demonstrate with just a picture:

This is what I have in the template field:

{% from 'dewpoint.jinja' import dew_point_entities %}
{{ dew_point_entities(
  'sensor.temp_outside',
  'sensor.humidity_outside'
) }}

Resources

HA Docs: Reusing Templates
Formula on Wikipedia

2 Likes

Awesome guide. Just integrated this into my home assistant, cleaning up a lot of repeated dewpoint templates!

1 Like