Smart door with the /execute command

In this tutorial, we’ll look at a simple way of building a smart door that automatically opens when one of the players approach it. The steps in this tutorial can be used for more projects that react to player presence, such as smart lamps.

What you should already know

  1. How to work with command blocks – read about it here
  2. How to use /fill – read about it here
  3. How to work with Minecraft coordinates
  4. How to write block names in commands


1. Preparing the build

Prepare an area in your world where’d you like to build your smart door. You can frame it with blocks. Dig a hole below the frame, this is where your commands will go.

2. Preparing the /fill command

First, we’ll look at the command we will open and close the door with. It will be the /fill command, which can fill a certain area with a specific block.

Mark the two corners of your door with blocks. What block it is doesn’t matter. To help you understand, the picture below uses two coloured blocks. Fill in their coordinates in the /fill command. Then add the identificator of the block you’d like to use for the closed door’s appearance.

/fill <first corner coordinate> <second corner coordinate> <block identifier>

Try sending the command in chat. The door should “close”, get filled with the block you selected.

Copy the finished command (scroll up with the up arrow and select all with Ctrl + A, and copy with Ctrl + C) and paste it in a command block aside, where it’ll rest for some time.

PRO TIP: You don’t have to use a forward slash in command blocks (/). Your work will be a little easier later on if you delete it now.

3. Detecting when a player is nearby

We can use the /execute command to find out whether a player is nearby. The /execute command allows us to change the way Minecraft commands are run. One of its functions are conditions – such as “Run a command only if someone approaches.”

Place down a command block below your door and open it. When you begin writing the execute command, Minecraft will hint you with some options on how to use execute. Only two options will interest us for now: if and run. We’ll start with the if option.

If modifies the command so that it runs if a condition is fulfilled. In the case of our smart door, we will detect whether a player is nearby. We will tell execute if to check for entities (which also include players).

Then, the command will have to know who to look for. You could write a friend’s username after entity, then you could write a command that would only run if your friend is connected to your world. The door should let everyone in though, so we will use a special placeholder called @a, or all players. But that would mean the command would run if it found any players, and wouldn’t check how far they are. Which is why we’ll have to write a filter.

Type an opening square bracket right after @a without a space, this will open new options. If you don’t know how to write a square bracket, it is best you find it on the internet for the layout you’re using.

A filter in Minecraft can specify what entities we’re looking for. You can think of it as “All players, but only those that…” in Minecraft form. A simple filter that we’ll find quite useful is distance. Write it after the square bracket and let Minecraft autofill an equals sign (=).

Now what remains is to write the range of blocks we’re looking at. This depends on the size of your door. We’ll use 0..5, which means “0 to 5 blocks away”.

IMPORTANT: if you only write one number instead of a range, Minecraft will only look for players exactly 5 blocks away. If you only wrote 5, the command wouldn’t react to players 4 blocks away, 3 blocks away…

/execute if entity @a[distance=0..5]

You can finish the filter by writing a closing square bracket, it should be coloured in blue.

That covers how we check if a player is nearby.

4. Putting our door in service

But what happens when a player actually approaches? You can set this with the run option, which you’ll write after your current command.

Now what’s left to do is to write the command to run when our previous condition is fulfilled. Remember the fill command that we wrote earlier? Now it’s time to make use of it. Save your unfinished command by clicking Done and return to where your fill command is. Copy it from there and paste it back in the command block, right after run.

IMPORTANT: The command written after run doesn’t include a forward slash that is normally used in other commands! If you didn’t remove it earlier, the command will be in red. You can click your mouse near the / and delete it, if you need to.

Our command checks whether someone’s standing at the door. So if someone’s standing there, the door should open. If your fill command is set to fill the area with a block, the door would close! Delete the name of the block from the command and replace it with air. That way you ensure the blocks disappear (become air).

The command is done! Don’t forget to mark the command as repeating (Switch from Impulse to Repeat, it’ll change colour to purple) and always active (Switch from Needs redstone na Always active) to eliminate the need for redstone. Save by clicking Done.

If you now stand at your door and try to build in it, the blocks will disappear. Minecraft knows that someone is standing at the door and is removing the blocks inside (your command is opening the door).

If you walk away from your door, it’ll stay open. We need to create a second command that’ll close the door. Return to your command block and duplicate it (hold Ctrl and press the mouse wheel). A copy of your command block will be placed in your inventory, you can place it somewhere else. Place the second command block next to the first.

Open this new command block. We need to edit two things. Hold the left arrow key and scroll to the beginning of the command. We must change if to unless.

The unless option works as an opposite to if; the command always runs, but not if the condition is fulfilled.

Now go back to the end of the command and change air to any block you want to use to resemble a closed door. We chose oak planks (written as oak_planks). Now, if there’s nobody standing at the door, this command will fill it with blocks.

Save the command. We’re done!

Wrapping up

The execute command is a useful way of automating things. Fill isn’t the only command you can use in /execute. You can create a smart lamp with /setblock, or a teleporter with the /tp command. All you need is an idea. Here’s a few challenges you can try:

  • Can you make a door that reacts to pigs instead of players?
  • Can you change the filter to ignore players in spectator mode?
  • Can you make an area that heals nearby players?

Leave a Reply