Why is this automation being triggered?

I’m using a Fibaro dual-channel relay device to control a bathroom fan. One channel is connected to the light switch, so when it’s turned on, the first automation also turns on the channel connected to the fan:

alias: EnSuite Bathroom Fan On
description: Turn on the EnSuite fan with the lights.
trigger:
  - platform: state
    entity_id: switch.fibaro_fgs221_ensuite_lights
    to: 'on'
condition: []
action:
  - data: {}
    entity_id: switch.fibaro_fgs221_ensuite_fan
    service: switch.turn_on
mode: single

The second automation is triggered either 20 minutes after the light is switched off or by the humidity being below 68% for 15 minutes:

alias: EnSuite Bathroom Fan Off
description: Turn off the EnSuite fan after 20 minutes or if humidity is low enough.
trigger:
  - platform: state
    entity_id: switch.fibaro_fgs221_ensuite_lights
    to: 'off'
    for: '0:20:00'
    from: 'on'
  - below: '68'
    entity_id: sensor.ensuite_ble_humidity
    for: '0:15:00'
    platform: numeric_state
condition: []
action:
  - data: {}
    entity_id: switch.fibaro_fgs221_ensuite_fan
    service: switch.turn_off
mode: single

The problem I’m having is that sometimes the fan will switch off early without the light being switched off. The last time, it was 11 minutes after the light was switched ON and it hadn’t been switched OFF. Other times it goes off after 2 or 3 minutes, so it’s quite random and intermittent - most of the time it works fine.

I suspect it’s a flaw in my logic, so any ideas are most welcome!

I can imagine a scenario where that happens.

The humidity drops below 68 (and stays below 68).
14 minutes after this you turn the light on.
1 minute later the light will turn off.

Ah, :bulb: :exclamation: That would make sense, I was assuming that the triggers were ANDed, but looking back at the logbook I can see a couple of times where switch.turn_off for the fan was called in succession, without the light being turned on. I need to rethink this approach.

Thanks!

A trigger is never AND, there’s never two events at the exact same time.

But is it a problem that the fan is switched off if the humidity is low enough?
You could add another automation, that switches the fan on if humidity exceeds some limit as well.
I guess it is more important to run the fan if humidity is high, than if the light is turned on?

What was happening was that sometimes I would switch on the light which would turn on the fan, but then the fan would switch off before the shower got the humidity above 68%.
Reducing the trigger level meant that the fan would trigger randomly.

But as I said - wouldn’t the proper trigger to turn the fan on be “humidity > X”, not “light turned on” (or both of them I you want)?
The would avoid the issue altogether.

Fair point, but I want the fan to only turn on when the light switch is turned on. The humidity in that bathroom varies so much that it’s not a reliable indicator, and I don’t enjoy being woken in the middle of the night by the fan running!
So, what I want to achieve is for the fan to only turn on with the switch and then turn off when the humidity is below 68% for 15 minutes, or 20 minutes after the light switch is turned off.