Start timer after light switch off

Within automations.yaml I have placed this code:

- alias: Fan
  trigger:
    platform: state
    entity_id: light.bathroom
    to: 'off'
  action:
    service: switch.turn_on
    entity_id: switch.sonoff218

When the state of the light in the batroom (Philips Hue) changes to OFF the fan (Sonoff) starts running. So far so good but I did not succeed adding a timer function so the fan stops after a certain time.

Can someone help me to add this timer function?

- alias: Fan
  trigger:
    platform: state
    entity_id: light.bathroom
    to: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff218
    - delay: 01:00:00
    - service: switch.turn_off
      entity_id: switch.sonoff218

This way, the fan will shut off after 1 hour. You could also work with timers, but will overcomplexify in your case I believe.

Actually, in your case I believe it’s better to create a timer, as you turn on and off the lights in between.

In configuration.yaml

timer:
  fan_timer:

In automations.yaml

- alias: Fan
  trigger:
    platform: state
    entity_id: light.bathroom
    to: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.sonoff218
    - service: timer.start
      data:
         entity_id: timer.fan_timer
         duration: '01:00:00'

- alias: Stop fan
  trigger:
    - platform: event
      event_type: timer.finished
      event_data: 
        entity_id: timer.fan_timer
  action:
    - service: switch.turn_off
      entity_id: switch.sonoff218

I’d generally go with a separate delay in a script. Cancelled when the light comes on, but called when the light goes off.
The major problem the ALL the usage cases above is you should be switching the fan on when the light comes on (but then start the ‘timer’ /‘delay’ (whatever) when the light goes off.

Thanks for helping! For now I have choosen the easiest (first) solution from Cadish. My “problem” is solved :wink:

I had exactly the same in the beginning. What I did to solve your problem:

  • If you use the first solution with the delay: if there are 2 people behind each other in the bathroom, and the second one turned on the lights 59 mins after the first one (if the delay is 1 hour), then the fan will shut off 1 min after this last one entered the bathroom. If you use timers, you won’t have this behaviour, and the fan will shut off 1 hour after the second one.
  • If I restart HA, the fan is never shut off. That’s why I’m using this script: How to make active timers survive a restart. Only downside is that it’s way more complex :slight_smile:

I use an Itho/Daalderop fan that is operated with a RF remote that - as far as I know - does not work with Home Assistant. The remote has a button that, if pressed once, turns the fan into the highest position for 10 minutes.

My switch 218 (Wemos D1 mini with Tasmota and a relay shield) switches the remote/button via the relay which is connected to the button switch of the remote. The relay closes only 0.5 seconds so in my case I do not run into issues as you describe.

Another way to do it (that will work across restarts) and does not require a timer:

- alias: switch_off
  trigger:
    platform: state
    entity_id: switch.sonoff218
    to: 'on'
    for: 
      hours: 1
  action:
    - service: switch.turn_off
      entity_id: switch.sonoff218
1 Like

Tom,
But the fan will stay on ???

Surely the same problem ???

(admittedly I set the states of all ‘problematic’ devices on restart but … )

@tom_l’s approach will also turn off your fan if you have 2 persons after eachother in the bathroom. For example: one person at 10:00AM until 10:15AM and another person as from 10:59AM. The fan will then shut off at 11:00AM, which you would not like in this case.

What makes you say that?

A way around that would be:

- alias: switch_off
  trigger:
    platform: state
    entity_id: light.bathroom
    to: 'off'
    for: 
      hours: 1
  action:
    - service: switch.turn_off
      entity_id: switch.sonoff218

Because when you restart, the fan is on, so it wont trigger the automation (it hasn’t seen a state change) so then won’t be on for (insert time period here) to switch the fan off.

It will trigger 1 hour after the restart when the fan has been on for an hour.

OK, if you’ve tested this I’ll accept your word for it.

Implicit in this is if I create the automation and reload it, the fan (which I turned on 10 mins before that) will turn off in 50 mins.??? (trying to get this clear, thx)

(you are up very late Tom ! )

Yep.Tested and working in my system for many lights and heaters. And it’s only 17:37 here at UTC+5.

I thought you were an Aussie.?

See: DuckDNS down

Bugger, you’re not getting much sunshine at the moment are you ? 4 or 5 hours ? Less ?

Edit: you must be on a satellite link up, hope they’re not charging you for time online !

:rofl:

Still 9 hours at the moment. https://www.timeanddate.com/sun/antarctica/mawson