Turn on lights on and keep them on when motion detection

Hello!

I’m new to Home Assistant so I’m not very good at it yet. I was reading through example automations and decided to give the ‘Turn on Lights for 10 Minutes After Motion Detected’ example a go. I have an Aeotec Multisensor 6 to detect the motion. At the moment, the timer part and switching the lights off is working, but I can’t get the lights to turn on when there’s motion. Here’s the current configuration:

automation:
-  alias: Turn on all lights when there is movement
   trigger:
     - platform: state
       entity_id: sensor.aeotec_zw100_multisensor_6_burglar_4
       above: '1'
   action:
     service: light.turn_on
     entity_id: script.timed_lamp

script:
  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: group.home_lights
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off

  timer_off:
    alias: "Turn off lamp after 5 minutes"
    sequence:
      - delay:
          minutes: 5
      - service: light.turn_off
        data:
          entity_id: group.home_lights

For the trigger, I’ve tried having it as :

to: '8'

As well as:

from: '0'
to: '8'

and the current:

above: '1'

Any suggestions as to why the lights aren’t being switched on when there’s motion?

Thanks! :slight_smile:

1 Like

Solved this issue…

changed under animation/action service: light.turn_on to service: homeassistant.turn_on

Works now! Thanks to sonofdays in the chat for the help! :slight_smile:

I’m having trouble keeping the lights on when there’s motion. With the current setup, it switches off after 5 minutes despite the motion sensor’s status being 8 (motion). It only works when it goes from state 0 to 8.

Can I add a condition to the turn off script where the script only starts if the status of the motion detector is 0(no motion)?

script:
  timer_off:
    alias: "Turn off lamp after 5 minutes"
    sequence:
      - delay:
          minutes: 5
      - service: light.turn_off
        data:
          entity_id: group.home_lights

I am playing around with lights automation at the moment, and stumbled upon the same issue. My setup is an Aeotec Multisensor 6 with Hue lights in the living room.

I solved the issue of lights turning off after the set amount of time despite there is motion with a condition that does an additional check: if the motion sensor is not in ‘no motion’ state then restart the timer, and only turn off the lights when the motion sensor is actually in ‘no motion’ state.
In addition I noticed that when I watch TV in the evening I am often not moving for some time and the lights turn off - I solved this issue in a similar way as described above by checking if the TV is still turned on or Plex (on the TV) is currently playing.

Here’s my current configuration:

automation:
  alias: "Turn on living room lights when there is movement"
  trigger:
    - platform: state
      entity_id: sensor.aeotec_zw100_multisensor_6_burglar_2
      to: '8'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp

script:
  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: group.living_room
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off
  timer_off:
    alias: "Turn off lamp after 15 minutes"
    sequence:
      - delay:
          minutes: 15
      - service_template: > 
          {% if ((not is_state('sensor.aeotec_zw100_multisensor_6_burglar_2', '0')) or (is_state('media_player.living_room_tv', 'on')) or (is_state('media_player.my_bravia_2015', 'playing')))  %}
            script.turn_on
          {% else %}
            light.turn_off
          {% endif %}
        data_template:
          entity_id: >
            {% if ((not is_state('sensor.aeotec_zw100_multisensor_6_burglar_2', '0')) or (is_state('media_player.living_room_tv', 'on')) or (is_state('media_player.my_bravia_2015', 'playing'))) %}
              script.timer_off_restart
            {% else %}
              group.living_room
            {% endif %}
  timer_off_restart:
    alias: "Turn off lamp after 15 minutes (restart)"
    sequence:
      # Cancel current script
      - service: script.turn_off
        data:
          entity_id: script.timer_off
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off

The if-statements are quite complicated, and I’m not sure if there’s a simpler way to do this. The restart script was necessary as apparently you can’t cancel and then restart a script from within itself.

Issues remaining:

  • My automation only kicks in after sunset. However, if there is motion before sunset and continues to be, then the automation is never triggered because the sensor never changes its state. To solve this a separate automation triggered at sunset itself may be required.
  • When I get up before sunrise and trigger the automation so the lights turn on, as long as there’s still motion even after sunrise, the lights will never turn off. To solve this another condition may be required in the if-statements to check if the sun has risen.

Happy to hear about any suggestions for improving the code. It’s already getting quite longish for a single automation.

2 Likes

Thanks for the reply! :slight_smile:

That’s actually quite good! I just resorted to simplifying it. I ended up just doing this:

-  alias: Turn on all lights when there is movement
   trigger:
     - platform: state
       entity_id: sensor.aeotec_zw100_multisensor_6_burglar_4
       to: '8'
   action:
     service: homeassistant.turn_on
     entity_id: group.home_lights

