###############################################################################
#   @author         :   Jeffrey Stone 
#   @date           :   06/12/2020
#   @package        :   Daily
#   @description    :   Package for handling daily automations and scripts.  
###############################################################################

automation:

  - id: 3a0175f8-9caf-45fb-a461-585474f73e6b
    alias: Coffee Time
    initial_state: true
    trigger:
        platform: time
        at: input_datetime.coffee_time
    action:
    - service: scene.turn_on
      entity_id: scene.diminshed_livingroom_lighting
    - service: scene.turn_on
      entity_id: scene.diminished_kitchen_lighting
    - service: scene.turn_on
      entity_id: scene.jeff_night_light
    - service: alarm_control_panel.aarlo_set_mode
      data:
        entity_id: alarm_control_panel.aarlo_ah_base_station
        mode: 'home'

  - id: ce3d7e63-3eaa-4cc5-959b-1334e6c5c4e6
    alias: Good Morning
    initial_state: true
    trigger:
        platform: time
        at: input_datetime.good_morning
    action:
    - service: scene.turn_on
      entity_id: scene.normal_livingroom_lighting
    - service: scene.turn_on
      entity_id: scene.normal_kitchen_lighting
    - service: scene.turn_on
      entity_id: scene.jeff_light_on
    - service: script.twitter_notify_image
      data_template:
        tweet: >-
          {{ [ 
            "Good Morning! Time to rise and shine at Anchorage House. ",
            "Good Morning from Anchorage House. Glad to see everyone made it. ", 
            "Birds are awake, and so is Anchorage House. Good morning everyone!",
            "Good morning. Anchorage House is up and online." 
            ] | random }}
          {% if is_state('calendar.holidays_in_united_states', 'on') %}
            Today is {{states.calendar.holidays_in_united_states.attributes.message}}.
          {% endif %}
        image: >-
          {{ [ "/config/www/tweet_images/coffee.jpg",
                "/config/www/tweet_images/coffee2.jpg"] | random }}

  - id: ff00b2a6-be6a-4f1f-ac14-84de9d42bb6a
    alias: Skylar Bedtime
    initial_state: true
    trigger:
        platform: time
        at: input_datetime.skylar_nightly_report
    condition:
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: scene.turn_on
      entity_id: scene.skylar_room_prebed


  - id: 745ee642-26ef-4818-8068-67f8ffccb29f
    alias: Master Bedroom lights dim
    initial_state: true
    trigger:
    - platform: time
      at: '22:30:00'
    condition:
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: scene.turn_on
      entity_id: scene.master_bedroom_dim

  - id: 68891fc2-b631-4195-ae63-b660e04f1615
    alias: Master Bedroom lights out
    initial_state: true
    trigger:
    - platform: time
      at: '23:00:00'
    condition:
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: scene.turn_on
      entity_id: scene.master_bedroom_off

  - id: 27b83e67-b509-427a-aa2f-1ce4bb7ad0ab
    alias: Master Bedroom Lights On
    initial_state: true
    trigger:
    - platform: time
      at: '20:00:00'
    condition:
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    action:
    - service: scene.turn_on
      entity_id: scene.master_bedroom_on


  - id: 0d5cea61-f295-4e5d-87a9-f12dfb5b67d6
    alias: Good Night
    initial_state: true
    trigger:
    - platform: time
      at: '22:30:00'
    action:
    - service: scene.turn_on
      entity_id: scene.night_time_livingroom_lighting
    - service: script.theater_lights_out
    - service: switch.turn_off
      entity_id: switch.incense
    - service: switch.turn_off
      entity_id: switch.artemis
    - service: switch.turn_off
      entity_id: switch.forest
    - service: switch.turn_off
      entity_id: switch.livingroom_fan
    - service: switch.turn_off
      data:
        entity_id: switch.ha_speaker
    - service: scene.turn_on
      entity_id: scene.kitchen_lighting_off
    - service: script.good_night_loft
    - service: input_boolean.turn_off
      entity_id: input_boolean.audible_notifications
    - service: script.reset_annc_switches
    - service: script.twitter_notify_image
      data_template:
        tweet: >-
          {{ [ 
            "Good Night from Anchorage House. ", 
            "Time to shut down this party. Good Night from Anchorage House. ",
            "I'm feeling tired. Think I'll take a quick nap. Good Night!",
            "Things are getting quiet. Time to say Good Night.",
            "We have come to the end of our broadcast day. Oh, who am I kidding. I''ll be up all night watching over Anchorage House. " 
            ] | random }}
        image: >-
          {{ [ "/config/www/tweet_images/off.jpg",
              "/config/www/tweet_images/clock.jpg"] | random }}
        

  - id: 79886677-4fdb-4cec-ad46-92a9c3c75681
    alias: Outside Lights on at Sunset
    initial_state: true
    trigger:
    - platform: sun
      event: sunset
      offset: -00:30:00
    action:
    - service: switch.turn_on
      data:
        entity_id: switch.front_porch_light
    - service: scene.turn_on
      entity_id: scene.normal_livingroom_lighting
    - service: script.sunset_garage_open
    - service: script.sundown_briefing
    - service: script.twitter_notify_image
      data_template:
        tweet: >-
          {{ [ 
            "Its getting dark at Anchorage House, so Im turning on the outside and inside lights. ", 
            "When the Sun goes down, I help out by turning on the lights. Its the little things. ",
            "Switching to dark mode! The outuisde and inside lights are on.",
            "It is time to turn on the outside lights. You know...so bugs know where to die.",
            "Anchorage House is switching to night mode!",
            "Anchorage House is going to the darkside. Of the planet that is. So the lights are on."
            ] | random }}
        image: >-
          {{ [ "/config/www/tweet_images/lamp.jpg",
              "/config/www/tweet_images/bright.jpg",
              "/config/www/tweet_images/light.jpg",
              "/config/www/tweet_images/lamp2.jpg"] | random }}

  - id: a01624c1-1222-4d53-a9b3-95eee9a6a4b0
    alias: Sunset
    initial_state: true
    trigger:
    - platform: sun
      event: sunset
    action:
    - service: alarm_control_panel.aarlo_set_mode
      data:
        entity_id: alarm_control_panel.aarlo_ah_base_station
        mode: 'Armed'

  - id: 18656c32-65ab-4578-9cc0-2de5820290de
    alias: Outside Lights off at Sunrise
    initial_state: true
    trigger:
    - platform: sun
      event: sunrise
    action:
    - service: homeassistant.turn_off
      entity_id: group.outside_lights
    - service: script.twitter_notify_image
      data_template:
        tweet: >-
          {{ [ 
            "The sun is up at Anchorage House, so Im turning off the outside lights. ", 
            "I see the sun! Time to turn off the outside lights at Anchorage House. ", 
            "Dawn has broken at Anchorage House. Turning off the outside lights.",
            "Anchorage house has converted to the lightside after a few hours on the darkside. I think its safe to turn off the lights.",
            "Light mode activated! So that means I'm turning off the outside lights.",
            "Hooray you live to drink another day. I guess I will get the lights since the sun is up."

            ] | random }}
        image: >-
          {{ [ "/config/www/tweet_images/sunrise.jpg",
              "/config/www/tweet_images/sunrise3.jpg",
              "/config/www/tweet_images/sunrise2.jpg"] | random }}

  # - id: inside_on_sunset
  #   alias: Inside on at Sunset
  #   initial_state: true
  #   trigger:
  #   - platform: sun
  #     event: sunset
  #     offset: -02:00:00
  #   condition:
  #   - condition: template
  #     value_template: >
  #       {%- if states.weather.klzu_hourly.state != "sunny" or states.weather.klzu_hourly.state != "partlycloudy" or states.weather.klzu_hourly.state != "clear" -%}
  #         true
  #       {%- endif -%}
  #   action:
  #     - service: scene.turn_on
  #       entity_id: scene.normal_livingroom_lighting
  #     - service: scene.turn_on
  #       entity_id: scene.normal_kitchen_lighting
  #     - service: script.twitter_notify
  #       data:
  #         message: >-
  #           {{ [ "It is dark inside the house due to the conditions outside the house so I am turning on the inside lights.",
  #                 "When it gets dark enough outside I turn on the inside lights. ",
  #                 "Since I monitor the conditions inside and outside the Anchorage House I know all the things. Like it is time to turn on the inside lights."
  #             ] | random }}

  - id: 9c4e0720-6496-49a9-ad49-fe59b52fc056
    alias: Inside off after Sunrise
    initial_state: true
    trigger:
    - platform: sun
      event: sunrise
      offset: 01:30:00
    condition:
    - condition: template
      value_template: >
        {%- if states.weather.klzu_hourly.state == "sunny" or states.weather.klzu_hourly.state == "partlycloudy" or states.weather.klzu_hourly.state == "clear" -%}
          true
        {%- endif -%}
    action:
    - service: scene.turn_on
      entity_id: scene.skylar_room_day
    
   

  - id: 86fd5de9-3136-4d86-aef9-268f097eae35
    alias: HA Startup
    initial_state: true
    trigger:
    - platform: homeassistant
      event: start
    action:
      service: frontend.set_theme
      data:
        name: midnight-AH

  - id: 82b96b73-2407-49d7-8ea2-1f78a69e025b
    alias: Jeff eta home
    initial_state: true
    trigger:
    - platform: state
      entity_id: sensor.jeff_destination
      to: Home
    condition:
    - condition: state
      entity_id: sensor.notification_mode
      state: Normal
    action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.jeff_travel_monitor
  - id: enable_bad_traffic_home_jeff
    alias: Notify Jeff - Bad Traffic
    initial_state: true
    trigger:
    - platform: numeric_state
      entity_id: sensor.jeff_ett_home
      above: 59
    condition:
    - condition: state
      entity_id: sensor.jeff_location
      state: Summit
    - condition: state
      entity_id: input_boolean.jeff_traffic_alerts
      state: 'on'
    action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.jeff_traffic_alert_home
    - service: script.twitter_notify_image
      data_template:
        tweet: >-
          {{ [ 
            "Looks like traffic is bad out there so I let @thejeffreystone know.  ", 
            "I dont think todays traffic is the worst I''ve seen, but its bad. Letting my residents know they need to plan their trips. ", 
            "Oh great...traffic cannot get any worse. Maybe everyone should stay where they are. "
            ] | random }}
        image: >-
          {{ [ 
              "/config/www/tweet_images/road.jpg",
              "/config/www/tweet_images/traffic.jpg",
              "/config/www/tweet_images/traffic-jam.jpg"
              ] | random }}


  - id: 49fcdf4f-5cb1-442a-9a1a-16eefc3da503
    alias: set soffee time
    trigger:
      - platform: time
        at: '00:02:15'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.coffee_time
        data_template:
          time: > 
            {% if states.binary_sensor.workday_sensor.state == 'on' %}
              06:00
            {% else %}
              07:15
            {% endif %}

  - id: 7ad448b3-f2f0-4dfc-9549-732fa6396bc5
    alias: set skylar nightly report time
    trigger:
      - platform: time
        at: '18:30:00'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.skylar_nightly_report
        data_template:
          time: > 
            {% if states.sensor.school_tomorrow.state == 'on' %}
              19:45
            {% else %}
              20:15
            {% endif %}

  - id: bd8e4e20-f138-49ec-a9f6-d23ab1450b98
    alias: set good morning time
    trigger:
      - platform: time
        at: '05:55:00'
    action:
      - service: input_datetime.set_datetime
        entity_id: input_datetime.good_morning
        data_template:
          time: > 
            {% if states.calendar.skylar_school.attributes.offset_reached == True %}
              06:45
            {% elif states.binary_sensor.workday_sensor.state == 'on' %}
              07:00
            {% else %}
              07:30
            {% endif %}

script:
  sunset_garage_open:
    sequence:
    - condition: state
      entity_id: group.garage_doors
      state: 'on'
    - service: script.turn_on
      entity_id: script.driveway_on