blueprint:
  name: Rozik-motion-lux-day_night-light_control-v2
  description: >
    Automatizace zapíná světla při pohybu a tmě (lux < prah). Lze samostatně aktivovat/deaktivovat
    denní a noční režim. Každý režim má vlastní výběr RGB/klasického svícení s nastavením barvy/teploty a jasu.
    Zhasíná po daném čase od POSLEDNÍHO pohybu. Pokud lux nevyhovuje, je po 1s kontrolován znovu.
    Lze manuálně blokovat na zvolenou dobu pomocí input_boolean. Noční režim lze prodloužit pro vybrané
    dny včetně automatické detekce CZ svátků (vyžaduje calendar entitu pro svátky).
  domain: automation
  input:
    motion_sensor:
      name: Pohybový senzor
      selector:
        entity:
          domain: binary_sensor

    light_target:
      name: Ovládaná světla (libovolný počet)
      selector:
        target:
          entity:
            domain: light

    lux_sensor:
      name: Senzor světelnosti
      selector:
        entity:
          domain: sensor

    lux_threshold:
      name: Prah světelnosti (lux)
      default: 400
      selector:
        number:
          min: 0
          max: 2000
          unit_of_measurement: lx

    timeout:
      name: Timeout zhasnutí (v sekundách)
      default: 10
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: s

    # ===== DENNÍ REŽIM =====
    day_mode_toggle:
      name: Aktivace denního režimu
      default: true
      selector:
        boolean: {}

    day_light_mode:
      name: Denní režim - typ svícení
      default: kelvin
      selector:
        select:
          options:
            - label: Klasické (teplota barvy)
              value: kelvin
            - label: RGB
              value: rgb

    brightness_day:
      name: Denní režim - jas
      default: 255
      selector:
        number:
          min: 1
          max: 255

    color_temp_day:
      name: Denní režim - teplota barvy (Kelvin)
      default: 2500
      selector:
        number:
          min: 2000
          max: 6535
          unit_of_measurement: K

    color_day:
      name: Denní režim - RGB barva
      default: [255, 255, 255]
      selector:
        color_rgb: {}

    # ===== NOČNÍ REŽIM =====
    night_mode_toggle:
      name: Aktivace nočního režimu (zaškrtnout pro zapnutí)
      default: true
      selector:
        boolean: {}

    night_mode_start:
      name: Čas začátku nočního režimu
      default: "21:45:00"
      selector:
        time: {}

    night_mode_end:
      name: Defaultní čas konce nočního režimu
      default: "06:30:00"
      selector:
        time: {}

    night_mode_end_override:
      name: Čas konce nočního režimu pro vybrané dny
      default: "08:00:00"
      selector:
        time: {}

    weekend_override_days:
      name: Dny s prodlouženým nočním režimem
      default: ["Sobota", "Neděle"]
      selector:
        select:
          multiple: true
          mode: dropdown
          options:
            - Pondělí
            - Úterý
            - Středa
            - Čtvrtek
            - Pátek
            - Sobota
            - Neděle
            - Dny svátků

    holiday_calendar:
      name: Kalendář CZ svátků (volitelné - nechej prázdné pokud nepoužíváš)
      default: ""
      selector:
        entity:
          domain: calendar
          multiple: false

    night_light_mode:
      name: Noční režim - typ svícení
      default: rgb
      selector:
        select:
          options:
            - label: Klasické (teplota barvy)
              value: kelvin
            - label: RGB
              value: rgb

    brightness_night:
      name: Noční režim - jas
      default: 10
      selector:
        number:
          min: 1
          max: 255

    color_temp_night:
      name: Noční režim - teplota barvy (Kelvin)
      default: 2700
      selector:
        number:
          min: 2000
          max: 6535
          unit_of_measurement: K

    color_night:
      name: Noční režim - RGB barva
      default: [191, 64, 64]
      selector:
        color_rgb: {}

    # ===== MANUÁLNÍ BLOKACE =====
    manual_override_entity:
      name: Entita blokace automatizace (input_boolean)
      selector:
        entity:
          domain: input_boolean

    manual_override_duration:
      name: Trvání blokace v minutách
      default: 60
      selector:
        number:
          min: 1
          max: 300
          unit_of_measurement: min

trigger:
  - platform: state
    entity_id: !input motion_sensor
    to: "on"
  - platform: state
    entity_id: !input motion_sensor
    to: "off"
  - platform: state
    entity_id: !input manual_override_entity
    to: "on"

variables:
  day_toggle_var: !input day_mode_toggle
  night_toggle_var: !input night_mode_toggle
  night_start: !input night_mode_start
  night_end_default: !input night_mode_end
  night_end_override: !input night_mode_end_override
  override_days: !input weekend_override_days
  holiday_cal: !input holiday_calendar
  day_mode_type: !input day_light_mode
  night_mode_type: !input night_light_mode

  today_name: >
    {{ ["Pondělí", "Úterý", "Středa", "Čtvrtek", "Pátek", "Sobota", "Neděle"][now().weekday()] }}

  is_holiday: >
    {% if holiday_cal != "" and "Dny svátků" in override_days %}
      {{ is_state(holiday_cal, 'on') }}
    {% else %}
      false
    {% endif %}

  night_end: >
    {% if today_name in override_days or is_holiday %}
      {{ night_end_override }}
    {% else %}
      {{ night_end_default }}
    {% endif %}

  night_active: >
    {% set start = strptime(night_start, '%H:%M:%S').time() %}
    {% set end = strptime(night_end, '%H:%M:%S').time() %}
    {% set now = now().time() %}
    {% if start < end %}
      {{ start <= now < end }}
    {% else %}
      {{ now >= start or now < end }}
    {% endif %}

