Door open/close detection

I want to create an automation that detects when a door is open for more than a certain amount of time…, and when the door is closed again, it should detect it again.
But if the door opens and closes again within 45 seconds, nothing should happen.

So
Door open for more than 45 sec than a trigger, then door close and after 10 min another trigger.
Door open for less than 45 sec and then closed again → no trigger at all.

I thought I could make this simple automation, but it turns out not to be as simple as I thought.
Now I’m stuck because I don’t know how to proceed.

Any help is much appreciated.

- alias: test
  description: 'door open/close detection'

  trigger:
    - platform: state
      entity_id: binary_sensor.door_contact
      from: 'off'
      to: 'on'
      for: '00:00:45'
    - platform: state
      entity_id: binary_sensor.door_contact
      from: 'on'
      to: 'off'
      for: '00:10:00'

I think you should only use the door open for trigger and then use “wait for trigger” in the actions for the second part. But I’m not sure.

1 Like

I’m a bit confused by what you want to do. If the door opens for more than 45 seconds, do you want something to happen?

The first trigger that you have written will fire after the door is open for 45 seconds, and then move into your automation actions. The second trigger will fire after the door has been closed for ten minutes, regardless of how long it was open for.

If you have multiple triggers, the automation will fire if any one of them happens.

It probably is as simple as you think it is, you’re just doing it wrong. :slight_smile:

What I want to accomplish is when I open the door for let’s say 10 sec and then close it again, no trigger is fire.
When the door stay open for more the 45 sec the trigger fire and only then when the door is closed again the second trigger will fire.

For example:
When I open the door and leave it open, then after 45 sec a light turns on. Then, when I close the door, 10 minutes later the light turns off. (This is the easy part)
The difficult part…:
When I open the door and directly close it, the light is not turning on or off. No trigger at all.

I hope this is more clearly now?

- alias: test
  description: 'door open/close detection'
  trigger:
    - platform: state
      entity_id: binary_sensor.door_contact
      from: 'off'
      to: 'on'
      for: '00:00:45'
    - platform: state
      entity_id: binary_sensor.door_contact
      from: 'on'
      to: 'off'
      for: '00:10:00'
action:
    - service: 'light.turn_{{ trigger.to_state.state }}'
      target:
        entity_id: light.your_light

Turns on the light only if the door is open for a minimum of 45 seconds.
Turns off light after the door has been closed for a minimum of 10 minutes.

1 Like

Create a helper called Door Left Open with type toggle.

Create an automation that looks like the following

Triggers

Trigger Type = Device
Set your sensor for door opened
Duration = 45 seconds

Actions

Action Type = Condition
Condition Type = State
Entity = input_boolean.whatever_you_called_your_helper
State = on

Add another action
Wait for Trigger
Device = Your contact sensor on closed state

Add another action
Wait for time to pass
Set time to 10 minutes

Add another action
Action Type = Condition
Condition Type = State
Entity = input_boolean.whatever_you_named_your_helper
state=off

Then just create another automation that looks at input_boolean.whatever_you_named_your_helper, if it’s on, turn on the light, if it’s off, turn it off.

You in theory could just set the light to come on and off from within the automation, but having the input boolean allows you to expand on it should you wish to going forwards with things like casting to a chromecast that the doors still open if that input boolean is on for more than 5 minutes or whatever.

alias: Door Left Open - Test
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 000000000000
    entity_id: binary_sensor.front_door_sensor_contact
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 45
      milliseconds: 0
condition: []
action:
  - condition: state
    entity_id: input_boolean.door_left_open
    state: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 0
  - wait_for_trigger:
      - type: not_opened
        platform: device
        device_id: 0000000000000
        entity_id: binary_sensor.front_door_sensor_contact
        domain: binary_sensor
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - condition: state
    entity_id: input_boolean.door_left_open
    state: 'off'
mode: single

This is the yaml from my automation, if it’s not what you were after, by all means try and explain again. As from what I could fathom, you want the light on if the doors open for 45 seconds, then for 10 minutes after the door has been closed, but nothing to happen if the door has just been opened then closed in less than 45 seconds.

First, I want to thank everyone for the time spent to read my posts and the answers to it.
Very appreciated.

The example I gave, switching the light, was to simplify what I have in mind.

The door switch must become part of the automation that control my heater.

In practice:
I have to open the living room door to let my cats go in or out the house.
When the heater is on and the door opens and closes within 45 seconds, the heater must stays on.
When the heater is on and the door opens for a longer time, more than 45 seconds, the heater must go off. Only then, when the door closes again, it should take 10 minutes before the heater turns back on.
I don’t want the door open for a “long” time and the heater stays on. Waste of energy and money.

Below my creation of the automation which work already, but without the door control.

- alias: CV Heater Control
  description: 'door open/close detection, 20on/15off timer'
  mode: restart

  trigger:
    - platform: state
      entity_id: climate.kamerthermostaat
      attribute: hvac_action
      to: heating
      id: heat
    - platform: time
      at: input_datetime.cv_heater_off
      id: 'off'
    - platform: time
      at: input_datetime.cv_heater_on
      id: 'on'
