Automations and time delays

Hi everyone,

I’m trying to do what is probably a basic thing, but I’m still fairly new to HA.

I’m wanting to work it so that when I open the door after sunset, it will turn on my outdoor lights and then do the opposite when I re-enter the house. That’s simple. But I’m trying to get it to work practically.

Firstly, after opening the screen door, I’m going to close it immediately to prevent bugs coming in, but I obviously don’t want to then have my lights switch back off. So my first thought was to have a timeout feature that prevents the closing of the door firing the automation to switch the lights back off.

My thought is to have this ‘time-out’ to be, say, 30 seconds. Plenty of time for me to open the door, perhaps put my shoes on and then close the screen door without the lights switching off.

Could somebody point me to an example of the YAML for this? This is what I have so far without any ‘timeout’ feature:

 - id: '1555374209260'
  alias: Front Door Open
  trigger:
  - entity_id: binary_sensor.0x00158d0003120be7_contact
    from: closed
    platform: state
    to: open
  condition:
  - before: sunset
    before_offset: '1:00'
    condition: sun
  action:
  - service: light.turn_on
    entity_id: light.front_door_lights
- id: '1555375051458'
  alias: Front Door Closed
  trigger:
  - entity_id: binary_sensor.0x00158d0003120be7_contact
    from: open
    platform: state
    to: closed
  condition: []
  action:
  - service: light.turn_off
    entity_id: light.front_door_lights

Okay, just replying to myself, it seems that the for condition is kind of what i want, but it seems to only apply to the “to” state, no the “from” state.

Is there a way to get that to work in reverse?

i’m not sure adding a delay will solve your problem.

adding a delay before the trigger will still trigger the off action. it will still turn the lights off after the delay.

what you have now:

open door
turn on lights
go out
close door
turn off lights

what you will have:

open door
turn on lights
go out
close door
delay
turn off lights

you’ll have to find a way for the system to know the difference between the door closing when you leave and when you come back.

1 Like

If you do this:

- id: '1555375051458'
  alias: Front Door Closed
  trigger:
  - entity_id: binary_sensor.0x00158d0003120be7_contact
    from: open
    platform: state
    to: closed
    for: '00:00:30'
  condition: []
  action:
  - service: light.turn_off
    entity_id: light.front_door_lights

it’ll turn off the light only after the door is closed for at least 30 seconds.

However, it’ll do that always (any time of the day). It’s not a big deal but if you want it to be more precise, add a condition so it only turn the light off after sunset.

1 Like

Yeah, okay.

Sounds like then I need to have the second automation (lights off when doors closed) only if the doors have been open for more than 30 seconds.

I think this is what I’m after:

  alias: Front Door Closed
  trigger:
  - entity_id: binary_sensor.0x00158d0003120be7_contact
    from: open
    platform: state
    to: closed
  condition:
  - condition: state
    entity_id: binary_sensor.0x00158d0003120be7_contact
    state: open
    for: 
      seconds: 30
  action:
  - service: light.turn_off

Thanks 123, you typed your response just as i was typing mine. Looks like i was somewhat on the right path.

Thank you both for your help.

I’m not sure the revised ‘Front Door Closed’ will work the way you want. The trigger and condition use the same sensor but have conflicting requirements.

The trigger want the door sensor to be open whereas the condition wants it to be closed for at least 30 seconds. Never gonna happen.

I thought you didn’t want the lights to turn off on you when you were outside and to only turn off after you came back in?

1 Like

Classic way to do this is with a timer.

  • Open the door after sunset and you start a 30-second timer.
  • Starting the timer also turns on the light.
  • When the timer expires it turns off the light.

In this scenario, closing the door has no affect on the timer or the light. The timer keeps counting down to 0 then it turns the light off.

What will affect the timer is if you re-open the door while the timer is counting down. This will cause the timer to restart its countdown … and that’s usually a desirable behavior.

Timer

timer:
  front_door:
    duration: '00:00:30' 

Automations

- id: '1555374209260'
  alias: Front Door Open
  trigger:
  - entity_id: binary_sensor.0x00158d0003120be7_contact
    from: closed
    platform: state
    to: open
  condition:
  - before: sunset
    before_offset: '1:00'
    condition: sun
  action:
  - service: timer.start
    entity_id: timer.front_door

- alias: 'Front Door timer started'
  trigger:
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.front_door
  action:
    - service: light.turn_on
      entity_id: light.front_door_lights

- alias: 'Front Door timer finished'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.front_door
  action:
  - service: light.turn_off
    entity_id: light.front_door_lights
2 Likes

Yeah, I think that’s the way I’m going to have to go. Just have it go on for X amount of time as there’s no real way for it to know that I’m coming back inside so to speak.

Again, thank you both.

1 Like

Sorry to be a pain again, for some reason the automation isn’t firing to turn on the lights (I removed the sun condition for testing).

Logs show that the automations are being loaded:

[homeassistant.components.automation] Initialized trigger Front Door timer finished
[homeassistant.components.automation] Initialized trigger Front Door Open
[homeassistant.components.automation] Initialized trigger Front Door timer started

My configuration.yaml:

timer:
  front_door:
    duration: '00:10:00' 

My automations.yaml:

- id: '1555374209260'
  alias: Front Door Open
  trigger:
  - entity_id: binary_sensor.0x00158d0003120be7_contact
    from: closed
    platform: state
    to: open
  action:
  - service: timer.start
    entity_id: timer.front_door

- alias: 'Front Door timer started'
  trigger:
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.front_door
  action:
    - service: light.turn_on
      entity_id: light.front_door_lights

- alias: 'Front Door timer finished'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.front_door
  action:
  - service: light.turn_off
    entity_id: light.front_door_lights

Nothing is showing in the logs to indicate an issue and I’ve triple checked all entity names. The reed sensor is reporting correctly too. I tried changing the from and to states to on/off but that then did come up with a syntax error in the logs.

keep @123’s code intact and try to enable all automations in there, i.e add to each one

initial_state: true

Just curious what is the advantage of adding that second automation over turning on light straight after starting the timer? Code consistency or something else as well? :wink:

Got it working, it was an issue with the state needing to be on/off rather than open/closed. It didn’t work when I first tried to correct it because I did this:

from: off
platform: state
to: on

Instead of:

from: 'off'
platform: state
to: 'on'

Stupid YAML. :slight_smile: