homeassistant:
  customize:
    package.door_sensors:
      door_notification_script: &door_notification_script
        message: >
          {% set doors = "" %}
            {% if states('binary_sensor.door_window_sensor_158d0004231f7b') == "on"  and
                  states('binary_sensor.door_window_sensor_158d0004248d5b') == "on" %}
              {% set doors = "Attention! Both Garage Doors are OPEN" %}
            {% elif states('binary_sensor.door_window_sensor_158d0004231f7b') == "off"  and
                    states('binary_sensor.door_window_sensor_158d0004248d5b') == "off" %}
              {% set doors = "Both Garage Doors are now CLOSED" %}
            {% else %}
              {% if trigger.from_state.state != trigger.to_state.state and trigger.to_state.state | lower == "on" %}
                Attention! Your {{ trigger.to_state.attributes.friendly_name }} is now OPENED!
              {% elif trigger.from_state.state != trigger.to_state.state and trigger.to_state.state | lower == "off" %}
                Your {{ trigger.to_state.attributes.friendly_name }} is now CLOSED!
              {% endif %}
            {% endif %}
          {{ doors }}

sensor:
  - platform: template
    sensors:
      single_car_garage_door_sensor_status:
        friendly_name: Single Car Garage Door Sensor Status
        value_template: >-
          {% if states('binary_sensor.door_window_sensor_158d0004248d5b') == 'on' %}
            Open
          {% elif states('binary_sensor.door_window_sensor_158d0004248d5b') == 'off' %}
            Closed
          {% else %}
            Unknown
          {% endif %}
        icon_template: >-
          {% set door_status = states('binary_sensor.door_window_sensor_158d0004248d5b') %}
          {% if door_status == 'unknown' %}
            mdi:alert-circle
          {% else %}
            {% if door_status == 'on' %}
              mdi:garage-open
            {% else %}
              mdi:garage
            {% endif %}
          {% endif %}

  - platform: template
    sensors:
      two_car_garage_door_sensor_status:
        value_template: >-
          {% if states('binary_sensor.door_window_sensor_158d0004231f7b') == 'on' %}
            Open
          {% elif states('binary_sensor.door_window_sensor_158d0004231f7b') == 'off' %}
            Closed
          {% else %}
            Unknown
          {% endif %}
        friendly_name: Double Car Garage Door Sensor Status
        icon_template: >-
          {% set door_status = states('binary_sensor.door_window_sensor_158d0004231f7b') %}
          {% if door_status == 'unknown' %}
            mdi:alert-circle
          {% else %}
            {% if door_status == 'on' %}
              mdi:garage-open
            {% else %}
              mdi:garage
            {% endif %}
          {% endif %}

binary_sensor:
  - platform: mqtt
    state_topic: "/garage/motion"
    name: "Garage Motion"
    device_class: motion
    payload_on: "on"
    payload_off: "off"
    value_template: "{{ value }}"


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

