Dynamic fan speed automation in response to a secondary humidity sensor

I have a Smartmi Air Humidifier 2 and I’m looking into making it smarter.

This device has an Auto mode but it wont allow me to set the target humidity and as a result, the humidity is always below what I wanted it to be.

I also tried using HA to set the target humidity but is doesn’t seem to be doing much better.

Because of that, I’m looking now into a way to implement this and make it to react better to the conditions.

This model allows me to set the fan speed and this will release more humidity in the air. The speed can go from 0 to 2000 and my idea is to implement something that will increase the fan speed until it reaches the target humidity. Once there, it should keep adjusting the speed up/down as needed.

Does anyone have implemented something similar?

Any insights?

Thanks to @Lionel_Glitchy on his post here, I managed to put together some automation that makes sense.

This is still under test and I will report back once I have the final version.

As explained by @Lionel_Glitchy, to use this you’ll need to create some helpers so you don’t have any parameters hard coded.

alias: Fan Humidity Automation
description: ""
trigger:
  - platform: state
    entity_id: input_number.fan_min_speed
  - platform: state
    entity_id: input_number.fan_max_speed
  - platform: state
    entity_id: input_number.humidity_target
  - platform: time_pattern
    minutes: /10
condition: []
action:
  - service: number.set_value
    target:
      entity_id: number.air_humidifier_motor_speed
    data:
      value: >
        {% set min_speed = states('input_number.fan_min_speed') | int %}   
        {% set max_speed = states('input_number.fan_max_speed') | int %}    
        {% set humidity_target = states('input_number.humidity_target') | int %}   
        {% set current_humidity = states('sensor.hall_humidity') | int %}  
        {% set current_fan_speed = states('number.air_humidifier_motor_speed') | int %}

        {% if current_humidity < humidity_target %}
           {% set new_speed = (current_fan_speed + 100) | int %}
          {% if new_speed > max_speed %}
             {{ max_speed | int }}
          {% elif new_speed < min_speed %}
             {{ min_speed | int }}             
          {% else %}
             {{ new_speed | int }}
           {% endif %}         
        {% elif current_humidity > humidity_target %}
           {% set new_speed = (current_fan_speed - 100) | int %}
          {% if new_speed > max_speed %}
             {{ max_speed | int }}
          {% elif new_speed < min_speed %}
             {{ min_speed | int }}             
          {% else %}
             {{ new_speed | int }}
           {% endif %}
        {% else %}
             {{ current_fan_speed | int }}
        {% endif %}