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

I’d like to create action with trigger is change range of humidity in time period.

Tigger -> When i open the water in my bath, the humidity change about 5-7% in 5 minutes period.
and - (condition?) when the temperature is lower than 22 - action turn on switch.

The condition and action is simple, but how to create trigger?
all data is send via mqtt
Please help

I use the following binary sensor to switch on my bathroom fan.
It detects the rate of change in my Aeotec humidity sensor and if this is above the setpoint (set by an input slider) it will activate.

bathroom_humidity_rising:
    friendly_name: "Bathroom Humidity Rising"
    value_template: >-
      {{ (states.sensor.bathroom_humidity_stats_mean.attributes.max_value | float - states.sensor.bathroom_humidity_stats_mean.attributes.min_value | float) > states.input_number.fan_on.state | int }}

The rate of change is calculated using the statistics sensor:

- platform: statistics
name: Bathroom Humidity Stats
entity_id: sensor.bathroom_multisensor_relative_humidity
sampling_size: 10

Automation to activate fan:

 alias: "bathroom fan start"
hide_entity: false
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_humidity_rising
    from: 'off'
    to: 'on'
  - platform: state  
    entity_id: binary_sensor.bathroom_occupancy
    from: 'off'
    to: 'on'
condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.bathroom_occupancy
        state: 'on' 
      - condition: state
        entity_id: binary_sensor.bathroom_humidity_rising
        state: 'on'
      - condition: time
        after: '05:30:00'
        before: '21:30:00'            
action:
  service: homeassistant.turn_on
  entity_id: switch.bathroom_fan_switch

I also use a binary sensor to detect the rate of change has stabilized to switch off the fan:

bathroom_humidity_normal:
    friendly_name: "Bathroom Humidity Normal"
    value_template: >-
      {{ (states.sensor.bathroom_humidity_stats_mean.attributes.max_value | float - states.sensor.bathroom_humidity_stats_mean.attributes.min_value | float) < states.input_number.fan_off.state | int }}

Hope this helps.

1 Like

Thanks for sharing Sluddsy. I’ve took your code and combined it into a package yaml file. However I struggle a bit with the input_number.fan_on and input_number.fan_off. Could you advice how to get ‘good’ values? For completeness, I share my ‘stats-mean’ after a shower session (in Dutch climate).

Hi,
I have set up sliders for the fan_on and fan_off so it can be adjusted through the gui. I did this so it was easier to find values that worked rather than having to update the config file every time.

See here for details

Thanks Sluddsy, now I get it. When the fan is on, and you think “it’s enough” you adjust the slider until the fan stops. So call it manual calibration?

Yes you could call it manual calibration.

Hi Sluddsly, vlamofiel,

thanks for sharing the above example. I’m considering to move from Domotics (in which I automated almost everything using LUA) to Home Assistant. The first automations I’ve ever made was to control the bathroom fan with similar behaviour as above. If the humidity rises over 3% in 5 minutes then switch on the fan until the humidity level before the rising is reached or when the timeout is triggered (e.g. not being able to reach the target humidity after 1,5 hours). As the fan has 3 levels (low, med, high) I added a condition to not use ‘high’ between 22:00 and 7:00 to reduce noise in bedroom.

I try to achieve something similar in HA; starting simple with just switching on the fan when humidity rises and switching the fan off when humidity is on target level (or timeout). How does the above example work? The binary sensors are using a template to check whether the delta (max - min) is above some threshold (defined by slider). How often is this checked? Is this configurable (e.g. allow for max 3% increase in 5 minutes or max 5% increase in 10 minutes)?

Let me know your thoughts on this one

You should check the trend binary sensor that is designed to follow how a sensor changes.
I use it to detect shower time and cooking time too, based on xiaomi temperature/humidity sensors.

Thanks! Will have a look there.

[edit]
How does this trend sensor work in practice (yes I’ve read the manual but some things remain unclear)? Is the sample frequency every second or does this depend on the refresh rate of the sensor being tracked?

E.g. I have a humidity sensor of which the value in HASS get updated at seemingly random intervals. How will I be able to get a value every minute (even if the value hasn’t been updated and therefore the same value is retrieved) and determine the trend? I want to have samples at a fixed time interval (e.g. every minute) and use a fixed amount of samples (e.g. 5) to determine whether the humidity increase is over 3% (3 / (60*5) =0.017) u=in 5 minutes.

