Automation - turn off fan if light is turned off, with "x" minutes delay. But, cancel this automation if is lights turned on again

Hi all,
I had an automation, that was: When someone turn off light on bathroom, then start the timer, and when this timer was gone, fan in bathroom was turned off. And if the lights was turned on during the timer is on, this timer was stopped. It prevent turn off fan if someone is back again in bathroom. How can i solve this?

Currently i have automation with:
Trigger:

device_id: 594f430136234c5fbeca5294d4d3dbb1
domain: light
entity_id: light.bath_room_ceiling_light
for:
  hours: 0
  minutes: 1
  seconds: 0
platform: device
type: turned_off

and Action:

device_id: b77d17460278430ebf1962c21f641727
domain: light
entity_id: light.bath_room_fan
type: turn_off

Rather than triggering when the light has been off for a minute, trigger as soon as the light goes off but put the delay in the action. See automation #2 in the first example in the docs.

Thank’s for your answer. I got it. But i have still the issue with stop timer, when bathroom light is turned on again.

I get around this by using scripts and have one automation first stop the timer script. You could make it such that when the light is turned on, you call script.turn_off for the timer script

Here is an old example of using scripts:
Last detected and time operations

I feel like you could do this with wait templates in the action

{{ (as_timestamp(states.sensor.time.last_changed) - as_timestamp(states.light.bath_room_ceiling_light.last_changed)) > 59 }}

and another one

{{ is_state('light.bath_room_ceiling_light', 'off') }}

Assuming you’ve set up time sensors.

Hi, I don’t have sensor. Automation is depending only by lights is turned on or off. Then wait 1 minute and turn off fan, or stop this automation, if is lights turned on again.

Hi sparkydale, thank for your reply. I will try it.

Here is the code for one such light (I use this code for many lights). Hopefully this is helpful.

input_number:
  kitchen_light_auto_off_time:
    name: Off after
    min: 1
    max: 9
    step: 1
    icon: mdi:camera-timer
    unit_of_measurement: min

  kitchen_min_lux:
    name: On below
    min: 0
    max: 30
    step: 1
    icon: mdi:brightness-6
    unit_of_measurement: lux


automation:
  - alias: 'Kitchen Remote Button 1'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: single
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: double
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: long_click_press
    action:
      - service_template: >
          {% if trigger.event.data.click_type == 'single' %}
            script.kitchen_button_single_click
          {% elif trigger.event.data.click_type == 'double' %}
            script.kitchen_button_double_click
          {% elif trigger.event.data.click_type == 'long_click_press' %}
            script.kitchen_button_long_press
          {% endif %}
          

- alias: 'Kitchen light auto ON motion'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        from: 'off'
        to: 'on'
    condition:
      condition: or
      conditions:
      - condition: template
        value_template: "{{ states('sensor.kitchen_multi_sensor_luminance') | int < states('input_number.kitchen_min_lux') | int }}"
      - condition: state
        entity_id: light.kitchen_light_level
        state: 'on'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_t_timer
      - service: light.turn_on
        data:
          entity_id: light.kitchen_light_level


  - alias: 'Kitchen light auto off'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        from: 'on'
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_t_timer
      - service: script.turn_on
        entity_id: script.kitchen_light_auto_off_t_timer



script:
  kitchen_button_single_click:
    alias: Kitchen button single click
    sequence:
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
    - service: script.turn_off
      entity_id: script.kitchen_light_auto_off_t_timer
    - service: switch.turn_off
      data:
        entity_id: switch.circadian_lighting_kitchen_circadian_lighting
    - service: light.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 255

  kitchen_button_double_click:
    alias: Kitchen button double click
    sequence:
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
    - service: light.turn_off
      data: 
        entity_id: light.kitchen_light_level

  kitchen_button_long_press:
    alias: Kitchen button long press
    sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.circadian_lighting_kitchen_circadian_lighting
    - service: automation.turn_on
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off

  kitchen_light_auto_off_t_timer:
    alias: Kitchen light off after preset time
    sequence:
    - delay: '00:0{{ states.input_number.kitchen_light_auto_off_time.state | int }}:00'
    - condition: state
      entity_id: light.kitchen_light_level
      state: 'on'
    - service: homeassistant.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 60
    - delay: '00:00:05'
    - service: homeassistant.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 30
    - delay: '00:00:05'
    - service: homeassistant.turn_off
      data:
        entity_id: light.kitchen_light_level

Pretty much like Dave I have a script and some fairly complex automation logic, but that’s because : -

  1. My fans all run 1/day on a schedule (stops seldom used fans seizing up)
  2. They run if the house is hot
  3. I can manually switch them on
  4. I can adjust or eliminate the timer
  5. The fans are not to run between set times (night) but this can be overridden with a permissive. And this can turn them on/off if occupied on a transition

But the use case here is simple
Light goes on fan goes on
Light goes off for 1min fan goes off
All done in two very small and neat automations, what’s the problem ?
What have I missed ? I thought I’d read the whole thread but maybe my glasses need changing their prescription ?

Hi Mutt, the problem is in case, that the lights goes on again, during the automation is fired. I need stop this automation, when the lights goes on again.

By the way I left out some stuff in my above post, which relates to how I control them with Xiaomi buttons. They control the above automations and scripts with single clicks, double clicks and long presses.

Thanks, your code inspire me

Yeah I get that, but the automation is to switch the fan off ONLY if the light has been off for one minute. If its off for 59 seconds and the light goes back on again, nothing happens

You are basically doing the same as a motion sensor with a light

No worries! (also, the odd code for turning the light off slowly is due to those particular lights being z-wave which don’t permit on/off transitions, so that is my pretend transition)

try this loop

timer:
bath_room:
  duration: '00:01:00'


- id: xxxxxxxxx
  alias: sssssssss
  initial_state: true
  trigger:
  - platform: state
    entity_id: light.bath_room_ceiling_light
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.bath_room
  action:
  - service_template: '{%if trigger.platform == "event"%} light.turn_off {%else%} light.turn_on {%endif%}'
    entity_id: light.bath_room_fan
  - condition: template
    value_template: '{{trigger.event_type != "timer.finished"}}'
  - service_template: timer.start
    data_template:
      entity_id: timer.bath_room
      duration: '00:01:00'

Later I’ll update my post with the full code for completeness, too hard right now

Jaun,
Noticed that you used this in your automation, you are aware that this cancels the last_triggered attribute of any such automation on a restart so you need special testing conditions in order for any automation running subsequent to a restart to ‘test and/or ride through’?
Not a problem if you never use last triggered as a condition in automations.

Hi All,
My working setup is now:
in automation.yaml:

- id: '1594201428926'
  alias: Bath room fan autostop
  description: Autostop bathroom fan for x seconds
  trigger:
  - entity_id: light.bath_room_ceiling_light
    platform: state
    to: 'off'
  condition: []
  action:
  - data: {}
    entity_id: script.1594201213808
    service: homeassistant.turn_on
- id: '1594203376210'
  alias: Bath room cancel timer
  description: ''
  trigger:
  - entity_id: light.bath_room_ceiling_light
    from: 'off'
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: script.1594201213808
    service: homeassistant.turn_off

And script.yaml:

'1594201213808':
  alias: Turn off after 30 second
  sequence:
  - delay: 00:00:30
  - device_id: b77d17460278430ebf1962c21f641727
    domain: light
    entity_id: light.bath_room_fan
    type: turn_off

Many thanks for your code and inspiration.
Michal

Mutt, thank you. I didnt know that.