################################################################################
# PACKAGE: Laundry Lights Backup Occupancy Automation
################################################################################
#
# Operation:
# - Turns Laundry lights on when either of these sensors becomes active:
#   - Laundry cupboard/wall PIR from ESPHome laundry environment device
#   - Backup ceiling PIR from Zigbee
#
# - Turns Laundry lights off only after BOTH PIRs are clear for the configured
#   delay period.
#
# Notes:
# - This is treated as a BACKUP automation.
# - The ESPHome devices should normally communicate  directly via MQTT.
# - The wall PIR should still operate the lights with MQTT only.
# - This HA automation provides a fallback path using HA entity states.
#
################################################################################

################################################################################
# USER SETTINGS / PACKAGE VARIABLES
################################################################################
input_number:
  laundry_lights_backup_clear_delay_seconds:
    name: "Laundry Lights Backup Clear Delay Seconds"
    min: 0
    max: 3600
    step: 1
    mode: box
    unit_of_measurement: s
    icon: mdi:timer-outline
    initial: 240

################################################################################
# AUTOMATIONS
################################################################################
automation:
  - id: laundry_lights_backup_occupancy_control
    alias: "Laundry Lights - Backup Occupancy Control"
    description: >
      Backup automation for Laundry lights. Turns lights on when either the
      ESPHome wall/cupboard PIR or Zigbee ceiling PIR detects movement/occupancy.
      Turns lights off after both PIRs are clear for the configured delay period.
    mode: restart
    triggers:
      # Either PIR becoming active turns the Laundry lights on.
      - trigger: state
        entity_id: binary_sensor.esp_laundryenv_laundry_pir
        to: "on"
        id: laundry_wall_pir_detected

      - trigger: state
        entity_id: binary_sensor.laundry_ceiling_pir_x03ir_occupancy
        to: "on"
        id: laundry_ceiling_pir_detected

      # Either PIR becoming clear causes a check. The action only continues
      # to the off-delay when BOTH PIRs are clear.
      - trigger: state
        entity_id:
          - binary_sensor.esp_laundryenv_laundry_pir
          - binary_sensor.laundry_ceiling_pir_x03ir_occupancy
        to: "off"
        id: laundry_pir_clear_check

      # On HA startup, re-evaluate the current state. This prevents lights
      # staying on indefinitely if HA restarted while the room was already clear.
      - trigger: homeassistant
        event: start
        id: ha_startup_check

    actions:
      - choose:
          ######################################################################
          # ON LOGIC:
          # If either PIR is active, turn the Laundry lights on immediately.
          ######################################################################
          - alias: "Turn lights on if either Laundry PIR is active"
            conditions:
              - condition: template
                value_template: >
                  {{
                    is_state('binary_sensor.esp_laundryenv_laundry_pir', 'on')
                    or is_state('binary_sensor.laundry_ceiling_pir_x03ir_occupancy', 'on')
                  }}
            sequence:
              - action: switch.turn_on
                target:
                  entity_id: switch.esp_laundrylights_relay_1_laundry_lights

          ######################################################################
          # OFF LOGIC:
          # Only start the off-delay if BOTH PIRs are clear.
          # After the delay, check both PIRs again before switching off.
          ######################################################################
          - alias: "Wait, then turn lights off if both Laundry PIRs remain clear"
            conditions:
              - condition: template
                value_template: >
                  {{
                    is_state('binary_sensor.esp_laundryenv_laundry_pir', 'off')
                    and is_state('binary_sensor.laundry_ceiling_pir_x03ir_occupancy', 'off')
                  }}
            sequence:
              - delay:
                  seconds: >
                    {{
                      states('input_number.laundry_lights_backup_clear_delay_seconds') | int(240)
                    }}

              # Re-check after the delay in case either PIR became active again.
              - condition: template
                value_template: >
                  {{
                    is_state('binary_sensor.esp_laundryenv_laundry_pir', 'off')
                    and is_state('binary_sensor.laundry_ceiling_pir_x03ir_occupancy', 'off')
                  }}

              # Only send OFF if the Laundry lights are currently ON.
              - condition: state
                entity_id: switch.esp_laundrylights_relay_1_laundry_lights
                state: "on"

              - action: switch.turn_off
                target:
                  entity_id: switch.esp_laundrylights_relay_1_laundry_lights
