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:
- I needed to add
delay: 00:00:01
, without it the light never send status to integration (knx in this case) - 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');
}
}