Turn a light on or off based on sensor value

I have a humidity sensor in my bathroom and the light/extractor fan set through a smart switch. I want to set it so that whenever the humidity gets above 60% it turns the fan on, and when it drops below 55% it’ll turn it off again.

The only way I’ve found of doing this so far is to have two separate automations defined, one to turn it on and one to turn it off, with the appropriate values set as triggers.

There must be a simpler way of doing this. My solution works although it’s a really ghetto way of doing it. Can anyone think of a better way that means fewer automations?

Post your two automations and I’ll see what I can do to combine them (if it’s practical).

  - alias: "Bathroom Extractor Fan On"
    trigger:
      - platform: numeric_state
        entity_id: sensor.bathroom_humidity
        above: 60
    condition:
      condition: and
      conditions:
        - condition: time
          after: '06:00:00'
          before: '21:30:00'
        - condition: state
          entity_id: input_boolean.sleeping
          state: 'off'
    action:
      - service: switch.turn_on
        entity_id: switch.bathroom_light

  - alias: "Bathroom Extractor Fan Off"
    trigger:
      - platform: numeric_state
        entity_id: sensor.bathroom_humidity
        below: 55
    condition:
      condition: and
      conditions:
        - condition: time
          after: '09:30:00'
          before: '21:30:00'
        - condition: state
          entity_id: input_boolean.sleeping
          state: 'off'
    action:
      - service: switch.turn_off
        entity_id: switch.bathroom_light

Here’s what I was able to do. The one thing I can’t figure out is how to elegantly handle the different schedules. I used the schedule from 06:00 to 21:30. For that reason, you may decide this isn’t good enough to replace your two automations.

  - alias: "Bathroom Extractor Fan"
    trigger:
      - platform: numeric_state
        entity_id: sensor.bathroom_humidity
        above: 60
      - platform: numeric_state
        entity_id: sensor.bathroom_humidity
        below: 55
    condition:
      condition: and
      conditions:
        - condition: time
          after: '06:00:00'
          before: '21:30:00'
        - condition: state
          entity_id: input_boolean.sleeping
          state: 'off'
    action:
      - service_template: "switch.turn_{{'on' if (trigger.to_state.state | int) >= 60 else 'off' }}"
        entity_id: switch.bathroom_light

Anyway, it was an interesting little exercise! I might look at it again tomorrow and add conditions to the action section so it can handle the different schedules … but then it starts to get clumsier than simply having two separate automations.


BTW, in case you thought the following trigger might be possible, it’s not.

      - platform: numeric_state
        entity_id: sensor.bathroom_humidity
        above: 60
        below: 55

This means to match for values that are > 60 AND < 55. There’s no value that can meet that criteria.

1 Like

That’s awesome! Seems to be working fine for me that’s amazing! I learned fairly early on conditions in HA are a bit clunky. Thank you so much.