###############################################################################
#   @author         :   Mahasri Kalavala
#   @date           :   11/19/2017
#   @package        :   Timers and Lights/Switches
#   @description    :   All lights and switches are now timer enabled
#                       and the timer automatically extends when there is
#                       motion. When timer elapses, it turns off lights.
###############################################################################
homeassistant:
  customize:
    timer.timer_kitchen:
      hidden: true
    timer.timer_familyroom:
      hidden: true
    timer.timer_frontroom:
      hidden: true
    timer.timer_garage:
      hidden: true
    timer.timer_diningroom:
      hidden: true
    timer.timer_masterbedroom:
      hidden: true
    timer.timer_masterbathroom:
      hidden: true
    timer.timer_officeroom:
      hidden: true

timer:
  timer_kitchen:
    duration: "00:10:00"
  timer_familyroom:
    duration: "00:05:00"
  timer_frontroom:
    duration: "00:05:00"
  timer_garage:
    duration: "00:05:00"
  timer_masterbedroom:
    duration: "00:05:00"
  timer_masterbathroom:
    duration: "00:05:00"
  timer_officeroom:
    duration: "00:05:00"

###############################################################################
#                _                        _   _
#     /\        | |                      | | (_)
#    /  \  _   _| |_ ___  _ __ ___   __ _| |_ _  ___  _ __  ___
#   / /\ \| | | | __/ _ \| '_ ` _ \ / _` | __| |/ _ \| '_ \/ __|
#  / ____ \ |_| | || (_) | | | | | | (_| | |_| | (_) | | | \__ \
# /_/    \_\__,_|\__\___/|_| |_| |_|\__,_|\__|_|\___/|_| |_|___/
#
###############################################################################

