Keep light on when motion is detected

Hi Please could someone help me with this.

I currently have a motion script to turn on a light in my bathroom when motion is detected and goes off after 4 minutes. I would like to keep it on though all the time motion is detected and then turn off 4 minutes after the last motion detected. Is that possible?

This is my current script’s:

bathroom_motion.yaml

` # This automation detects movement from my sensor and turn’s on my under bath light’s if the luminace is below 10.
alias: “Turn on Under Bath Lights with Motion”
trigger:

  • platform: state
    entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_12
    state: ‘on’
    condition:
  • condition: numeric_state
    entity_id: sensor.aeotec_zw100_multisensor_6_luminance_12
    below: 20
    action:
    service: script.motion_bathroom`

motion.bathroom.yaml

` sequence:

  • service: script.turn_off
    data:
    entity_id: script.motion_bathroom_timer
  • service: switch.turn_on
    data:
    entity_id: switch.bath_lights
  • service: script.turn_on
    data:
    entity_id: script.motion_bathroom_timer`

bathroom_motion_timer.yaml

` sequence:

  • delay:
    minutes: 4
  • service: switch.turn_off
    data:
    entity_id: switch.bath_lights`

Thanks

MIke

You may have to split up the automation you have into two:

  • an automation to turn on the lights when motion is detected
  • an automation to turn off the lights when no motion is detected for 4 minutes

The trigger of the latter would look like this:

trigger:
  platform: state
  entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_12
  from: 'on'
  to: 'off'
  for:
    minutes: 4

Thanks buddy, but I am still non the wiser :frowning: I am very new to all this so most of it is still confusing could you elaborate a little more please.

Thanks

Mike

I think the example on this page is the easiest. I have it working. https://home-assistant.io/cookbook/turn_on_light_for_10_minutes_when_motion_detected/

Every time when motion is detected, the timer is reset by the script. And the timer will start again

Cheers buddy, This is the actual script I am using and it doesn’t seem to reset.

Ok… strange. Can you show your section from the config yaml here ?

It looks like you are missing the entity_id when you call the script in your bathroom_motion.yaml.
I have it like this:

action:
  service: homeassistant.turn_on
  entity_id: script.timed_lamp

To help you a bit more, my complete config:
kitchen_lights.yaml:

alias: 001 - Turn on kitchen lights when there is movement
trigger:
  - platform: state
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_14
    to: 'on'
condition: 
  condition: and
  conditions: 
    - condition: numeric_state
      entity_id: sensor.fibaro_system_fgms001_motion_sensor_luminance_14
      below: 25
    - condition: state
      entity_id: input_boolean.motion_actief
      state: 'on'
action:
  service: homeassistant.turn_on
  entity_id: script.timed_lamp

sensors.yaml:

timed_lamp:
alias: "Turn on lamp and set timer"
sequence:
  # Cancel ev. old timers
  - service: script.turn_off
    data:
       entity_id: script.timer_off
  - service: light.turn_on
    data:
      entity_id: light.keuken
      brightness: 125
  # Set new timer
  - service: script.turn_on
    data:
      entity_id: script.timer_off

timer_off:
  alias: "Turn off keukenverlichting na 2 min. geen beweging"
  sequence:
    - delay:
        minutes: 2
    - service: light.turn_off
      data:
        entity_id: light.keuken

This should work i guess. Although i have some issues with the light turning off when i am in the kitchen. But i think it is due to a parameter of my Fibaro sensor, have to look at it when i have some more time.

P.S. For syntax highlighting large blocks of code, type 3 backticks and then yaml, paste your code and close with three backticks, like this:

‘’‘yaml
blablablacodeblabla
‘’’
For a single word or line just use one backtick in front and at the end like this: ‘fakecode123’.

@ThinkPad did you ever get this working? I have identical setup (fibaro motion sensor) and script but light keeps turning of while I’m in the kitchen.

Was posting about this here:

@Tyfoon I just have a trigger for on and a timer for off and it works for me. After the 4 mins is up since last detection on the sensor side and it switches to off, the lights go out. Gen 5 aeotech Z wave multisensor - timer settings for sensor are from OZWCP. I copied it the automations from here somewhere - or maybe the cookbooks.

Automation for light on

alias: Kitchen Motion
    trigger:
      platform: state
      entity_id: binary_sensor.kitchen_sensor_2_0
      to: 'on'
    condition:
      condition: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: 3.5
    action:
      service: homeassistant.turn_on
      entity_id: light.kitchen

And automation timer to switch it off

alias: Kitchen Timer
trigger:
  platform: state
  entity_id: binary_sensor.kitchen_sensor_2_0
  to: 'off'
action:
  service: homeassistant.turn_off
  entity_id: light.kitchen

Unfortunately not, and as my gf got annoyed of it, i made the light ‘dumb’ again with a button she has to press herself. I’m not sure what went wrong, as it turned on by the sensor fine. But the ‘keep on during motion’ part wasn’t working good.

The problem is “wrong girlfriend”. :wink:

Sorry couldn’t resist.

Im trying to use the code from here …from the examples: “turn on light for 10 min” to send a curl command… my setup:

This is the switch I’m trying to actuate with the automation:

Switches:
  platform: command_line
  switches:
    recording_living_room:
      command_on: 'curl "http://10.0.0.5:8081/allon?ot=2&oid=0"'
      command_off: 'curl "http://10.0.0.5:8081/alloff?ot=2&oid=0"'
      friendly_name: iSpy

My automation code all under automation.yaml and point from configuration.yaml with !include automation.yaml:

## AUTOMATION ##
### Turn on recording when group.family is "not_home" ###
- alias: Turn on iSpy when family is not home
  trigger:
    - platform: state
      entity_id: group.family
      from: 'home'
      to: 'not home'
  action:
    service: homeassistant.turn_on
    entity_id: script.recording

  recording:
    alias: "Turn on recording and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.timer_off
      - service: switch.recording_living_room
        data:
          entity_id: switch.recording_living_room
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off

  timer_off:
    alias: "Turn off recording after 5 minutes"
    sequence:
      - delay:
          minutes: 5
      - service: switch.recording_living_room_off
        data:
          entity_id: switch.recording_living_room

A few questions:
-its no currently working and I believe its because I need a separate file with “timer_off.yaml” + “recording.yaml” + “tun_on”?
-my actual goal is to use the variables “home” and “not_home” as the trigger? but I have to get it to record before I mess with this part…any advise?

Assuming your switch is defined in your configuration.yaml file, your code should look like this:

switch:
  - platform: command_line
    switches:
      recording_living_room:
        command_on: 'curl "http://10.0.0.5:8081/allon?ot=2&oid=0"'
        command_off: 'curl "http://10.0.0.5:8081/alloff?ot=2&oid=0"'
        friendly_name: iSpy

Your second bit of code needs to be split as it has both 1 automation and 2 scripts. So your automation.yaml file should look like this:

## AUTOMATION ##
### Turn on recording when group.family is "not_home" ###
- alias: Turn on iSpy when family is not home
  trigger:
    - platform: state
      entity_id: group.family
      from: 'home'
      to: 'not_home'
  action:
    service: script.turn_on
    entity_id: script.recording

And your scripts.yaml file (don’t forget to include it in configuration.yaml) should look like this:

recording:
  alias: "Turn on recording and set timer"
  sequence:
    # Cancel ev. old timers
    - service: script.turn_off
      entity_id: script.timer_off
    - service: switch.turn_on
      entity_id: switch.recording_living_room
    # Set new timer
    - service: script.turn_on
      entity_id: script.timer_off

timer_off:
  alias: "Turn off recording after 5 minutes"
  sequence:
    - delay:
        minutes: 5
    - service: switch.turn_off
      entity_id: switch.recording_living_room

This should hopefully make it work:slight_smile:

Turn off light after 4 minutes the last motion detected. Is’t possible in Automation Editor also. Do i create condition type > time ? (Hassbian 0.60.0)

 for:
    minutes: 4