YAML Butler
← All guides

Motion sensor lights in Home Assistant

Build a motion light that stays on while a room is occupied, then turns off after a quiet interval.

A useful motion light feels predictable: it turns on promptly, stays on while somebody is moving, and switches off only after the room has been quiet. Begin with one sensor and one light. Once that works reliably, add darkness checks, brightness levels, or a manual override. Starting with several rooms at once makes it harder to tell whether a problem comes from the automation or the hardware.

Check the sensor before writing YAML

Open Developer Tools and inspect the motion sensor. A motion detector is usually a binary_sensor whose raw state is "on" during motion and "off" when clear. The dashboard may show Detected and Clear instead. Walk through the room and watch how long the device takes to return to off. That hardware cooldown adds to your chosen timeout, so a two-minute software wait may feel longer in practice. Copy the actual entity IDs for the sensor and light.

Use separate motion and clear triggers

The example gives each trigger an ID. Motion turns the light on; a sensor that remains clear for two minutes turns it off. A choose block selects the action using that ID. This avoids a long running delay and makes the trace easier to inspect. A new motion event prevents the clear trigger from reaching its full duration. Keep the state values quoted so YAML does not interpret them as another data type.

A starting-point automation

Every entity ID below is an example. Replace it with your actual entity ID and review all settings before use.

alias: Hall motion lighting
description: Turn on with motion and off after two clear minutes.
triggers:
  - trigger: state
    entity_id: binary_sensor.hall_motion
    to: "on"
    id: occupied
  - trigger: state
    entity_id: binary_sensor.hall_motion
    to: "off"
    for: "00:02:00"
    id: clear
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: occupied
        sequence:
          - action: light.turn_on
            target:
              entity_id: light.hall
    default:
      - action: light.turn_off
        target:
          entity_id: light.hall
mode: restart

Add darkness without blocking the off branch

A common mistake is adding a global sun condition to the whole automation. If sunrise arrives while the light is on, that condition may prevent the later off branch from running. Put a darkness condition inside the motion branch instead. A lux sensor can work better than sunset in rooms shaded by trees or nearby buildings. Pick a threshold by checking actual readings while the room feels too dark, rather than copying an arbitrary value.

Plan the manual controls

Decide what should happen when somebody turns the light on manually. With this basic rule, the next clear event may turn it off. If that is unwelcome, create an input_boolean helper for an override and check it before automatic changes. Give the override an obvious dashboard control and a deliberate reset policy. Avoid a rule that instantly undoes every manual adjustment; people should be able to understand why the light changed.

Test transitions and restarts

Test motion, continued movement, leaving the room, and returning just before the timeout. Then inspect the automation trace to see which trigger fired. A state trigger with for does not preserve its pending duration across a Home Assistant restart or automation reload. If surviving restarts matters, store a deadline in an input_datetime helper and use a separate recovery rule. For ordinary hallway lighting, accepting that limitation may be sufficient.

Tune for the room

PIR motion sensors do not reliably detect a person sitting still. A desk or reading room may need a longer timeout or a presence sensor. Bathrooms may need a door or humidity signal as additional evidence. Make one change at a time, observe several normal uses, and record the reason for each exception. Broad room groups are convenient later, but target a single light while you are still validating behavior.

Describe the complete cycle

A good generator request names both the start and the finish: detect motion, turn on one named light, wait for continuous clearance, then turn that light off. Include any brightness preference and whether the rule should run during daylight. If you supply only the turn-on behavior, do not assume the generated draft will infer your preferred timeout. Read each branch back as a sentence and compare it with what somebody standing in the room would expect.

Further reference: official Home Assistant documentation.

Common questions

Why does the light turn off while I am sitting?

Many motion sensors detect movement rather than continued occupancy. Increase the quiet interval or add a presence sensor.

Does the two-minute timer survive a restart?

No. Pending state-trigger durations reset when Home Assistant restarts or automations reload. Use a persisted deadline if needed.

Put your own idea into YAML.

Describe your devices, timing, and exceptions. YAML Butler will give you a starting point to review.

Generate your automation ↗