Motion Sensor Check - Help with Coding

Hi all,

I have an automation that turns off my garage lights when the doors are closed for 10 minutes. I have a motion sensor that doesn’t quite hit the entire garage and most times, if you are going out the back door, it will not see you and not trigger motion. The problem I am having is that I want the lights to stay on if I am in the main part of the garage and motion is detected but since I can slip out the backdoor undetected, the lights aren’t being turned off because there was no motion stopping in the last 10 minutes.

I am migrating over from Smartthings and it had a way to do ‘turn off the light if no motion was detected in the last X minutes’. What I am seeing here is only the option to say ‘turn off lights if motion has changed to not detected in the last X minutes’.

Any help or suggestions? I have been ChatGPT’ing the heck out of this one and it hasn’t been able to give me a solution

Please post your automation configuration, properly formatted. It really helps us understand how you are trying to do what you are trying to do… and find the source of an issue.

I think you may be misinterpreting how triggers and conditions work with duration. The “duration”/for configuration option used in triggers and conditions set the minimum amount of time the entity must have been in that state, without changing.

It’s not “… not detected in the last X minutes”

  • For trigger events it’s: “… changed to not detected (and stayed that way) for X minutes”
  • For conditions it’s: “… not detected for, at least, the last X minutes”
alias: Garage Lights Smart Auto-Off
description: ""
triggers:
  - entity_id:
      - light.garage_lights
    to: "on"
    trigger: state
    for:
      hours: 0
      minutes: 0
      seconds: 5
conditions:
  - condition: device
    device_id: 9971ba8e052e69bad2bfe7b8f1c98975
    domain: cover
    entity_id: c6d706e620a1b6e020395baeb3a4b714
    type: is_closed
  - type: is_not_open
    condition: device
    device_id: 3d785e35db1b0880385f59e1d36b4539
    entity_id: 1a17fa3465b60a10ba587093483436f3
    domain: binary_sensor
  - type: is_not_moving
    condition: device
    device_id: 4b8dd42de761699b8a5bcbcd49dadccf
    entity_id: e6029841b2788f3df9c26e4ee0b39780
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 10
actions:
  - target:
      entity_id: light.garage_lights
    action: light.turn_off
    data: {}
  - target:
      entity_id: 7d6f3dadaaacc10bd407c7cd33685cc7
    action: switch.turn_off
    data: {}
mode: restart

OK that seems to be formatted correctly.

The challenge here is that, say first thing in the morning, that motion sensor hasn’t seen anything move in at least 12 hours, so if I say ‘no movement in the last 12 hours’, that will cause the lights to turn off just fine until it does detect movement, then the lights will stay on until it’s been 12 hours since the last motion, correct?

This is also part of a much larger automation. Let me break it down:

I have a door leading to the garage from the house, a side door and the big 2 car garage door. I also have that motion detector.

I want the light to turn on when the interior garage door opens but only if the other two doors are closed since we like to watch football in the garage in the evening and smoke cigars and I don’t want the light blasting on when the door are open to allow the smoke to escape.

The side door is right where my trash cans are, so we frequently open the interior door, go out the side door for a second and then go back inside. This is way out of the view of the motion sensor. I want the motion sensor to see me moving around the garage and keep the lights on when I am in there BUT I don’t want the lights to turn on when motion is detected when any of the doors open.

This worked perfectly in Smartthings since it had the ‘no motion detected in the last x minutes’ thing you could add.

Been on HA for about 2 months now and I this is the one automation I cannot get to work the way I want it to. Everything else I have figured out, we’ll me and ChatGPT but using that has helped me understand how that code works!

Thanks in advance I appreciate the help!

I’m not sure how this hypothetical fits into what you’ve posted. If it is meant as a replacement for the motion condition in the provided automation… then “no movement for 12 hours” will not cause anything to happen since it is not a trigger. The only event that will cause that specific automation to turn the light off is the light being on for 5 seconds. Conditions are only checked immediately after the trigger. The automation does not wait for them to come true… if they aren’t all true in that instant the automation goes back to listening for the light to turn from “off” to “on” and stay “on” for 5 seconds.

Is there a better way I need to go about the flow of what I am trying to accomplish then? Take a peek the longer post on what I am trying to do with this space. I have been steady at this one for at least a week, trying several different ways to get it done. I’ve even tried to break it into several different automations:

To Turn on The Light When the Interior Door Opens

alias: Garage Lights On
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.centralite_3320_l_3
    from: "off"
    to: "on"
conditions:
  - condition: state
    entity_id: binary_sensor.centralite_3320_l
    state: "off"
  - condition: state
    entity_id: cover.garage_door_controller
    state: closed
actions:
  - type: turn_on
    device_id: 90c4c51e6783435fc4c35154e41bc040
    entity_id: f0b437f689575ae9e007bc0ce0f67275
    domain: light
    brightness_pct: 100
mode: single

Turn on the light when the side and big garage door open:

alias: Garage Lights On Exterior Doors Open
description: ""
triggers:
  - type: opened
    device_id: 3d785e35db1b0880385f59e1d36b4539
    entity_id: 1a17fa3465b60a10ba587093483436f3
    domain: binary_sensor
    trigger: device
  - device_id: 9971ba8e052e69bad2bfe7b8f1c98975
    domain: cover
    entity_id: c6d706e620a1b6e020395baeb3a4b714
    type: opened
    trigger: device
conditions: []
actions:
  - type: turn_on
    device_id: 90c4c51e6783435fc4c35154e41bc040
    entity_id: f0b437f689575ae9e007bc0ce0f67275
    domain: light
mode: single

One to try and turn on the light when there is motion (because the light keeps turning off when I am in there but out of range of the motion sensor)

alias: Garage Motion On
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.centralite_3326_l
    from: "off"
    to: "on"
conditions:
  - condition: state
    entity_id: cover.garage_door_controller
    state: closed
  - condition: state
    entity_id: binary_sensor.centralite_3320_l
    state: "off"
actions:
  - type: turn_on
    device_id: 90c4c51e6783435fc4c35154e41bc040
    entity_id: f0b437f689575ae9e007bc0ce0f67275
    domain: light
mode: single

It’s driving me nuts

Well, I have a new source for AI help… Claude. He got it going the way I needed it. I had done the same kind of trouble shooting with ChatGPT but the wait template was never something that it came up with. Thanks Claude

Now to clean up the other automations…

Thanks for looking. I do appreciate the help and time!

alias: Garage Light Control
description: Turn on garage lights when door sensor opens, turn off after 10 minutes if no motion
trigger:
  - platform: state
    entity_id: binary_sensor.centralite_3320_l_3
    from: "off"
    to: "on"
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.centralite_3320_l
        state: "off"
      - condition: state
        entity_id: cover.garage_door_controller
        state: "closed"
action:
  - service: light.turn_on
    target:
      entity_id: light.garage_lights
  - delay: "00:10:00"
  - wait_template: >-
      {{ (now() - states.binary_sensor.centralite_3326_l.last_changed).total_seconds() >= 300 }}
  - service: light.turn_off
    target:
      entity_id: light.garage_lights
mode: restart