- id: garage_auto_claus_presence
  alias: Garage auto Claus
  description: >
    Åbn garage ved ankomst i bil, luk ved afrejse.
    Bruger input_boolean.left_home_by_car til at huske at afrejsen var i bil,
    så ankomst-logikken ikke afhænger af at Tesla-trackeren opdaterer hurtigt.
    Leaving-trigger bruger garageport=on som bevis for bil (Tesla speed er for langsom).

  trigger:
    # Telefon ankommer hjem (debounce 10s mod GPS-flimmer)
    - platform: state
      entity_id: device_tracker.claus_iphone_15pro
      to: "home"
      for:
        seconds: 10
      id: arriving

    # Bil ankommer hjem (backup hvis telefon allerede var "home" via WiFi)
    - platform: state
      entity_id: device_tracker.snowywhite_location_tracker
      to: "home"
      id: car_arriving

    # Telefon forlader hjemmet (debounce 10s mod GPS-flimmer)
    - platform: state
      entity_id: device_tracker.claus_iphone_15pro
      from: "home"
      for:
        seconds: 10
      id: leaving

  condition:
    - condition: time
      after: "05:00:00"
      before: "23:00:00"

  action:
    - choose:

        # ÅBN via telefon: garage lukket + forlod hjemmet i bil
        - conditions:
            - condition: trigger
              id: arriving

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

            # Vi ved at brugeren kørte hjemmefra
            - condition: state
              entity_id: input_boolean.left_home_by_car
              state: "on"

          sequence:
            - service: cover.toggle
              target:
                entity_id: cover.anne

            - service: input_boolean.turn_off
              target:
                entity_id: input_boolean.left_home_by_car

            - service: notify.mobile_app_claus_iphone_15pro
              data:
                message: "Garage åbnet (ankomst i bil)"

        # ÅBN via bil-tracker: telefon allerede hjemme + forlod i bil
        - conditions:
            - condition: trigger
              id: car_arriving

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

            - condition: state
              entity_id: device_tracker.claus_iphone_15pro
              state: "home"

            - condition: state
              entity_id: input_boolean.left_home_by_car
              state: "on"

          sequence:
            - service: cover.toggle
              target:
                entity_id: cover.anne

            - service: input_boolean.turn_off
              target:
                entity_id: input_boolean.left_home_by_car

            - service: notify.mobile_app_claus_iphone_15pro
              data:
                message: "Garage åbnet (bil-tracker ankomst)"

        # LUK: garage åben + forlader hjemmet => kører i bil
        - conditions:
            - condition: trigger
              id: leaving

            - condition: state
              entity_id: binary_sensor.garageport
              state: "on"

          sequence:
            # Markér at vi kørte hjemmefra (bruges ved ankomst)
            - service: input_boolean.turn_on
              target:
                entity_id: input_boolean.left_home_by_car

            - delay: "00:00:30"

            # Dobbelttjek at garagen stadig er åben efter forsinkelsen
            - condition: state
              entity_id: binary_sensor.garageport
              state: "on"

            - service: cover.toggle
              target:
                entity_id: cover.anne

            - service: notify.mobile_app_claus_iphone_15pro
              data:
                message: "Garage lukket (afrejse i bil)"

  mode: single
  max_exceeded: silent
