Automating a humidity-controlled Fan with trends as opposed to absolute humidity values

Hello, and thanks in advance for any pointers that might help me accomplish what I’m trying to do - forgive the long-winded description also.

I’m running HASSIO and have a variety of sensors around the place measuring temp, humidity and controlling my oil boiler. All running via ESP32’s on ESPHome.

However, trying to automate the operation of a bathroom fan, based on humidity levels - this has not worked out too well as during the winter here and with relative humidity levels, setting a fixed upper and lower humidity level to trigger the fan is not working. Today humidity might start at 85% but be 60% by evening when heating kicks in.
What I really want to do is trigger the fan when a humidity spike is registered, and then run the fan, say for 30mins - don’t necesarily worry about what the end humidity level is, then start the process again.

Ideally I’d do this directly on the ESPHome device so that the fan would run autonomously and only report status and values to HA, but when I tried to use the -platform: trends in ESPHome I realised this was only applicable to HomeAssistant, and I have yet to use the automations aspect of HA - my setup is quite static.

I begin looking at the trends option and have started writing the code to catch a sudden increase in humidity, but am now confused as to what to do with this value once I have captured it - do I create an automation in automations.yaml (and if so how), or do I try to use a Lambda in ESPHome.

Furthermore, in either of these instances, how does the system deal with the potential of the fan being on already, or how does the time delay work if a subsequent trigger is received while the previous countdown is operating.

here is my current ESPHome Fan Controller code, with the fixed humidity control values commented out: -

sensor:
  - platform: dht
    pin: GPIO14
    model: SI7021
    temperature:
      name: "Bathroom Temperature"
    humidity:
      name: "Bathroom Humidity"

#     on_value_range:
#       - above: 85.0
#         then:
#           - switch.turn_on: relay
#       - below: 83.0
#         then:
#           - switch.turn_off: relay
#      filters: 
#      - lambda: return x * 1;
#      - sliding_window_moving_average: 
#          window_size: 1 
#          send_every: 1
#   update_interval: 10s

    
binary_sensor:
  - platform: template
    name: "Fan Status"
    lambda: !lambda |-
      if (id(relay).state) {
        return true;
      } else {
        return false;
      }

This is my attempt at creating a trend monitor in configuration.yaml

binary_sensor:
  
        
  # Tracks 20 humidity readings over 1 minute. If the change is greater than 3%, evals to true. (3/(60*))
  - platform: trend
    sensors:
      bathroom_high_humidity:
        max_samples: 20
        entity_id: sensor.bathroom_humidity
        sample_duration: 60
        min_gradient: 0.05

Any assistance that can be provided I would be most grateful - or opinions on the best way to achieve what I’m trying to do.

Thank you in advance.
Ger

1 Like

Hi Ger, did you ever found working code for this?

Hello,
Unfortunately not - I’m still running on the static code. However, I made some progress on it, incidentally just this week.
I’m awaiting a replacement sensor to test fully, and if it works I’ll let you know and post the code.
Regards,
Ger

Maybe this is something “in between”. Static code behaving dynamically. I prefer “ramp up” in esphome though.

The ducoauto switch is to disable the automation. I use external helpers and node red to e.g. set max of off and during that disable the automation in esphome.


- platform: copy
    source_id: sensor_humidity
    id: autoswitch
    internal: True
    on_value_range:
      - below: 65
        then:
          - if:
              condition:
                switch.is_on: ducoauto
              then:
                - fan.turn_on:
                    id: ducofan
                    speed: 10
      - above: 65
        below: 70
        then:
          - if:
              condition:
                switch.is_on: ducoauto
              then:
                - fan.turn_on:
                    id: ducofan
                    speed: 20
      - above: 70
        below: 75
        then:
          - if:
              condition:
                switch.is_on: ducoauto
              then:
                - fan.turn_on:
                    id: ducofan
                    speed: 40
      - above: 75
        below: 80
        then:
          - if:
              condition:
                switch.is_on: ducoauto
              then:
                - fan.turn_on:
                    id: ducofan
                    speed: 60
      - above: 80
        below: 85
        then:
          - if:
              condition:
                switch.is_on: ducoauto
              then:
                - fan.turn_on:
                    id: ducofan
                    speed: 75
      - above: 85
        then:
          - if:
              condition:
                switch.is_on: ducoauto
              then:
                - fan.turn_on:
                    id: ducofan
                    speed: 100

switch:
  - platform: template
    name: "${esp_name} - Automatisch"
    id: ducoauto
    optimistic : True
    restore_state: yes
    restore_mode: RESTORE_DEFAULT_ON
    icon: mdi:fan-auto

EDIT It would maybe help to correctly tag your original post to esphome as well