blueprint:
  name: Auto zhasnout po odchodu rodiny nebo po obnově napájení
  description: |
    1) Rodina not_home po nastavenou dobu -> pro každé vybrané světlo: pokud je ON a je ON alespoň X, vypne ho.
    2) Power restore indikátor se vrátí z unavailable a je stabilně dostupný po nastavenou dobu ->
       - defaultně vypne všechna vybraná světla, která jsou ON
       - volitelně použije filtr "svítí déle než" (může být nespolehlivé po výpadku kvůli last_changed).

    Výběr světel, rodiny i restore indikátoru je v UI (multi-select). Časy jsou duration.

  domain: automation

  input:
    lights:
      name: "Světla k hlídání"
      selector:
        entity:
          domain: light
          multiple: true

    family_entity:
      name: "Rodina / přítomnost"
      selector:
        entity: {}

    restore_indicator:
      name: "Indikátor obnovy napájení"
      selector:
        entity: {}

    away_for:
      name: "Rodina pryč – spustit po"
      default: {hours: 0, minutes: 10, seconds: 0}
      selector: {duration: {}}

    away_min_on:
      name: "Rodina pryč – zhasnout jen když svítí déle než"
      default: {hours: 0, minutes: 5, seconds: 0}
      selector: {duration: {}}

    restore_for:
      name: "Obnova napájení – spustit po"
      default: {hours: 0, minutes: 1, seconds: 0}
      selector: {duration: {}}

    restore_min_on:
      name: "Obnova napájení – zhasnout jen když svítí déle než"
      default: {hours: 0, minutes: 0, seconds: 30}
      selector: {duration: {}}

    restore_use_min_on_filter:
      name: '(Volitelné) Restore: používat filtr "svítí déle než"'
      default: false
      selector: {boolean: {}}

mode: restart

trigger:
  - platform: state
    entity_id: !input family_entity
    to: not_home
    for: !input away_for
    id: family_away

  - platform: state
    entity_id: !input restore_indicator
    from: unavailable
    id: power_restore

variables:
  lights: !input lights
  away_min_on: !input away_min_on
  restore_for: !input restore_for
  restore_min_on: !input restore_min_on
  restore_use_min_on_filter: !input restore_use_min_on_filter

action:
  - choose:
      # -------------------------
      # Scénář A: Rodina pryč
      # -------------------------
      - conditions:
          - condition: trigger
            id: family_away
          - condition: state
            entity_id: !input family_entity
            state: not_home
        sequence:
          - variables:
              away_cutoff_ts: >
                {{ as_timestamp(now()) - (
                     (away_min_on.hours   | int(0)) * 3600 +
                     (away_min_on.minutes | int(0)) * 60 +
                     (away_min_on.seconds | int(0))
                   ) }}
          - repeat:
              for_each: "{{ lights }}"
              sequence:
                - condition: template
                  value_template: >
                    {% set e = states[repeat.item] %}
                    {{ e is not none
                       and e.state == 'on'
                       and as_timestamp(e.last_changed) <= away_cutoff_ts }}
                - service: light.turn_off
                  target:
                    entity_id: "{{ repeat.item }}"

      # -------------------------
      # Scénář B: Power restore
      # -------------------------
      - conditions:
          - condition: trigger
            id: power_restore
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id: !input restore_indicator
                to: unavailable
            timeout:
              hours: "{{ restore_for.hours | int(0) }}"
              minutes: "{{ restore_for.minutes | int(0) }}"
              seconds: "{{ restore_for.seconds | int(0) }}"
            continue_on_timeout: true

          # Pokud indikátor během čekání spadl zpět do unavailable -> abort
          - condition: template
            value_template: "{{ wait.trigger is none }}"

          - condition: not
            conditions:
              - condition: state
                entity_id: !input restore_indicator
                state: unavailable

          - variables:
              restore_cutoff_ts: >
                {{ as_timestamp(now()) - (
                     (restore_min_on.hours   | int(0)) * 3600 +
                     (restore_min_on.minutes | int(0)) * 60 +
                     (restore_min_on.seconds | int(0))
                   ) }}

          - repeat:
              for_each: "{{ lights }}"
              sequence:
                - choose:
                    # filtr ON>=X (jen když je zapnutý checkbox)
                    - conditions:
                        - condition: template
                          value_template: "{{ restore_use_min_on_filter }}"
                      sequence:
                        - condition: template
                          value_template: >
                            {% set e = states[repeat.item] %}
                            {{ e is not none
                               and e.state == 'on'
                               and as_timestamp(e.last_changed) <= restore_cutoff_ts }}
                        - service: light.turn_off
                          target:
                            entity_id: "{{ repeat.item }}"
                  default:
                    # default: vypni vše, co je ON
                    - condition: template
                      value_template: >
                        {% set e = states[repeat.item] %}
                        {{ e is not none and e.state == 'on' }}
                    - service: light.turn_off
                      target:
                        entity_id: "{{ repeat.item }}"
