Doors, Windows, the Gate: Contact Sensors, Left-Open Openings, and Home Modes

Doors, Windows, the Gate: Contact Sensors, Left-Open Openings, and Home Modes

Module 18 · Lesson 6

Doors, Windows, the Gate: Contact Sensors, Left-Open Openings, and Home Modes

After the lessons on flooding and life-safety hazards, we're back to a scenario where Home Assistant works really well: reminding you about an open door, reacting to your home mode, and logging events. A contact sensor won't stop a burglar: but it will tell you a door is open when it should be closed.

You're not building a professional monitored alarm system here. You're building a layer of reminders and contextual response: the same open window means a reminder in Home mode, and an alarm with escalation in Away mode.

Plan on about 90 to 105 minutes. You'll start with one contact sensor on the front door and a home-mode helper: you'll add the rest of the openings later without rewriting the logic.

The rule for this lesson
Not every opening is a critical alarm.
Context decides: home mode, time of day, and how long something's been open.

A real-life problem: the door's open, and you're already at work

You left this morning. Halfway to work, you're not sure you locked the front door. Or you left a bedroom window cracked open: it rains that evening. Or at 11:00 PM somebody left the garage gate open. A contact sensor won't fix anything on its own, but Home Assistant can remind you, or: in Away mode: trigger a full alarm with the escalation from Lesson 3.

This is a different category from flooding or smoke. Here, conditions and delays make sense. A door open for 30 seconds while you carry in groceries is normal. The same door open for 20 minutes in Away mode: is not.

Openings versus flooding and smoke: different response logic

Aspect Flooding / smoke (Lessons 4-5) Openings (Lesson 6)
Urgency Immediate response Depends on context
Conditions in the automation For smoke/CO/gas: no filters Home mode, time of day, delay: yes
Notification type Always script.intrusion_alarm A reminder or an alarm: depends on the scenario
HA's role Limiting damage or an extra life-safety layer Reminders, mode logic, a supporting alarm

What's actually the risk

An alarm while leaving the house: you set Away, open the door: and get an escalation because there's no exit delay.

Every opening triggers a full alarm: the household turns off notifications after three false alerts a day.

No home mode: you don't know if anyone's home, so you treat every opening the same way.

Inverted contact-sensor logic: the entity shows "closed" while the door is open.

A contact sensor as the only protection against intrusion: the magnet won't stop an intruder or call for help.

A forgotten Away mode: you're back home, but the system still thinks you're out.

Home Assistant is not a burglar alarm system
Contact sensors tell you whether something is open. They don't replace a lock, video monitoring, or professional security. HA helps you notice a problem sooner and respond according to your home mode: that's it.

Which sensors to choose: contact sensors

A contact sensor is a pair of parts: a sensor on the frame and a magnet on the door or window sash. When the magnet moves away from the sensor, the entity switches to on (open) or off (closed): it depends on the integration. Always verify this by hand after mounting.

Location Priority Mounting notes
Front door High: start here Magnet and sensor aligned, small gap
Ground-floor windows High A tilted window also counts as open
Garage gate / garden gate Medium-high Check whether you have a separate gate entity or just a contact sensor
Patio / balcony Medium During rain and wind: a preview of Lesson 7

Which entities we're watching

Entity Meaning
binary_sensor.front_door Front door: on = open
binary_sensor.living_room_window Living room window
binary_sensor.garage_gate Garage gate
binary_sensor.garden_gate Garden gate
group.critical_openings A group of doors, windows, and gates to monitor
input_boolean.leaving_home_in_progress 90 seconds to leave after setting Away mode: blocks the alarm
input_select.home_mode Mode: Home, Night, Away, Guests

Check the opening logic before you automate
In the example we assume on means open and off means closed. Yours might be reversed, or the entity might have a different device_class. Open and close the door by hand before wiring up the alarm.

Home mode: context for the automation

The input_select.home_mode helper tells the system how to treat events. In Lesson 8 you'll extend modes with motion and presence: for now, a simple manual choice on the dashboard is enough.

Home: someone's home: openings are normal, timed reminders.

Night: the house is asleep: quieter notifications, more sensitive door alerts.

