Trigger -> If humidity changed about 5% in time period (5min)

Could you please share your code for this? I will be looking to implement a similar idea very soon. Thanks

I do not in fact ! These rules are for the whole house vent, that covers the bathrooms, kitchen and some other places. Most of the time this should be on, I turn it off only when heating goes on, at night and when inside humidity is way below outside.
I use the trend sensor to force it on when I detect in kitchen or bathroom that humidity raises. And I don’t care it stays on.

In your case, I would make another trend, inversed, to monitor the humidity decrease. And either stop it when the trend turns off (doesn’t dry anymore), or stop it when it is on for X minutes. In the first case, you’ll need to know the status of the fan.

You can also check on the trend value, accessible as
{{ (states.binary_sensor.TRENDNAME.attributes['gradient'] | float) < VALUE }}

Remember that it’s a binary sensor, so it only tells if it’s on or off, there’s a gradient attribute and also the underlaying entity to monitor values. You can use those for triggers or conditions based on values.

@Mister_Slowhand, we both have a different use case it seems. However I did try the trend sensor and what I noticed in the past few days is the following: humidity decreases (fan on), trend sensor activates at roughly -2% per 5 minutes (so decrease of humidity), after a couple of minutes(!) the trend sensors switched from on to off (triggering my fan to go off) because what I believe is caused by the humidity decrease has stabilized (e.g. -4% per x time) and therefore the trend is ~0. I did try different sample durations, gradients and max_samples but all with roughly the same result (being fan switched off way to early.

@sparkydave
this is what I use in my setup. Note that I use sliders (as described earlier in this post) to ‘tweak’ the value for the sensor to go on/off via the GUI. Don’t forget to define the time somewhere…

Binary sensors:

- platform: template
  sensors:
    bathroom_humidity_rising:
      friendly_name: "Bathroom Humidity Rising"
      icon_template: mdi:water-percent
      value_template: >-
        {{ states.sensor.bathroom_humidity_stats_mean.attributes.change | int >= states.input_number.fan_on.state | int }}
    
    bathroom_humidity_dropping:
      friendly_name: "Bathroom Humidity Dropping"
      icon_template: mdi:water-percent
      value_template: >-
        {{ states.sensor.bathroom_humidity_stats_mean.attributes.change | int <= states.input_number.fan_off.state | int }}

Statistics sensor:

# Statistics sensor
# https://www.home-assistant.io/components/sensor.statistics/ 
- platform: statistics
  name: Bathroom Humidity Stats
  icon: mdi:water-percent
  entity_id: sensor.bathroom_sensor_humidity
  sampling_size: 15

Automation:

# Switch fan max when humidity is rising
- alias: Bathroom - Switch fan max
  hide_entity: false
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id:  binary_sensor.bathroom_humidity_rising
    to: 'on'
  condition:
  - condition: state
    entity_id:  switch.fan_high
    state: 'off'
  action:
  - service: homeassistant.turn_on
    entity_id: switch.fan_high
  - service: timer.start
    entity_id: timer.fan_timeout

- alias: Bathroom - Restart timer
  hide_entity: false
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id:  binary_sensor.bathroom_humidity_rising
    to: 'on'
  condition:
  - condition: state
    entity_id: switch.fan_high
    state: 'on'
  action:
  - service: timer.cancel
    entity_id: timer.fan_timeout
  - service: timer.start
    entity_id: timer.fan_timeout

# Switch fan off when humidity is no longer dropping
- alias: Bathroom - Switch fan off
  hide_entity: false
  initial_state: 'on'
  trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_humidity_dropping
    to: 'off'
  action:
  - service: homeassistant.turn_off
    entity_id: switch.fan_high
  - service: timer.cancel
    entity_id: timer.fan_timeout
2 Likes

I’ve tried using this and it works for very quick showers where the humidity is still rising by the time you have finished the shower. That results in humidity also dropping quickly. You get a humidity change profile that resembles:
(shower on) 2,4,(fan on) 6,10,16,12,16 (shower off) 14, 16, 9, 3, -10, -5, -6, -4, -3, -4, -2 (fan off)

I’ve found the problem is on a humid day, or if you have a longer shower, the humidity reaches a peak and the change slows down causing the fan to turn off while you are still in the shower. You get a humidity profile that resembles:
(shower on) 2,4,(fan on) 6,10,14,11,5, 3, 2 ,1, 0 -1, (fan off) 0, 1, 2, 0, -1… … (shower off)

There is a good chance that the fan won’t turn on either now as the room is saturated it will only very slowly dry out without the fan. The humidity change now is limited to small shifts up or down which won’t trigger the fan.

Any ideas?

Here is what I am currently using - I am comparing the humidity to another sensor in the lounge which adjusts for humid days.
The fan will also turn on if the humidity rises quickly for fast response but also turns on if it exceeds the humidity of the lounge, this allows for slow increases in humidity. The fan runs until the room is not drying any more which is the bit I am really pleased with. It probably only needs 1 trend sensor as it has a + or - gradient value to show which way the humidity is moving that can be used directly.

configuration.yaml

binary_sensor: 
  - platform: trend
    sensors:
      bathroom_humidity_rising:
        entity_id: sensor.bathroom_humidity
        friendly_name: Bathroom Humidity Rising
        sample_duration: 7200
        min_gradient: 0.02
      bathroom_humidity_falling:
        entity_id: sensor.bathroom_humidity
        friendly_name: Bathroom Humidity Falling
        sample_duration: 7200
        min_gradient: -0.01

Automations.yaml

- alias: "Turn on Bathroom Fan"
  trigger:
      - platform: state
        entity_id: sensor.bathroom_humidity, sensor.lounge_humidity        
  condition:
      - condition: template
        value_template: "{{ (states.sensor.bathroom_humidity.state | float - states.sensor.lounge_humidity.state | float > 10) or (states.sensor.bathroom_humidity.state | float == 99.9) or states.binary_sensor.bathroom_humidity_rising.attributes['gradient'] | float > 0.04 }}"
  action:
     - service: switch.turn_on
       entity_id: switch.bathroom_fan 
       
- alias: "Turn off Bathroom Fan"
  trigger:
      - platform: state
        entity_id: sensor.bathroom_humidity       
  condition:
      - condition: template
        value_template: "{{states.sensor.bathroom_humidity.state | float - states.sensor.lounge_humidity.state | float < 7 and states.switch.bathroom_fan.state == 'on' and states.binary_sensor.bathroom_humidity_falling.state == 'off' and states.binary_sensor.bathroom_humidity_rising.state == 'off'}}"
  action:
     - service: switch.turn_off
       entity_id: switch.bathroom_fan

image

2 Likes

I’ve solved this by checking the ‘last update’ value of the ‘humidity_dropping’ sensor. If the dropping sensor goes from on to off (humidity change is no longer below threshold) the fans can switch off. This compensates for any fluctuations

 trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_humidity_dropping
    to: 'off'
    for:
      minutes: 5

@Crhass how do you calculate the gradient? I’m trying to see if humidity rises 5% in a minute. Or 3% in 30 seconds. Is there a formula?

I’m using a bmp280 sensor on an Arduino and it is updating every second, and my corresponding ESPHome is showing the humidity change nearly instantly in the UI. If I blow on the sensor it immediately jumps up. I’m just completely in the dark on how to set up the trend sensor.

Formula is 5/60, so try a gradient of 0.08.

Once you set up a trend sensor, if you click on it you will see the current gradient. The value can be used directly in an automation trigger.

The gradient set in the actual sensor config is the value where the binary sensor changes state.

The gradient is measured in sensor units per second - so if you want to know when the temperature is falling by 2 degrees per hour, use a gradient of (-2) / (60 x 60) = -0.00055

So 3% in 30 seconds should be 3/30?

1 Like

I’m taking a less-coding but more-hardware approach to this.

I have 2 Aqara Temp & Humidity sensors in my bathroom area - one in the doored-off section with the shower and the other in the area with the sink.

I have a template sensor that compares the % of the two. The fan goes on when the difference hits 7%, and I have it go off 1 minute after it comes down to 2%.

I think I’ll end up tweaking the thresholds a bit to tighten things up a bit, but so far - it’s working.

I found that my fan ran when not required such as when raining outside and outside humidity is around 100%. Using the average sensor (from the min max sensor) and including all humidity sensors in the house (including the room with the fan) and the outside humidity gives a better value to compare. I turn on at 15 above average and off at 10 above. I have left in the statistic sensors to keep it on if humidity is still falling but I am not sure it’s still needed with this method.