Current config:
bath_hum_rise:
entity_id: sensor.bathroom_sensor_humidity
attribute: Humidity
friendly_name: “Humidity rising”
max_samples: 5
min_gradient: 0.017

Is this correct (e.g. will it turn on if humidity increase is >3% in 5 minutes)?

I’m also moving from domoticz and had the same amongst other sensors to drive the house internal ventilation unit.(CO2 livingroom, Humidity bathroom, house cooling during summer nights)

I have this now and waiting for someone to shower to fine tune the gradient:

  - platform: trend
    sensors:
      bathroom_humidity_rising:
        entity_id: sensor.badkamer_vochtigheid
        friendly_name: Badkamer Gebruik
        sample_duration: 7200
        min_gradient: 0.01
        device_class: moisture

I know domoticz fired scripts in intervals (5 minutes for this one) but you have to see this trend sensor differently.
To explain it easily: imagine it drawing a graph all the time based on sensor updates. when the graph reaches an angle (gradient) the sensor is ‘on’ and turns off when the angle is below gradient it again.
A line plotted over an hour with 2.88 increase is the same as 0.0008 per second.

0.017 is 5.1 in 5 minutes
0.01 is 3 in 5 minutes

Take note that this sensor is not sufficient to stop the fan, only to indicate the showering has begun.
You would have to use it with a timer or a template sensor to store the starting value in.
I don’t like making sensors and switches for everything and have started to use variables like there is in Domoticz: https://github.com/rogro82/hass-variables

Indeed detecting the humidity rise is not the main challenge; detecting that the target value has been reached however is (e.g. humidity level is ‘stable’ over x time or value Y has been reached or timer Z has passed). Out of curiosity why do you use a sample duration of 7200?

I understood the Sample duration is just used for cleaning up old samples. Sample values older than 2hrs will be deleted. I was hoping that I could still access the value from old samples (2 hrs ago) to see if the current humidity was close to the starting point. However, I was unsuccessful, I never changed it to something else. I started to store it in a variable/MQTT when the shower session starts.

With the trend sensor I was able to see when and how long my daughter’s shower session was, and I was able to store the starting humidity in a variable and a MQTT topic (I am testing both to see what I like best)

Your humidity stable comment made me have an idea though, just thinking aloud here…
“What if a trend sensor looks for a min_gradient of a low number (maybe -0.0001?) when It turns off, it is stable after a downtrend… when on, Humidity is still decreasing…and kind of removes the need for a timer and starting value…”

Again, I am in the same process as you figuring out how I can replace my first domoticz script in HA. I am open to suggestions

Ok, we have the same understanding on Sample_duration then ;). Storing values in variables is the ‘Domitcz’ way (which really worked well for me). However would be nice to have a solid alternative in HASS as well.

Humidity stable is actually tricky and I guess you need to use two sensors to determine whether the current humidity change range is within upper and lower limits. Alternatively monitoring whether the Humidity decline rate is below some threshold should be working just fine (this also compensates for temperature changes and hence potential different humidity targets).

Note that in your example you are not specifying the max_samples (default is 2). This might not be sufficient to get a reliable trend and will definitely result in not storing samples up to 2 hours back.

I don’t understand why you use variables ?
The point in the trend sensor is to detect increase (or decrease with proper option) of an entity value. If detected, it turns true. This can trigger an automation. In which you can use the entity value as a condition if you want to turn on the vent only if humidity is above X% ?
Example :

- alias: 'A_ VMC Start'
  trigger:
    - platform: state
      entity_id: group.windows
      to: 'off'
    - platform: state
      entity_id: binary_sensor.cooking
      to: 'on'
    - platform: state
      entity_id: binary_sensor.shower
      to: 'on'
  condition:
    - condition: state
      entity_id: switch.vmc
      state: 'off'
      for:
        minutes: 5
    - condition: state
      entity_id: group.windows
      state: 'off'
    - condition: template
      value_template: "{{ ((states.sensor.m_humidity.state | float) - (states.sensor.humidite_maison.state | float)) < 40 }}"
    - condition : template
      value_template: "{{ (((states.sensor.temperature_maison.state | float) - (states.sensor.m_temperature.state | float)) | abs) < 20 }}"
  action:
    - service: switch.turn_on
      entity_id: switch.vmc