Away: the house is empty: a critical opening = an alarm with escalation.

Guests: someone new is in the house: less aggressive alerts, more reminders.

Three levels of response

Scenario Response Script
Door open 5+ minutes in Home mode A calm reminder script.notify_household
Door open in Night mode An urgent notification script.notify_household_urgent
Gate open during night hours An urgent notification script.notify_household_urgent
An opening in Away mode (after the exit delay) A full alarm with escalation script.intrusion_alarm

Helpers, a group, and home mode

input_select:
  home_mode:
    name: "Home mode"
    options:
      - "Home"
      - "Night"
      - "Away"
      - "Guests"
    initial: "Home"
    icon: mdi:home-account

input_boolean:
  away_opening_test:
    name: "Away-mode opening test"
    icon: mdi:door-open

  leaving_home_in_progress:
    name: "Leaving home in progress"
    icon: mdi:exit-run

group:
  critical_openings:
    name: "Critical openings"
    entities:
      - binary_sensor.front_door
      - binary_sensor.living_room_window
      - binary_sensor.garage_gate
      - binary_sensor.garden_gate

leaving_home_in_progress gives you 90 seconds to leave calmly after setting Away mode. Without this helper, opening the door on your way out will trigger the alarm: and you'll quickly stop trusting the whole system.

One group to start, separate groups later
At first you can use a single critical_openings group. In a bigger house, it's better to split doors, gates, and windows, since each has a different meaning: a door in Away mode is an alarm, a window during rain is a weather reminder, and a gate after 10 PM is a separate nighttime alert.

away_opening_test simulates the alarm scenario without physically opening a door: set the mode to Away, wait for the exit delay to pass, turn on the helper, and check the escalation. Return to Home mode after the test.

Automation 1: a door or window open too long

Instead of delay, we use for: in the trigger: each opening gets its own 5-minute timer, and mode: parallel means one window's wait doesn't cancel another's.

automation:
  - alias: "Security: door or window open too long"
    description: "A reminder after 5 minutes open in Home or Guests mode"
    mode: parallel
    triggers:
      - trigger: state
        entity_id:
          - binary_sensor.front_door
          - binary_sensor.living_room_window
        to: "on"
        for:
          minutes: 5
    conditions:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_select.home_mode
            state: "Home"
          - condition: state
            entity_id: input_select.home_mode
            state: "Guests"
    actions:
      - action: script.notify_household
        data:
          title: "Open too long"
          message: >
            {{ state_attr(trigger.entity_id, 'friendly_name') or trigger.entity_id }}
            has been open for at least 5 minutes.

Automation 2: exit delay after switching to Away mode

This is the lesson's most important automation. Without it, the Away-mode alarm will trigger the moment you leave the house yourself.

automation:
  - alias: "Security: delay after enabling Away mode"
    description: "Gives you time to leave calmly after setting Away mode"
    mode: restart
    triggers:
      - trigger: state
        entity_id: input_select.home_mode
        to: "Away"
    actions:
      - action: input_boolean.turn_on
        target:
          entity_id: input_boolean.leaving_home_in_progress
      - delay:
          seconds: 90
      - action: input_boolean.turn_off
        target:
          entity_id: input_boolean.leaving_home_in_progress
      - if:
          - condition: template
            value_template: >
              {{ expand('group.critical_openings')
                 | selectattr('state', 'eq', 'on')
                 | list
                 | count > 0 }}
        then:
          - action: script.notify_household_urgent
            continue_on_error: true
            data:
              title: "Home not fully secured"
              message: >
                Away mode is active, but at least one entry point
                is still open. Check the dashboard.

Resetting the exit helper after an HA restart

The leaving_home_in_progress helper can come back as on after a restart and block opening alarms. Reset it at startup. Also, for: 5 minutes in a trigger doesn't survive a restart either: the open-door timer starts over from zero.

automation:
  - alias: "Security: reset exit helper after HA startup"
    mode: single
    triggers:
      - trigger: homeassistant
        event: start
    actions:
      - action: input_boolean.turn_off
        target:
          entity_id: input_boolean.leaving_home_in_progress

The garden gate is often used regularly by a courier or guests: consider giving it its own category with different thresholds than the front door and ground-floor windows.

