Turn off lights after 5 minutes if there is no motion

I’m using the example that was posted here Turn on lights on and keep them on when motion detection and it has been working great for me.
You could also look at using the new Timer component Timer Docs which may be a little easier.

Hi,

Thanks for all the help I ended up doing like this.

  • id: Office_on_off
    alias: Lys Kontor PÅ
    trigger:
    platform: state
    entity_id: binary_sensor.fibaro_motion_sensorr_01
    to: ‘on’
    action:
    service: homeassistant.turn_on
    entity_id: switch.tplink_hs100_stikk, switch.lys_kontor

  • alias: Lys Kontor AV
    trigger:
    platform: state
    entity_id: binary_sensor.fibaro_motion_sensorr_01
    to: ‘off’
    for: ‘00:03:00’
    action:
    service: homeassistant.turn_off
    entity_id: switch.tplink_hs100_stikk, switch.lys_kontor

This seems to work. It feels a bit slow but it can have something to do with my currant loaction of the RPI.

Thanks for great help. This forum and community is just the best. Every one wants to help and share :slight_smile:

1 Like

Is there a way to do this with the automations editor yet? I’m just doing a first time setup with home assistant, and was going to try keep all my automations in the automations editor.

I have a bunch of motion sensors around the house. I can use them to turn lights on, but can’t figure out how to set the delay to switch them back off.

Can I use the state attributes? I see one called “no motion since” listed which has a value in seconds.

Is it working now because you used "homeassistant.turn_on instead of “light.turn_on”?

Maybe an issue asking the light service to turn on a switch?

“service: light.turn_on
entity_id: switch.tplink_hs100”

This is how without the automation configurator.

automation kitchen_light_movement_off:
  alias: Turn off kitchen lights when there is no movement
  trigger:
    platform: state
    entity_id: binary_sensor.Family_Room_Motion
    to: 'off'
    for:
      minutes: 10
  action:
    - service: light.turn_off
      entity_id: light.kitchen_light

Rob,

Yeah, I can do it that way, but was looking to keep it all in the automations editor. Would be good if there was the ability to reference the attributes, or to add that for: time command

I know I’m a bit late to this party, but thought I should share in case someone else has a similar use-case…I have zigbee light switches controlling the outside lights and SR501 PIR sensors to detect movement outside (via ESP8266 ESPHome). I wanted the exterior lights to act as security lights normally except if I turn on the lights manually, as then I don’t want any motion detection automation switching them off. My solution based on the great contributions in this thread using 2 automations are as follows. The off automation is normally in a disabled state.

- id: '1618811753220'
  alias: Turn on gym patio lights on motion detection
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.gym_patio_motion
    from: 'off'
    to: 'on'
  condition:
  - condition: or
    conditions:
    - condition: sun
      before: sunrise
    - condition: sun
      after: sunset
  - condition: state
    entity_id: light.gym_patio_lights
    state: 'off'
  action:
  - service: automation.turn_on
    target:
      entity_id: automation.turn_off_gym_patio_lights_on_motion_detection_ended
  - service: light.turn_on
    target:
      entity_id: light.gym_patio_lights
  mode: single
- id: '1618989416766'
  alias: Turn off gym patio lights on motion detection ended
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.gym_patio_motion
    to: 'off'
    for: 00:01:00
  condition:
  - condition: state
    entity_id: light.gym_patio_lights
    state: 'on'
  action:
  - service: light.turn_off
    target:
      entity_id: light.gym_patio_lights
  - service: automation.turn_off
    target:
      entity_id: automation.turn_off_gym_patio_lights_on_motion_detection_ended
  mode: single

Happy for feedback / comments to improve etc.

Key features:

  1. I can use the lights as normal exterior lights. When I turn them on, they stay on and the motion detection/security light feature becomes disabled
  2. If I’m not using them as general party lighting, they automatically become securty lights, lighting up if there’s motion in their vicinity.
1 Like

If you’re interested, the two automations can be consolidated into one.