condition: []

action:
  - choose:
      # ===== MANUÁLNÍ BLOKACE =====
      - conditions:
          - condition: state
            entity_id: !input manual_override_entity
            state: "on"
        sequence:
          - delay:
              minutes: !input manual_override_duration
          - service: input_boolean.turn_off
            target:
              entity_id: !input manual_override_entity
          - service: light.turn_off
            target: !input light_target

      # ===== DETEKCE POHYBU =====
      - conditions:
          - condition: state
            entity_id: !input motion_sensor
            state: "on"
          - condition: state
            entity_id: !input manual_override_entity
            state: "off"
          - condition: or
            conditions:
              - condition: template
                value_template: "{{ day_toggle_var and not night_active }}"
              - condition: template
                value_template: "{{ night_toggle_var and night_active }}"
        sequence:
          # Kontrola lux - první pokus
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: !input lux_sensor
                    below: !input lux_threshold
                sequence:
                  # Rozsvícení - NOČNÍ REŽIM
                  - choose:
                      - conditions:
                          - condition: template
                            value_template: "{{ night_toggle_var and night_active and night_mode_type == 'rgb' }}"
                        sequence:
                          - service: light.turn_on
                            target: !input light_target
                            data:
                              brightness: !input brightness_night
                              rgb_color: !input color_night
                      - conditions:
                          - condition: template
                            value_template: "{{ night_toggle_var and night_active and night_mode_type == 'kelvin' }}"
                        sequence:
                          - service: light.turn_on
                            target: !input light_target
                            data:
                              brightness: !input brightness_night
                              color_temp_kelvin: !input color_temp_night
                      # Rozsvícení - DENNÍ REŽIM
                      - conditions:
                          - condition: template
                            value_template: "{{ day_toggle_var and not night_active and day_mode_type == 'rgb' }}"
                        sequence:
                          - service: light.turn_on
                            target: !input light_target
                            data:
                              brightness: !input brightness_day
                              rgb_color: !input color_day
                      - conditions:
                          - condition: template
                            value_template: "{{ day_toggle_var and not night_active and day_mode_type == 'kelvin' }}"
                        sequence:
                          - service: light.turn_on
                            target: !input light_target
                            data:
                              brightness: !input brightness_day
                              color_temp_kelvin: !input color_temp_day

              # Kontrola lux - druhý pokus po 1s (lux nad prahem)
              - conditions:
                  - condition: numeric_state
                    entity_id: !input lux_sensor
                    above: !input lux_threshold
                sequence:
                  - delay: "00:00:01"
                  - choose:
                      - conditions:
                          - condition: numeric_state
                            entity_id: !input lux_sensor
                            below: !input lux_threshold
                        sequence:
                          # Rozsvícení po druhé kontrole - NOČNÍ REŽIM
                          - choose:
                              - conditions:
                                  - condition: template
                                    value_template: "{{ night_toggle_var and night_active and night_mode_type == 'rgb' }}"
                                sequence:
                                  - service: light.turn_on
                                    target: !input light_target
                                    data:
                                      brightness: !input brightness_night
                                      rgb_color: !input color_night
                              - conditions:
                                  - condition: template
                                    value_template: "{{ night_toggle_var and night_active and night_mode_type == 'kelvin' }}"
                                sequence:
                                  - service: light.turn_on
                                    target: !input light_target
                                    data:
                                      brightness: !input brightness_night
                                      color_temp_kelvin: !input color_temp_night
                              # Rozsvícení po druhé kontrole - DENNÍ REŽIM
                              - conditions:
                                  - condition: template
                                    value_template: "{{ day_toggle_var and not night_active and day_mode_type == 'rgb' }}"
                                sequence:
                                  - service: light.turn_on
                                    target: !input light_target
                                    data:
                                      brightness: !input brightness_day
                                      rgb_color: !input color_day
                              - conditions:
                                  - condition: template
                                    value_template: "{{ day_toggle_var and not night_active and day_mode_type == 'kelvin' }}"
                                sequence:
                                  - service: light.turn_on
                                    target: !input light_target
                                    data:
                                      brightness: !input brightness_day
                                      color_temp_kelvin: !input color_temp_day

  # ===== ČEKÁNÍ NA TIMEOUT =====
  - wait_for_trigger:
      - platform: state
        entity_id: !input motion_sensor
        to: "on"
    timeout: !input timeout
    continue_on_timeout: true

  - condition: state
    entity_id: !input motion_sensor
    state: "off"

  - condition: state
    entity_id: !input manual_override_entity
    state: "off"

  - service: light.turn_off
    target: !input light_target

mode: restart

