Skip to content

Instantly share code, notes, and snippets.

@Alexivia
Last active January 16, 2024 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexivia/59155b3f8939a388e7c9254086efca7d to your computer and use it in GitHub Desktop.
Save Alexivia/59155b3f8939a388e7c9254086efca7d to your computer and use it in GitHub Desktop.
Jinja macros to create dew point sensors in Home Assistant.
{% macro dew_point(i_temperature, i_humidity) %}
{# https://www.home-assistant.io/docs/configuration/templating/#reusing-templates #}
{# https://en.wikipedia.org/wiki/Dew_point #}
{# https://iridl.ldeo.columbia.edu/dochelp/QA/Basic/dewpoint.html #}
{# https://gasquip.com/dew-point-calculator-for-moisture/ #}
{% set temperature = i_temperature|float %}
{% set humidity = i_humidity|float %}
{% set a = 6.1121|float %}
{% set b = 17.67|float %}
{% set c = 243.5|float %}
{% set gamma_m = log(humidity / 100.0) + (b * temperature / (c + temperature)) %}
{% set dew_point = (c * gamma_m) / (b - gamma_m) %}
{{ dew_point|float }}
{% endmacro %}
{% macro dew_point_text(i_dew_point) %}
{# https://www.mrfixitbali.com/air-conditioning/temperature-humidity-dew-point-251.html #}
{% set comfort_levels = {
"Extremely Dry": [-30.0, -10.0],
"Very Dry": [-10.0, 7.0],
"Dry": [7.0, 10.0],
"Comfortable": [10.0, 13.0],
"Ok": [13.0, 16.0],
"Tolerable": [16.0, 19.0],
"Uncomfortable": [19.0, 22.0],
"Very Uncomfortable": [22.0, 25.0],
"Extremely Uncomfortable": [25.0, 28.0],
"Unbearable": [28.0, 45.0],
} %}
{% set dew_point = i_dew_point|float %}
{% for comfort_level in comfort_levels -%}
{% if dew_point >= comfort_levels[comfort_level][0] and dew_point < comfort_levels[comfort_level][1] -%}
{{ comfort_level }}
{%- endif %}
{%- endfor %}
{% endmacro %}
@Alexivia
Copy link
Author

I recently added a smart switch in my bathroom ventilation, where I already a temperature and humidity sensor monitoring the data in my Home Assistant installation. My main purpose was to control the ventilation automatically so it would become one less (boring) thing to have to remember to do.
These Jinja macros allow me to define software sensors in Home Assistant, based on the temperature and humidity values, so I can monitor the dew point of the room. I use the dew point instead of the relative humidity, since this allows for a better control of "safe" humidity levels, while also considering the room's temperature.
I included links for some sources I used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment