---
title: "Logbook"
description: "Spook adds an action that hands an automation what the logbook shows, like who unlocked the front door in the last hour, with who or what caused each entry."
canonical_url: "https://spook.boo/logbook"
---
# Logbook

> Spook adds an action that hands an automation what the logbook shows, like who unlocked the front door in the last hour, with who or what caused each entry.

The logbook integration in Home Assistant keeps a diary of your home: the door unlocked, the lights went on, an automation ran. Where it knows, it says who or what did it: a person, an automation, a script, or an action. You find it under **Logbook** in the sidebar.

The logbook is for reading, in the logbook panel or in a logbook card. An automation cannot ask it anything. "Who unlocked the front door in the last hour?" is a question the logbook answers on screen, and not in a notification. Spook adds an action that hands an automation the same entries, as a response. An [assistant](https://spook.boo/assistants) with Spook's tools can read the logbook too, with `spook__timeline`.

## Actions

Spook adds one action to the logbook. You find it wherever you add an action, marked with a 👻.

### Get logbook entries

Gets what the logbook shows for one or more entities in a stretch of time, newest first, like the logbook panel lists them. Each entry says when, what happened, and who or what caused it, where the logbook knows.

Think of a notification when the front door is unlocked while you are away, saying who did it. Or a daily summary of how often the garage door opened. The logbook knows, and now your automation does too.

Spook reads the logbook the way the logbook panel does, through the logbook itself, so you get the same entries. It is not an admin action: anyone who can see the logbook panel can use it.

**How to use it**

1. Open the automation or script that needs the entries, and select **Add action**.
2. Search for **logbook entries**, and select **Logbook: Get logbook entries 👻**.
3. Under **Entities**, choose the entities to read the logbook of, like the front door lock.
4. Set **Duration** to how far back to read, like an hour. Leave it empty for the last day.
5. Under **Response variable**, give the entries a name, like `logbook`. The steps after this one can use it.
6. Select **Save**.

Just want to see it work? [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=logbook.get_events) opens it in your Home Assistant, ready to run once.

**Technical details**

| Action properties |
| --- |
| Action | Logbook: Get logbook entries 👻 |
| Action name | `logbook.get_events` |
| Action targets | No targets |
| Action response | Yes, the logbook entries of the entities |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=logbook.get_events) [![Open your Home Assistant instance and show the Actions tool with a specific action selected.](https://my.home-assistant.io/badges/developer_call_service.svg)](https://my.home-assistant.io/redirect/developer_call_service/?service=logbook.get_events) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `entity_id` | string \| list of strings | Yes | `lock.front_door` |
| `start_date_time` | datetime | No | A day before the end |
| `end_date_time` | datetime | No | Now |
| `duration` | time period | No | `{"hours": 1}` |
| `limit` | integer | No | `100` |

The stretch of time to read works like this:

- Nothing given: the last day.
- A `duration` alone: that long back from now. `{"hours": 1}` is the last hour.
- A `start_date_time` and a `duration`: from the start on, for that long.
- A `start_date_time` alone: from the start until now.
- An `end_date_time` alone: the day before it.

A week is the most it reads at once, and asking for more is refused rather than cut short. The logbook reads every entry of the entities in the stretch of time before Spook can leave any out, in one of the few threads the recorder reads the database with, the same ones the logbook and history pages use. A week of a few entities is quick, also on a big database. A month of a busy one would hold up the history graphs of everyone looking.

At most `limit` entries come back, the most recent ones, and a thousand at most. The response holds:

| **Attribute** | **Type** | **Description** |
| --- | --- | --- |
| `start` | string | The start of the stretch of time read. |
| `end` | string | The end of the stretch of time read. |
| `total` | integer | How many entries the logbook has in that stretch of time, also the ones over the limit. |
| `entries` | list | The entries, newest first. |

Every entry has the same fields, `null` where the logbook does not say:

| **Attribute** | **Type** | **Description** |
| --- | --- | --- |
| `when` | string | When it happened, in Home Assistant's own time zone. |
| `entity_id` | string or `null` | The entity it is about. |
| `name` | string or `null` | The name of the entity, or of the entry. |
| `state` | string or `null` | The state it changed to, like `unlocked`. The logbook panel words it in your language; this is the state itself. |
| `message` | string or `null` | What happened, for an entry that is not a change of state, like an automation that was triggered. |
| `domain` | string or `null` | The integration that wrote an entry that is not a change of state. |
| `context_user_id` | string or `null` | The ID of the user who did it. |
| `user` | string or `null` | Who did it: the name of their person, or their user name when they have no person. |
| `by` | mapping or `null` | What caused it, where the logbook knows: an `automation`, a `script`, an `action` like `lock.unlock`, another `entity_id`, or an `event`. |

**Example actions in YAML**

Who unlocked the front door in the last hour, as a notification:

```yaml
actions:
  - action: logbook.get_events
    data:
      entity_id: lock.front_door
      duration:
        hours: 1
    response_variable: logbook
  - variables:
      unlocks: >-
        {{ logbook.entries | selectattr('state', 'eq', 'unlocked') | list }}
  - condition: template
    value_template: "{{ unlocks | count > 0 }}"
  - action: notify.mobile_app_phone
    data:
      title: Front door
      message: >-
        {% for entry in unlocks %}
        {{ as_datetime(entry.when).strftime('%H:%M') }}:
        {% if entry.user %}{{ entry.user }}
        {% elif entry.by and entry.by.automation %}automation {{ entry.by.name }}
        {% elif entry.by and entry.by.script %}script {{ entry.by.name }}
        {% else %}the lock itself{% endif %}
        {% endfor %}
```

An unlock with a key or a keypad has no user in Home Assistant, and no automation or script behind it: the lock did it itself, and the notification says so.

**Known limitations**

- A week is the most it reads at once.
- Sensors that measure all the time, like a temperature, are never in the logbook. Asking for one is refused, and the error says so, rather than handing back nothing every time. The same goes for an entity the recorder or the logbook is set to leave out.
- One of those in a list of entities refuses the whole call, and no entries come back. Home Assistant's own logbook leaves it out without a word; Spook would rather you knew.
- The logbook only has what the recorder keeps: 10 days, unless you changed that.
- The `state` is the state itself, like `unlocked`, not the sentence the logbook panel makes of it.
- An entity that no longer exists is read, not refused: the logbook keeps its entries.

## Use cases

What people use this for:

- Who unlocked the front door, sent along in the notification that it is unlocked.
- How often the garage door opened today, in an evening summary.
- Whether an automation turned the heating on, or a person did, before deciding to turn it off again.


## Sitemap

See the full [sitemap](https://spook.boo/sitemap.md) for all pages.
