Turn on lights on and keep them on when motion detection

can not for the life of me get this to work, any one else tried??? most likely my formating

update… code as expected was correct my problem was my lack of knowledge re how I split up my automation file…

All of this should be able to be done with one automation as well. I haven’t tested this but it should work.

- alias: Lights on/off based on motion
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor
      to: 'on'
    - platform: state
      entity_id: binary_sensor.motion_sensor
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    service_template: >
      {% if trigger.to_state.state == "on" %}
      homeassistant.turn_on
      {% elif trigger.to_state.state == "off" %}
      homeassistant.turn_off
      {% endif %}
    entity_id: group.kitchen
5 Likes

I like how you have done this,
but how would you make the “for:” timer dynamic (use a input slider//template)?

If you were to replace your automatons and script posted above with this, I think it should do the same thing. This is untested though.

entrence_delay:
  name: Entrence Delay
  initial: 2
  min: 0
  max: 20
  step: .5

.

- alias: Lights on/off based on motion
  trigger:
    - platform: state
      entity_id: sensor.fibaro_system_fgms001_motion_sensor_burglar_9_10
      to: '8'
    - platform: state
      entity_id: sensor.fibaro_system_fgms001_motion_sensor_burglar_9_10
      to: '0'
      for:
        seconds: '{{ states.input_slider.entrence_delay.state|float * 60 }}'
  action:
    service_template: >
      {% if trigger.to_state.state == "8" %}
      homeassistant.turn_on
      {% else %}
      homeassistant.turn_off
      {% endif %}
    entity_id:
      - light.entre
2 Likes

I tried this and it doesn’t work, as you cannot turn off something with a data_template, i.e. you get an error in the log

“Invalid service data for light.turn_off: extra keys not allowed @ data[‘brightness’]. Got ‘255’
extra keys not allowed @ data[‘color_temp’]. Got ‘255’”

pity as I have wasted a morning trying to do this in 1 automation instead of 2.

Tried with scene.turn_on/scene.turn_off but there is no scene off… so back to 2 automatons I think arggh

1 Like

I also found out that switching off lights with one automation that waits for sensor state to be off for xx minutes does not work well: it seems multiple off timers start, each time the motion sensor state changes to off, and the lights start to turn off randomly until all timers have ended. I didn’t notice it at first because the delay time was only 30 seconds, but it gets annoying with a 30 minute delay time.

So, back to using a timer script with the automation to trigger it…

You might want to look into using Appdaemon, It looks difficult but it is not to hard to get going. It solves all these problems! :slight_smile:

hello, I’m new to Home Assistant. I have made an esp8266 MQTT motion sensor. when motion is detected it will be sending MQTT message “Motion Detected” to HA… what I want to do he when HA received Motion detected message I want the light to turn on and keep it on for 3 mins then turn it back off. I have made an automation to turn on the light when motion is detected. someone, please help me can I make it to auto turn off after 3 mins?

You basically just need to scroll up in this thread, and you will find plenty of examples where lights are turned on triggered by motion detected or sun setting, and then turned off after a set amount of time.

Hello @Ashfaaa
This is the way I’m doing it, thanks to @Pilot, which I’m happy with…

- alias: Light off after 90 sec when motion off
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor
      to: 'off'
      for:
        minutes: 1
        seconds: 30
  action:
    - service_template: light.turn_off
      entity_id: group.kitchen
1 Like

Hi @Ashfaaa, someone had the same (arduino sketch) problem.

You can use the automation with timed scripts, or use another sketch.

1 Like

you have any better sketch.? im kinda new to all this

Sorry, no. But everything you need is the linked thread.

1 Like

I know this is an old post but just to let you know I found your example was exactly what I was looking for. Thanks

2 Likes

Hi,
Find below my config sample which allow to turn on the light on motion is on and turn it off after 30s:

a) The script

script_entrance_night_light:
  alias: Night Light at Entrance
  sequence:
  - data:
      brightness_pct: 5
      entity_id: light.yeelight_white_xxxxxx
    service: light.turn_on
  - delay: '30'
  - data:
      entity_id: light.yeelight_white_xxxxxx
    service: light.turn_off

b) Automation:

- alias: Entrance Night Light On
  id: '1516061021603'
  trigger:
  - entity_id: binary_sensor.motion_sensor_xxxx
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: state
    entity_id: light.yeelight_white_xxxxx
    state: 'off'
  action:
  - data:
      entity_id: script.script_entrance_night_light
    service: script.turn_on

What is the question?

Hi,

Not sure if it helps, but my lights only become motion activated at 9pm. I just wrote an automation to turn my motion automations on at 9 and off at 7am when they become normal lights. Iv hardcoded these times but you could use a input_number to set the time they cone off/on.

I use something similar for my heating:

Let me know if you’d like to see my code

You can control several entities using a comma:

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

Why the script? Can’t you simply use this?:

- alias: Motion Detection - Bathroom Light on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015ab008
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.bathroom
    
- alias: Motion Detection - Bathroom Light off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015ab008
    to: 'off'
    for: '00:05:00'
  action:
    service: light.turn_off
    entity_id: light.bathroom
1 Like

That’s smart and would help.

Yes please i would like to see the code