Help please automation fan question

Hi
Ive got some trouble configure the fan automation. I would like to change the ventilator to 2 when the bathroom humidity is above 50% but for 30 min max.
When it’s 7 pm it needs to set back to the lowest point (1).

But how can I avoid this scenario: when it triggers at 6:45 pm for instance, it changes to level 2 but it cant continue after 7 pm > then it must change back to level 1. The same applies for every time before 7 pm. So before 7 pm its allowed to use level 2, after its not

Whats the best way to do this?

alias: Test
description: ""
trigger:
  - platform: time
    at: "19:00:00"
    id: "1"
  - platform: numeric_state
    entity_id:
      - sensor.xiaomibadkamer_humidity
    above: 50
    id: "2"
condition: []
action:
  - if:
      - condition: trigger
        id:
          - "2"
      - condition: time
        before: "19:00:00"
    then:
      - service: fan.set_preset_mode
        metadata: {}
        data:
          preset_mode: "2"
        target:
          entity_id: fan.wtw_ventilation
      - delay:
          hours: 0
          minutes: 30
          seconds: 0
          milliseconds: 0
      - service: fan.set_preset_mode
        metadata: {}
        data:
          preset_mode: "1"
        target:
          entity_id: fan.wtw_ventilation
mode: single

alias: Test
description: ""
trigger:
  - platform: time
    at: "19:00:00"
    variables:
      level: "1"
  - platform: numeric_state
    entity_id: sensor.xiaomibadkamer_humidity
    above: 50
    variables:
      level: "{{ iif(now().hour < 19, 2, 1) }}"
  - platform: numeric_state
    entity_id: sensor.xiaomibadkamer_humidity
    above: 50
    for:
      minutes: 30
    variables:
      level: "1"
condition: []
action:
  - service: fan.set_preset_mode
    data:
      preset_mode: "{{ level }}"
    target:
      entity_id: fan.wtw_ventilation
mode: single

EDIT

Correction. Changed 7 to 19 in the template.

Hi, thx for the advice! I will test it in the upcoming days.
One question, when I want to add more triggers in the future. Do I need to define it twice like you did with the bathroom trigger?
Or did you do that only for defining the variables

And this line

 level: "{{ iif(now().hour < 7, 2, 1) }}"

The 7 stands for 7 pm I guess? And the 1 and 2 for the fan level?

The automation has three triggers and one service call that sets the fan’s preset_mode. Each trigger has a level variable that is used to set the preset_mode’s value to either 1 or 2.

  1. Time Trigger
    At 19:00 it sets preset_mode to 1.

  2. Numeric State Trigger
    When the humidity increases and crosses the threshold value of 50, it sets preset_mode to 2 if the hour is before 19 otherwise it sets it to 1.

  3. Numeric State Trigger
    When the humidity increases and crosses the threshold value of 50, and stays above 50 for at least 30 minutes, it sets preset_mode to 1.

The 7 is a mistake I made and should be 19 (I have corrected the example).

Any progress to report?

not yet, still testing. I will come back with the results asap

hmm I get some unexpected run results. It looks like it’s not filling in the variable, it’s empty (the preset_mode) and that’s why it’s failing I guess.

I changed the automation slightly, because 1 needed to be low and 2 medium.

alias: Test
description: ""
trigger:
  - platform: time
    at: "19:00:00"
    variables:
      level: "low"
  - platform: numeric_state
    entity_id: sensor.xiaomibadkamer_humidity
    above: 50
    variables:
      level: "{{ iif(now().hour < 19, medium, low) }}"
  - platform: numeric_state
    entity_id: sensor.xiaomibadkamer_humidity
    above: 50
    for:
      minutes: 30
    variables:
      level: "low"
condition: []
action:
  - service: fan.set_preset_mode
    data:
      preset_mode: "{{ level }}"
    target:
      entity_id: fan.wtw_ventilation
mode: single

result:
image

@123 I get the same issue when I define the variables like this. So under Action.

action:
  - variables:
      level: "{{ iif(now().hour < 19, medium, low) }}"

How the fan looks like :
image

In that case, you need to delimit the two words with quotes to indicate they are strings. Otherwise they’ll be interpreted as the names of (non-existent) variables.

    variables:
      level: "{{ iif(now().hour < 19, 'medium', 'low') }}"

Here’s the revised version:

alias: Test
description: ""
trigger:
  - platform: time
    at: "19:00:00"
    variables:
      level: "low"
  - platform: numeric_state
    entity_id: sensor.xiaomibadkamer_humidity
    above: 50
    variables:
      level: "{{ iif(now().hour < 19, 'medium', 'low') }}"
  - platform: numeric_state
    entity_id: sensor.xiaomibadkamer_humidity
    above: 50
    for:
      minutes: 30
    variables:
      level: "low"
condition: []
action:
  - service: fan.set_preset_mode
    data:
      preset_mode: "{{ level }}"
    target:
      entity_id: fan.wtw_ventilation
mode: single

Hi @123 I have seen some strange behavior in the latest updated automation.
It triggers and sets to medium at times I don’t expect. Before 7 pm can be any time, or does this “reset” at 0:00?
Can’t we make it a time-to-time approach to make automation robust? for example from 7 PM to 6 AM in the morning, the level must be low, so not changeable. And the rest of the time of a day, it may change to medium if it triggers.
image

What are the times that you didn’t expect?

It’s currently designed to operate the way you requested. It sets the fan to medium, whenever humidity increases above 50 for 30 minutes, as long as the current hour is less than 19 (meaning between 0 and 18 inclusively).

The “to 6 AM” is a new requirement; it wasn’t mentioned in your first post (or subsequent ones). However it’s easy to adapt the existing template to support it.

The following template sets the fan speed to medium if the current hour is greater than or equal to 6 and less than 19, otherwise it’s set to low.

level: "{{ iif(6 <= now().hour < 19, 'medium', 'low') }}"