#    - platform: state
#      entity_id: binary_sensor.tuindeur_contact
#      from: 'off'
#      to: 'on'
#      for: '00:00:45'
#      id: 'off'
#    - platform: state
#      entity_id: binary_sensor.tuindeur_contact
#      from: 'on'
#      to: 'off'
#      for: '00:10:00'
#      id: 'on'
    - platform: homeassistant
      event: start
      id: 'on'

  action:
    - choose:
        - conditions: '{{ trigger.id == "heat" }}'
          sequence:
            - service: input_datetime.set_datetime
              target:
                entity_id: input_datetime.cv_heater_off
              data:
                timestamp: '{{ utcnow().strftime("%s") | int + (20*60) }}'

    - choose:
        - conditions:
            - '{{ trigger.id == "off" }}'
            - '{{ state_attr("climate.kamerthermostaat", "hvac_action") == "heating" }}'
          sequence:
            - service: input_datetime.set_datetime
              target:
                entity_id: input_datetime.cv_heater_on
              data:
                timestamp: '{{ utcnow().strftime("%s") | int + (15*60) }}'

    - choose:
        - conditions:
            - condition: or
              conditions:
                - '{{ trigger.id == "on" }}'
                - condition: and
                  conditions:
                    - '{{ trigger.id == "off" }}'
                    - '{{ state_attr("climate.kamerthermostaat", "hvac_action") == "heating" }}'
          sequence:
            - service: climate.turn_{{ trigger.id }}
              target:
                entity_id: climate.kamerthermostaat

For future reference, don’t do that. The requirements you listed in your most recent post should have been in your first post.

Withholding requirements only serves to waste everyone’s time solving an artificial, “dumbed-down” problem while you actually have something more complex in mind. Solutions for the simplified version don’t always translate well to more sophisticated requirements.

BTW, this:

{{ utcnow().strftime("%s") | int + (20*60) }}

can be reduced to this with no loss of functionality:

{{ now().timestamp() + (20*60) }}

FWIW, the post you marked as being the Solution is a longer way of doing it compared to shorter versions suggested to you. There’s no burning need to employ wait_for_trigger to fulfill the original requirements.

Please tell me what the suggested shorter version is?
Without wait_for_trigger?
I’m open for suggestions and to learn from the master chief, that’s you. :slight_smile:

If you tried the example I posted, which part of it didn’t work as you expected? It’s designed to do this:

  • Turns on the light only if the door is open for a minimum of 45 seconds.
  • Turns off light after the door has been closed for a minimum of 10 minutes.

I tried the example you posted, but it didn’t work the way as I expected.
When I open and immediately closes the door and the climate control is in “off” state, it will turn to the “on” state after 10 minutes despite the automation want it in the “off” state.

Below the complete automation I use now with the timestamp modification you suggested.

- alias: CV Heater Control
  description: 'door open/close detection, 20on/15off timer'
  mode: restart

  trigger:
    - platform: state
      entity_id: climate.kamerthermostaat
      attribute: hvac_action
      to: heating
      id: heat
    - platform: time
      at: input_datetime.cv_heater_off
      id: 'off'
    - platform: time
      at: input_datetime.cv_heater_on
      id: 'on'
    - platform: state
      entity_id: binary_sensor.tuindeur_contact
      from: 'off'
      to: 'on'
      for: '00:00:45'
      id: 'off'
    - platform: state
      entity_id: binary_sensor.tuindeur_contact
      from: 'on'
      to: 'off'
      for: '00:10:00'
      id: 'on'
    - platform: homeassistant
      event: start
      id: 'on'

  action:
    - choose:
        - conditions: '{{ trigger.id == "heat" }}'
          sequence:
            - service: input_datetime.set_datetime
              target:
                entity_id: input_datetime.cv_heater_off
              data:
                timestamp: '{{ now().timestamp() + (20*60) }}'

    - choose:
        - conditions:
            - '{{ trigger.id == "off" }}'
            - '{{ state_attr("climate.kamerthermostaat", "hvac_action") == "heating" }}'
            - '{{ trigger.entity_id != "binary_sensor.tuindeur_contact" }}'
          sequence:
            - service: input_datetime.set_datetime
              target:
                entity_id: input_datetime.cv_heater_on
              data:
                timestamp: '{{ now().timestamp() + (15*60) }}'

    - choose:
        - conditions:
            - condition: or
              conditions:
                - '{{ trigger.id == "on" }}'
                - '{{ trigger.entity_id == "binary_sensor.tuindeur_contact" }}'
                - condition: and
                  conditions:
                    - '{{ trigger.id == "off" }}'
                    - '{{ state_attr("climate.kamerthermostaat", "hvac_action") == "heating" }}'
          sequence:
            - service: climate.turn_{{ trigger.id }}
              target:
                entity_id: climate.kamerthermostaat

The example I posted doesn’t refer to a climate entity. It’s meant to address the requirements you listed in your first post. That’s what others who read the first post expect the rest of the thread to focus on. That’s why I said your first post should contain precisely what you want and not a watered down version of it.

What you did was take my example and attempt to adapt it to work with different requirements. You need to improve the conditions in choose to correctly handle this new arrangement. Or revert to using wait_for_trigger. Either way, good luck.