
This article may contain affiliate links. If you buy through these links, the price is the same for you and the store pays me a small commission that helps keep Tecnoyfoto running.
You have reached the final chapter with concepts you already know: YAML structures, entities, triggers, conditions, sequences, choose, scripts, run modes, templates, variables, and includes. The final project will not add a flashy feature just to force another topic into the course. It will ask you to do something more important: combine what you have learned and justify every decision.
A solution is not finished simply because it turns on a light. It must be readable, behave correctly when events occur close together, handle unavailable data, and leave enough evidence for someone else to reproduce the result.
We will build the project first and then compare your decisions with a reference solution. Try not to open the solution files until you have at least written your design in plain language.
The project brief
We will use the virtual entities installed at the beginning of the course. The home must respond to entry, presence, motion, and temperature.
Entry through the front door
When the door opens:
- If it is nighttime and someone is home, a reusable welcome routine will run: turn on the living room light and create a welcome message.
- If it is night and no one is home, the light will remain off, and an entry notification will indicate that the house is empty.
- During the day, the automation will do nothing.
When the door closes and no one is home, a notification will record that it was closed. This branch is not a security system; it merely demonstrates that one automation can distinguish between opening and closing.
Motion-controlled lighting
At night, motion in the hallway will turn on the light. The light will remain on while motion is present and will turn off thirty seconds after motion ends.
If motion is detected again while the previous sequence is waiting, the timer must restart. We do not want two independent countdowns competing with each other.
Temperature-controlled fan
When the room temperature changes:
- At 25 °C or above, turn on the fan.
- Below 25 °C, turn it off.
- If the state is not usable, do not change the fan and create a warning.
The temperature must be read only once per run and stored before any decision is made.
Remove ambiguity before writing YAML
Requirements for the home almost always contain hidden decisions. Here are several in this project:
What does “nighttime” mean? The practice environment provides binary_sensor.course_night, which is controlled manually. In a real installation, we could use the sun, a time range, or a helper. For this project, we will use the practice environment entity and will not mix criteria.
What does “someone is home” mean? We will use binary_sensor.course_someone_home. We will not try to infer presence from motion or the door.
What happens if the door opens and motion is detected almost simultaneously? Both sequences may turn on the same light. Turning on a light that is already on is safe. The motion automation will normally be responsible for turning it off. The welcome script will add a safety shutoff after five minutes, but only if the sensor has reported no motion for at least thirty seconds at that point.
That final rule was not fully specified. We made it explicit so that a welcome with no subsequent motion cannot leave the light on indefinitely, while still preserving the motion automation’s thirty-second grace period. Another valid solution would be to create a helper that represents why the light should remain on. That design would be more precise, but it would add complexity we do not need in this course.
Turn requirements into a decision table
Before writing code, define paths you can verify:
| Event | Nighttime | Presence | Outcome |
|---|---|---|---|
| Door changes to open | Yes | Yes | Run welcome routine |
| Door changes to open | Yes | No | Entry notification |
| Door changes to open | No | Either | Do nothing |
| Door changes to closed | Either | No | Closure notification |
| Motion starts | Yes | Either | Turn on, wait for motion to end and 30 s, turn off |
| Motion starts | No | Either | Do nothing |
| Temperature changes | — | — | Decide only if the value is valid |
The table reveals gaps before they disappear into the indentation. It will also become our test plan.
Divide the project by responsibilities
We could write one enormous automation with all the triggers. That would work, but each behavior has a different run cycle:
- Entry reacts to two door states and selects a branch.
- Motion waits and must restart when new activity occurs.
- Temperature reacts to one reading and makes a safe decision.
We will create three automations and a script:
scripts.yaml
└── course_welcome_home
automations/manual/final-project.yaml
├── course_final_entry
├── course_final_motion_light
└── course_final_temperature
This separation does not follow a “one file per automation” rule. We group all three automations because they form one small, cohesive project, but each retains its own id, alias, and mode.
Step 1: the welcome script
The welcome routine is a named sequence that another automation or the dashboard can call:
course_welcome_home:
alias: "YAML Course - Welcome"
sequence:
- action: light.turn_on
target:
entity_id: light.course_living_room
- action: persistent_notification.create
data:
title: "YAML Course - Welcome"
message: "The door has opened and someone's home."
- delay:
minutes: 5
- condition: state
entity_id: binary_sensor.course_hallway_motion
state: "off"
for:
seconds: 30
- action: light.turn_off
target:
entity_id: light.course_living_room
mode: restart
The condition inside the sequence is evaluated after the five-minute delay. It requires the sensor to be off and to have remained that way for thirty seconds. If motion is present or has just ended, the condition returns false and stops the script before the light is turned off. It does not wait another thirty seconds; it checks how long the entity has already been in that state.
We chose restart so that a new welcome cancels the previous countdown and starts a new one. In a real home, we should also consider whether a persistent notification is the right channel. Here, it lets us observe the result without configuring a phone.
Step 2: an entry automation with trigger IDs
The door has two relevant events. We identify them and let choose select the first compatible branch:
- id: "course_final_entry"
alias: "YAML Course - Final project - Entrance"
triggers:
- trigger: state
entity_id: binary_sensor.course_front_door
from: "off"
to: "on"
id: "opened"
- trigger: state
entity_id: binary_sensor.course_front_door
from: "on"
to: "off"
id: "closed"
conditions: []
actions:
- choose:
- conditions:
- condition: trigger
id: "opened"
- condition: state
entity_id: binary_sensor.course_night
state: "on"
- condition: state
entity_id: binary_sensor.course_someone_home
state: "on"
sequence:
- action: script.turn_on
target:
entity_id: script.course_welcome_home
- conditions:
- condition: trigger
id: "opened"
- condition: state
entity_id: binary_sensor.course_night
state: "on"
- condition: state
entity_id: binary_sensor.course_someone_home
state: "off"
sequence:
- action: persistent_notification.create
data:
title: "YAML Course - Entrance"
message: "The door opened at night with the house empty."
- conditions:
- condition: trigger
id: "closed"
- condition: state
entity_id: binary_sensor.course_someone_home
state: "off"
sequence:
- action: persistent_notification.create
data:
title: "YAML Course - Closed door"
message: "The door's closed and no one's home."
mode: single
We do not add default. When the door opens during the day, the desired result is to do nothing. Its absence is an explicit decision, not a forgotten branch.
We use script.turn_on because the entry automation only needs to start the welcome routine; it should not wait five minutes for the script to finish. The automation can therefore finish immediately and respond when the door closes again. The script itself uses mode: restart, so another welcome restarts its countdown without keeping the calling automation busy.
This decision revisits a distinction from chapter 7: a direct call to script.course_welcome_home would wait for the script to finish, while script.turn_on starts an independent run.
Step 3: motion controls its own countdown
This automation reuses the pattern of chapter 6:
- id: "course_final_motion_light"
alias: "YAML Course - Final project - Motion-controlled light"
triggers:
- trigger: state
entity_id: binary_sensor.course_hallway_motion
from: "off"
to: "on"
conditions:
- condition: state
entity_id: binary_sensor.course_night
state: "on"
actions:
- action: light.turn_on
target:
entity_id: light.course_living_room
- wait_for_trigger:
- trigger: state
entity_id: binary_sensor.course_hallway_motion
from: "on"
to: "off"
timeout:
minutes: 10
continue_on_timeout: false
- delay:
seconds: 30
- action: light.turn_off
target:
entity_id: light.course_living_room
mode: restart
If motion appears again during the final thirty seconds, the trigger fires again. restart stops the previous run, turns on the light—even if it is already on—and waits again for the sensor to change to off.
The timeout prevents an unlimited wait if the sensor becomes stuck at on. Because continue_on_timeout is false, the sequence ends after ten minutes without turning off the light. This is a conservative choice: when the sensor data is questionable, we do not want to turn off the light while motion may still be present. The welcome script’s safety shutoff will not act either if the detector remains on, so the failure will be visible and require investigation.
Step 4: temperature with a consistent reading
The third automation stores the received text and whether it is valid:
- id: "course_final_temperature"
alias: "YAML Course - Final project - Fan"
triggers:
- trigger: state
entity_id: sensor.course_living_room_temperature
conditions: []
actions:
- variables:
temperature_text: >-
{{ states('sensor.course_living_room_temperature') }}
- variables:
temperature_is_valid: >-
{{ temperature_text not in ['unknown', 'unavailable'] }}
- choose:
- conditions:
- condition: template
value_template: "{{ temperature_is_valid }}"
sequence:
- variables:
temperature: "{{ temperature_text | float }}"
- choose:
- conditions:
- condition: template
value_template: "{{ temperature >= 25 }}"
sequence:
- action: switch.turn_on
target:
entity_id: switch.course_fan
default:
- action: switch.turn_off
target:
entity_id: switch.course_fan
default:
- action: persistent_notification.create
data:
title: "YAML Course - Temperature unavailable"
message: >-
The fan was left unchanged because the sensor's
state is {{ temperature_text }}.
mode: restart
The first action reads the sensor once. The second validates that same stored reading; it does not query the entity again. We convert with float only inside the branch where we already know the value is usable. The unavailable state does not silently turn into zero, and the entire decision uses one consistent temperature snapshot.
Install without replacing your files
The practice environment solution contains scripts.yaml and a manual automation file. Do not replace your real files with them.
If you follow the structure of Chapter 10, the relevant lines are:
automation ui: !include automations.yaml
automation manual: !include_dir_merge_list automations/manual
script: !include scripts.yaml
Add the course_welcome_home key to your existing script map and place the list of three automations in the manual directory. If you already use packages or another valid structure, adjust the location while preserving the expected data type.
Before reloading:
- Make a backup.
- Confirm that you have not duplicated the top-level keys.
- Validate the configuration.
- Reload scripts and automations.
- Check that all four entities—the three automations and the script—appear.
Test plan: negative paths must also pass
Run each test from a known initial state. The practice environment’s input_boolean helpers let you control the door, motion, presence, and nighttime.
Entry
- Nighttime
on, presenceon, door closed. Open the door: the light turns on and the welcome appears. - Nighttime
on, presenceoff. Open the door: the empty-home notification appears, and the welcome does not run. - Nighttime
off. Open the door: no branch runs. - Presence
off. Close the door: the closure notification appears. - Presence
on. Close the door: no empty-home notification appears. - With presence
on, open and close the door again within five minutes: the automation must process both changes because it does not wait for the script.
Movement
- At night, activate motion: the light turns on.
- Clear motion: the light remains on for thirty seconds and then turns off.
- During those thirty seconds, activate motion again: the previous countdown is canceled.
- During the day, activate motion: the automation creates a trace, but the top-level condition blocks the actions.
Temperature
- Change the value to 24.5 °C: the fan turns off.
- Change it to 25 °C: the fan turns on; verify the exact threshold.
- Change it to 27 °C: the fan remains on with no error.
- Simulate an unavailable state from States: the fan retains its state and a notification appears.
After each test, open the corresponding trace. Do not look only at the final device state: check the trigger ID, conditions, selected branch, and variable values.
How to evaluate your own solution
A good solution should let you answer yes to each question:
- Are the requirements written before the code?
- Does each automation have a recognizable responsibility?
- Are reusable sequences in scripts for a specific reason?
- Do the modes represent what should happen with overlapping runs?
- Does unavailable data have defined behavior?
- Do the files provide the list or map expected at the include point?
- Does the configuration validate, and can it be reloaded?
- Are there positive, negative, and boundary tests?
- Do the traces confirm the expected path?
- Could you explain each block without simply saying, “Home Assistant needs it”?
The reference solution is not the only correct one. You could group everything into a package, put each automation in a separate file, or use a sun condition instead of the nighttime helper in your real home. What should not change is the clear relationship between each decision and a requirement.
What you have learned and what comes next
At the beginning of the course, YAML may have seemed like the language that “makes” an automation work. Now you can separate the layers:
- YAML represents the structure.
- Home Assistant interprets this structure as entities, triggers, conditions and actions.
- Jinja calculates dynamic values within certain fields.
- The actual states and execution context determine the result.
You can also read an automation before running it, build one from a requirement, reuse a sequence, choose a mode, organize files, and diagnose a trace. That is a complete foundation for working with real configurations without relying on blocks you copied but do not understand.
More advanced topics remain: blueprints, custom macros and templates, complex events, parallel actions, action responses, calendars, MQTT, and large architectures under version control. We did not leave them out because they are unhelpful; each one deserves to be learned after the fundamentals you have just applied.
Keep the practice environment. Change one requirement at a time, predict which block you will need to modify, and repeat the test plan. When you can explain why an automation works—and determine why it does not—YAML stops being a block to copy and becomes a tool you can use with confidence.

