[Automation] Turn lights on when entering, on door close, trigger delayed lights off

Hi!

I want my front door sensor to turn on lights when entering from the door.
When this happens and door is held open, lights should be staying on during that time.
When door get closed, id like to wait X amount time and then turn off lights. Ofc, during this time user can turn lights on/off and that should cancel automation of lights.

I came up with this.

- alias: Stop front door light automation
  trigger:
    platform: state
    entity_id: binary_sensor.doors_front
    to: "off"
  condition:
    condition: state
    entity_id: input_boolean.door_light_automated
    state: "on"
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.door_light_automated
    - delay: 00:02:00
    - condition: state
      entity_id: input_boolean.door_light_automated
      state: "off"
    - service: light.turn_off
      entity_id: light.et_seinavalo

- alias: Cancel front door light automation
  trigger:
    platform: state
    entity_id: light.et_seinavalo
    to: "off"
  condition:
    - condition: state
      entity_id: input_boolean.door_light_automated
      state: "on"
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.door_light_automated

- alias: Start front door light automation
  trigger:
    platform: state
    entity_id: binary_sensor.doors_front
    to: "on"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id:
          - input_boolean.door_light_automated
          - light.et_kattovalo
          - light.et_seinavalo
          - light.et_vaatekaapin_valo
        state: "off"
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.door_light_automated
    - delay: 00:00:01
    - service: light.turn_on
      entity_id: light.et_seinavalo

I have couple issues with this:

  1. I needed to add delay: 00:00:01, without it the light never send status to integration (knx in this case)
  2. As there is no way to cancel delay, im sceptical on delay: 00:02:00. Seems that when reopeing door during this time, automation is already running and does not trigger reset of delay and keep lights on and start delay countdown after closing.

Maybe I should try to use numericial value for time (now() + 5 minutes) and keep resetting that to postpone delay, if door is held open.

Ideas?

PS: I had it working on my own RETE engine that im migrating away from

global FRONTDOOR_ETSV_COUNTDOWN = 1000 * 60 * config('rules.frontdoor.countdown', 2); // 2 Minutes

rule FRONTDOOR_ETSV_INITIALIZE_TIME
{
    when
    {
        $e : SystemEvent $e.name === 'CONFIG_SET' &&
                         $e.options.name === 'rules.frontdoor.countdown';
    }
    then
    {
        var value = parseInt($e.options.value);
        FRONTDOOR_ETSV_COUNTDOWN = 1000 * 60 * value;
        log('FRONTDOOR_ETSV_COUNTDOWN is set to ' + (FRONTDOOR_ETSV_COUNTDOWN / 60 / 1000) + ' minute(s)');
    }
}

rule FRONTDOOR_ETSV_INITIALIZE
{
	when
	{
		$c : Context isUndefined($c.get('FRONTDOOR_ETSV_ACTIVATED'));
	}
	then
	{
		log('FRONTDOOR_ETSV_COUNTDOWN is set to ' + (FRONTDOOR_ETSV_COUNTDOWN / 60 / 1000) + ' minute(s)');
		modify($c, function(){
			this.set('FRONTDOOR_ETSV_ACTIVATED', false);
			this.set('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN', null);
		});
	}
}

rule FRONTDOOR_ETSV_ACTIVATE_BY_FRONTDOOR
{
	when
	{
		$c : Context isFalse($c.get('FRONTDOOR_ETSV_ACTIVATED')) &&
				     state('ETSV') != 'ON' &&
					 state('DUSK') == 'ON';
		$event: StateEvent $event.name == "FRONTDOOR" &&
						   $event.newState == 'OPEN';
	}
	then
	{
		$c.set('FRONTDOOR_ETSV_ACTIVATED', true);
		$c.set('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN', null);

		command('ETSV', 'ON');
		log('Front door activated wall lights');
	}
}


rule FRONTDOOR_ETSV_COUNTDOWN
{
	when
	{
		$c : Context isTrue($c.get('FRONTDOOR_ETSV_ACTIVATED'));
		$event: StateEvent $event.name == "FRONTDOOR" &&
						   $event.newState == 'CLOSE';
	}
	then
	{
		var countDown = new Date(new Date().getTime() + FRONTDOOR_ETSV_COUNTDOWN);
		$c.set('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN', countDown);
	}
}

rule FRONTDOOR_ETSV_COUNTDOWN_RESET_BY_FRONTDOOR
{
	when
	{
		$c : Context isTrue($c.get('FRONTDOOR_ETSV_ACTIVATED')) && isNotNull($c.get('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN'));
		$event: StateEvent $event.name == "FRONTDOOR" &&
						   $event.newState == 'OPEN';
	}
	then
	{
		modify($c, function(){
			this.set('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN', null);
		});
	}
}

rule FRONTDOOR_ETSV_DEACTIVATE_BY_COUNTDOWN
{
	when
	{
		$c : Context isTrue($c.get('FRONTDOOR_ETSV_ACTIVATED')) &&
					 isNotNull($c.get('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN')) &&
					 $c.get('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN') < now();
	}
	then
	{
		$c.set('FRONTDOOR_ETSV_ACTIVATED', false);
		$c.set('FRONTDOOR_ETSV_ACTIVATED_COUNTDOWN', null);

		command('ETSV', 'OFF');
		log('Timer deactivated front door wall lights');
	}
}

I’d also have a look at this cookbook entry. The same logic should apply:

  1. Turn on the light when the door opens
  2. When the door has been closed for X minutes, turn it off if it’s not already off
1 Like

Hehe, I was looking that page and did not got my head wrapped around for statement, but seems that it can be used nicely afer third look…

Current version:

- alias: Start front door light automation
  trigger:
    platform: state
    entity_id: binary_sensor.doors_front
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.dusk
        state: "on"
      - condition: state
        entity_id:
          - input_boolean.doors_front_light_auto
          - light.et_kattovalo
          - light.et_seinavalo
          - light.et_vaatekaapin_valo
        state: "off"
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.doors_front_light_auto
    - delay: 00:00:01
    - service: light.turn_on
      entity_id: light.et_seinavalo

- alias: Stop front door light automation
  trigger:
    platform: state
    entity_id: binary_sensor.doors_front
    to: "off"
    for:
      minutes: 2
  condition:
    condition: state
    entity_id: input_boolean.doors_front_light_auto
    state: "on"
  action:
    - delay: 00:00:01
    - service: input_boolean.turn_off
      entity_id: input_boolean.doors_front_light_auto
    - service: light.turn_off
      entity_id: light.et_seinavalo

- alias: Cancel front door light automation
  trigger:
    platform: state
    entity_id: light.et_seinavalo
    to: "off"
  condition:
    condition: state
    entity_id: input_boolean.doors_front_light_auto
    state: "on"
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.doors_front_light_auto
  • Turns lights on when door is opened
    • Only when dusk state is active
    • Only when there is no lights active in that zone
    • Marks automation state active | input_boolean.doors_front_light_auto
  • Turns lights off after delay when door is closed
    • Only if automation state is active | input_boolean.doors_front_light_auto
    • Marks automation state inactive | input_boolean.doors_front_light_auto
    • Prevents lights out if lights are controlled manually
  • Cancels automation state if lights are manually used
    • Marks automation state inactive | input_boolean.doors_front_light_auto

Beautiful