# 🏠 Track hvornår huset bliver tomt
- id: track_house_empty_time
  alias: Track House Empty Time
  mode: restart

  trigger:
    - platform: state
      entity_id: binary_sensor.family_presence
      to: "off"

    - platform: homeassistant
      event: start

  condition:
    - condition: state
      entity_id: binary_sensor.family_presence
      state: "off"

  action:
    - service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.house_became_empty
      data:
        timestamp: "{{ now().timestamp() }}"


# 🔄 Reset daglig tæller
- id: roborock_reset_daily_counter
  alias: Roborock Reset Daily Counter

  trigger:
    - platform: time
      at: "00:00:00"

  action:
    - service: input_number.set_value
      target:
        entity_id: input_number.roborock_auto_runs_today
      data:
        value: 0


# 🤖 Smart rengøring
- id: roborock_smart_cleaning_start
  alias: Roborock Smart Cleaning Start
  mode: single

  trigger:
    - platform: state
      entity_id: binary_sensor.family_presence
      to: "off"
      for: "00:05:00"

  variables:
    roborock_cleaned_area: "{{ state_attr('vacuum.roborock_s8_pro_ultra', 'cleaned_area') | default('ukendt', true) }}"
    roborock_cleaning_time: "{{ state_attr('vacuum.roborock_s8_pro_ultra', 'cleaning_time') | default('ukendt', true) }}"

  action:
    # Vent til Roborock er vækket og rapporterer tilstand
    - wait_template: >
        {{ states('vacuum.roborock_s8_pro_ultra') not in ['unknown', 'unavailable']
           and states('sensor.roborock_s8_pro_ultra_batteri') not in ['unknown', 'unavailable'] }}
      timeout: "00:02:00"

    # Genberegn variabler EFTER Roborock er vågnet
    - variables:
        roborock_family_away: "{{ is_state('binary_sensor.family_presence', 'off') }}"
        roborock_auto_runs: "{{ states('input_number.roborock_auto_runs_today') | int(0) }}"
        roborock_is_cleaning: "{{ is_state('vacuum.roborock_s8_pro_ultra', 'cleaning') }}"
        roborock_battery: "{{ states('sensor.roborock_s8_pro_ultra_batteri') | int(0) }}"
        roborock_state: "{{ states('vacuum.roborock_s8_pro_ultra') }}"
        roborock_status: "{{ state_attr('vacuum.roborock_s8_pro_ultra', 'status') | default('ukendt', true) }}"
        roborock_error: "{{ state_attr('vacuum.roborock_s8_pro_ultra', 'error') | default('ingen', true) }}"

    - choose:
        - conditions:
            - condition: template
              value_template: >
                {{
                  roborock_family_away
                  and roborock_auto_runs < 2
                  and not roborock_is_cleaning
                  and roborock_state not in ['error', 'unknown', 'unavailable']
                  and roborock_battery > 20
                }}
          sequence:
            - service: button.press
              target:
                entity_id: button.roborock_s8_pro_ultra_kokken_bryggers

            - delay: "00:00:20"

            - choose:
                - conditions:
                    - condition: state
                      entity_id: vacuum.roborock_s8_pro_ultra
                      state: "cleaning"
                  sequence:
                    - service: input_number.increment
                      target:
                        entity_id: input_number.roborock_auto_runs_today

                    - service: notify.mobile_app_claus_iphone_15pro
                      data:
                        title: "🧹 Roborock startet"
                        message: >
                          Hus har været tomt i {{
                          ((as_timestamp(now()) - state_attr('input_datetime.house_became_empty','timestamp')) / 60) | int
                          }} min.

                - conditions:
                    - condition: not
                      conditions:
                        - condition: state
                          entity_id: vacuum.roborock_s8_pro_ultra
                          state: "cleaning"
                  sequence:
                    - service: notify.mobile_app_claus_iphone_15pro
                      data:
                        title: "⚠️ Roborock start fejlede"
                        message: >
                          Startkommando sendt, men den begyndte ikke at køre.
                          State: {{ states('vacuum.roborock_s8_pro_ultra') }}.
                          Status: {{ state_attr('vacuum.roborock_s8_pro_ultra', 'status') | default('ukendt', true) }}.
                          Error: {{ state_attr('vacuum.roborock_s8_pro_ultra', 'error') | default('ingen', true) }}.
                          Batteri: {{ states('sensor.roborock_s8_pro_ultra_batteri') }}%.
                          Cleaned area: {{ state_attr('vacuum.roborock_s8_pro_ultra', 'cleaned_area') | default('ukendt', true) }}.
                          Cleaning time: {{ state_attr('vacuum.roborock_s8_pro_ultra', 'cleaning_time') | default('ukendt', true) }}.
      default:
        - service: notify.mobile_app_claus_iphone_15pro
          data:
            title: "ℹ️ Roborock ikke startet"
            message: >
              Smart rengøring blev sprunget over.
              Familie hjemme: {{ not roborock_family_away }}.
              Auto-kørsler i dag: {{ roborock_auto_runs }}.
              Kører allerede: {{ roborock_is_cleaning }}.
              State: {{ roborock_state }}.
              Batteri: {{ roborock_battery }}%.


# 🏠 Stop når nogen kommer hjem
- id: roborock_stop_when_home
  alias: Roborock Stop When Someone Comes Home
  mode: restart

  trigger:
    - platform: state
      entity_id: binary_sensor.family_presence
      to: "on"

  condition:
    - condition: state
      entity_id: vacuum.roborock_s8_pro_ultra
      state: "cleaning"

  action:
    - service: vacuum.return_to_base
      target:
        entity_id: vacuum.roborock_s8_pro_ultra

    - service: notify.mobile_app_claus_iphone_15pro
      data:
        title: "🏠 Roborock stoppet"
        message: "Rengøring stoppet fordi nogen er kommet hjem."


# 🧹 Syd på arbejdsdage
- id: roborock_syd_workday_vacuum
  alias: Roborock støvsug syd på arbejdsdage
  mode: single

  trigger:
    - platform: time
      at: "10:00:00"

  condition:
    - condition: state
      entity_id: binary_sensor.family_presence
      state: "off"

    - condition: state
      entity_id: binary_sensor.arbejdsdag
      state: "on"
    - condition: not
      conditions:
        - condition: state
          entity_id: vacuum.roborock_s8_pro_ultra
          state: "cleaning"

  action:
    - service: button.press
      target:
        entity_id: button.roborock_s8_pro_ultra_syd


# 🧽 Syd vask på arbejdsdage mandag og onsdag
- id: roborock_syd_workday_mop
  alias: Roborock vask syd på arbejdsdage mandag og onsdag
  mode: single

  trigger:
    - platform: time
      at: "12:00:00"

  condition:
    - condition: state
      entity_id: binary_sensor.family_presence
      state: "off"

    - condition: state
      entity_id: binary_sensor.arbejdsdag
      state: "on"
    - condition: time
      weekday:
        - mon
        - wed
    - condition: not
      conditions:
        - condition: state
          entity_id: vacuum.roborock_s8_pro_ultra
          state: "cleaning"

  action:
    - service: button.press
      target:
        entity_id: button.roborock_s8_pro_ultra_syd_vask
