# --- @SyncNetOps || SNO-perfekt-Heating || V2.3.0 - Fix 1 ---

blueprint:
  name: SNO-perfekt-Heating V2.3.0-F1
  description: >
    # 🌟 SNO-perfekt-Heating (Pro-Edition) V2.3.0-F1 by SyncNetOps 🌡️☀️
    
    Diese Version bietet maximale Flexibilität durch konfigurierbare Prioritäten, dynamische Fenster-Absenkung, Urlaubsmodus und intelligente Auto-Resets.
    
    🌐 **HILFE & DOKUMENTATION:** [https://sno.mb222.de/ph-faq/](https://sno.mb222.de/ph-faq/)
    
    ---
    **PRIORITÄTEN-KETTE (Wer gewinnt?):**
    1. **✋ Manuell** (Kann sich nun automatisch deaktivieren)
    2. **☀️ Sommer** (Ignoriert geöffnete Fenster)
    3. **🌴 Urlaub** (Pausiert nach X Tagen automatisch)
    4. **🪟 Fenster Offen** (Greift nur in den von dir erlaubten Modi!)
    5. **🔥 Extern** (Kamin-Erkennung)
    6. **🚶 Abwesend**
    7. **🕒 Auto** (Zeitpläne & Raum-Präsenz)
  domain: automation
  input:
    # --- 1️⃣ BASIS SETUP ---
    section_core:
      name: "1️⃣ Kern-Konfiguration"
      icon: mdi:thermostat
      input:
        climate_entity:
          name: Klima-Entität (Thermostat)
          selector: {entity: {domain: climate}}
        home_presence:
          name: Anwesenheit Haus (Optional)
          description: "Leer = Haus gilt als 'immer bewohnt'."
          default: []
          selector: {entity: {domain: [binary_sensor, input_boolean]}}
        no_home_presence_wait:
          name: Wartezeit bis Abwesenheit (Sekunden)
          default: 60
          selector: {number: {min: 0.0, max: 3600.0, step: 1.0, mode: slider}}
        room_presence:
          name: Raum-Anwesenheit (Optional)
          description: "Leer = Raum gilt als 'immer belegt'."
          default: []
          selector: {entity: {domain: [binary_sensor, input_boolean]}}
        away_temperature:
          name: Abwesenheits-Temperatur (Regler)
          default: 16.5
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        away_temperature_entity:
          name: Abwesenheits-Temperatur (Entität / Optional)
          default: []
          selector: {entity: {domain: [input_number, number]}}

    # --- 2️⃣ MANUELLER MODUS ---
    section_manual:
      name: "2️⃣ Manueller Modus & Auto-Reset"
      icon: mdi:hand-back-right
      input:
        manual_temperature_control:
          name: ✋ Manueller Schalter (Optional)
          default: []
          selector: {entity: {domain: [binary_sensor, input_boolean]}}
        manual_temperature:
          name: ✋ Manuelle Temperatur (Eingabe)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        
        manual_auto_off_time_enabled:
          name: ⏰ Auto-Aus per Uhrzeit
          description: "Deaktiviert den manuellen Modus zu einer festen Uhrzeit."
          default: false
          selector: {boolean: {}}
        manual_auto_off_time:
          name: ↳ Deaktivierungs-Uhrzeit
          default: "22:00:00"
          selector: {time: {}}
          
        manual_auto_off_duration_enabled:
          name: ⏳ Auto-Aus nach Dauer
          description: "Deaktiviert den manuellen Modus nach X Minuten."
          default: false
          selector: {boolean: {}}
        manual_auto_off_duration:
          name: ↳ Dauer in Minuten
          default: 120
          selector: {number: {min: 1, max: 1440, mode: box}}
          
        manual_auto_off_presence:
          name: 🚶 Auto-Aus bei Haus-Abwesenheit
          description: "Deaktiviert den manuellen Modus, sobald das Haus verlassen wird."
          default: false
          selector: {boolean: {}}

    # --- 3️⃣ URLAUBSMODUS ---
    section_vacation:
      name: "3️⃣ Urlaubsmodus"
      icon: mdi:palm-tree
      input:
        vacation_switch:
          name: 🌴 Urlaubsmodus Schalter (Optional)
          default: []
          selector: {entity: {domain: [input_boolean, binary_sensor]}}
        vacation_temperature:
          name: 🌴 Urlaubs-Temperatur (Regler)
          default: 15.0
          selector: {number: {min: 4.0, max: 30.0, step: 0.5, mode: slider}}
        vacation_temperature_entity:
          name: 🌴 Urlaubs-Temperatur (Entität / Optional)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        vacation_auto_off_days_enabled:
          name: 📅 Auto-Aus nach Tagen
          description: "Deaktiviert den Urlaub automatisch nach X Tagen."
          default: false
          selector: {boolean: {}}
        vacation_auto_off_days:
          name: ↳ Anzahl Tage
          default: 14
          selector: {number: {min: 1, max: 365, mode: box}}

    # --- 4️⃣ FENSTER-ERKENNUNG ---
    section_window:
      name: "4️⃣ Fenster-Erkennung (Dynamisch)"
      icon: mdi:window-open
      input:
        window_sensor:
          name: 🪟 Fenster-Sensor (Optional)
          default: []
          selector: {entity: {domain: binary_sensor}}
        window_temperature:
          name: 🪟 Fenster Offen Temperatur (Regler)
          default: 5.0
          selector: {number: {min: 4.0, max: 30.0, step: 0.5, mode: slider}}
        window_temperature_entity:
          name: 🪟 Fenster Offen Temperatur (Entität / Optional)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        window_active_modes:
          name: 🎯 Fenster-Absenkung zulassen in...
          description: "Wähle, in welchen Modi ein offenes Fenster die Heizung absenken darf."
          default: ["auto", "away", "manual", "vacation"]
          selector:
            select:
              multiple: true
              custom_value: false
              options:
                - label: 🕒 Auto (Zeitpläne)
                  value: auto
                - label: 🚶 Abwesend
                  value: away
                - label: ✋ Manuell
                  value: manual
                - label: 🌴 Urlaub
                  value: vacation
                - label: 🔥 Extern (Kamin)
                  value: extern

    # --- 5️⃣ SOMMER & EXTERN ---
    section_summer_extern:
      name: "5️⃣ Sommer & Extern (Kamin)"
      icon: mdi:weather-sunny
      collapsed: true
      input:
        extern_mode_switch:
          name: 🔥 Extern-Modus Schalter (Optional)
          default: []
          selector: {entity: {domain: [input_boolean, binary_sensor, switch]}}
        extern_temperature:
          name: 🔥 Extern-Temperatur (Regler)
          default: 15.0
          selector: {number: {min: 4.0, max: 30.0, step: 0.5, mode: slider}}
        extern_temperature_entity:
          name: 🔥 Extern-Temperatur (Entität / Optional)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        summer_mode_toggle:
          name: ☀️ Sommer-Modus Schalter (Optional)
          default: []
          selector: {entity: {domain: [input_boolean, binary_sensor]}}
        summer_sensor:
          name: ☀️ Sommer-Sensor (Optional)
          default: []
          selector: {entity: {domain: sensor, device_class: temperature}}
        summer_sensor_threshold:
          name: ☀️ Sommer-Sensor Schwellwert (°C)
          default: 22.0
          selector: {number: {min: 10.0, max: 40.0, step: 0.5, mode: slider}}
        summer_temperature_static:
          name: ☀️ Sommer-Temperatur (Regler)
          default: 5.0
          selector: {number: {min: 4.0, max: 30.0, step: 0.5, mode: slider}}
        summer_temperature_entity:
          name: ☀️ Sommer-Temperatur (Entität / Optional)
          default: []
          selector: {entity: {domain: [input_number, number]}}

    # --- 6️⃣ ZEITPLÄNE (AUTO) ---
    section_schedules:
      name: "6️⃣ Zeitpläne (Morgen, Tag, Abend, Nacht)"
      icon: mdi:clock-outline
      collapsed: true
      input:
        morning_time:
          name: ⏰ Startzeit Morgen
          default: "06:00:00"
          selector: {time: {}}
        morning_eco_temperature:
          name: Morgen Eco (Regler)
          default: 19.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        morning_eco_temperature_entity:
          name: Morgen Eco (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        morning_comfort_temperature:
          name: Morgen Komfort (Regler)
          default: 21.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        morning_comfort_temperature_entity:
          name: Morgen Komfort (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        
        day_time:
          name: ⏰ Startzeit Tag
          default: "09:00:00"
          selector: {time: {}}
        day_eco_temperature:
          name: Tag Eco (Regler)
          default: 19.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        day_eco_temperature_entity:
          name: Tag Eco (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        day_comfort_temperature:
          name: Tag Komfort (Regler)
          default: 21.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        day_comfort_temperature_entity:
          name: Tag Komfort (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}

        evening_time:
          name: ⏰ Startzeit Abend
          default: "17:00:00"
          selector: {time: {}}
        evening_eco_temperature:
          name: Abend Eco (Regler)
          default: 18.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        evening_eco_temperature_entity:
          name: Abend Eco (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        evening_comfort_temperature:
          name: Abend Komfort (Regler)
          default: 21.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        evening_comfort_temperature_entity:
          name: Abend Komfort (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}

        night_time:
          name: ⏰ Startzeit Nacht
          default: "21:00:00"
          selector: {time: {}}
        night_eco_temperature:
          name: Nacht Eco (Regler)
          default: 17.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        night_eco_temperature_entity:
          name: Nacht Eco (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}
        night_comfort_temperature:
          name: Nacht Komfort (Regler)
          default: 21.0
          selector: {number: {min: 5.0, max: 30.0, step: 0.1, mode: slider}}
        night_comfort_temperature_entity:
          name: Nacht Komfort (Entität)
          default: []
          selector: {entity: {domain: [input_number, number]}}

    # --- 7️⃣ DASHBOARD STATUS ---
    section_status:
      name: "7️⃣ Dashboard Status & Helper"
      icon: mdi:monitor-dashboard
      collapsed: true
      input:
        status_text_entity:
          name: ℹ️ Status-Text Ausgabe (Optional)
          default: []
          selector: {entity: {domain: input_text}}
        window_status_boolean:
          name: ℹ️ Fenster-Status Schalter (Optional)
          default: []
          selector: {entity: {domain: input_boolean}}
        summer_status_boolean:
          name: ℹ️ Sommer-Status Schalter (Optional)
          default: []
          selector: {entity: {domain: input_boolean}}
        extern_status_boolean:
          name: ℹ️ Extern-Status Schalter (Optional)
          default: []
          selector: {entity: {domain: input_boolean}}

mode: restart
max_exceeded: silent

# --- 1. SICHERE TRIGGER VARIABLEN (Fix für TemplateSyntaxError) ---
trigger_variables:
  trig_man_ctrl: !input manual_temperature_control
  trig_man_dur_en: !input manual_auto_off_duration_enabled
  trig_man_dur: !input manual_auto_off_duration
  trig_man_time_en: !input manual_auto_off_time_enabled
  trig_man_time: !input manual_auto_off_time
  trig_man_pres: !input manual_auto_off_presence
  trig_home: !input home_presence
  trig_home_wait: !input no_home_presence_wait
  trig_vac_switch: !input vacation_switch
  trig_vac_days_en: !input vacation_auto_off_days_enabled
  trig_vac_days: !input vacation_auto_off_days

variables:
  # --- 2. CORE VARIABLEN REGISTRIEREN ---
  climate_entity: !input climate_entity
  home_presence: !input home_presence
  no_home_presence_wait: !input no_home_presence_wait
  room_presence: !input room_presence
  window_active_modes: !input window_active_modes
  
  away_temperature: !input away_temperature
  away_temperature_entity: !input away_temperature_entity
  
  manual_temperature_control: !input manual_temperature_control
  manual_temperature: !input manual_temperature
  
  vacation_switch: !input vacation_switch
  vacation_temperature: !input vacation_temperature
  vacation_temperature_entity: !input vacation_temperature_entity
  
  window_sensor: !input window_sensor
  window_temperature: !input window_temperature
  window_temperature_entity: !input window_temperature_entity
  
  extern_mode_switch: !input extern_mode_switch
  extern_temperature: !input extern_temperature
  extern_temperature_entity: !input extern_temperature_entity
  
  summer_mode_toggle: !input summer_mode_toggle
  summer_sensor: !input summer_sensor
  summer_sensor_threshold: !input summer_sensor_threshold
  summer_temperature_static: !input summer_temperature_static
  summer_temperature_entity: !input summer_temperature_entity
  
  morning_time: !input morning_time
  morning_eco_temperature: !input morning_eco_temperature
  morning_eco_temperature_entity: !input morning_eco_temperature_entity
  morning_comfort_temperature: !input morning_comfort_temperature
  morning_comfort_temperature_entity: !input morning_comfort_temperature_entity
  
  day_time: !input day_time
  day_eco_temperature: !input day_eco_temperature
  day_eco_temperature_entity: !input day_eco_temperature_entity
  day_comfort_temperature: !input day_comfort_temperature
  day_comfort_temperature_entity: !input day_comfort_temperature_entity
  
  evening_time: !input evening_time
  evening_eco_temperature: !input evening_eco_temperature
  evening_eco_temperature_entity: !input evening_eco_temperature_entity
  evening_comfort_temperature: !input evening_comfort_temperature
  evening_comfort_temperature_entity: !input evening_comfort_temperature_entity
  
  night_time: !input night_time
  night_eco_temperature: !input night_eco_temperature
  night_eco_temperature_entity: !input night_eco_temperature_entity
  night_comfort_temperature: !input night_comfort_temperature
  night_comfort_temperature_entity: !input night_comfort_temperature_entity

  status_text_entity: !input status_text_entity
  window_status_boolean: !input window_status_boolean
  summer_status_boolean: !input summer_status_boolean
  extern_status_boolean: !input extern_status_boolean

  manual_auto_off_time_enabled: !input manual_auto_off_time_enabled
  manual_auto_off_time: !input manual_auto_off_time
  manual_auto_off_duration_enabled: !input manual_auto_off_duration_enabled
  manual_auto_off_duration: !input manual_auto_off_duration
  manual_auto_off_presence: !input manual_auto_off_presence
  vacation_auto_off_days_enabled: !input vacation_auto_off_days_enabled
  vacation_auto_off_days: !input vacation_auto_off_days

  # --- 3. FALLBACK TEMPERATUREN BERECHNEN ---
  eff_away: >-
    {% set ent = away_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(16.5) }}
    {% else %} {{ away_temperature | float(16.5) }} {% endif %}

  eff_window: >-
    {% set ent = window_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(5.0) }}
    {% else %} {{ window_temperature | float(5.0) }} {% endif %}

  eff_extern: >-
    {% set ent = extern_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(15.0) }}
    {% else %} {{ extern_temperature | float(15.0) }} {% endif %}

  eff_summer: >-
    {% set ent = summer_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(5.0) }}
    {% else %} {{ summer_temperature_static | float(5.0) }} {% endif %}

  eff_vacation: >-
    {% set ent = vacation_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(15.0) }}
    {% else %} {{ vacation_temperature | float(15.0) }} {% endif %}

  eff_m_eco: >-
    {% set ent = morning_eco_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(19.0) }}
    {% else %} {{ morning_eco_temperature | float(19.0) }} {% endif %}
  eff_m_comf: >-
    {% set ent = morning_comfort_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(21.0) }}
    {% else %} {{ morning_comfort_temperature | float(21.0) }} {% endif %}

  eff_d_eco: >-
    {% set ent = day_eco_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(19.0) }}
    {% else %} {{ day_eco_temperature | float(19.0) }} {% endif %}
  eff_d_comf: >-
    {% set ent = day_comfort_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(21.0) }}
    {% else %} {{ day_comfort_temperature | float(21.0) }} {% endif %}

  eff_e_eco: >-
    {% set ent = evening_eco_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(18.0) }}
    {% else %} {{ evening_eco_temperature | float(18.0) }} {% endif %}
  eff_e_comf: >-
    {% set ent = evening_comfort_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(21.0) }}
    {% else %} {{ evening_comfort_temperature | float(21.0) }} {% endif %}

  eff_n_eco: >-
    {% set ent = night_eco_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(17.0) }}
    {% else %} {{ night_eco_temperature | float(17.0) }} {% endif %}
  eff_n_comf: >-
    {% set ent = night_comfort_temperature_entity %}
    {% if ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] %} {{ states(ent) | float(21.0) }}
    {% else %} {{ night_comfort_temperature | float(21.0) }} {% endif %}

  # --- 4. STATUS & LOGIK ENGINE ---
  is_manual: >-
    {% set ent = manual_temperature_control %}
    {{ ent != [] and ent != none and is_state(ent, 'on') }}
    
  is_vacation: >-
    {% set ent = vacation_switch %}
    {{ ent != [] and ent != none and is_state(ent, 'on') }}

  is_room_occupied: >-
    {% set ent = room_presence %}
    {{ ent == [] or ent == none or is_state(ent, 'on') }}

  is_extern: >-
    {% set ent = extern_mode_switch %}
    {{ ent != [] and ent != none and is_state(ent, 'on') }}

  is_summer_toggle: >-
    {% set ent = summer_mode_toggle %}
    {{ ent != [] and ent != none and is_state(ent, 'on') }}

  is_summer_sensor: >-
    {% set ent = summer_sensor %}
    {{ ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', ''] and states(ent) | float(0) >= (summer_sensor_threshold | float(22.0)) }}

  is_summer: "{{ is_summer_toggle | bool or is_summer_sensor | bool }}"

  # --- KORREKTUR: ROBUSTE FENSTER-ERKENNUNG & ZÄHLUNG ---
  is_window_open: >-
    {% set ent = window_sensor %}
    {% if ent != [] and ent != none %}
      {% set members = state_attr(ent, 'entity_id') %}
      {% set targets = members if members != none else ent %}
      {{ expand(targets) | selectattr('state', 'eq', 'on') | list | length > 0 }}
    {% else %} false {% endif %}

  open_windows_text: >-
    {% set ent = window_sensor %}
    {% if ent != [] and ent != none %}
      {% set members = state_attr(ent, 'entity_id') %}
      {% set targets = members if members != none else ent %}
      {% set opens = expand(targets) | selectattr('state', 'eq', 'on') | list %}
      {% set count = opens | length %}
      {% if count > 0 %}
        {% set names = opens | map(attribute='name') | join(', ') %}
        {% if count == 1 %} 1 Fenster ({{ names }}) offen
        {% else %} {{ count }} Fenster ({{ names }}) offen {% endif %}
      {% else %} geschlossen {% endif %}
    {% else %} geschlossen {% endif %}

  is_away: >-
    {% if home_presence == [] or home_presence == none or is_state(home_presence, 'on') %} false
    {% else %}
      {% set entity_obj = states[home_presence] %}
      {% if entity_obj %}
        {{ (now() - entity_obj.last_changed).total_seconds() >= (no_home_presence_wait | float(0)) }}
      {% else %} false {% endif %}
    {% endif %}
    
  current_period: >-
    {% set t = now() %}
    {% set m = today_at(morning_time) %}
    {% set d = today_at(day_time) %}
    {% set e = today_at(evening_time) %}
    {% set n = today_at(night_time) %}
    {% if m <= t < d %} Morgen
    {% elif d <= t < e %} Tag
    {% elif e <= t < n %} Abend
    {% else %} Nacht {% endif %}
    
  # --- 5. DYNAMISCHE PRIORITÄTEN BERECHNUNG ---
  base_mode: >-
    {% if is_manual | bool %}manual
    {% elif is_summer | bool %}summer
    {% elif is_vacation | bool %}vacation
    {% elif is_extern | bool %}extern
    {% elif is_away | bool %}away
    {% else %}auto{% endif %}

  base_temp: >-
    {% if is_manual | bool %}
      {% set ent = manual_temperature %}
      {{ states(ent) | float(21.0) if (ent != [] and ent != none and states(ent) not in ['unknown', 'unavailable', 'none', '']) else 21.0 }}
    {% elif is_summer | bool %}{{ eff_summer | float }}
    {% elif is_vacation | bool %}{{ eff_vacation | float }}
    {% elif is_extern | bool %}{{ eff_extern | float }}
    {% elif is_away | bool %}{{ eff_away | float }}
    {% else %}
      {% if current_period == 'Morgen' %}{{ eff_m_comf | float if is_room_occupied | bool else eff_m_eco | float }}
      {% elif current_period == 'Tag' %}{{ eff_d_comf | float if is_room_occupied | bool else eff_d_eco | float }}
      {% elif current_period == 'Abend' %}{{ eff_e_comf | float if is_room_occupied | bool else eff_e_eco | float }}
      {% else %}{{ eff_n_comf | float if is_room_occupied | bool else eff_n_eco | float }}{% endif %}
    {% endif %}
    
  calc_target_temp: >-
    {% if is_window_open | bool and base_mode in window_active_modes %}
      {{ eff_window | float }}
    {% else %}
      {{ base_temp | float }}
    {% endif %}
    
  status_string: >-
    {% if is_window_open | bool and base_mode in window_active_modes %}🪟 {{ open_windows_text }} - Soll: {{ calc_target_temp }} °C
    {% elif base_mode == 'manual' %}✋ Manuell - Soll: {{ calc_target_temp }} °C
    {% elif base_mode == 'summer' %}☀️ Sommermodus - Soll: {{ calc_target_temp }} °C
    {% elif base_mode == 'vacation' %}🌴 Urlaub - Soll: {{ calc_target_temp }} °C
    {% elif base_mode == 'extern' %}🔥 Extern-Betrieb - Soll: {{ calc_target_temp }} °C
    {% elif base_mode == 'away' %}🚶 Abwesend - Soll: {{ calc_target_temp }} °C
    {% else %}
      {% set mode_str = 'Komf.' if is_room_occupied | bool else 'ECO' %}
      🕒 Auto ({{ current_period }} | {{ mode_str }}) - Soll: {{ calc_target_temp }} °C
    {% endif %}

trigger:
  - platform: state
    entity_id: !input climate_entity
    from: unavailable
  - platform: time
    at: !input morning_time
  - platform: time
    at: !input day_time
  - platform: time
    at: !input evening_time
  - platform: time
    at: !input night_time
  - platform: state
    entity_id: !input home_presence
  - platform: state
    entity_id: !input room_presence
  - platform: state
    entity_id: !input manual_temperature_control
  - platform: state
    entity_id: !input manual_temperature
  - platform: state
    entity_id: !input vacation_switch
  - platform: state
    entity_id: !input vacation_temperature_entity
  - platform: state
    entity_id: !input extern_mode_switch
  - platform: state
    entity_id: !input extern_temperature_entity
  - platform: state
    entity_id: !input summer_mode_toggle
  - platform: state
    entity_id: !input summer_sensor
  - platform: state
    entity_id: !input window_sensor
  - platform: state
    entity_id: !input away_temperature_entity

  # --- AUTO-RESET TRIGGERS (Nutzung der sicheren trigger_variables) ---
  - platform: template
    value_template: >-
      {% if trig_man_dur_en and trig_man_ctrl != [] and trig_man_ctrl != none %}
        {% if is_state(trig_man_ctrl, 'on') %}
          {{ (now() - states[trig_man_ctrl].last_changed).total_seconds() >= (trig_man_dur | float(120) * 60) }}
        {% else %}false{% endif %}
      {% else %}false{% endif %}
      
  - platform: template
    value_template: >-
      {% if trig_man_time_en and trig_man_ctrl != [] and trig_man_ctrl != none %}
        {{ is_state(trig_man_ctrl, 'on') and now().strftime('%H:%M') == trig_man_time[0:5] }}
      {% else %}false{% endif %}
      
  - platform: template
    value_template: >-
      {% if trig_man_pres and trig_man_ctrl != [] and trig_man_ctrl != none and trig_home != [] and trig_home != none %}
        {% if is_state(trig_man_ctrl, 'on') and is_state(trig_home, 'off') %}
          {{ (now() - states[trig_home].last_changed).total_seconds() >= (trig_home_wait | float(0)) }}
        {% else %}false{% endif %}
      {% else %}false{% endif %}
      
  - platform: template
    value_template: >-
      {% if trig_vac_days_en and trig_vac_switch != [] and trig_vac_switch != none %}
        {% if is_state(trig_vac_switch, 'on') %}
          {{ (now() - states[trig_vac_switch].last_changed).total_seconds() >= (trig_vac_days | float(14) * 86400) }}
        {% else %}false{% endif %}
      {% else %}false{% endif %}

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

action:
  # 0. Auto-Reset Ausführung (Schaltet die Schalter hart auf OFF)
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = manual_temperature_control %}
              {% if ent != [] and ent != none and is_state(ent, 'on') %}
                {% set cond_time = (manual_auto_off_time_enabled and now().strftime('%H:%M') == manual_auto_off_time[0:5]) %}
                {% set cond_dur = (manual_auto_off_duration_enabled and (now() - states[ent].last_changed).total_seconds() >= (manual_auto_off_duration | float(120) * 60)) %}
                {% set home = home_presence %}
                {% set cond_pres = (manual_auto_off_presence and home != [] and home != none and is_state(home, 'off') and (now() - states[home].last_changed).total_seconds() >= (no_home_presence_wait | float(0))) %}
                {{ cond_time or cond_dur or cond_pres }}
              {% else %}false{% endif %}
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: !input manual_temperature_control
          - stop: "Manueller Modus automatisch deaktiviert. Neustart der Automation abwarten."

      - conditions:
          - condition: template
            value_template: >-
              {% set ent = vacation_switch %}
              {% if ent != [] and ent != none and is_state(ent, 'on') %}
                {{ vacation_auto_off_days_enabled and (now() - states[ent].last_changed).total_seconds() >= (vacation_auto_off_days | float(14) * 86400) }}
              {% else %}false{% endif %}
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: !input vacation_switch
          - stop: "Urlaubsmodus automatisch deaktiviert. Neustart der Automation abwarten."

  # 1. Sommer-Status Helper aktualisieren
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = summer_status_boolean %}
              {{ ent != [] and ent != none and is_state(ent, 'on') == false and is_summer | bool == true }}
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: !input summer_status_boolean
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = summer_status_boolean %}
              {{ ent != [] and ent != none and is_state(ent, 'on') == true and is_summer | bool == false }}
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: !input summer_status_boolean

  # 2. Fenster-Status Helper aktualisieren
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = window_status_boolean %}
              {{ ent != [] and ent != none and is_state(ent, 'on') == false and (is_window_open | bool and base_mode in window_active_modes) == true }}
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: !input window_status_boolean
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = window_status_boolean %}
              {{ ent != [] and ent != none and is_state(ent, 'on') == true and (is_window_open | bool and base_mode in window_active_modes) == false }}
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: !input window_status_boolean

  # 3. Extern-Status Helper aktualisieren
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = extern_status_boolean %}
              {{ ent != [] and ent != none and is_state(ent, 'on') == false and is_extern | bool == true }}
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: !input extern_status_boolean
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = extern_status_boolean %}
              {{ ent != [] and ent != none and is_state(ent, 'on') == true and is_extern | bool == false }}
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: !input extern_status_boolean

  # 4. Status Text Helper aktualisieren
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set ent = status_text_entity %}
              {{ ent != [] and ent != none and states(ent) != status_string }}
        sequence:
          - service: input_text.set_value
            data:
              value: "{{ status_string }}"
            target:
              entity_id: !input status_text_entity

  # 5. Temperatur an Thermostat senden (Sicherer Float)
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {% set target = calc_target_temp | float(-99) %}
              {% set current = state_attr(climate_entity, 'temperature') | float(-99) %}
              {{ current != target }}
        sequence:
          - service: climate.set_temperature
            data:
              temperature: "{{ calc_target_temp | float }}"
            target:
              entity_id: !input climate_entity