Lab / Explainer / 4 steps

No. 06Explainer7 min

From sensor to automation

How a smart home decides: states, triggers, conditions and actions, tested on one cold evening, plus where an AI assistant may help.

Short version

Everything in Home Assistant is a state. An automation says when (trigger), only if (condition) and then (action). An assistant may read states and suggest changes, but heating, locks and alarms need a person to say yes.

Home Assistant runs a lot of my house: lights, radiator heads per room, presence. Under the dashboards it is a simple machine. This page uses an invented house with three rooms and one invented evening. Nothing here connects to a real home.

01 · States

Everything in the house is a state.

A temperature sensor's state is a number, a window sensor is open or closed, and the number of people at home is a state too. Home Assistant keeps the latest state of everything, notes when it last changed and remembers every change. Move the evening clock.

19:30the evening clock

1 tick is 1 change: 11 since 17:00

The house at 19:30

Living room 18.5 °C
window closed heating 17 °C
Bedroom 17.6 °C
Hall 1 at home locked

What Home Assistant keeps

StateValueLast changed
Living room temperature 18.5 °C 19:00
Bedroom temperature 17.6 °C 18:40
Living room window closed 19:20
People at home 1 18:05
Living room heating set to 17 °C 08:20
Front door locked 08:20

Newest change: 19:20: living room window becomes closed.

Show as text
TimeWhat changedLiving roomBedroomWindowPeople home
17:00Start of the evening (heating set to 17 °C, front door locked)19.4 °C18.0 °Cclosed0
17:15Living room temperature19.2 °C18.0 °Cclosed0
17:25Living room temperature (drops below 19 °C, nobody home)18.9 °C18.0 °Cclosed0
17:45Bedroom temperature18.9 °C17.8 °Cclosed0
18:05People at home (someone comes home)18.9 °C17.8 °Cclosed1
18:20Living room temperature (cooking warms the room)19.1 °C17.8 °Cclosed1
18:35Living room temperature19.3 °C17.8 °Cclosed1
18:40Bedroom temperature19.3 °C17.6 °Cclosed1
18:45Living room window (window opened for air)19.3 °C17.6 °Copen1
18:50Living room temperature (drops below 19 °C again, window open)18.8 °C17.6 °Copen1
19:00Living room temperature18.5 °C17.6 °Copen1
19:20Living room window (window closed)18.5 °C17.6 °Cclosed1

Before 17:00 the window last changed at 07:50, and the heating and the front door at 08:20. Neither the heating nor the front door changes this evening.

fig. 1 Home Assistant sees the house as a list of states. Move the clock: only the states that change get a new value and a new time, and the newest change is in pen.

Automations never look at devices. They look at states and react when a state changes.

02 · Your automation

An automation is: when, only if, then.

Build one. Pick a trigger, a condition and an action. You get the automation as one sentence, the moments it would have fired on this evening, and below the drawing the YAML that Home Assistant would store.

When
Only if
Then

Your automation

When the living room drops below 19 °C

only if someone is home

then set the living room heating to 20.5 °C

This evening, 17:00 to 19:30

Fires once, at 18:50.

  • 17:25: blocked, nobody home
  • 18:50: fires
Show as text

Every trigger and condition on this evening. The action does not change when it fires.

WhenOnly ifOn this evening
the living room drops below 19 °Cno conditionFires 2 times: at 17:25 and 18:50.
the living room drops below 19 °Csomeone is homeFires once, at 18:50. Blocked at 17:25: nobody home.
the living room drops below 19 °Cthe window is closedFires once, at 17:25. Blocked at 18:50: window open.
someone comes homeno conditionFires once, at 18:05.
someone comes homesomeone is homeFires once, at 18:05.
someone comes homethe window is closedFires once, at 18:05.
it is 17:30no conditionFires once, at 17:30.
it is 17:30someone is homeNever fires this evening. Blocked at 17:30: nobody home.
it is 17:30the window is closedFires once, at 17:30.
fig. 2 When picks the moment, only if guards it, then says what happens. The pen dots are the moments this automation would run its action this evening.

Your automation as YAML, the way Home Assistant stores it

alias: "Living room below 19 °C, someone home, heat to 20.5 °C"
description: "Example from jasperhaynes.com/lab: change the entity ids to your own"
triggers:
  - trigger: numeric_state
    entity_id: sensor.living_room_temperature
    below: 19
conditions:
  - condition: numeric_state
    entity_id: zone.home # its state is the number of people at home
    above: 0
actions:
  - action: climate.set_temperature
    target:
      entity_id: climate.living_room
    data:
      temperature: 20.5
mode: single

The entity ids are examples: change them to your own before you paste this in Home Assistant (Settings, Automations, Edit in YAML).

Three example automations as text and YAML

1. When the living room drops below 19 °C, only if someone is home, then set the living room heating to 20.5 °C. Fires once, at 18:50.

alias: "Living room below 19 °C, someone home, heat to 20.5 °C"
description: "Example from jasperhaynes.com/lab: change the entity ids to your own"
triggers:
  - trigger: numeric_state
    entity_id: sensor.living_room_temperature
    below: 19
conditions:
  - condition: numeric_state
    entity_id: zone.home # its state is the number of people at home
    above: 0
