Automation - Check door open continuously, not just on state change

Hi all,

I have an automation checking the state of a door sensor on my Office door and when left open for 60 seconds the aircon is turned off to save power.

My issue with this is it only checks on the door state change, not a continuous check of the state every few seconds or something. This means if I open the door, then turn the aircon on, the aircon never turns off because the door never changes from closed to open.

I have done some searching, and the closest option I have found is to use time_pattern. However this is out of sync from when the door opens. Meaning if someone opens the door briefly for a question, sometimes the time_pattern check is at 58/60 seconds and the aircon turns off two seconds later.

Any suggestions would be greatly appreciated, thank you in advance!

It depends a lot on what kind of device your door sensor is. For example, I use Fibaro and Ecolink door sensors, both are Z-Wave, and both have settings for the Z-Wave device itself for how often it reports its various status (open/close state, temperature, battery, etc) and I can tweak that setting to send an update regardless of change.

If you have a battery operated device and don’t have a configuration to alter the frequency in which it sends updates to HA then it will be hard to do. What you might be able to do is use other sensors to augment your indication of if the door is opened or if the room is otherwise occupied, such as motion sensors or mmwave sensors. You could also monitor how long your aircon has been on and add a trigger for “if the door doesn’t change in the next 30 minutes then do something”.

I think that unless you can change your device to send reports regardless of state changes you will probably need to include other logic or sensors to try to figure it out. You could also change your door sensor from whatever it is to a reed switch plus an ESP device, that way you have much more control over knowing when it opens or closes. It adds a bit of bulk to your solution but the results might be more stable if this is an important sensor for you.

Another sensor that might be a good back up is a vibration sensor set to very high sensitivity. I like the ThirdReality sensors for this, that way you have a form of backup that lets you know that something happened with the door.

I have an esp32 and reed sensor set up monitoring multiple doors in the house including this one. It is configured through esphome and sends an update each time the state changes.

You may have a good thought there modifying the esp to send a integer value to home assistant for seconds_open, only turning off the aircon when it reaches 60. I’ll see how it goes and report back

HA does not “pull” sensor states. Sensors “push” them, and HA updates its internal state as soon as any new state arrives. If the door sensor isn’t sending a new state, there’s nothing HA can do about that.

Explain what you are trying to do, and what sensors (including their entity IDs) you have available.

Automations that use a time_pattern are rarely the correct way to solve a problem.

You can easily write an automation to turn aircon off if the door has been open for 60 seconds. Just explain your requirements and the entities you have.

1 Like

Continuous checking isn’t needed if you take all changes into account that would require you to turn the airco off.

Your automation should be two triggers: door open x:xx time and airco turning on.
It should also have two conditions: door open xx:xx time and airco is on

By doing it this way no polling is required and reactions are instant.

Personally I would not automate it like this either way, as it does not allow manual override. If some one turns the airco on while the door is open, then tere must be a reason for it. Turning it off immediately is the HA equivalent of “Computer says no”. It would make my family members angry pretty quickly.

1 Like

What you have described is a common situation and the common solution is to use multiple triggers (one for each monitored event) along with multiple ANDed conditions (one for each monitored event).

Basic Example:

alias: example 
trigger:
  - platform: state
    entity_id: binary_sensor.door
    from: 'off'
    to: 'on'
    for:
      seconds: 60
  - platform: state
    entity_id: switch.ac
    from: 'off'
    to: 'on'
condition:
  - condition: state
    entity_id: binary_sensor.door
    state: 'on'
  - condition: state
    entity_id: switch.ac
    state: 'on'
action:
   ... your actions ...

The example can be enhanced to support additional requirements.

Personally I would not automate it like this either way, as it does not allow manual override. If some one turns the airco on while the door is open, then tere must be a reason for it. Turning it off immediately is the HA equivalent of “Computer says no”. It would make my family members angry pretty quickly

I do have a manual override switch that does exactly this, although I love the auto off automation so rarely use the override. I am the only one that uses the office anyway, or so it should be lol.

You can easily write an automation to turn aircon off if the door has been open for 60 seconds. Just explain your requirements and the entities you have.

My requirements are:

  1. If the door state is open (on state change or on most recent state update) then turn off the aircon no more than 60 seconds later.
  2. If the door is open and the aircon is turned on even after an hour since, turn off the aircon no more than 60 seconds later.

No need for the aircon to turn on again.

I am using an esp32 and esphome with a binary_sensor which reports the door state back to HA, probably in somewhat similar fashion to a zigbee door sensor.

Here is my esphome yaml: (Some details such as wifi removed)

esphome:
  name: officedoor
  friendly_name: Office Door

esp32:
  board: esp32dev
  framework:
    type: arduino

binary_sensor:
  - platform: gpio
    pin:
      number: 27
      mode: INPUT_PULLUP
      inverted: True
    name: "Office Door State"  
    filters:
      - delayed_on : 100ms

@123 gave you en examplein the post above that runs the automation if the door was open for 60 seconds (exactly). I would not put the delay in the esp, you’d want HA to know when the door opens immediately, and then measure time.

Ah forgive me, took too long to compose my reply and missed that. Thanks @123, however my yaml understanding is lacking.

Is this checking the state of the aircon?

  - platform: state
    entity_id: switch.ac
    from: 'off'
    to: 'on'

That’s a State Trigger so it’s detecting when someone turns on a switch. I don’t know how your AC unit is modeled in Home Assistant; I used a switch but maybe it’s climate for your AC unit.

This is a State Condition and checks if the switch is on

  - condition: state
    entity_id: switch.ac
    state: 'on'

The concept is that for each trigger there’s a matching condition. So if the door is opened the State Trigger detects it and then checks if the door is open (yes, it was just opened) AND if the AC is on. Similarly if someone turns on the AC, the State Trigger detects it and checks if the AC is on (yes, it was just turned on) AND if the door is open.

The only way the actions are executed is if the door is open and the AC is on. This is achieved without the use of a Time Pattern Trigger constantly checking the state of the door or AC unit.