Automation 3: an opening in Away mode

automation:
  - alias: "Security: opening in Away mode"
    description: "Alarm when something opens while the house is empty"
    mode: single
    triggers:
      - trigger: state
        entity_id:
          - binary_sensor.front_door
          - binary_sensor.living_room_window
          - binary_sensor.garage_gate
          - binary_sensor.garden_gate
        to: "on"
        id: "real_opening"

      - trigger: state
        entity_id: input_boolean.away_opening_test
        to: "on"
        id: "test"

    conditions:
      - condition: state
        entity_id: input_select.home_mode
        state: "Away"
      - condition: state
        entity_id: input_boolean.leaving_home_in_progress
        state: "off"

    actions:
      - action: script.turn_on
        target:
          entity_id: script.intrusion_alarm
        data:
          variables:
            title: >
              {% if trigger.id == 'test' %}
              TEST: opening in Away mode
              {% else %}
              ALARM: opening in Away mode
              {% endif %}
            message: >
              {% if trigger.id == 'test' %}
              This is an Away-mode opening test. Check the escalation and acknowledgment.
              {% else %}
              {{ state_attr(trigger.entity_id, 'friendly_name') or trigger.entity_id }}.
              The house is in Away mode.
              {% endif %}

      - choose:
          - conditions:
              - condition: trigger
                id: "test"
            sequence:
              - delay:
                  seconds: 2
              - action: input_boolean.turn_off
                target:
                  entity_id: input_boolean.away_opening_test

At this stage, we're not yet handling an entry delay for when you return home. If you come back and open the door while the mode is still set to Away, the alarm can trigger. In Lesson 8, we'll come back to switching modes automatically based on presence.

Automation 4: resetting the test outside Away mode

If you trigger the test in Home mode, the conditions will stop the alarm: but the helper will stay on. This automation turns it off and explains the mistake.

automation:
  - alias: "Security: reset opening test outside Away mode"
    description: "Turns off the test helper if triggered in the wrong mode"
    mode: single
    triggers:
      - trigger: state
        entity_id: input_boolean.away_opening_test
        to: "on"
    conditions:
      - condition: not
        conditions:
          - condition: state
            entity_id: input_select.home_mode
            state: "Away"
    actions:
      - delay:
          seconds: 2
      - action: input_boolean.turn_off
        target:
          entity_id: input_boolean.away_opening_test
      - action: script.notify_household
        data:
          title: "Opening test canceled"
          message: "The Away-mode opening test requires the home mode to be set to Away."

Automation 5: the gate open at night

automation:
  - alias: "Security: gate open at night"
    description: "An urgent notification about an open gate during night hours"
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.garage_gate
        to: "on"

      - trigger: time
        at: "22:00:00"

    conditions:
      - condition: state
        entity_id: binary_sensor.garage_gate
        state: "on"
      - condition: or
        conditions:
          - condition: time
            after: "22:00:00"
          - condition: time
            before: "06:00:00"

    actions:
      - action: script.notify_household_urgent
        data:
          title: "Gate open at night"
          message: >
            {{ state_attr('binary_sensor.garage_gate', 'friendly_name') or 'Garage gate' }}
            is open during night hours.

The 10:00 PM time trigger also catches a gate that was already open earlier: not just one that opened after 10 PM.

Automation 6: a door open in Night mode

automation:
  - alias: "Security: door open in Night mode"
    description: "An urgent notification when the door opens at night"
    mode: single
    triggers:
      - trigger: state
        entity_id: binary_sensor.front_door
        to: "on"
    conditions:
      - condition: state
        entity_id: input_select.home_mode
        state: "Night"
    actions:
      - action: script.notify_household_urgent
        data:
          title: "Door open at night"
          message: >
            {{ state_attr('binary_sensor.front_door', 'friendly_name') or 'Front door' }}
            was opened in Night mode.

Automation 7: check windows after switching to Away

When you switch to Away mode, the system reminds you about open windows: before you leave. This isn't an alarm, just a checklist.