It requires that you create input_boolean.gym_patio_lights to serve as an indicator (to keep track of who turned on the light, namely you or the automation).

- id: 'auto_gym_patio_lights'
  alias: Automatic gym patio lights
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.gym_patio_motion
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.gym_patio_motion
    to: 'off'
    for: '00:01:00'
  action:
  - choose:
    - conditions:
      - "{{ trigger.to_state.state == 'on' }}"
      - "{{ is_state('sun.sun', 'below_horizon') }}"
      - "{{ is_state('light.gym_patio_lights', 'off') }}"
      sequence:
      - service: homeassistant.turn_on
        target:
          entity_id:
          - light.gym_patio_lights
          - input_boolean.gym_patio_lights 
    - conditions:
      - "{{ trigger.to_state.state == 'off' }}"
      - "{{ is_state('light.gym_patio_lights', 'on') }}"
      - "{{ is_state('input_boolean.gym_patio_lights', 'on') }}"
      sequence:
      - service: homeassistant.turn_off
        target:
          entity_id:
          - light.gym_patio_lights
          - input_boolean.gym_patio_lights 

EDIT

Correction. Change states to is_state.

5 Likes

Really great, thanks for sharing :pray::star_struck:

I did exactly as you posted, but it is not working. I can’t figure out what I am doing wrong. Any help please?

alias: "Lights: Bathroom Automation Complex"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.p1_motion_ias_zone
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.p1_motion_ias_zone
    to: "off"
    for: "00:02:00"
action:
  - choose:
      - conditions:
          - "{{ trigger.to_state.state == 'on' }}"
          - "{{ states('sun.sun', 'below_horizon') }}"
          - "{{ is_state('light.bathroom', 'off') }}"
        sequence:
          - service: homeassistant.turn_on
            target:
              entity_id:
                - light.bathroom
                - input_boolean.bathroom_lights
            data: {}
      - conditions:
          - "{{ trigger.to_state.state == 'off' }}"
          - "{{ is_state('light.bathroom', 'on') }}"
          - "{{ is_state('input_boolean.bathroom_lights', 'on') }}"
        sequence:
          - service: homeassistant.turn_off
            target:
              entity_id:
                - light.bathroom
                - input_boolean.bathroom_lights
            data: {}

Check the automation’s trace.

It says no action taken, like one of the conditions, was impossible to meet.

If I remove the Sun and input_bolean, it works. With one or the other, it doesn’t. But to me, the automation looks perfect. It all makes sense.

Is there anywhere else I can find some details?

It’s expecting the sun to be below the horizon and the input_boolean to be off.

Is the input_boolean actually off now and has the sun set in your location? If not, then the conditions won’t evaluate to true.

So, when I am testing, I change the sun elevation value (above or below the horizon) dependent on day/night time.
Regarding the input_boolean, how exactly do you configure it? I created the helper, assigned a name, and used it in the automation (so it’s related to the automation), but how does it change status when the lights get turned on by the switch or from another source? How can you relate it?

Look closely at the automation and you’ll see it sets the input_boolean’s state.

I see that. Like I see that the automation got triggered. It just doesn’t meet the conditions but the conditions exist.

Regarding the input_boolean, the whole use of it is not to take actions in case the value is in a state that is not triggered by the automation. Like I turned the lights manually, and I don’t want the automation to interfere. But how does the status of that specific input_boolean change when I manually turn on the lights?

Hey @123 , now I got it how that works. Thanks.

I removed - "{{ states('sun.sun', 'below_horizon') }}" and it works, what do you think is the issue if the sun elevation is certainly true?

I see the problem now. The template’s function is incorrect.

Change it to:

          - "{{ is_state('sun.sun', 'below_horizon') }}"

I’ve corrected the example posted above.

1 Like

Oh man, that’s it. Thank you. Lesson learned.

I notice that even if I manually turn on the light via switch the timer will eventually switch them off.

Is there anything I should change?