Sleep Light: Gradually Dim Any Light at Bedtime with Home Assistant
The Sleep Light script helps you wind down naturally by slowly dimming any smart light to off over a period of your choice. Perfect for bedtime routines, kids' rooms, or a relaxing evening fade-out, it's as simple as picking a light, setting the duration, and letting Home Assistant handle the rest. This script dims a light from its current brightness down to off (0%) in small steps, spreading the fade over a total time you select. You can control how quickly it fades with the "step size," so you can make it smooth and gradual or a little faster - your choice! Tip: Combine with a white noise machine or smart speaker for a relaxing "lights out" routine! What Does This Script Do?
How to Use This Sleep Light Script
alias: Sleep Light
description: >
Gradually dims a selected light to off over a chosen duration,
simulating a natural "fade out" effect for bedtime.
fields:
target_light:
name: Light
description: The light to gradually dim
selector:
entity:
domain: light
required: true
dim_duration:
name: Duration
description: "The total duration over which the light dims to off"
selector:
duration: {}
required: true
step_size:
name: Step Size (%)
description: "How many brightness percentage points to decrease at each step (default: 2%)"
default: 2
selector:
number:
min: 1
max: 10
step: 1
unit_of_measurement: "%"
required: false
mode: single
sequence:
- variables:
current_brightness: "{{ state_attr(target_light, 'brightness')|default(255)|int }}"
current_brightness_pct: "{{ (current_brightness / 255 * 100)|round(0) }}"
step: "{{ step_size | int }}"
steps: "{{ (current_brightness_pct // step) | int }}"
total_duration_seconds: >
{{ (dim_duration.hours|int * 3600) +
(dim_duration.minutes|int * 60) +
(dim_duration.seconds|int) }}
delay_seconds: "{{ (total_duration_seconds / steps) | round(0) if steps > 0 else 0 }}"
- repeat:
count: "{{ steps }}"
sequence:
- service: light.turn_on
target:
entity_id: "{{ target_light }}"
data:
brightness_pct: >
{{ (current_brightness_pct - (repeat.index * step))
if (current_brightness_pct - (repeat.index * step)) > 0
else 0 }}
- delay:
seconds: "{{ delay_seconds }}"
In your Home Assistant UI, go to Settings > Automations & Scenes > Scripts, click Add Script, and switch to YAML mode. Paste the code.
Name it something like "Sleep Light" and save.
You can trigger it manually, from an automation (like your bedtime routine), or via voice assistant.
Choose your light, set the total duration (e.g. 15–45 minutes), and optionally adjust the step size for a smoother or faster fade. Use Cases & Ideas