automation:
  - alias: "Security: open windows after leaving"
    description: "Checks openings when switching to Away mode"
    mode: single
    triggers:
      - trigger: state
        entity_id: input_select.home_mode
        to: "Away"
    actions:
      - action: script.notify_household
        data:
          title: "Away mode: check openings"
          message: >
            {% set open_items = expand('group.critical_openings')
               | selectattr('state', 'eq', 'on')
               | map(attribute='name') | list %}
            {% if open_items | length > 0 %}
            Still open: {{ open_items | join(', ') }}. Close them before you leave.
            {% else %}
            All critical openings are closed. Have a good trip.
            {% endif %}

Don't copy the entities from this lesson
Swap the sensor list for your own entities. One contact sensor on the front door is enough to start: you'll add the rest to the group later.

Dashboard: openings and home mode

type: entities
title: Openings and home mode
entities:
  - input_select.home_mode
  - input_boolean.leaving_home_in_progress
  - group.critical_openings
  - input_boolean.away_opening_test
  - input_boolean.intrusion_alarm_acknowledged

Away mode has to be visible
As long as you're setting home mode by hand, show it very clearly on the dashboard and your phone. If you come home and forget to switch back to Home, a normal door opening can trigger an alarm with escalation. We'll add presence in Lesson 8, but at this stage, that responsibility sits with you.

Keep home_mode at the top of your security dashboard. You can also show the leaving_home_in_progress helper: when it's on, you know you still have a moment to leave without triggering the alarm.

How to test, step by step

1. Open and close the door by hand. Check that the entity's state matches reality.

2. In Home mode, leave the door open for 5+ minutes. Check for the reminder from script.notify_household.

3. Switch to Away with a window still open: check the opening checklist.

4. While in Away mode, open the door within 90 seconds: the alarm should not trigger.

5. Wait for leaving_home_in_progress to go back to off. Turn on away_opening_test and check the escalation.

6. Click the test in Home mode: the helper should turn off with a cancellation message.

7. After testing, return to Home mode and close every opening shown on the dashboard.

An offline contact sensor needs monitoring too
An unavailable entity doesn't mean "closed." In Lesson 10, we'll come back to monitoring battery levels and sensor state. With openings, a false sense of security is just as dangerous as it is with flooding.

Common mistakes

A full alarm on every opening: in Home mode, you wake the household every evening.

A forgotten Away mode after coming home: every opening triggers escalation.

No exit delay after switching to Away: an alarm during a normal exit from the house.

delay instead of for: on "open too long": a new opening cancels the previous window's timer.

Inverted on/off logic: the system thinks the door is closed while it's standing open.

HA as the only protection against intrusion: a contact sensor is information, not physical security.

What not to do

Don't treat a contact sensor as a professional monitored alarm system.

Don't call script.intrusion_alarm on every opening in Home mode.

Don't leave Away mode active once you're back home.

Assignment: openings and home mode

To do

☐ Pair a contact sensor on the front door and check its on/off logic.

☐ Create input_select.home_mode, the critical_openings group, and the away_opening_test and leaving_home_in_progress helpers.

☐ Add the "open too long" automation with a for: 5 minutes trigger.

☐ Add a 90-second exit delay after switching to Away mode.

☐ Add the Away-mode opening automation with the leaving_home_in_progress = off condition.

☐ Add the nighttime gate alert, the Night-mode door alert, and the opening checklist for Away mode.

☐ Test the exit: set Away, open the door within 90 seconds: the alarm doesn't trigger.

☐ Once the delay passes, run away_opening_test and check the escalation.

☐ Update the risk map from Lesson 1: which doors, windows, and gates you have covered with sensors.

Key takeaways

Context decides: the same opening is a reminder or an alarm.

The exit delay protects you from an alarm during a normal exit.

home_mode is the foundation of the logic: manual for now, automatic in Lesson 8.

Not every opening calls the security alarm: reminders have their own scripts too.

A contact sensor informs: it doesn't replace a lock or a security system.

What's next

In Lesson 7, we'll connect openings with the weather: rain and an open window, wind and a skylight, frost by a cracked-open bathroom window. Delays and conditions come back there too: but in a different context than intrusion.

If you have a home mode, an openings group, and three levels of response, Home Assistant is starting to think contextually: exactly the way it should for everyday home security.

Finished this lesson?