Home Assistant Guide

Simple tutorials for powerful automations

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.

What Does This Script Do?

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!

  • Dims any supported smart light gently to off
  • Lets you choose the total dimming duration (e.g. 20 minutes)
  • Customizable step size for smoother or quicker fades
  • Works with any light entity in Home Assistant
  • No blueprints needed - just add this as a script!

How to Use This Sleep Light Script

  1. Copy the Script YAML:
    
    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 }}"
    
    					
  2. Add the Script to Home Assistant:
    In your Home Assistant UI, go to Settings > Automations & Scenes > Scripts, click Add Script, and switch to YAML mode. Paste the code.
  3. Save the Script:
    Name it something like "Sleep Light" and save.
  4. Run the Script:
    You can trigger it manually, from an automation (like your bedtime routine), or via voice assistant.
  5. Customize as Needed:
    Choose your light, set the total duration (e.g. 15–45 minutes), and optionally adjust the step size for a smoother or faster fade.

Tip: Combine with a white noise machine or smart speaker for a relaxing "lights out" routine!

Use Cases & Ideas

  • Dim the lights in a child's bedroom at bedtime
  • Create a relaxing fade-out for your evening reading nook
  • Use as part of a "goodnight" automation for the whole house
  • Wind down after a long day with gradual light reduction