-  alias: Turn off all lights when there is no movement
   trigger:
     - platform: state
       entity_id: sensor.aeotec_zw100_multisensor_6_burglar_4
       to: '0'
   action:
     service: homeassistant.turn_off
     entity_id: group.home_lights

It pretty much acts like that timer, since it takes about 4 minutes to change state from 8 (motion) to 0 (no motion). I initially wanted the timer to be 5 minutes. The good thing is that it doesn’t interfere if I’m not home since I’ve set another automation so that my lights can switch on at sunset and turn off before midnight.

Today I did notice that the lights switched off while I was watching TV because I wasn’t moving much. I guess I can easily add a condition in the second automation so that it will only turn off if the tv is off (living room).

1 Like

I am about to start the same thing.
and the code above is how I would have started.
I notice that you want the delay to be 5 min (instead of 4). If you are using the multi_sensor_6 you can adjust this timer in OZWCP. (i think it is the 2nd field in the config.)

I’ve been around the same issue, little bit different though, but here’s what I found about the example:

Actually I found the problem with light turning of during the movement, just by looking into code.
The easiest way to accomplish this would be:

movement on -
stop earlier timer.
set light on.

movement off -
start timer

timer -
wait X
light off

Then you can add your own timer to switch lights off.

3 Likes

I have been searching the forums and came across about 4 different threads about setting up sensor with motion to activate a light. I have tried to use the different examples. Of course I change the info to match my setup. I guess I am getting myself confused. Is the code that triggers the light all supposed to be in one ymal file? or is it supposed to be in separate files? Is there a way to test the automation by button or switch to trouble shoot and see if it works?

Your YAML will typically be a single file unless you decide to split it up. A single file is fine as long as you can keep track of everything (the file’s not to long to make it hard to find stuff).

You can force an automation to trigger from the UI. Automations show up on a card (on the default view unless you’ve customized). Click on one of them and you’ll see a “trigger” button/link.

Late to the party here, but for those of us without “smart” TVs that can be checked, I plan on testing this out with a luminance condition from the multisensor so that the the action doesn’t re-trigger if I’ve dimmed the lights to watch a movie or whatever.

The theory is that I want the theater light dimmer in the media room to turn on when I walk in so that I don’t step on a cat (it’s pitch black in there and we have six cats). Then, I’ll dim or turn them off manually and do my thing. I do not want them to turn if I’m in there watching a movie or playing video games (luminance) and the sensor detects movement. I plan on testing this out tomorrow when I get around to installing the media room switches.

automation:
- alias: Turn theater lights to 50% 
  trigger:
    platform: state
    entity_id: sensor.aeotec_zw100_multisensor_6_burglar_11_10
    from: '0'
    to: '8'
  action:
    service: light.turn_on
    entity_id: light.ge_12724_3-way_dimmer_switch_level_12_0
    data:
      brightness: 128
  condition:
   - platform: numeric_state
     entity_id: sensor.aeotec_zw100_multisensor_6_luminance_11_3
     below: 100

Thanks for this I an new to home assistant and had been working on this for days. I had to make a few changes to get it to work for me. Here is what I did:

automation:
  alias: "Turn on kitchen lights when there is movement"
  trigger:
    - platform: state
      entity_id: sensor.vision_zp3111_multisensor_4in1_alarm_level_3_1
      state: '255'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp

script:
  timed_lamp:
    alias: "Turn on light and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.timer_off
      - service: homeassistant.turn_on
        data:
          entity_id: switch.ge_12722_onoff_relay_switch_switch_2_0
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off
  timer_off:
    alias: "Turn off light after 1 minutes"
    sequence:
      - delay:
          minutes: 1
      - service_template: > 
          {%- if not is_state("sensor.vision_zp3111_multisensor_4in1_alarm_level_3_1", "0") -%}
            script.turn_on
          {% else %}
            homeassistant.turn_off
          {% endif %}
        data_template:
          entity_id: >
            {%- if not is_state("sensor.vision_zp3111_multisensor_4in1_alarm_level_3_1", "0") -%}
              script.timer_off_restart
            {% else %}
              switch.ge_12722_onoff_relay_switch_switch_2_0
            {% endif %}
  timer_off_restart:
    alias: "Turn off light after 1 minutes (restart)"
    sequence:
      # Cancel current script
      - service: script.turn_off
        data:
          entity_id: script.timer_off
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.timer_off
2 Likes

I can’t get this to work on a second light. Every time I add another automation or another script home assistant will not start back up. Here is an example of what I did for the second automation that didn’t work.

automation:
  alias: "Turn on kitchen lights when there is movement"
  trigger:
    - platform: state
      entity_id: sensor.vision_zp3111_multisensor_4in1_alarm_level_3_1
      state: '255'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp
  alias: "Turn on bath lights when there is movement"
  trigger:
    - platform: state
      entity_id: binary_sensor.ecolink_pir_motion_sensor_sensor_4_0
      state: 'on'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp

If I remove the second automation (Turn on bath lights when there is movement) the first one works fine. I have searched but cant find anything on this.

use a - (minus sign) before the “alias”. Dashes denote the beginning of an item in a list. Do “automation:” accepts a list of child automations.

automation:
  - alias: one
    etc

  - alias: two
    etc
2 Likes

Thanks that woked great for the automation! I can’t get it work on scripts. Can the same be done for scripts?

Under scripts, you’d have

scripts:
  script_name:
  etc..

  other_script:
1 Like

Thanks for the help. That is what I have but still not working on second switch. I will try again and double check my spaces and stuff.

Check the log for errors.

I took a different approach to this.
instead of starting the timer when the sensor SEES movement, I start the timer when the sensor sees NO movement.
when there is movement I cancel the timer.
This way, even though there is continuous movement, the lights want turn off.

in addition, I have added an input_slider, so I can change the delay timer throughout the day if necessary.

automation

 - alias: Turn on entrence light by movement
   trigger:
     platform: state
     entity_id: sensor.fibaro_system_fgms001_motion_sensor_burglar_9_10
     state: '8'
   action:
     service: homeassistant.turn_on
     entity_id: script.timed_lamp_entrence

 - alias: Turn off entrence lights set by movement
   trigger:
     platform: state
     entity_id: sensor.fibaro_system_fgms001_motion_sensor_burglar_9_10
     state: '0'
   action:
     service: homeassistant.turn_on
     entity_id: script.timer_off_entrence

script

  timed_lamp_entrence:
    alias: "Turn on entrence light and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.timer_off_entrence
      - service: light.turn_on
        entity_id: light.entre
        data_template:
          brightness: "{{ states('input_slider.current_hue_brightness')|int }}"
          color_temp: "{{ states('input_slider.color_temp')|int }}"

  timer_off_entrence:
    alias: "Turn off entrence light after x minutes"
    sequence:
      - delay: '00:{{ states.input_slider.entrence_delay.state | int }}:00'
      - service: light.turn_off
        data:
          entity_id: group.entrence_lights

input_slider

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

if interested, here is my github

6 Likes

Must of had some spaces that were wrong in my script because I did it again and it is working now. Not sure where the log is. I have a OZW_Log in the same folder as my configuration.yaml file. I look in there and its like another language to me so I don’t know if I would see what was wrong. I would like to add a slider to adjust the timed light so it will stop turning off on us when we are in the shower. Not sure what I should be putting in the entity_id: part or if I ever have it set up right. Any help would be appreciated.

configuration.yaml

input_slider:
  bathroom_shower:
    name: Shower Timer
    initial: 1
    min: 1
    max: 31
    step: 15

automation.yaml

- alias: "Turn on bath lights when there is movement"
  trigger:
    - platform: state
      entity_id: binary_sensor.ecolink_pir_motion_sensor_sensor_4_0
      state: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: script.bathroom_timed_light

script.yaml

  bathroom_timed_light:
    alias: "Turn on bathroom light and set timer"
    sequence:
      # Cancel ev. old timers
      - service: script.turn_off
        data:
           entity_id: script.bathroom_timer_off
      - service: homeassistant.turn_on
        data:
          entity_id: switch.ge_12722_onoff_relay_switch_switch_5_0
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.bathroom_timer_off

  bathroom_timer_off:
    alias: "Turn off bathroom light after 1 minutes"
    sequence:
      - delay:
        data_template:
          entity_id: ???????????????
          minutes: '{{ states.input_slider.bathroom_shower.state | int }}'
      - service_template: > 
          {%- if not is_state("binary_sensor.ecolink_pir_motion_sensor_sensor_4_0", "off") -%}
            script.turn_on
          {% else %}
            homeassistant.turn_off
          {% endif %}
        data_template:
          entity_id: >
            {%- if not is_state("binary_sensor.ecolink_pir_motion_sensor_sensor_4_0", "off") -%}
              script.bathroom_timer_off_restart
            {% else %}
              switch.ge_12722_onoff_relay_switch_switch_5_0
            {% endif %}
  bathroom_timer_off_restart:
    alias: "Turn off bathroom light after 1 minutes (restart)"
    sequence:
      # Cancel current script
      - service: script.turn_off
        data:
          entity_id: script.bathroom_timer_off
      # Set new timer
      - service: script.turn_on
        data:
          entity_id: script.bathroom_timer_off

can you even use a slider like that?

1 Like

After looking at mcnovy script I canged this and now it works but I dont see the slider on the frontend. Am I missing somthing?

bathroom_timer_off:
    alias: "Turn off bathroom light after 1 minutes"
    sequence:
      - delay: '00:{{ states.input_slider.bathroom_shower.state | int }}:00'
1 Like