---
title: "Lock"
description: "Spook adds an action to the lock integration of Home Assistant that sets the default code of locks, so a dashboard or automation does not ask for it."
canonical_url: "https://spook.boo/lock"
---
# Lock

> Spook adds an action to the lock integration of Home Assistant that sets the default code of locks, so a dashboard or automation does not ask for it.

The lock integration is the one behind every smart lock in Home Assistant. Some locks want a code for every lock or unlock, and Home Assistant can keep a default code for those, so a dashboard button or an automation does not have to ask.

Home Assistant offers that setting one lock at a time, in its settings. Spook adds an action that sets it from an automation or a script, on as many locks as you like.

## Actions

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

### Set default code

Sets the code one or more locks use when an action gives none.

The same setting as **Default code** in the settings of a lock. Set it on the front door lock, and the button on your dashboard unlocks it without asking for the code every time. Set it from an automation, and take it away again when you leave the house.

A lock only uses a default code that fits its code format, like four digits. A code that does not fit, or a lock that takes no code at all, is refused, with a message that names the lock and never the code.

A code is a secret. Spook itself never writes it in a log, never repeats it in an error, and never shows it back to you. Home Assistant does keep it in a few places of its own: see the known limitations. To take a code away, set it again, empty.

**How to use it**

1. Open the automation or script that should do this, and select **Add action**.
2. Search for **default code**, and select **Lock: Set default code 👻**.
3. Under **Entity**, pick the locks.
4. Type the code under **Default code**.
5. Select **Save**.

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

**Technical details**

| Action properties |
| --- |
| Action | Lock: Set default code 👻 |
| Action name | `lock.set_default_code` |
| Action targets | No |
| Action response | No response |
| Spook's influence | Newly added action |
| Tools | [Try this action](https://my.home-assistant.io/redirect/developer_call_service/?service=lock.set_default_code) [![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=lock.set_default_code) |

| Action data parameters |
| --- |
| Attribute | Type | Required | Default / Example |
| `entity_id` | string or list | Yes | `"lock.front_door"` |
| `default_code` | string or `null` | Yes | `"1234"` |

An empty `default_code`, or `null`, takes the code away. Put a code in quotes in YAML: unquoted, a code with a leading zero is read as a number, and loses it.

Every lock you name is checked before any of them are changed. If one of them does not exist, takes no code, or the code does not fit it, the whole call fails and nothing is touched.

**Example actions in YAML**

A default code for the front door lock:

```yaml
action: lock.set_default_code
data:
  entity_id: lock.front_door
  default_code: "1234"
```

And taking it away again:

```yaml
action: lock.set_default_code
data:
  entity_id: lock.front_door
  default_code: null
```

**Known limitations**

- Only locks with a unique ID can be changed, the same ones that have settings in the UI.
- The lock has to be loaded to set a code: only the lock itself knows whether it takes one. Taking a code away works either way.
- With a default code set, anybody who can press unlock on your dashboard opens the door. Keep that in mind before you set one.
- The code is in the action data of your automation or script, and so in its traces, like anything else you write there.
- Home Assistant writes the data of every action call, the code included, in its own log when `homeassistant.core` logs at debug level, and in the recorder's database as part of the event for the call. Spook cannot keep it out of either.
- A `!secret` does not keep it out of any of these: Home Assistant reads the secret before the action runs, and from then on it is the code itself.
- Changing these settings is for admins, the same as in the UI. An automation or a script can still use it.

## Use cases

What people use this for:

- Put a default code on the front door lock so the dashboard does not ask for it, and take it away when everybody leaves.
- Set the same code on every lock in the house in one action, instead of opening each one in the UI.


## Sitemap

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