Hi there.
I liked this, but needed dew point calculations for various rooms. Turns out, the new macros syntax is pretty neat to make multiple template sensors with a macro function.
Here’s how it works on my side.
Step 1: Create a new folder custom_templates/ in your configuration folder, if it doesn’t exist already.
Step 2: Create a new file custom_templates/macros.jinja:
{% macro calculate_dew_point(entity_temperature, entity_humidity) %}
{% set T, RH = states(entity_temperature), states(entity_humidity) %}
{% if (T == 'unknown') or (RH == 'unknown') %}
unknown
{% elif (T == 'unavailable') or (RH == 'unavailable') %}
unavailable
{% else %}
{% set T = T | float %}
{% set RH = RH | float %}
{% set a = 6.1121 | float %}
{% set b = 18.678 | float %}
{% set c = 257.14 | float %}
{% set d = 234.5 | float %}
{% set e = 2.71828 | float %}
{% set gamma = log((RH/100)*e**((b-T/d)*(T/(c+T)))) | float %}
{% set dew_point = ((c * gamma) / (b - gamma)) | round(1) %}
{{ dew_point }}
{% endif %}
{% endmacro %}
Step 3: Create your template sensors like this:
template:
- sensor:
- name: "Kitchen - Dew Point"
unique_id: kitchen_dewpoint
unit_of_measurement: "°C"
icon: mdi:water-percent-alert
state: >
{% from 'macros.jinja' import calculate_dew_point %}
{{ calculate_dew_point('sensor.kitchen_temperature', 'sensor.kitchen_humidity') }}
P.S.: The P formula from the original code got removed in step 2, as it wasn’t used for the dew point result.