automation:
  - alias: Garage Motion Reset
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.garage_motion
        to: "on"
        from: "off"
    action:
      - delay: "00:00:30"
      - service: mqtt.publish
        data:
          topic: "/garage/motion"
          payload: "off"
          retain: false

  - alias: Turn Garage Lights On When Motion Detected or Door Opened
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d000272bf48
        to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_on
        entity_id: switch.garage

  ################################################################################
  # Notifies when either of the garage door is opened or closed
  ###############################################################################
  - alias: Notify Garage Door Status
    initial_state: true
    trigger:
      platform: state
      entity_id:
        - binary_sensor.door_window_sensor_158d0004231f7b # 2 Car Garage
        - binary_sensor.door_window_sensor_158d0004248d5b # Single car garage
    condition:
      - condition: template
        value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
      - condition: template
        value_template: "{{ trigger.from_state.state not in ['unavailable', 'unknown'] }}"
      - condition: template
        value_template: "{{ trigger.to_state.state not in ['unavailable', 'unknown'] }}"
      - condition: template
        value_template: "{{ states('input_boolean.garage_door_notifications') == 'on' }}"
    action:
      - service: switch.turn_on
        entity_id: switch.garage
      - service: script.notify_me
        data_template:
          message: >
            {% if trigger.to_state.state | lower == "on" %}
                {{ trigger.to_state.attributes.friendly_name }} is now OPENED!
            {% elif trigger.to_state.state | lower == "off" %}
                {{ trigger.to_state.attributes.friendly_name }} is now CLOSED!
            {% endif %}
      - service: script.led_message
        data_template:
          <<: *door_notification_script
      - service: script.voice_notify
        data_template:
          <<: *door_notification_script
      - delay: "00:00:05"
      - service: camera.snapshot
        data_template:
          entity_id: "camera.garage"
          filename:
            "{{ '/config/www/downloads/camera/garage/garage_' ~
            ((state_attr('automation.notify_garage_door_status', 'last_triggered') |string).replace('-','_')
            .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg') }}"

      - service: script.notify_me_with_picture
        data_template:
          title: 'Garage Door Status {{ now().strftime("%d %h %Y, %I:%M:%S %p") }}'
          message: >-
            {%- set doors = "" -%}
            {%- if states('binary_sensor.door_window_sensor_158d0004231f7b') == "on"  and
                  states('binary_sensor.door_window_sensor_158d0004248d5b') == "on" -%}
              {% set doors = "Both Garage Doors are OPEN" -%}
            {%- elif states('binary_sensor.door_window_sensor_158d0004231f7b') == "off"  and
                    states('binary_sensor.door_window_sensor_158d0004248d5b') == "off" -%}
              {% set doors = "Both Garage Doors are CLOSED" -%}
            {%- else -%}
            {% set doors = states.binary_sensor.door_window_sensor_158d0004248d5b.name ~ " is " ~
                            ('Closed' if states('binary_sensor.door_window_sensor_158d0004248d5b') == 'off' else 'OPEN')
                             ~ " and " ~ state_attr('binary_sensor.door_window_sensor_158d0004231f7b', 'friendly_name') ~ " is " ~
                            ('Closed' if states('binary_sensor.door_window_sensor_158d0004231f7b') == 'off' else 'OPEN') %}
            {%- endif %}
            Your {{doors}} on {{ now().strftime("%d %h %Y, at %I:%M:%S %p") }}. Please check the garage snapshot below.
          file: >
            {{ '/config/www/downloads/camera/garage/garage_' ~
                ((state_attr('automation.notify_garage_door_status', 'last_triggered') |string).replace('-','_')
                .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg') }}
          caption: "{{ trigger.to_state.attributes.friendly_name }}: {{ 'OPEN' if trigger.to_state.state == 'on' else 'CLOSED' }}"

      - service: notify.notify_smtp
        data_template:
          title: 'Garage Door Status {{ now().strftime("%d %h %Y, %I:%M:%S %p") }}'
          message: >-
            {%- set doors = "" -%}
            {%- if states('binary_sensor.door_window_sensor_158d0004231f7b') == "on"  and
                  states('binary_sensor.door_window_sensor_158d0004248d5b') == "on" -%}
              {%- set doors = "Both Garage Doors are OPEN" -%}
            {%- elif states('binary_sensor.door_window_sensor_158d0004231f7b') == "off"  and
                    states('binary_sensor.door_window_sensor_158d0004248d5b') == "off" -%}
              {%- set doors = "Both Garage Doors are CLOSED" -%}
            {%- else -%}
              {%- set doors = states.binary_sensor.door_window_sensor_158d0004248d5b.name ~ " is " ~
                            ('Closed' if states('binary_sensor.door_window_sensor_158d0004248d5b') == 'off' else 'OPEN')
                             ~ " and " ~ state_attr('binary_sensor.door_window_sensor_158d0004231f7b', 'friendly_name') ~ " is " ~
                            ('Closed' if states('binary_sensor.door_window_sensor_158d0004231f7b') == 'off' else 'OPEN') -%}
            {%- endif -%}
            Your {{doors}} on {{ now().strftime("%d %h %Y, at %I:%M:%S %p") }}. Please check the garage snapshot below.
          data:
            images:
              - "{{ '/config/www/downloads/camera/garage/garage_' ~
                ((state_attr('automation.notify_garage_door_status', 'last_triggered') |string).replace('-','_')
            .replace(' ', '_').replace(':','_').replace('.','_').replace('+','_') ~ '.jpg') }}"

      - service_template: "switch.turn_{{- trigger.to_state.state }}"
        entity_id: switch.garage

  # Notify Entry Door Status
  ###############################################################################
  - alias: Voice Notify Entry Door Status
    initial_state: true
    trigger:
      platform: state
      entity_id:
        - binary_sensor.door_window_sensor_158d000424a6d6 # Front Door
        - binary_sensor.door_window_sensor_158d00040ad8fc # Back Door
        - binary_sensor.door_window_sensor_158d0004880f30 # Garage Entry Door
    condition:
      - condition: template
        value_template: "{{ trigger.from_state.state not in ['unavailable', 'unknown']  }}"
      - condition: template
        value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
    action:
      - service: script.voice_notify
        data_template:
          message: >
            {% if trigger.to_state.state | lower == "on" %}
              Your {{ trigger.to_state.attributes.friendly_name.replace('Sensor', '') }} is OPEN,
              {% if states('alarm_control_panel.home') == 'armed_home' or
                    states('alarm_control_panel.home') == 'armed_away' %}
              But your home security system is ON.
              {% endif %}
            {% elif trigger.to_state.state | lower == "off" %}
              Your {{ trigger.to_state.attributes.friendly_name.replace('Sensor', '') }} is CLOSED!
            {% endif %}
          greeting: "no"
          only_at_night: >
            {% if states('alarm_control_panel.home') == 'armed_home' %}
              no
            {% else %}
              yes
            {% endif %}
      - service: script.led_message
        data_template:
          message: >
            {% if trigger.to_state.state | lower == "on" %}
              Your {{ trigger.to_state.attributes.friendly_name.replace('Sensor', '') |lower -}} is open.
            {% else %}
              Your {{ trigger.to_state.attributes.friendly_name.replace('Sensor', '') |lower -}} is closed.
            {% endif %}

  - alias: When Front Door Opens Turn Front Room Lights ON
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d000424a6d6
      to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: script.frontdoor_cam
      - condition: template
        value_template: "{{ states('sun.sun') == 'below_horizon' }}"
      - service: switch.turn_on
        entity_id: switch.front_room

  - alias: When Back Door Opens Turn Kitchen Lights ON
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d00040ad8fc
        to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service_template: "script.{{ ['patio_cam', 'playarea_cam'] | random }}"
      - condition: template
        value_template: "{{ states('sun.sun') == 'below_horizon' }}"
      - service: switch.turn_on
        entity_id: switch.kitchen_switch

  ################################################################################
  #  When I open the garage door
  #     if it is dark outside, OR
  #     both the garage doors are closed
  #       ==> turn the Garage lights ON, and start timer!
  ################################################################################
  - alias: Garage Lights On When Door is Opened
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d0004880f30
      to: "on"
    condition:
      condition: or
      conditions:
        - condition: template
          value_template: '{{states.sun.sun.state == "below_horizon"}}'
        - condition: and
          conditions:
            - condition: template
              value_template: "{{ states('binary_sensor.door_window_sensor_158d0004248d5b') == 'off' }}"
            - condition: template
              value_template: "{{ states('binary_sensor.door_window_sensor_158d0004231f7b') == 'off' }}"
        - condition: state
          entity_id: input_boolean.light_automations
          state: "on"
    action:
      - service: switch.turn_on
        entity_id: switch.garage
      - service: timer.start
        entity_id: timer.timer_garage

###############################################################################
# MASTER BATHROOM AUTOMATIONS
###############################################################################
#
# Only turn ON lights at 25% at night
#
  - alias: Master Bathroom Door Opened After Dark
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d000424718f
      to: "on"
    condition:
      - condition: template
        value_template: '{{states("sun.sun") == "below_horizon"}}'
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        entity_id: light.master_bathroom_lights
        data:
          brightness: 64 # 25% of brightness
          transition: 5

  - alias: Master Bathroom Door Opened During The Day
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d000424718f
      to: "on"
    condition:
      - condition: template
        value_template: '{{states("sun.sun") == "above_horizon"}}'
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: light.turn_on
        entity_id: light.master_bathroom_lights
        data:
          brightness: 254 # 100% of brightness
          transition: 5

  - alias: Master Bathroom Toilet Door Opened
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d00044e5cb3
      to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_on
        entity_id: switch.master_bathroom_toilet_light

  - alias: Turn off Master Bathroom Toilet if the door is open for too long
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.door_window_sensor_158d00044e5cb3
        to: "on"
        for:
          minutes: 3
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_off
        data_template:
          entity_id: switch.master_bathroom_toilet_light

###############################################################################
# GUEST BATHROOM AUTOMATIONS
###############################################################################

  - alias: Guest Bathroom Door Opened
    initial_state: true
    trigger:
      platform: state
      entity_id: binary_sensor.door_window_sensor_158d00045ab168
      to: "on"
    condition:
      - condition: state
        entity_id: input_boolean.light_automations
        state: "on"
    action:
      - service: switch.turn_on
        entity_id: switch.guest_bathroom_lights