This is part of the house vent automation. I’ve kept the part where it starts when windows are closed, or when the trend sensors for bathroom and kitchen. In the conditions, it will start only it it’s off for a while (5mn), if windows are closed and depending on relative internal humidity (compared to external provided by weather component). Those are the templates conditions.
It’s all on the fly, I don’t keep any previous status.

The trend sensor are defined like this :

  shower:
    # On if +7%/minute
    entity_id: sensor.humidity_158d000227327d
    sample_duration: 600
    min_gradient: 0.06
    device_class: moving
    #max_samples: 3

  cooking:
    # On if +5%/minute
    entity_id: sensor.humidity_158d00022880d3
    sample_duration: 600
    min_gradient: 0.02
    device_class: moving
    #max_samples: 3
1 Like

I seem to have difficulties in understanding the trend sensor. Why does a gradient to 0.006 result in a +7% per minute? I somehow got the impression that my trend sensor is not polling the humidity sensor on a ‘per-second’ interval.

For example: if I set my trend sensor to 0.02 (+5%/minute?) the sensor doesn’t get triggered (whilst the humidity chart tells me that this increase was reached).

Also I now see the following in the post above:

0.017 is 5.1 in 5 minutes
0.02 is 5% in 1 minute

Which one is true (if any)? How is this calculated?

The trend sensor isn’t polling anything “per second”. The trend sensor updates ONLY WHEN the underlaying sensor updates. This will force you to tune the “sample_duration” value to adapt. For instance, the Xiaomi Aqara temperature/humidity sensor updates once in a while, when there’s a change but also “sometimes” when nothing changed.

As for the calculation method, you can’t trust my comment in the configuration. :wink: I just copy/pasted what I have in my binary_sensor.yaml file and, because I did tweak the trend sensors a lot, the comment ain’t correct. Hence the commented “max_sample” too.
You should calculate a rate per second, so 0.06 is 3.6 points per minute (3.6 / 60), not 7.
0.02 is 1.2%/min (1.2 / 60).

If your 0.02 gradient (which is not very hard to get in a bathroom) doesn’t trigger anything, there are 2 possibilities:

  • see what your samples are, how many and how old they are. If the sensor gives 1 value per second, with only 2 samples, it could not work (too frequent, not enough older samples)
  • the binary sensor is already on ! (because 0.02 is a small amount)

Thanks for sharing these insights in the trend sensor; I did not have the same understanding based on the documentation. Anyways my sensor, although not Xiaomi, behaves in a similar way. The sensor now works (made a typo earlier) and although it’s only switched on briefly, it’s sufficient to trigger the fans. Now I need to realize the same to detect that humidity level is no longer changing and therefore fans can be switched off again.

Mister_Slowhand, I was wondering. and the reason I am looking at variables… how do you decide when the ventilation system can be turned off? (in my case the Turbo mode that uses double the electricity consumption)
I appreciate different insights… It looks as you have done the same…

Thanks for the tip on the trend sensor.

I was previously turning my exhaust fan on when the humidity was 10 higher than the house baseline and off when 7 higher.

I just set up 2 trend sensors, 1 with a positive gradient of 0.02 and 1 negative gradient of 0.02

The negative sensor is most useful as it can be put in the condition of my automation for turning off, now it not only needs to be less than 7 but also needs to have stopped falling.

Why did I not just lower the target to less than 7? Because the fan stays on when it rains.

I wonder if I can just rely on the trend sensor alone to turn it off? I feel if the shower is producing humidity whilst the fan is on, the gradient may be low.

It is not so useful for turning on the fan as the humidity can creep up below the gradient but still exceed 10. One example is towels on the radiator drying. I may use it for faster turn on response but still need the difference rule I already have as a low gradient would be too sensitive.

I gave up on the trend sensor as in my (limited) experience this sensor indicates a trend of ~0 if the change is constant. For example the trend sensor is able to detect a humidity increase or decrease but is not usable to determine whether the decrease has reached a certain level (e.g. no less then -1% per x time).

I now use the statistics sensor instead.where I monitor the ‘change’ value. If change is > 5 then switch on the fans. If change was < -2 but is now increasing again (e.g. binary sensor switches from on to off) then switch off the fans (as the humidity change is too limited). On top there is a timeout to ensure that after a given time (say 3 hours) the fans will switch off anyway.