Door sensor automation dont work

Hi!

I am trying to do an automation, that does the following:

When the door is closed and there is no motion for some time it turns off a lamp (but it wonr turn off for at least some minimum time has passed).

Here is my yaml code (i did this with the gui):

type or paste code herealias: Turn lamp off when door is closed
description: ""
trigger:
  - type: not_opened
    platform: device
    device_id: xx
    entity_id: xxx
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: and
    conditions:
      - type: is_no_motion
        condition: device
        device_id: xx
        entity_id: xxx
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 20
      - type: is_not_open
        condition: device
        device_id: xx
        entity_id: xxx
        domain: binary_sensor
        for:
          hours: 0
          minutes: 0
          seconds: 40
action:
  - type: turn_off
    device_id: xx
    entity_id: xxx
    domain: light
mode: single 

But its not working, when i close the door it turn off the lamp immediately.

I think i dont see some obvious, but what eludes me?

Thanks is advance!

Your binary sensor could be registering motion after the door is closed, which means your automation conditions will pass.

Look at the automation trace and you’ll be able to confirm if this is indeed the case.

Conditions don’t wait. Your automation should do this, assuming the second condition is the same device as the trigger:

  • door closes, trigger
  • no motion for 20s? Let’s say yes.
  • door closed for 40s? No, it closed less than 1s ago.
  • end without action

Try to use entity triggers, conditions and actions rather than device versions.

Okay, and how can i make it work?

What are the entity IDs (should start binary_sensor. and light.) for the door sensor, motion sensor and light? I think you want two triggers: no motion for 20s and door closed for 40s, with matching conditions so that the action only runs when the second criterion becomes true — but I don’t trust device conditions.

Making up your entity IDs:

trigger:
  - platform: state
    entity_id: binary_sensor.your_door
    to: 'off'
    for: '00:00:40'
  - platform: state
    entity_id: binary_sensor.your_motion
    to: 'off'
    for: '00:00:20'
condition:
  - condition: state
    entity_id: binary_sensor.your_door
    state: 'off'
    for: '00:00:40'
  - condition: state
    entity_id: binary_sensor.your_motion_sensor
    state: 'off'
    for: '00:00:20'
action:
  - service: light.turn_off
    target:
      entity_id: light.your_light

Conditions are AND by default.

Yes, i did exactly the same. I put my sensor ids to the code and still not good. It works, but it turns off the lamp immediately, its like it skips the conditions. The trigger works, the action work, but the conditions is not checked before the action.

I experimented with a delay. I made a simple automation that if the door is closed, after that there is a 10 sec delay and than it turns off the lamp, which works. But why the above mentioned code is not working?

There should be something obvious which im not seeing :slight_smile:

The trigger should fire 40s after the door closes and 20s after motion stops. If the trigger works, then the lamp doesn’t go off “immediately” when the door is shut. It goes off immediately after the trigger fires, but that’s a time after the last physical change as defined.

If it does turn off immediately after the door shuts, please look at the logbook to check you don’t have another automation doing this; and if not, please post the full automation trace for the latest run where it “turned off immediately”; as well as the code for your automation even if you think it’s the same as above.

Yes, sorry i made a mistake, it works.

But in your code the triggers and the conditions are the same, if i omit the conditions i will achieve the same result, wont i? So its like HA wont check the conditions.

The “matched pairs” means that the action will run as soon as both are true.