Bathroom fan and light with motion and humidity sensor

Hi
Still learning homeassistant :slight_smile:
can someone help me or guide how to make a automation that does:
Turn on light and fan when there is motion in bathroom and turn off light and after 120sec and fan after 360sec with no motion. but if the humdity is above 60% then dont turn off fan until humidity is under 60%.? :slight_smile:

thank you.

Show us what you’ve tried. See points 2, 4, 9 and 11 here.

This is what i have done. But dont really understand how make condition “if and or” ? :confused:
alias: Test
description: ‘’

trigger:
  - type: motion
    platform: device
    device_id: 73f18b42f868e28e37b784691bfcce20
    entity_id: binary_sensor.aqara_light_sensor_1_wc_occupancy
    domain: binary_sensor
  - type: humidity
    platform: device
    device_id: 53933222f9a41acdd308decb05c756ae
    entity_id: sensor.aqara_humidity_sensor_humidity
    domain: sensor
    above: 60
condition: []
action:
  - type: turn_on
    device_id: a1f1d95d1f7c2950632faa668a2f1850
    entity_id: light.0x001788010b4ff271
    domain: light
    brightness_pct: 100
  - type: turn_on
    device_id: 1742a8c55bce007bc75cd5e1b8d692a6
    entity_id: light.0x001788010b52d714
    domain: light
    brightness_pct: 100
  - type: turn_on
    device_id: 52bda051e99111b3bf7d848d69542b96
    entity_id: switch.fan_wc
    domain: switch
  - condition: state
    entity_id: binary_sensor.aqara_light_sensor_1_wc_occupancy
    attribute: occupancy
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - type: turn_off
    device_id: 1742a8c55bce007bc75cd5e1b8d692a6
    entity_id: light.0x001788010b52d714
    domain: light
  - condition: state
    entity_id: binary_sensor.aqara_light_sensor_1_wc_occupancy
    attribute: occupancy
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - condition: state
    entity_id: sensor.aqara_humidity_sensor_humidity
    attribute: humidity
    for:
      hours: 0
      minutes: 0
      seconds: 0
    state: '60'
  - type: turn_off
    device_id: 52bda051e99111b3bf7d848d69542b96
    entity_id: switch.fan_wc
    domain: switch
mode: single

First, have a look at those guidelines again and format your code (there’s a button for it in the editor’s toolbar </>).

1 Like

Also a great idea to have a read through automations in general to understand triggers, conditions and actions

One little trick I learned is to use in some cases triggers as conditions and vice versa.

There’s also a handful of really great topics here if you search the forums for what you’re looking for.

1 Like

I have now changed it so the fan only starts when humidity level is XX% and not with the lights, no need for the fan to turn on everytime someone is in the bathroom and not using the shower. it also saves med som $ on the electical bill :slight_smile: but thanks for the help :slight_smile:

Depending on the actual use case, I prefer to use timers for this rather than simply triggering off directly and turning it off after its been on for ‘x’ minutes. You can create a helper timer and then start the timer each time there is motion (or other trigger) and then the light won’t go out on you till ‘x’ minutes after the last motion was detected. Otherwise this will likely be turning off while you’re brushing your teeth. The humidity sensor will help keep it on better but i’m guessing it will still happen.

(and formatting your post here so people can actually read the code will be helpful)

1 Like

Now i have the lights turn on with motion, and if no motion is detectec in 180 seconds, the light turns off. And new automation for the fan/ventilator so it only turns on when humitidy level is above 55%. :slight_smile: it works great now.

This is what i have for a fan only setup.

Works when above 60% between 5-9pm i think so i doesnt run overnight. Might help

alias: 3 - SHM - Kitchen Humidity
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.kitchen_extractor_temp_relative_humidity_measurement
    above: '70'
  - platform: device
    type: turned_on
    device_id: 74ce6c1aaf3ce62e331adae2ccbf6cd7
    entity_id: switch.kitchen_extractor
    domain: switch
condition:
  - condition: time
    before: '21:00:00'
    after: '16:45:00'
action:
  - type: turn_on
    device_id: 74ce6c1aaf3ce62e331adae2ccbf6cd7
    entity_id: switch.kitchen_extractor
    domain: switch
  - wait_for_trigger:
      - platform: numeric_state
        entity_id: sensor.kitchen_extractor_temp_relative_humidity_measurement
        for:
          hours: 0
          minutes: 1
          seconds: 0
        below: '70'
  - type: turn_off
    device_id: 74ce6c1aaf3ce62e331adae2ccbf6cd7
    entity_id: switch.kitchen_extractor
    domain: switch
mode: single

1 Like

i have made this now :slight_smile:

alias: Fan ON
description: ''
trigger:
  - type: humidity
    platform: device
    device_id: 53933222f9a41acdd308decb05c756ae
    entity_id: sensor.aqara_humidity_sensor_humidity
    domain: sensor
    above: 55
condition: []
action:
  - type: turn_on
    device_id: 52bda051e99111b3bf7d848d69542b96
    entity_id: switch.fan
    domain: switch
mode: single

and then

alias: Fan OFF
description: ''
trigger:
  - type: humidity
    platform: device
    device_id: 53933222f9a41acdd308decb05c756ae
    entity_id: sensor.aqara_humidity_sensor_humidity
    domain: sensor
    below: 50
condition: []
action:
  - type: turn_off
    device_id: 52bda051e99111b3bf7d848d69542b96
    entity_id: switch.fan
    domain: switch
mode: single

Looks good but im not an expert.

This part of my code combines the 2 automations into one.

Will switch on fan then constantly loop/wait until the humidity drops below 70% for over 1 minute, it will then switch off the fan.

I previously had them separate also but had issues, test it and see what happens.

alias: Control Fan
description: ''
trigger:
  - id: 'on'
    platform: numeric_state
    entity_id: sensor.aqara_humidity_sensor_humidity
    above: 55
  - id: 'off'
    platform: numeric_state
    entity_id: sensor.aqara_humidity_sensor_humidity
    below: 50
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.fan
mode: single

1 Like

Love @123’s solution here. FWIW. I added a statistics average sensor to calc the average humidity over the last 24 hours and use this instead of hard-coding the above/below numeric values. Allows for seasonality / rainy days etc. I also use a threshold above the average, so when humidity > (average + threshold ) the fan turns on and then back off when it’s back below the (threshold + buffer).

I created a binary_sensor that triggers my fan toggle automation:

    master_bath_fan_sb:
      friendly_name: Master Fan S/B Status
      value_template: >-
        {% set humid = states('sensor.master_bathroom_sensor_relative_humidity_measurement')|int(default=0) %}
        {% set avg = states('sensor.master_bathroom_humidity_average')|int(default=0) %}
        {% set thresh = states('input_number.master_bath_humidity_threshold')|int(default=0) %}
        {% if states('climate.6900_master') == 'cool' and avg > 49 %}
          {% set thresh = thresh / 2 %}
        {% endif %}
        {{ (humid - avg) > thresh }}

Automation:

- id: '1555779101294'
  alias: Master Bath Fan - TOGGLE
  trigger:
  - entity_id: binary_sensor.master_bath_fan_sb
    for: 00:00:30
    platform: state
  condition: []
  action:
  - data:
      entity_id: switch.master_bath_fan
    service_template: switch.turn_{{trigger.to_state.state}}

I use an input_number to more easily save / update the threshold and also of not that if my AC is on, then I leave the fan on a little longer to take more humidity out before turning off.