Help with template sensor to dynamically change whole home humidifier to target

Hi – I have a whole home humidifier and live in an area where temps can range wildly. I’d like to be able to control my ecobee target humidity (which in turn controls the humidifier) based on relative humidity and outdoor temperatures.

Optimal humidity based on outdoor temp:
relativehumiditychart

Template sensor to set target humidity

The basic calculation works, however, I’d like to set a minimum value of 20% and maximum value of 50% and can’t figure out how to do it, something in my template is off so its showing multiple values.

Example, right now its -15f outside (Northern United States) … which is below (18%) my minimum value (20%) so the template should set it to 20%.

The idea is to setup an automation that runs every 30 minutes to set the thermostat target humidity using this template value. Thanks!

- platform: template
  sensors:
    outside_temp_humidity_target:
      unique_id: 1acb4422-a19f-4618-b2ff-036a02afb7ed
      friendly_name: Outside Temp Humidity Target
      unit_of_measurement: "%"
      device_class: humidity
      value_template: >
        {% set humidity_min = 20 | float %}
        {% set humidity_max = 50 | float %}
        {% set t = states('sensor.pirateweather_temperature') | float %} {{ ((t / 2) + 25) | round|int }}
        {% if (t > humidity_min | float) and (t < humidity_max | float) %}
          {{ t }}
        {% else %}
          {{ humidity_min }}
        {% endif %}
1 Like

If anyone sees this and has the same question, I think I was able to answer poking around a bit more. Probably a more eloquent way to write this, but, seems to work for my purposes. Shoutout to link for the idea, in a different format, that prompted me.

- platform: template
  sensors:
    outside_temp_humidity_target:
      unique_id: 1acb4422-a19f-4618-b2ff-036a02afb7ed
      friendly_name: Outside Temp Humidity Target
      unit_of_measurement: "%"
      device_class: humidity
      value_template: >
        {% set out_temp = states('sensor.pirateweather_temperature') | float(0) %} {# grab temperature #}
        {% set rh_target = ((out_temp / 2) + 25) | round|int %} {# use formula to find optimal indoor humidity #}
        {% set rh_round = (((rh_target/2) |int)) *2  %} {# round to the closest '2' integer for ecobee #}
        {{ ([20, rh_round, 44] | sort)[1] }} {# set a min of 20%, max of 44% #}

Automation – checks every 30 minutes for changes and adjusts my thermostat’s target humidity

alias: Set target humidity dynamically
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.outside_temp_humidity_target
    for:
      hours: 0
      minutes: 30
      seconds: 0
condition: []
action:
  - service: climate.set_humidity
    data:
      humidity: "{{ states(target_humidity) }}"
    target:
      entity_id: climate.downstairs
variables:
  target_humidity: sensor.outside_temp_humidity_target
mode: single
1 Like