actions:
  - action: climate.set_temperature
    target:
      entity_id: climate.living_room
    data:
      temperature: 20.5
mode: single

2. When someone comes home, with no condition, then set the living room heating to 20.5 °C. Fires once, at 18:05.

alias: "Someone comes home, always, heat to 20.5 °C"
description: "Example from jasperhaynes.com/lab: change the entity ids to your own"
triggers:
  - trigger: numeric_state
    entity_id: zone.home # its state is the number of people at home
    above: 0
actions:
  - action: climate.set_temperature
    target:
      entity_id: climate.living_room
    data:
      temperature: 20.5
mode: single

3. When it is 17:30, only if someone is home, then send a message to your phone. Never fires this evening.

alias: "At 17:30, someone home, message my phone"
description: "Example from jasperhaynes.com/lab: change the entity ids to your own"
triggers:
  - trigger: time
    at: "17:30:00"
conditions:
  - condition: numeric_state
    entity_id: zone.home # its state is the number of people at home
    above: 0
actions:
  - action: notify.send_message
    target:
      entity_id: notify.my_phone # your phone
    data:
      message: "It is 17:30"
mode: single

The trigger decides the moment, the condition guards it. Most surprises come from a missing condition.

03 · Replay

Test it on a replay of one evening.

Here is the evening again, drawn to scale: the living room temperature, who is home and the window. Every version starts from the same trigger (the living room drops below 19 °C) and does the same thing (heat it); the last version adds a second trigger. Pick a version and watch which moments it lets through.

When the living room drops below 19 °C or the window closes, only if someone is home, the window is closed and it is below 19 °C, heat it.

Trigger moments

123

Living room temperature

19.5 19 °C 18.5 ✓✓✓

Someone home

×✓✓

Window open

✓×✓

What happens

  1. 117:25 drops below 19 °Cblocked: nobody home
  2. 218:50 drops below 19 °Cblocked: window open
  3. 319:20 the window closesall conditions met: fires

Fires once, at 19:20: someone is home, the window has just closed and the room is at 18.5 °C.

Show as text
VersionTrigger momentsResult
When the living room drops below 19 °C, heat it.17:25 (drops below 19 °C): no condition: fires; 18:50 (drops below 19 °C): no condition: firesFires twice: at 17:25 it heats an empty house, at 18:50 it heats with the window open.
When the living room drops below 19 °C, only if someone is home, heat it.17:25 (drops below 19 °C): blocked: nobody home; 18:50 (drops below 19 °C): all conditions met: firesFires once, at 18:50: someone is home, but the window is open.
When the living room drops below 19 °C, only if the window is closed, heat it.17:25 (drops below 19 °C): all conditions met: fires; 18:50 (drops below 19 °C): blocked: window openFires at 17:25 for an empty house. When the window closes at 19:20 nothing happens: the condition was checked at 18:50, and the room stays at 18.5 °C.
When the living room drops below 19 °C or the window closes, only if someone is home, the window is closed and it is below 19 °C, heat it.17:25 (drops below 19 °C): blocked: nobody home; 18:50 (drops below 19 °C): blocked: window open; 19:20 (the window closes): all conditions met: firesFires once, at 19:20: someone is home, the window has just closed and the room is at 18.5 °C.

The temperature scale runs from 18.2 to 19.6 °C. Someone is home from 18:05, and the window is open from 18:45 to 19:20.

fig. 3 The trigger decides the moments; the condition is only checked at those moments. When the window closes at 19:20 nothing happens, unless that is a trigger too.

A condition is only checked at the moment the trigger fires. A replay shows you those moments before your house does.

04 · AI on top

An assistant may read and suggest; some things need your yes.

With an AI assistant connected to Home Assistant you can ask questions in plain language. At 19:30 on the same evening, three requests and the rule I use at home for each. The replies are written for this page; nothing is sent to a model.

Answer straight awaygate open

It is 17.6 °C in the bedroom.

Read: bedroom temperature, 17.6 °C

Ask you firstgate asks you

The living room is 18.5 °C and the heating is set to 17 °C. Shall I set it to 20.5 °C?

You: Yes.

Living room heating: 17 → 20.5 °C

Never from a chatgate shut

I do not unlock doors or switch off alarms from a chat. Use the Home Assistant app and your own code.

Front door: still locked

Show as text

"How warm is the bedroom?" It reads one state, so the rule is: answer straight away. Assistant: "It is 17.6 °C in the bedroom." Result: Read: bedroom temperature, 17.6 °C.

"Make the living room warmer." It changes the heating, so the rule is: ask you first. Assistant: "The living room is 18.5 °C and the heating is set to 17 °C. Shall I set it to 20.5 °C?" You: "Yes." Result: Living room heating: 17 → 20.5 °C.

"Unlock the front door." It opens a lock, so the rule is: never from a chat. Assistant: "I do not unlock doors or switch off alarms from a chat. Use the Home Assistant app and your own code." Result: Front door: still locked.

fig. 4 Reading a state goes straight through. Changing the heating waits for your yes. A lock or an alarm is never opened from a chat.

Reading is cheap and safe. Acting on heating, locks and alarms needs a person in the loop.