Turn a light off after x mins if it was turn on via rule

So I found this for turning on a light by motion, and off after 10 mins. Great.

But if I only wanted the turn off rule to fire if it was turned on via motion (in my case a door reed switch) and not when turned on manually, how do you add that exception.

I’m coming from openhab where this was all in one rule where if “night” and if reed switch opned, then turn light on and start a 10 min timer…at the completion of the timer turn off the light (and extend the timer if the door is opened a 2nd time in that 10 mins.

Can I effect the same in HA?

Add an input boolean, set it if motion is triggered and disable it again when you trun of the light.

This is what I use for doors with a 2 minute timer (This can be done in different ways, but the behavior is how you describe):

These next 3 peices of yaml will turn on a light for 2 minutes after a door is opened. Each time the door opens, it extends the timer by 2 minutes. So it will only shut the lights off after 2 minutes since the last open.

automation1:

- alias: Foyer light trigger on main door 
  trigger:
    - platform: state
      entity_id: sensor.main_door_hindge
      to: 'open'
  condition:
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"
  action:
    - service: homeassistant.turn_on
      entity_id: script.foyer_door_is_open

Door opening script

alias: Foyer Door is Open
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
      entity_id: script.foyer_light_timer
  - service: switch.turn_on
    data:
      entity_id: switch.foyer_s_switch_7_0
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.foyer_light_timer

timer script

alias: Foyer Light Timer
sequence:
  - delay:
      minutes: 2
  - service: switch.turn_off
    data:
      entity_id: switch.foyer_s_switch_7_0

This second automation is similar for motion detection:

automations to turn on lights depending on time:

- alias: Hall Motion Detected During Night
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_ms_sensor_32_0
      to: 'on'
  condition:
    - condition: time
      after: '23:00:00'
      before: '07:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id: script.hall_motion_night
    
- alias: Hall Motion Detected During Day
  trigger:
    - platform: state
      entity_id: binary_sensor.hallway_ms_sensor_32_0
      to: 'on'
  condition:
    - condition: time
      after: '07:00:00'
      before: '23:00:00'
  action:
    - service: homeassistant.turn_on
      entity_id: script.hall_motion_day

motion timer script

alias: Hall Light Timer
sequence:
  - delay:
      minutes: 5
  - service: light.turn_off
    data:
      entity_id: light.hall_d_level_10_0

day time lights for motion

alias: Hall Motion Day
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
      entity_id: script.hall_light_timer
  - service: light.turn_on
    data:
      entity_id: light.hall_d_level_10_0
      brightness: 255
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.hall_light_timer

night time motion script

alias: Hall Motion Night
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
      entity_id: script.hall_light_timer
  - service: light.turn_on
    data:
      entity_id: light.hall_d_level_10_0
      brightness: 10
      #brightness: "{{ 255 * 0.15 | round(0) | int }}"
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.hall_light_timer

These can be simplified if you want to, otherwise you can use them and just change the entity id’s.

1 Like

Hey great thanks. Quite a different way of operating from OH, and it’s taking a bit to get my head around.

So am I able to put those 3 automations into one file…ie DoorOpening.yaml, and then when I get another set, have another file, or does it all just exist in one Automations.yaml file?

you can do either. Depends on how you want to split up your config. take a look at ways to organize your config:

Yeah I think I tried this with sensors and couldn’t get it, but I did manage to separate all the sensors to a single new file…I couldn’t work out the directory of files, does that work for everything, or is it an automation only thing?

Ie per example (from the page you linked)

└── .homeassistant
    ├── automation
    │   ├── lights
    │   │   ├── turn_light_off_bedroom.yaml
    │   │   ├── turn_light_off_lounge.yaml
    │   │   ├── turn_light_on_bedroom.yaml
    │   │   └── turn_light_on_lounge.yaml
    │   ├── say_hello.yaml
    │   └── sensors
    │       └── react.yaml
    └── configuration.yaml (not included)

I’ve been using this and it works perfectly for me

- alias: Motion Lighting in the Office
  trigger:
  - platform: state
    entity_id: binary_sensor.centralite_3326l_05989ed5_1_1280
    from: 'off'
    to: 'on'
  condition:
  - condition: state
    entity_id: group.office_lights
    state: 'off'
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - service: homeassistant.turn_on
    entity_id: group.office_lights
  - wait_template: "{{ is_state('binary_sensor.centralite_3326l_05989ed5_1_1280', 'off') }}"
  - condition: state
    entity_id: group.office_lights
    state: 'on'
  - service: homeassistant.turn_off
    entity_id: group.office_lights

If the lights are turned on my the motion sensor they will turn off when motion turns to off, but if the lights were already on, then they will stay on. In my case I have two lights in my office in a group, so if either one is on, then the automation does not run.

Usually If I’m going to be in the office for more than a minute, I’d use my echo to turn the lights on and then use the echo to turn them off when I leave.

Hi, so if I’m readying this right, they only stay on while there’s motion? Not a timed event for off?

1 Like

Hello,

the scripted version is probably ok but there is nothing simplier ?
I mean using only one automation rule…

trigger:
… (motion.sensor)
Condition:
… (sun, light_level, etc…)
Action:

  • service : homeassistant.turn_on
    entity_id: switch.office_lights
  • wait : 2 minutes
  • service : homeassistant.turn_off
    entity_id: switch.office_lights

is there a way doing something like this ?

Thanks

That is correct. The motion sensor I have is pretty quick to turn off so for me it’s perfect. Wallk in lights go on. Walk out and they go off

Yes that’s possible but it wouldn’t have the extend ‘time’ ability. Meaning, if you open the door and close the door, then within 2 minutes you open it again, as soon as the first automation is done, it will shut off your light.

So you could open the door at 8 am and close it. Then at 8:01:58 you open the door again. No matter what, at 8:02:00, the light will shut off. That’s why the timer method is more favorable. Anyways, this is what it would look like without the timer:

trigger:
… (motion.sensor)
condition:
… (sun, light_level, etc…)
action:
  - service : homeassistant.turn_on
    entity_id: switch.office_lights
  - delay:
      minutes: 2
  - service: homeassistant.turn_off
    entity_id: switch.office_lights

Also, @ptdalen @psyciknz @dedlefou you don’t have to use the script method. You guys can actually use the timer component. Only reason I used a script to handle my business is solely because the timer component didn’t exist when I wrote it back in 2016.

1 Like

Thank you so much Petro !
It’s exactly what I’m looking for :slight_smile: just a 30sec light.on in fact…

I “dissected” your approach with script and I should confess that it’s not too complicate :wink: and more smart & elegant… I’ll need it too !

Tks again

In my case I really want the lights to turn off almost immediately after the motion turns to ‘off’, even 1 minute is too long. Although, once in a while I will say that since my timeout is pretty short, I’ve been meaning to tweak it to be like

wait_template: "{{ is_state('binary_sensor.centralite_3326l_05989ed5_1_1280', 'off') }}" for 10 seconds

but not sure how to do that.

But maybe if I did delay that would work

Just to be clear, if I did a 1 minute delay after motion was detected ie ‘on’, then 30 seconds later it went ‘off’ then 35 seconds later it went on, the timer would start over. Since my motion sensor turns off 30 seconds after motion, the lights would stay on only 30 seconds after motion went to ‘off’. I could change the delay to 45 seconds or so, which would give me 15 seconds of buffer to ‘trigger’ to keep the lights on if I was in the room longer.

so wait template waits until movement is gone. so if you want to add 10 seconds, just add a delay after:

  - wait_template: "{{ is_state('binary_sensor.centralite_3326l_05989ed5_1_1280', 'off') }}"
  - delay:
      seconds: 10

to clarify, the wait template should wait til movement is gone. the situation

above shouldn’t happen because of the wait template.

duh!!, genius. So, if in that 10 seconds, motion is triggered again, the lights would stay on, since the automation would trigger again, right?

I’m not sure. I’ve always thought it doesn’t cancel the previous automation, so after that 10 seconds it should end the automation regardless if it gets triggered again. Thats why I always create a timer.

I’m going to try and will report back. I could definitely create a timer that was 10 seconds longer than the motion sensor timout.

But this is where I get stuck

I come in to a room, motion is triggered, lights turn on
2 minute Timer starts
I stay in room moving around, motion stays on for 3 minutes.
It never goes off and back on, so the timer does not get reset, right?

That’s what I was using the wait template. for me about 90% of the time its perfect, the only times are when I come into room, do something where I’m still enough to not keep the sensor ‘on’ then the lights turn off, and then back on as soon as I move.

you can use the cancel timer event at the begining of the automation. then start it up during the automation. This in essence could ‘keep it on forever’

1 Like

Hmm, Still having trouble with the timer logic

Walk into room, timer is not running
Trigger – Motion
Lights turn on
Timer starts 2 minutes
Motion never goes to off, I’m having a dance party, haha.
Timer ends, lights go off
I take a break from dancing, then get up, lights turn back on, repeat, etc.

Or
I walk into room,
Lights on
Timer starts
Motion stops,
timer still going
Motion Starts
Timer canceled
Timer starts over, lights still on

The second scenario is the most common, but the with the first scenario, the lights would turn off if motion continued throughout the timer right.

With mine, as long as there is motion the lights stay on, then turn off as soon at motion goes to ‘off’, If I could have an ‘off + 10 seconds’ that seems like it would work better, right? Or am I over thinkng it