Automation lights on when movement after sunset, but doesn't turn of after a certain time

I am already working on an automation for a while but i can’t get it working. :expressionless:

What should the script do?

Turn on lights when movement at the frontdoor turn light (only after sunset) and turn them of after 3 minutes.

I think i have made an error in the script but i don’t know what is the exact error.

- id: '1596908154170'
  alias: Licht gang aan bij beweging
  description: ''
  trigger:
  - device_id: fa5b1a617d224b7d914bafe5804ef87a
    domain: binary_sensor
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
    platform: device
    type: turned_on
  condition:
  - after: sunset
    condition: sun
  action:
  - brightness_pct: 50
    device_id: b069ad2526d546f8b019624e780a8713
    domain: light
    entity_id: light.rechter_lamp_gang_beneden
    type: turn_on
  - brightness_pct: 50
    device_id: 4e6944df3d2d4c02820af25b2287524f
    domain: light
    entity_id: light.linker_lamp_gang_beneden
    type: turn_on
  mode: single

This automation will turn the lights on if the motion sensor is on for 3 minutes after sunset.

Nothing here will turn any lights off.

What it does now. It turns on the lights when movement is made and stays on forever.

Change the trigger to a state change from off to on.

Then you need an action of delay for 00:03:00 after your light turn on

Then a turn off action.

Also I suggest setting it as a reset. That way if it senses motion again it will reset the timer.

- id: '1596908154170'
  alias: Licht gang aan bij beweging
  description: ''
  trigger:
  - device_id: fa5b1a617d224b7d914bafe5804ef87a
    domain: binary_sensor
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    to: 'on'
  condition:
    condition: or
    conditions:
      - after: sunset
        condition: sun
      - before: sunrise
        condition: sun
  action:
  - brightness_pct: 50
    device_id: b069ad2526d546f8b019624e780a8713
    domain: light
    entity_id: light.rechter_lamp_gang_beneden
    type: turn_on
  - brightness_pct: 50
    device_id: 4e6944df3d2d4c02820af25b2287524f
    domain: light
    entity_id: light.linker_lamp_gang_beneden
    type: turn_on
  mode: single

new automation for turning light off:

- id: '11111111'
  alias: Licht off
  description: ''
  trigger:
  - device_id: fa5b1a617d224b7d914bafe5804ef87a
    domain: binary_sensor
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    to: 'off'
    for: '00:03:00'
  action:
    device_id: b069ad2526d546f8b019624e780a8713
    domain: light
    entity_id: light.rechter_lamp_gang_beneden
  - type: turn_off
    device_id: 4e6944df3d2d4c02820af25b2287524f
    domain: light
    entity_id: light.linker_lamp_gang_beneden
  - type: turn_off
  mode: single

I may have some errors in there as I’ve simply modified your code, but you should get the idea.

Accidentaly i put it in dutch. See underneath what i did with the help from you Guys and it works perfectly fine. :wink:

I changed it to this.

- id: '1596908154170'
  alias: Licht gang aan bij beweging
  description: ''
  trigger:
  - device_id: fa5b1a617d224b7d914bafe5804ef87a
    domain: binary_sensor
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
    platform: device
    type: turned_on
  condition:
  - after: sunset
    condition: sun
  action:
  - brightness_pct: 50
    device_id: b069ad2526d546f8b019624e780a8713
    domain: light
    entity_id: light.rechter_lamp_gang_beneden
    type: turn_on
  - brightness_pct: 50
    device_id: 4e6944df3d2d4c02820af25b2287524f
    domain: light
    entity_id: light.linker_lamp_gang_beneden
    type: turn_on
  mode: single
- id: '1598098522916'
  alias: Doe ganglichten beneden uit
  description: Wanneer er voor 3 minuten geen beweging in de gang is en het donker
    is
  trigger:
  - device_id: fa5b1a617d224b7d914bafe5804ef87a
    domain: binary_sensor
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
    platform: device
    type: turned_off
  condition: []
  action:
  - device_id: b069ad2526d546f8b019624e780a8713
    domain: light
    entity_id: light.rechter_lamp_gang_beneden
    type: turn_off
  - device_id: 4e6944df3d2d4c02820af25b2287524f
    domain: light
    entity_id: light.linker_lamp_gang_beneden
    type: turn_off
  mode: single 

It works perfectly. Thank you. :stuck_out_tongue_winking_eye:

Thank you for helping out.

I have one other question about this automation.
Very often because of bad weather the lights are going on a little bit to late.
I mean. “Put lights on after sunset” is a little bit to late for example for reading so it should help when i change the automation to "put lights on after sunset but for example “10 minutes before sunset” What do i need to change in the automation.
Thanks for helping out.

Anybody who can help out?

Are you using multisensors which have motion and lux? If so, use the light level in the room rather than sun elevation. It’s much better in my opinion.

Try :

