Need help with relative humidity trigger

I have 2 Xiaomi Aqara Humidity sensors installed in 2 bathrooms and I am trying to configure it to trigger via relative humidity. Right now, I can get readings from each and my Nest thermostat. What I am trying to accomplish is to have the bathroom fans turn on when the humidity delta exceeds certain threshold, and turn off once it returns to a normalized range.

The config I have right now, which doesn’t seem to work at all.
- alias: Turn on Master Bathroom Fan
initial_state: on
trigger:
- platform: numeric_state
entity_id: sensor.humidity_158d0002c8ed66
value_template: ‘{{ sensor.humidity_158d0002c8ed66 - sensor.nest_thermostat_humidity }}’
above: 20
action:
service: light.turn_on
entity_id: light.master_bath_fan

- alias: Turn off Master Bathroom Fan
  initial_state: on         
  trigger:
    platform: state                
    entity_id: light.master_bath_fan
    to: 'on'
    for:             
      minutes: 15               
  condition:
  - condition: numeric_state
    entity_id: sensor.humidity_158d0002c8ed66
    value_template: '{{ sensor.humidity_158d0002c8ed66 - sensor.nest_thermostat_humidity }}'
    below: 10        
  action:             
    service: light.turn_off
    entity_id: light.master_bath_fan
                          
- alias: Turn on Shared Bathroom Fan
  initial_state: on
  trigger:                   
  - platform: numeric_state
    entity_id: sensor.humidity_158d0002c9d057
    value_template: '{{ sensor.humidity_158d0002c9d057 - sensor.nest_thermostat_humidity }}'
    above: 20                   
  action:   
    service: light.turn_on
    entity_id: light.shared_bath_fan
         
- alias: Turn off Shared Bathroom Fan
  initial_state: on             
  trigger:
    platform: state           
    entity_id: light.shared_bath_fan
    to: 'on'
    for:             
      minutes: 15                  
  condition:
  - condition: numeric_state
    entity_id: sensor.humidity_158d0002c9d057
    value_template: '{{ sensor.humidity_158d0002c9d057 - sensor.nest_thermostat_humidity }}'
    below: 10        
  action:             
    service: light.turn_off
    entity_id: light.shared_bath_fan

First of all, this will not work:
{{ sensor.humidity_158d0002c9d057 - sensor.nest_thermostat_humidity }}

Check in your templates editor to see the result:

You need this: (convert to int as an entity state is a string)

{{ (states("sensor.humidity_158d0002c9d057") | int) - (states("sensor.nest_thermostat_humidity") | int) > 20}}

Your Turn on automation then becomes “simpler”:

- alias: Turn on Shared Bathroom Fan
  initial_state: on
  trigger:
    - platform: state
      entity_id: sensor.humidity_158d0002c9d057
  condition:
    - condition: template
      value_template: '{{ (states("sensor.humidity_158d0002c9d057") | int) - (states("sensor.nest_thermostat_humidity") | int) > 20}}'
    - condition: state
      entity_id: light.shared_bath_fan
      state: 'off'
  action:
    service: light.turn_on
    entity_id: light.shared_bath_fan

Now for the Turn off, what you wrote basically means:
Wait for 15 min, and check the delta. If delta is below 10, switch off. Your automation will not run again if the delta was more than 10 so your fan will stay on “forever”
So I’d change it this way so that it switches off if delta is < 10 OR fan has been on for 15+ min (regardless of delta)

- alias: Turn off Shared Bathroom Fan
  initial_state: on
  trigger:
    - platform: time_pattern
      minutes: '/1'
      seconds: 0
  condition:
    - condition: state
      entity_id: light.shared_bath_fan
      state: 'on'
    - condition: or
      conditions:
        - condition: template
          value_template: '{{ (states("sensor.humidity_158d0002c9d057") | int) - (states("sensor.nest_thermostat_humidity") | int) < 10}}'
        - condition: template
          value_template: '{{ (as_timestamp(now()) - as_timestamp(state_attr("light.shared_bath_fan","last_updated") | default(0)) | int >= 900)}}'
  action:
    service: light.turn_off
    entity_id: light.shared_bath_fan

Thank you for the pointers!

1 Like