List of 10 last breached motion sensors/doors/windows?

The following Trigger-based Template Sensor records the name and time when a monitored binary_sensor changes state to on. It keeps the ten most recent events.

template:
  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.front_door
          - binary_sensor.kitchen_window
          - binary_sensor.garden_gate
        from: 'off'
        to: 'on'
    sensor:
      - name: Recent Openings
        state: "{{ now().timestamp() | timestamp_custom() }}"
        attributes:
          openings: >
            {% set current = this.attributes.get('openings', []) %}
            {% set new = [{
              "name": trigger.to_state.name,
              "time": now().isoformat() }] %}
            {{ (new + current)[:10] }}

You can display the recorded events using a Markdown card with the following template:

|Name||Time|
|:----|:-:|:----|
{% for x in state_attr('sensor.recent_openings', 'openings') | default([], true) -%}
  |{{x.name}}||{{x.time|as_timestamp|timestamp_custom}}|
{% endfor -%}

EDIT

Correction. Convert time string to timestamp before submitting to timestamp_custom filter.

3 Likes