- id: '1596908154170'
  alias: Licht gang aan bij beweging
  description: ''
  trigger:
  - device_id: fa5b1a617d224b7d914bafe5804ef87a
    domain: binary_sensor
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
    platform: device
    type: turned_on
  condition:
  - below: '5'
    condition: numeric_state
    entity_id: sun.sun
    value_template: '{{ state_attr(''sun.sun'', ''elevation'') }}'
  action:
  - brightness_pct: 50
    device_id: b069ad2526d546f8b019624e780a8713
    domain: light
    entity_id: light.rechter_lamp_gang_beneden
    type: turn_on
  - brightness_pct: 50
    device_id: 4e6944df3d2d4c02820af25b2287524f
    domain: light
    entity_id: light.linker_lamp_gang_beneden
    type: turn_on
  mode: single
- id: '1598098522916'
  alias: Doe ganglichten beneden uit
  description: Wanneer er voor 3 minuten geen beweging in de gang is en het donker
    is
  trigger:
  - device_id: fa5b1a617d224b7d914bafe5804ef87a
    domain: binary_sensor
    entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
    platform: device
    type: turned_off
  condition: []
  action:
  - device_id: b069ad2526d546f8b019624e780a8713
    domain: light
    entity_id: light.rechter_lamp_gang_beneden
    type: turn_off
  - device_id: 4e6944df3d2d4c02820af25b2287524f
    domain: light
    entity_id: light.linker_lamp_gang_beneden
    type: turn_off
  mode: single 

Hi. I don´t use the motion sensor with motion and Lux from fibaro, but i do not use the lux yet.
How can i change the script to use the lux instead of sunset.
How much lux should be the case if you define lights on for example for reading?
Thanks for helping out.

Thanks i will try this.

here is a package I use which works really well. The light has 3 modes, ON, OFF and AUTO. Auto being motion based, but only when the light level in the room is below the set point. I use input_numbers so that the lux value can easily be adjusted, but once it’s set I never really need to change it.

#  All code relating to the functioning of the kitchen light

input_number:
  kitchen_light_auto_off_time:
    name: Off after
    min: 1
    max: 9
    step: 1
    icon: mdi:camera-timer
    unit_of_measurement: min
  kitchen_min_lux:
    name: On below
    min: 0
    max: 30
    step: 1
    icon: mdi:brightness-6
    unit_of_measurement: lux
automation:
  - alias: 'Kitchen Remote Button 1'
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: single
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: double
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d0002241b37
          click_type: long_click_press
    action:
      - service_template: >
          {% if trigger.event.data.click_type == 'single' %}
            script.kitchen_button_single_click
          {% elif trigger.event.data.click_type == 'double' %}
            script.kitchen_button_double_click
          {% elif trigger.event.data.click_type == 'long_click_press' %}
            script.kitchen_button_long_press
          {% endif %}

          
  - alias: 'Kitchen light auto ON motion'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        from: 'off'
        to: 'on'
    condition:
      condition: or
      conditions:
      - condition: template
        value_template: "{{ states('sensor.kitchen_multi_sensor_luminance') | int < states('input_number.kitchen_min_lux') | int }}"
      - condition: state
        entity_id: light.kitchen_light_level
        state: 'on'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_t_timer


  - alias: 'Kitchen light auto off'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.kitchen_multi_sensor_sensor
        from: 'on'
        to: 'off'
    action:
      - service: script.turn_off
        entity_id: script.kitchen_light_auto_off_t_timer
      - service: script.turn_on
        entity_id: script.kitchen_light_auto_off_t_timer

script:
  kitchen_button_single_click:
    alias: Kitchen button single click
    sequence:
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
    - service: script.turn_off
      entity_id: script.kitchen_light_auto_off_t_timer
    - service: switch.turn_off
      data:
        entity_id: switch.circadian_lighting_kitchen_circadian_lighting
    - service: light.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 255
  kitchen_button_double_click:
    alias: Kitchen button double click
    sequence:
    - service: automation.turn_off
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
    - service: light.turn_off
      data: 
        entity_id: light.kitchen_light_level
  kitchen_button_long_press:
    alias: Kitchen button long press
    sequence:
    - service: switch.turn_on
      data:
        entity_id: switch.circadian_lighting_kitchen_circadian_lighting
    - service: automation.turn_on
      data:
        entity_id: 
          - automation.kitchen_light_auto_on_motion
          - automation.kitchen_light_auto_off
  kitchen_light_auto_off_t_timer:
    alias: Kitchen light off after preset time
    sequence:
    - delay: '00:0{{ states.input_number.kitchen_light_auto_off_time.state | int }}:00'
    - condition: state
      entity_id: light.kitchen_light_level
      state: 'on'
    - service: homeassistant.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 60
    - delay: '00:00:05'
    - service: homeassistant.turn_on
      data:
        entity_id: light.kitchen_light_level
        brightness: 30
    - delay: '00:00:05'
    - service: homeassistant.turn_off
      data:
        entity_id: light.kitchen_light_level

With the recent additional functionality added to HA in the last few versions I actually need to revise the above code because it can be simplified, but I’m waiting for HA 0.115 to drop because there are more awesome features coming which I’m quite excited about.