###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   04/15/2017
#   @package        :   Daily Light Routine around the house
#   @description    :   Daily light routine stuff
###############################################################################

automation:
  #  ÛÛÛÛÛÛ   ÛÛÛÛÛÛ                                ÛÛÛ
  # °°ÛÛÛÛÛÛ ÛÛÛÛÛÛ                                °°°
  #  °ÛÛÛ°ÛÛÛÛÛ°ÛÛÛ   ÛÛÛÛÛÛ  ÛÛÛÛÛÛÛÛ  ÛÛÛÛÛÛÛÛ   ÛÛÛÛ  ÛÛÛÛÛÛÛÛ    ÛÛÛÛÛÛÛ
  #  °ÛÛÛ°°ÛÛÛ °ÛÛÛ  ÛÛÛ°°ÛÛÛ°°ÛÛÛ°°ÛÛÛ°°ÛÛÛ°°ÛÛÛ °°ÛÛÛ °°ÛÛÛ°°ÛÛÛ  ÛÛÛ°°ÛÛÛ
  #  °ÛÛÛ °°°  °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ °°°  °ÛÛÛ °ÛÛÛ  °ÛÛÛ  °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ
  #  °ÛÛÛ      °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ      °ÛÛÛ °ÛÛÛ  °ÛÛÛ  °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ
  #  ÛÛÛÛÛ     ÛÛÛÛÛ°°ÛÛÛÛÛÛ  ÛÛÛÛÛ     ÛÛÛÛ ÛÛÛÛÛ ÛÛÛÛÛ ÛÛÛÛ ÛÛÛÛÛ°°ÛÛÛÛÛÛÛ
  # °°°°°     °°°°°  °°°°°°  °°°°°     °°°° °°°°° °°°°° °°°° °°°°°  °°°°°ÛÛÛ
  #                                                                 ÛÛÛ °ÛÛÛ
  #                                                                °°ÛÛÛÛÛÛ
  #                                                                 °°°°°°
  ###############################################################################
  # Turn Indoor lights ON only during weekdays
  ###############################################################################
  - alias: Morning Indoor Lights ON
    initial_state: true
    trigger:
      platform: time_pattern
      minutes: "/5"
      seconds: 00
    condition:
      - condition: state
        entity_id: "binary_sensor.workday_sensor"
        state: "on"
      - condition: template
        value_template: "{{ states('sensor.wakeup_hour') |int == now().hour |int }}"
      - condition: template
        value_template: "{{ states('sensor.wakeup_minute') |int == now().minute|int }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_on
        data:
          entity_id: switch.kitchen_switch

  ###############################################################################
  # Turn ON Master Bedroom lights ON in the morning
  # Turn On Bedroom Lights 10 minutes after wake up time
  ###############################################################################
  - alias: Morning Master Bedroom Lights ON (Mallika)
    initial_state: true
    trigger:
      platform: time_pattern
      minutes: "/5"
      seconds: 00
    condition:
      - condition: state
        entity_id: "binary_sensor.workday_sensor"
        state: "on"
      - condition: template
        value_template: "{{ states('sensor.wakeup_hour') |int == now().hour|int }}"
      - condition: template
        value_template: "{{ now().minute | int == (states('sensor.wakeup_minute') | int + 10) }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        data:
          entity_id: light.master_bedroom_2
          transition: 15
          brightness: 25
          color_temp: 154
      - service: switch.turn_on
        data:
          entity_id: switch.chromecast_monitor

  - alias: Morning Master Bedroom Lights OFF (Mallika)
    initial_state: true
    trigger:
      platform: time_pattern
      minutes: "/5"
      seconds: 00
    condition:
      - condition: state
        entity_id: "binary_sensor.workday_sensor"
        state: "on"
      - condition: template
        value_template: "{{ states('sensor.wakeup_hour') |int == now().hour|int }}"
      - condition: template
        value_template: "{{ now().minute|int == (states('sensor.wakeup_minute') |int + 15) }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_off
        data:
          entity_id: light.master_bedroom_2

  ###############################################################################
  # Turn off outdoor lights 15 minutes after sunrise
  ###############################################################################
  - alias: Morning Outdoor Lights OFF
    initial_state: true
    trigger:
      platform: sun
      event: sunrise
      offset: "00:15:00"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_off
        entity_id: switch.frontyard_light
      - service: switch.turn_off
        entity_id: switch.backyard_light

  ###############################################################################
  # Turn OF Kitchen Lights after 3 hours of wake up time during school/work days
  ###############################################################################
  - alias: Morning Kitchen Light OFF
    initial_state: true
    trigger:
      platform: time_pattern
      minutes: "/5"
      seconds: 00
    condition:
      - condition: state
        entity_id: "binary_sensor.workday_sensor"
        state: "on"
      - condition: template
        value_template: "{{ now().hour|int == (states('sensor.wakeup_hour') |int + 3) }}"
      - condition: template
        value_template: "{{ states('sensor.wakeup_minute') |int == now().minute|int }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_off
        entity_id: switch.kitchen_switch
      - service: switch.turn_off
        entity_id: switch.front_room

  #  ÛÛÛÛÛÛÛÛÛÛ                                  ÛÛÛ
  # °°ÛÛÛ°°°°°Û                                 °°°
  #  °ÛÛÛ  Û °  ÛÛÛÛÛ ÛÛÛÛÛ  ÛÛÛÛÛÛ  ÛÛÛÛÛÛÛÛ   ÛÛÛÛ  ÛÛÛÛÛÛÛÛ    ÛÛÛÛÛÛÛ
  #  °ÛÛÛÛÛÛ   °°ÛÛÛ °°ÛÛÛ  ÛÛÛ°°ÛÛÛ°°ÛÛÛ°°ÛÛÛ °°ÛÛÛ °°ÛÛÛ°°ÛÛÛ  ÛÛÛ°°ÛÛÛ
  #  °ÛÛÛ°°Û    °ÛÛÛ  °ÛÛÛ °ÛÛÛÛÛÛÛ  °ÛÛÛ °ÛÛÛ  °ÛÛÛ  °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ
  #  °ÛÛÛ °   Û °°ÛÛÛ ÛÛÛ  °ÛÛÛ°°°   °ÛÛÛ °ÛÛÛ  °ÛÛÛ  °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ
  #  ÛÛÛÛÛÛÛÛÛÛ  °°ÛÛÛÛÛ   °°ÛÛÛÛÛÛ  ÛÛÛÛ ÛÛÛÛÛ ÛÛÛÛÛ ÛÛÛÛ ÛÛÛÛÛ°°ÛÛÛÛÛÛÛ
  # °°°°°°°°°°    °°°°°     °°°°°°  °°°° °°°°° °°°°° °°°° °°°°°  °°°°°ÛÛÛ
  #                                                              ÛÛÛ °ÛÛÛ
  #                                                             °°ÛÛÛÛÛÛ
  #                                                              °°°°°°
  ###############################################################################
  # Turn outdoor lights on 15 minutes before sunset
  ###############################################################################
  - alias: Evening Outdoor Lights ON (At Sunset)
    #    initial_state: true
    trigger:
      platform: sun
      event: sunset
      offset: "+00:00:00"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_on
        entity_id: switch.frontyard_light
      - service: switch.turn_on
        entity_id: switch.backyard_light

  - alias: Christmas Lights ON (At Sunset)
    trigger:
      platform: sun
      event: sunset
      offset: "+00:00:00"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_on
        entity_id: switch.left
      - service: switch.turn_on
        entity_id: switch.backyard_light

  - alias: Christmas Lights OFF (At Sunrise)
    initial_state: true
    trigger:
      platform: sun
      event: sunrise
      offset: "00:15:00"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_off
        entity_id: switch.left
      - service: switch.turn_off
        entity_id: switch.backyard_light

  ###############################################################################
  # Turn indoor lights on 30 minutes before sunset
  ###############################################################################
  - alias: Evening Indoor Lights ON Before Sunset
    initial_state: true
    trigger:
      platform: sun
      event: sunset
      offset: "-00:30:00"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
      - condition: state
        entity_id: input_boolean.movie_time
        state: "off"
    action:
      - service: switch.turn_on
        data:
          entity_id: switch.kitchen_switch
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_1
          transition: 3000
          brightness: 255
          color_temp: 154
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_2
          transition: 3000
          brightness: 255
          color_temp: 154
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_3
          transition: 3000
          brightness: 255
          color_temp: 154
      - service: light.turn_on
        data:
          entity_id: light.master_bedroom_1
          brightness: 25
          color_temp: 366
      - service: light.turn_on
        data:
          entity_id: light.master_bedroom_2
          brightness: 25
          color_temp: 366
      - service: switch.turn_on
        entity_id: switch.hasika_bed_accent
      - service: switch.turn_on
        entity_id: switch.front_room
      - service: switch.turn_on
        entity_id: switch.officeroom_accent_lights

  #  ÛÛÛÛÛÛ   ÛÛÛÛÛ  ÛÛÛ           ÛÛÛÛÛ       ÛÛÛÛÛ
  # °°ÛÛÛÛÛÛ °°ÛÛÛ  °°°           °°ÛÛÛ       °°ÛÛÛ
  #  °ÛÛÛ°ÛÛÛ °ÛÛÛ  ÛÛÛÛ   ÛÛÛÛÛÛÛ °ÛÛÛÛÛÛÛ   ÛÛÛÛÛÛÛ
  #  °ÛÛÛ°°ÛÛÛ°ÛÛÛ °°ÛÛÛ  ÛÛÛ°°ÛÛÛ °ÛÛÛ°°ÛÛÛ °°°ÛÛÛ°
  #  °ÛÛÛ °°ÛÛÛÛÛÛ  °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ   °ÛÛÛ
  #  °ÛÛÛ  °°ÛÛÛÛÛ  °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ °ÛÛÛ   °ÛÛÛ ÛÛÛ
  #  ÛÛÛÛÛ  °°ÛÛÛÛÛ ÛÛÛÛÛ°°ÛÛÛÛÛÛÛ ÛÛÛÛ ÛÛÛÛÛ  °°ÛÛÛÛÛ
  # °°°°°    °°°°° °°°°°  °°°°°ÛÛÛ°°°° °°°°°    °°°°°
  #                       ÛÛÛ °ÛÛÛ
  #                      °°ÛÛÛÛÛÛ
  #                       °°°°°°
  ###############################################################################
  # Set the mood for bed time - Dim Family Room lights if they are ON
  ###############################################################################
  - alias: Night Dim TV Lights
    initial_state: true
    trigger:
      platform: time_pattern
      minutes: "/5"
      seconds: 00
    condition:
      - condition: template
        value_template: "{{ states('sensor.bedtime_hour') |int == now().hour|int }}"
      - condition: template
        value_template: "{{ (states('sensor.bedtime_minute') |int - 15 ) == now().minute|int }}"
      # - condition: state
      #   entity_id: light.family_room
      #   state: "on"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        data:
          entity_id: light.hue_color_lamp_1, light.hue_color_lamp_2, light.hue_color_lamp_3
          brightness: 30
          color_temp: 154
          transition: 15

  ###############################################################################
  # TURN INDOOR LIGHTS OFF AFTER BED TIME
  ###############################################################################
  - alias: Night Turn Lights Off
    initial_state: true
    trigger:
      platform: time_pattern
      minutes: "/5"
      seconds: 00
    condition:
      - condition: template
        value_template: "{{ states('sensor.bedtime_hour') |int == now().hour|int }}"
      - condition: template
        value_template: "{{ states('sensor.bedtime_minute') |int == now().minute|int }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: script.all_indoor_lights_off

  ###############################################################################
  # Provide Bed time Report via TTS
  ###############################################################################
  - alias: Night Bedtime Report
    initial_state: true
    trigger:
      platform: time
      at: "22:10:00"
    condition:
      - condition: template
        value_template: "{{ states('input_boolean.nightly_report') == 'on' }}"
    action:
      - service: script.good_night_tts

  ###############################################################################
  # TOGGLE WORKING IN OFFICE BOOLEAN
  ###############################################################################

  - alias: Suresh Working in Office Start
    initial_state: true
    trigger:
      platform: time
      at: "08:00:00"
    condition:
      - condition: state
        entity_id: "binary_sensor.workday_sensor"
        state: "on"
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.working_in_office_room

  - alias: Suresh Working in Office End
    initial_state: true
    trigger:
      platform: time
      at: "18:00:00"
    condition:
      - condition: state
        entity_id: "binary_sensor.workday_sensor"
        state: "on"
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.working_in_office_room