automation:
  # Master Bathroom:
  # Motion Detected - Turn ON the light and extend timer
  ###############################################################################
  - alias: Master Bathroom Motion & Timer
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d000464c25a
        to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: timer.start
        entity_id: timer.timer_masterbathroom
      - service: light.turn_on
        entity_id: light.master_bathroom_lights

  - alias: Master Bathroom Timer Elapsed
    initial_state: true
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_masterbathroom
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service_template: >
          {{ 'light.turn_off' if states('binary_sensor.motion_sensor_158d000464c25a') != 'on' else 'timer.start' }}
        data:
          entity_id: "{{ 'light.master_bathroom_lights' if states('binary_sensor.motion_sensor_158d000464c25a') != 'on' else 'timer.timer_masterbathroom' }}"

  # Office Room:
  # Motion Detected - Turn ON the light and extend timer
  ###############################################################################
  - alias: Officeroom Motion & Timer
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d000272bfd7
        to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: timer.start
        entity_id: timer.timer_officeroom
      - service: switch.turn_on
        entity_id: switch.office_room

  - alias: Officeroom Timer Elapsed
    initial_state: true
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_officeroom
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service_template: >
          {{ 'switch.turn_off' if states('binary_sensor.motion_sensor_158d000272bfd7') != 'on' else 'timer.start' }}
        data:
          entity_id: "{{ 'switch.office_room' if states('binary_sensor.motion_sensor_158d000272bfd7') != 'on' else 'timer.timer_officeroom' }}"

  # # Front Room:
  # # Motion Detected - Turn ON the light and extend timer
  # ###############################################################################
  - alias: Frontroom Motion & Timer
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d00016db6d2
        from: "off"
        to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: timer.start
        entity_id: timer.timer_frontroom
      - condition: template
        value_template: "{{ states('sun.sun') == 'below_horizon' }}"
      - service: switch.turn_on
        entity_id: switch.front_room

  # Kitchen:
  # Motion Detected - Turn ON the light and extend timer
  ###############################################################################
  - alias: Kitchen Motion & Timer
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.motion_sensor_158d0001a662fe
          - binary_sensor.kitchen_motion_sensor_sensor
          - binary_sensor.basement_door_sensor_sensor
        from: "off"
        to: "on"
    condition:
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_on
        entity_id: switch.kitchen
      - service: timer.start
        entity_id: timer.timer_kitchen
  # Kitchen:
  # Timer Elapsed - Turn OFF lights
  ###############################################################################
  - alias: Kitchen Timer Elapsed
    initial_state: true
    trigger:
      - platform: event
        event_type: timer.finished
        event_data:
          entity_id: timer.timer_kitchen
    condition:
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service_template: >
          {% if now().hour|int >= states('sensor.bedtime_hour') |int and
                now().minute|int >= states('sensor.bedtime_minute') |int %}
            switch.turn_off
          {% else %}
            switch.turn_on
          {% endif %}
        entity_id: switch.kitchen
  # Family Room:
  # Motion Detected - When TV is OFF, turn ON the light 100% and extend timer
  ###############################################################################
  - alias: Family Room Motion & Timer
    initial_state: true
    trigger:
      - platform: state
        entity_id:
          # - binary_sensor.tv_multi_sensor_sensor
          - binary_sensor.motion_sensor_158d0001a25041
        from: "off"
        to: "on"
    condition:
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - condition: template
        value_template: "{{ states('input_boolean.movie_time') != 'on' }}"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        entity_id: light.hue_color_lamp_1, light.hue_color_lamp_2, light.hue_color_lamp_3
        data_template:
          brightness: >
            {% if states('binary_sensor.sharp_tv') == "on" %}
              25
            {% else %}
              254
            {% endif %}
          color_temp: 154
          transition: 5
      - service: timer.start
        entity_id: timer.timer_familyroom
  # Family Room:
  # Timer Elapsed - Turn OFF lights
  ###############################################################################
  - alias: Family Room Timer Elapsed
    initial_state: true
    trigger:
      - platform: state
        entity_id: timer.timer_familyroom
        from: "active"
        to: "idle"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_off
        entity_id: light.hue_color_lamp_1, light.hue_color_lamp_2, light.hue_color_lamp_3
      - delay: "00:00:05"
      - service: light.turn_off
        entity_id: light.hue_color_lamp_1, light.hue_color_lamp_2, light.hue_color_lamp_3
  # Master Bedroom:
  # Motion Detected - Turn ON the lights and extend timer
  ###############################################################################
  - alias: Master Bedroom Motion & Timer
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.upstairs_multi_sensor_sensor
        from: "off"
        to: "on"
    condition:
      - condition: state
        entity_id: sun.sun
        state: "below_horizon"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        data:
          entity_id: light.master_bedroom_1
          rgb_color: [224, 175, 102]
          brightness: 10
      - service: light.turn_on
        data:
          entity_id: light.master_bedroom_2
          rgb_color: [224, 175, 102]
          brightness: 10
      - service: timer.start
        entity_id: timer.timer_masterbedroom

  # Master Bedroom:
  # Timer Elapsed - Turn OFF lights
  ###############################################################################
  - alias: Master Bedroom Timer Elapsed
    initial_state: true
    trigger:
      - platform: state
        entity_id: timer.timer_masterbedroom
        from: "active"
        to: "idle"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_off
        entity_id: light.master_bedroom_1
      - service: light.turn_off
        entity_id: light.master_bedroom_2

  # Start the timer when the lights are ON
  ###############################################################################
  - alias: Garage Timer Start When Garage Lights ON
    initial_state: true
    trigger:
      - platform: state
        entity_id: switch.garage
        from: "off"
        to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: timer.start
        entity_id: timer.timer_garage

  # Garage:
  # Timer Elapsed - Turn OFF lights, when "I am not working"
  ###############################################################################
  - alias: Garage Timer Elapsed
    initial_state: true
    trigger:
      - platform: state
        entity_id: timer.timer_garage
        from: "active"
        to: "idle"
    condition:
      - condition: state
        entity_id: input_boolean.working_in_garage
        state: "off"
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_off
        entity_id: switch.garage