Trigger on humidity, but only at certain hours

Sorry for something that should be a very trivial question, but I can’t seem to find the answer in the docs:
I am measuring the humidity in a room, and want to run a fan at a certain value. But I only want to run the fan between certain times. For example: Run the fan if the humidity is above 70% and time is between 21:00 and 05:00.
I can find many examples with a trigger and a condition, and also triggering on humidity or time. But I cannot figure out how to trigger on both time and humidity.

What would be the correct way of doing this? Do I perhaps need two automations - one turning on the fan and one turning it off?

Two triggers, two conditions:

trigger:
- platform: time
  at: "05:00"
- platform: numeric_state
  entity_id: sensor.humidity
  above: 70
condition:
- condition: time
  after: "04:59:59"
  before: "21:00:00"
- condition: numeric_state
  entity_id: sensor.humidity
  above: 70
action:
...

This will trigger when the humidity rises above 70 or the time is 05:00, and will then require that it is after 04:59 and before 21:00, and that the humidity is above 70.

First of all welcome to the community, from someone who’s only recently here! First of all, in almost all cases, you’ll never have to run 2 automations to switch something on and of anymore. Home Assistant 2021.7 introduced trigger ID’s, which enables you to do all of this in one single automation. One source that has helped me a lot to, kind of, understand that is the following YouTube video by a guy called Smart Home Junkie How to use Trigger IDs in Home Assistant - Tutorial - YouTube (Profile - smarthomejunkie)

I think below should work for you, after altering the entity id’s for your specific situation. You should be able to paste this in a new automation after selecting ‘Edit in YAML’:

alias: Control Fan based on Humidity Value within set Timerange
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.bathroom_temperature_humidity_sensor_humidity
    above: '70'
    id: switchon_above70
  - platform: time
    at: '05:00:00'
    id: switchoff_5am
  - platform: numeric_state
    entity_id: sensor.bathroom_temperature_humidity_sensor_humidity
    id: switchoff_below70
    below: '70'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: switchon_above70
          - condition: time
            after: '21:00:00'
            before: '05:00:00'
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kitchen_microwave
      - conditions:
          - condition: trigger
            id: switchoff_5am
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kitchen_microwave
      - conditions:
          - condition: trigger
            id: switchoff_below70
          - condition: time
            after: '21:00:00'
            before: '05:00:00'
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.kitchen_microwave
    default: []
mode: single

You’ll see that the automation uses 3 triggers, with 3 different trigger id’s:

  1. switchon_above70 - This will take the humidity value of your sensor when above 70 within the timerange
  2. switchoff_5am - This will switch off at 5am, no matter what the humidity is
  3. switchoff_below70 - This will switch off if the humidity falls below 70 within the timerange

All in one automation. I don’t have a fan, so if your fan is not a switch you should alter the service calls accordingly. I hope this helps.

Thank you for the explanation. I had to urgently go away for a couple of days. I will test this as soon as I get back.

Nope, none of these suggestions work.

To be clear, if within the active timeframe and the humidity gets higher than the threshold, both of these work fine. But not if the humidity is already above the threshold when entering the timeframe.

In Tinkerer’s example, if the humidity is above 70 before 05:00, the moment the Time Trigger executes at 05:00 both conditions will be satisfied (current time is within range and humidity is above 70) and the action will be executed.

If this is not happening for you then check the automation’s trace to understand why not.

1 Like

Beginner’s mistake. Got it working now. Thank you for your help!