alias: "Motion: Lounge"
description: >
  Lounge lights based on motion + dark-enough (lux), lighting scene slot,
  and a countdown timer.

# =============================================================================
# TRIGGERS
# =============================================================================
# We react to:
# 1. Motion turning ON
# 2. Motion turning OFF (after 15 seconds)
# 3. Our countdown timer finishing

triggers:
  # Motion detected
  - entity_id: binary_sensor.lounge_presence_ld2450_presence  # <-- Change to YOUR presence sensor
    to: "on"
    id: motion-on
    trigger: state

  # Motion cleared (after small delay to avoid flapping)
  - entity_id: binary_sensor.lounge_presence_ld2450_presence  # <-- Change to YOUR presence sensor
    to: "off"
    for:
      seconds: 15  # <-- Adjust to suit your sensor sensitivity
    id: motion-off
    trigger: state

  # Timer finished (used to turn lights off gracefully)
  - event_type: timer.finished
    event_data:
      entity_id: timer.countdown_motion_lounge  # <-- Change if your timer name differs
    id: timer-finished
    trigger: event

# =============================================================================
# CONDITIONS (Global Gates)
# =============================================================================
# These conditions prevent the automation from running in certain scenarios.
# You can disable or remove ones you don’t use.

conditions:
  # # Global override (optional – disabled by default)
  # - condition: state
  #   entity_id: input_boolean.light_override  # <-- Only if you use a house-wide override
  #   state: "off"
  #   enabled: false

  # # Room suppression switch (optional – disabled by default)
  # - condition: state
  #   entity_id: input_boolean.lounge_motion_suppressed  # <-- Optional per-room suppression
  #   state: "off"
  #   enabled: false

  # # Manual override (this is the important one)
  # # If this is ON, the automation does nothing.
  # - condition: state
  #   entity_id: input_boolean.lounge_manual_override  # <-- Change per room
  #   state: "off"

# =============================================================================
# ACTIONS
# =============================================================================

actions:
  - choose:

      # -----------------------------------------------------------------------
      # MOTION ON
      # -----------------------------------------------------------------------
      - conditions:
          - condition: trigger
            id: motion-on
          - condition: state
            entity_id: binary_sensor.lounge_dark_enough  # <-- Your "dark enough" binary sensor
            state: "on"

        sequence:

          # Cancel any running countdown timer
          - target:
              entity_id: timer.countdown_motion_lounge  # <-- Your room timer
            action: timer.cancel

          # Choose lighting scene based on time-of-day slot
          - choose:

              # EARLY MORNING
              - conditions:
                  - condition: state
                    entity_id: sensor.lighting_scene_slot  # <-- Your time-of-day sensor
                    state: early_morning
                sequence:
                  - target:
                      entity_id: scene.lights_lounge_early_morning  # <-- Change per room
                    action: scene.turn_on

              # MORNING
              - conditions:
                  - condition: state
                    entity_id: sensor.lighting_scene_slot
                    state: morning
                sequence:
                  - target:
                      entity_id: scene.lights_lounge_morning
                    action: scene.turn_on

              # DAYTIME
              - conditions:
                  - condition: state
                    entity_id: sensor.lighting_scene_slot
                    state: daytime
                sequence:
                  - target:
                      entity_id: scene.lights_lounge_daytime
                    action: scene.turn_on

              # EVENING
              - conditions:
                  - condition: state
                    entity_id: sensor.lighting_scene_slot
                    state: evening
                sequence:
                  - target:
                      entity_id: scene.lights_lounge_evening
                    action: scene.turn_on

              # LATE EVENING
              - conditions:
                  - condition: state
                    entity_id: sensor.lighting_scene_slot
                    state: late_evening
                sequence:
                  - target:
                      entity_id: scene.lights_lounge_late_evening
                    action: scene.turn_on

              # OVERNIGHT
              - conditions:
                  - condition: state
                    entity_id: sensor.lighting_scene_slot
                    state: overnight
                sequence:
                  - target:
                      entity_id: scene.lights_lounge_overnight
                    action: scene.turn_on

            # Default fallback if slot not matched
            default:
              - target:
                  entity_id: scene.lights_lounge_daytime  # <-- Safe fallback
                action: scene.turn_on


      # -----------------------------------------------------------------------
      # MOTION OFF
      # -----------------------------------------------------------------------
      # Instead of turning lights off immediately,
      # we start a countdown timer.

      - conditions:
          - condition: trigger
            id: motion-off

        sequence:
          - target:
              entity_id: timer.countdown_motion_lounge  # <-- Your room timer
            action: timer.start


      # -----------------------------------------------------------------------
      # TIMER FINISHED
      # -----------------------------------------------------------------------
      # If no motion occurred during the timer period,
      # we gently turn off the light.

      - conditions:
          - condition: trigger
            id: timer-finished

        sequence:
          - target:
              entity_id: light.lounge  # <-- Change to your room light
            data:
              transition: 60  # <-- Fade out over 60 seconds (adjust to taste)
            action: light.turn_off

mode: single
