Here is one way to detect if a person is entering or exiting a door. It seems to be working pretty well at this point so I thought I would share. This assumes you have a door contact to determine when a door is opened or closed, and a motion sensor to determine when a person is on the inside of the door. So, when the door opens from the inside when there is already motion detected, the sensor will post a passage attribute ‘exiting’, but when the door opens from the outside (before motion is detected on the inside), the sensor will post a passage attribute ‘entering’. There is an obvious flaw in this logic, that if something on the inside triggers the motion sensor while a person is ‘entering’, this sensor will post this passage as an ‘exiting’ attribute. So you need to be careful to use a motion sensor that is not likely triggered unless someone is using the door. Hope this helps someone else.
template:
- trigger:
- platform: state
entity_id: binary_sensor.side_door_contact
not_from:
- unknown
- unavailable
variables:
door_state: "{{trigger.to_state.state}}"
last_changed: "{{ trigger.to_state.last_changed.isoformat() }}"
motion_state: "{{ states('binary_sensor.mudroom_zone_motion') }}"
binary_sensor:
- name: Side Door Passage
device_class: opening
state: "{{ door_state }}"
attributes:
passage: |-
{% set current = this.attributes.get('passage', {}) %}
{% if door_state == 'on' %}
{{ 'entering' if motion_state == 'off' else 'exiting' }}
{% else %}
{{ current }}
{% endif %}
last_opened: |-
{% set current = this.attributes.get('last_opened', {}) %}
{{ last_changed if door_state == 'on' else current }}
last_closed: |-
{% set current = this.attributes.get('last_closed', {}) %}
{{ last_changed if door_state == 'off' else current }}