Tracking of lock entry

HA newbie here. I have some locks connected by zwave. They fire events like " fired Notification CC ‘notification’ event ‘Access Control’: ‘Keypad unlock operation’". So a few questions on this:

  1. Is there any way to configure the logbook so that it shows more details about the events? I’d love to be able to click in and see which user unlocked the door (which is part of the raw event)
  2. I’d like to store some sort of record of the last x times a lock was unlocked with keypad, and which user ID did it. Can anyone point me to documentation or examples of how to create a widget that would do such a thing and that could show up on a dashboard? I feel like I need to set up an event listener, filtered to the particular type of event from particular devices, but I don’t know after how to store this, and then how to access it from some sort of dashboard widget.

What lock are you using?

Its a Baldwin, but identifies as a Kwikset 916, as I think they make the internal hardware/software.

If I turn event tracing for ‘*’ I can see the lock events that I want to peer into. Just wondering what is the most effective way to build a listener that will store a few things and then surface them to the dashboard. Sorry if this is obvious, I’m a beginner and only starting at customizing.

Perhaps a job for a trigger-based template sensor. You can store pretty much anything in the attributes of a template sensor. And a trigger-based template sensor keeps it state and attributes across restarts.
Here’s an example I put together with what I learned from @123 from this post and this post. This will create an attribute called “log” which has the last 10 events and captures the pertinent data I extracted from my own Kwikset 888 lock. You will have to change the parameters to suit your device.

  - trigger:
      - platform: event
        event_type: zwave_js_notification
        event_data:
          device_id: your_device_id_goes_here
          label: Access Control
    sensor:
      - name: "Front Door Lock Event"
        state: "{{now() | as_local }}"
        unique_id: front_door_lock_event
        attributes:
          log: >
            {% set log = this.attributes.get('log', []) %}
            {% if trigger.event.data.parameters.userId is defined %}
              {% set user = trigger.event.data.parameters.userId %}
            {% else %}
              {% set user = 'None' %}
            {% endif %}
            {% if trigger.event.data.event_label is defined %}
              {% set new = [{
                  "event": trigger.event.data.event_label,
                  "user": user,
                  "time": (trigger.event.time_fired | as_local).strftime('%Y-%m-%dT%H:%M:%S%z') }] %}
              {% set log = new + log %}
            {% endif %}
            {% if 0 <= 10 < log|count %}
              {% set log = log | rejectattr('time', 'eq', log[10].time) | list %}
            {% endif %}
            {{ log }}
1 Like

I believe you can reduce the template to this:

- trigger:
      - platform: event
        event_type: zwave_js_notification
        event_data:
          device_id: your_device_id_goes_here
          label: Access Control
    sensor:
      - name: "Front Door Lock Event"
        state: "{{now() | as_local }}"
        unique_id: front_door_lock_event
        attributes:
          log: >
            {% set log = this.attributes.get('log', []) %}
            {% set new = [{
               "event": trigger.event.data.event_label | default ('unknown', true),
               "user": trigger.event.data.parameters.userId | default ('None', true),
               "time": (trigger.event.time_fired | as_local).isoformat() }] %}
            {{ (new + log)[:10] }}
1 Like

Wow thank you both so much for this. I will play with it and see if I can get it to work, I think trigger-based template sensor is the magic formula I was looking for.