- id: stue_motion_lys
  alias: Stue lys via bevægelse
  mode: restart

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

  condition:
    - condition: numeric_state
      entity_id: sensor.stue_belysningsstyrke
      below: 60

  action:
    - choose:
        - conditions:
            - condition: time
              after: "06:00:00"
              before: "16:00:00"
          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.stue_bright
        - conditions:
            - condition: time
              after: "16:00:00"
              before: "22:00:00"
          sequence:
            - service: scene.turn_on
              target:
                entity_id: scene.stue_annes_favorit
      default:
        - service: scene.turn_on
          target:
            entity_id: scene.stue_relax_minus_syd

- id: stue_motion_sluk_morgen
  alias: Sluk stue lys efter 60 min uden bevægelse om morgenen
  mode: restart

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

  condition:
    - condition: time
      after: "06:00:00"
      before: "16:00:00"

  action:
    - wait_for_trigger:
        - platform: state
          entity_id: binary_sensor.stue_bevaegelse
          to: "on"
      timeout: "01:00:00"
      continue_on_timeout: true
    - condition: state
      entity_id: binary_sensor.stue_bevaegelse
      state: "off"
    - service: light.turn_off
      target:
        entity_id: light.livingroom

- id: stue_motion_sluk_eftermiddag_aften
  alias: Sluk stue lys efter 120 min uden bevægelse mellem 16 og 19
  mode: restart

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

  condition:
    - condition: time
      after: "16:00:00"
      before: "19:00:00"

  action:
    - wait_for_trigger:
        - platform: state
          entity_id: binary_sensor.stue_bevaegelse
          to: "on"
      timeout: "02:00:00"
      continue_on_timeout: true
    - condition: state
      entity_id: binary_sensor.stue_bevaegelse
      state: "off"
    - service: light.turn_off
      target:
        entity_id: light.livingroom

- id: stue_motion_sluk_aften_tv
  alias: Sluk stue lys efter 10 min uden bevægelse når TV er slukket mellem 19 og 00
  mode: restart

  trigger:
    - platform: state
      entity_id: binary_sensor.stue_bevaegelse
      to: "off"
    - platform: state
      entity_id: media_player.samsung_s95ca_55_3
      to: "off"

  condition:
    - condition: time
      after: "19:00:00"
      before: "00:00:00"

  action:
    - condition: state
      entity_id: media_player.samsung_s95ca_55_3
      state: "off"
    - wait_for_trigger:
        - platform: state
          entity_id: binary_sensor.stue_bevaegelse
          to: "on"
      timeout: "00:10:00"
      continue_on_timeout: true
    - condition: state
      entity_id: binary_sensor.stue_bevaegelse
      state: "off"
    - condition: state
      entity_id: media_player.samsung_s95ca_55_3
      state: "off"
    - service: light.turn_off
      target:
        entity_id: light.livingroom

- id: stue_motion_sluk_nat
  alias: Sluk stue lys efter 30 min uden bevægelse om natten
  mode: restart

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

  condition:
    - condition: time
      after: "00:00:00"
      before: "06:00:00"

  action:
    - wait_for_trigger:
        - platform: state
          entity_id: binary_sensor.stue_bevaegelse
          to: "on"
      timeout: "00:30:00"
      continue_on_timeout: true
    - condition: state
      entity_id: binary_sensor.stue_bevaegelse
      state: "off"
    - service: light.turn_off
      target:
        entity_id: light.livingroom