- alias: Presence Simulation - Tidsblokke (realistisk)
  mode: restart

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

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

  action:
    - variables:
        lights_morning:
          - light.livingroom
          - light.indkorsel_2
          - light.andreas_vaerelse
          - light.bedroom
          - light.daniels_vaerelse
          - light.forgang

        lights_midday:
          - light.livingroom


        lights_evening:
          - light.livingroom
          - light.indkorsel_2
          - light.andreas_vaerelse
          - light.bedroom
          - light.daniels_vaerelse
          - light.forgang

        rand_music: "{{ range(0,100) | random }}"

    - choose:
        # 🌅 MORGEN
        - conditions:
            - condition: time
              after: "06:59:00"
              before: "12:00:00"
          sequence:
            - repeat:
                for_each: "{{ lights_morning }}"
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: "{{ repeat.item }}"
                  - delay:
                      seconds: "{{ range(30,180) | random }}"

            - delay:
                minutes: "{{ range(10,30) | random }}"

            - repeat:
                for_each: "{{ lights_morning | reverse }}"
                sequence:
                  - service: light.turn_off
                    target:
                      entity_id: "{{ repeat.item }}"
                  - delay:
                      seconds: "{{ range(20,120) | random }}"

            - choose:
                - conditions:
                    - condition: template
                      value_template: "{{ rand_music < 20 }}"
                  sequence:
                    - service: media_player.play_media
                      target:
                        entity_id: media_player.kokken
                      data:
                        media_content_type: music
                        media_content_id: "spotify:playlist:37i9dQZF1EVHGWrwldPRtj"

                    - delay:
                        minutes: "{{ range(5,15) | random }}"

                    - service: media_player.media_stop
                      target:
                        entity_id: media_player.kokken

        # 🌤️ MIDDAG
        - conditions:
            - condition: time
              after: "11:59:00"
              before: "18:00:00"
          sequence:
            - repeat:
                for_each: "{{ lights_midday }}"
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: "{{ repeat.item }}"
                  - delay:
                      seconds: "{{ range(60,300) | random }}"

            - delay:
                minutes: "{{ range(5,20) | random }}"

            - repeat:
                for_each: "{{ lights_midday | reverse }}"
                sequence:
                  - service: light.turn_off
                    target:
                      entity_id: "{{ repeat.item }}"
                  - delay:
                      seconds: "{{ range(30,180) | random }}"

            - choose:
                - conditions:
                    - condition: template
                      value_template: "{{ rand_music < 30 }}"
                  sequence:
                    - service: media_player.play_media
                      target:
                        entity_id: media_player.kokken
                      data:
                        media_content_type: music
                        media_content_id: "spotify:playlist:37i9dQZF1EVHGWrwldPRtj"

                    - delay:
                        minutes: "{{ range(5,15) | random }}"

                    - service: media_player.media_stop
                      target:
                        entity_id: media_player.kokken

        # 🌙 AFTEN
        - conditions:
            - condition: time
              after: "17:59:00"
              before: "22:30:00"
          sequence:
            - repeat:
                for_each: "{{ lights_evening }}"
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: "{{ repeat.item }}"
                  - delay:
                      seconds: "{{ range(20,120) | random }}"

            - delay:
                minutes: "{{ range(15,60) | random }}"

            - repeat:
                for_each: "{{ lights_evening | reverse }}"
                sequence:
                  - service: light.turn_off
                    target:
                      entity_id: "{{ repeat.item }}"
                  - delay:
                      seconds: "{{ range(20,120) | random }}"

            - choose:
                - conditions:
                    - condition: template
                      value_template: "{{ rand_music < 50 }}"
                  sequence:
                    - service: media_player.play_media
                      target:
                        entity_id: media_player.kokken
                      data:
                        media_content_type: music
                        media_content_id: "spotify:playlist:37i9dQZF1EVHGWrwldPRtj"

                    - delay:
                        minutes: "{{ range(10,20) | random }}"

                    - service: media_player.media_stop
                      target:
                        entity_id: media_player.kokken
