###############################################################################
#   @author         :   Jeffrey Stone 
#   @date           :   02/19/2019
#   @package        :   Notify
#   @description    :   A Collection of Notification Scripts and Configs. 
#                       Package modified from https://github.com/skalavala/smarthome/blob/master/packages/notify.yaml with inspiration
#			from https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/config/script/speech_engine.yaml		
###############################################################################
#homeassistant:

#ifttt:
#  key: !secret IFTTT_API_KEY

tts:
  - platform: google_translate
    cache: true
    cache_dir: /tmp/tts
    time_memory: 300
  - platform: amazon_polly
    aws_access_key_id: !secret aws_key
    aws_secret_access_key: !secret aws_secret
    region_name: 'us-east-1'
    text_type: ssml
    voice: Brian
    cache: True

notify:
  - platform: ios
  - name: all_ios
    platform: group
    services:
      - service: ios_jeffreystonesiphone
      - service: ios_jeffreystonesipad
      - service: ios_katherinestonesiphone
  - name: ios_parents
    platform: group
    services:
      - service: ios_jeffreystonesiphone
      - service: ios_jeffreystonesipad
      - service: ios_katherinestonesiphone
  - name: jeff_ios
    platform: group
    services:
      - service: ios_jeffreystonesiphone
      - service: ios_jeffreystonesipad
  - name: kat_ios
    platform: group
    services:
      - service: ios_katherinestonesiphone
  - name: 'Master Bedroom Echo'  
    platform: command_line
    command: "/home/homeassistant/bin/alexa_wrapper -d 'Master Bedroom'"
  - name: 'Living Room Echo'  
    platform: command_line
    command: "/home/homeassistant/bin/alexa_wrapper -d 'Living Room'"
  - name: 'Kitchen Echo'  
    platform: command_line
    command: "/home/homeassistant/bin/alexa_wrapper -d 'Kitchen'"
  - name: 'All Echo'  
    platform: command_line
    command: "/home/homeassistant/bin/alexa_wrapper -d 'All'"
  - name: twitter
    platform: twitter
    consumer_key: !secret twitter_consumer_key
    consumer_secret: !secret twitter_consumer_secret
    access_token: !secret twitter_access_token
    access_token_secret: !secret twitter_access_secret

  
script:


###############################################################################
# Txt Notify
###############################################################################
  text_notify_all:
    sequence:
      - service: notify.all_ios
        data_template:
          message: >
            {{ message }}

  text_notify_jeff:
    sequence:
      - condition: state
        entity_id: input_boolean.text_notify_jeff
        state: 'on'
      - service: notify.jeff_ios
        data_template:
          message: >
            {{ message }}

  text_notify_kat:
    sequence:
      - condition: state
        entity_id: input_boolean.text_notify_kat
        state: 'on'
      - service: notify.kat_ios
        data_template:
          message: >
            {{ message }}

  text_notify:
    sequence:
      - condition: state
        entity_id: input_boolean.text_notifications
        state: 'on'
      - service_template: >
            {% if who == 'jeff' %}
              notify.jeff_ios
            {% elif who == 'kat' %}
              notify.kat_ios
            {% elif who == 'skylar' %}
              notify.skylar_ios
            {% elif who == 'parents' %}
              notify.ios_parents
            {% else %}
              notify.all_ios
            {% endif %}
        data_template:
          title: '{{ title }}'
          message: '{{ message }}'
          #data:
          #   attachment:
          #     url: "{{ url }}"
          #     content-type: "{{ content_type }}"
          #     hide-thumbnail: false
          #  push:
          #     sound: "{{ ios_sound }}"
          #    badge: 0
          #     category: "{{ ios_category }}"
          #   entity_id: "{{ camera_entity }}"

  text_alert:
    sequence:
      - service_template: >
            {% if who == 'jeff' %}
              notify.jeff_ios
            {% elif who == 'kat' %}
              notify.kat_ios
            {% elif who == 'skylar' %}
              notify.skylar_ios
            {% elif who == 'parents' %}
              notify.ios_parents
            {% else %}
              notify.all_ios
            {% endif %}
        data_template:
          title: '{{ title }}'
          message: '{{ message }}'
          #data:
          #   attachment:
          #     url: "{{ url }}"
          #     content-type: "{{ content_type }}"
          #     hide-thumbnail: false
          #  push:
          #     sound: "{{ ios_sound }}"
          #    badge: 0
          #     category: "{{ ios_category }}"
          #   entity_id: "{{ camera_entity }}"

###############################################################################
# Alert Notify
# Conditions:
#   => Vacation mode should be off
# Services:
#   => Audible 
#   => iOS 
###############################################################################
  alert_notify:
    sequence:
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: 'off'
      - service: switch.turn_on 
        entity_id: switch.ha_speaker
      - service: tts.google_translate_say
        data_template:
          entity_id: media_player.guest_tv
          message: >
            {{ message }}

  jarvis_alert:
    sequence:
    - service: switch.turn_on 
      entity_id: switch.ha_speaker
    - service: tts.amazon_polly_say
      data_template:
        entity_id: media_player.guest_tv
        message: >-
          <speak>
            {{ message }}
          </speak>
        cache: true


###############################################################################
# Voice Notify
# Conditions:
#   => Only Announce when people are home.
#   => Only Announce when Audible Notifications are on
###############################################################################

  voice_notify:
    sequence:
      - condition: template
        value_template: '{{ states.input_boolean.audible_notifications.state == "on" }}'
      - condition: state
        entity_id: sensor.family_status
        state: 'Home'
      - condition: state
        entity_id: input_boolean.vacation_mode
        state: 'off'
      # - service: switch.turn_on 
      #   entity_id: switch.ha_speaker
      # - condition: state
        # entity_id: media_player.guest_tv
        # state: 'idle'
      - service: tts.google_translate_say
        data_template:
          entity_id: media_player.guest_tv
          message: >
            {{message }}

  jarvis_voice:
    sequence:
    - service: mqtt.publish
      data_template:
        topic: 'house/polly/lastmsg'
        payload: "This message is from {{ now().strftime('%-I') }}:{{ now().strftime('%M') }} {{ now().strftime('%p') }}. {{ message | truncate(220)}}"
        retain: true
    - condition: template
      value_template: '{{ states.binary_sensor.quiet_time.state == "off" }}'
    - condition: state 
      entity_id: binary_sensor.vlc_player
      state: 'off'
    - condition: state
      entity_id: sensor.family_status
      state: 'Home'
    - condition: state
      entity_id: input_boolean.vacation_mode
      state: 'off'
    - service: switch.turn_on 
      entity_id: switch.ha_speaker 
    - service: media_player.turn_on
      entity_id: media_player.theater
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.theater
        volume_level: >
          {% if states.input_boolean.audible_notifications.state == 'on' %}
          .65
          {% else %}
          .4
          {% endif %}
    # media_player.googlehome0715, media_player.googlehomehub3492, media_player.upstairs_speaker
    - service: tts.amazon_polly_say
      data_template:
        entity_id: >
          {% if states.input_boolean.audible_notifications.state == 'on' %}
          media_player.theater
          {% else %}
          media_player.house
          {% endif %}
        message: >-
          <speak>
            <break time="5.0s" />
            {{ message }}
            <break time="1.0s" />
          </speak>
        cache: true
  
  ah_report:
    sequence:
    - condition: or
      conditions:
        - condition: state
          entity_id: sensor.family_status
          state: 'Home'
        - condition: state
          entity_id: input_boolean.guest_mode
          state: 'on'

    - service: script.jarvis_voice
      data_template:
        message: >-

          {% macro greeting() %}
            {% if now().strftime('%H')|int < 12 and now().strftime('%H')|int > 6 %}
              Good morning,
            {% elif now().strftime('%H')|int >= 12 and now().strftime('%H')|int < 17 %}
              Good afternoon,
            {% else %}
              Good evening,
            {% endif %}
          {% endmacro %}

          {% macro confirmation() %}
            {{ [
                "Okay.", 
                "If you insist.",
                "I am afraid I can't do that Dave,,,I am kidding,",
                "Leave it to me.",
                "As you wish.",
                "By your command.",
                "Affirmative.",
                "No Problem.",
                "I think I can handle that.",
                "Working on it now.",
                ",Oh, you were talking to me. Let me take care of that."
                "Why not. It's not like I ever sleep.",
                "As Always, a great pleasure watching you work.",
                "I'm not even supposed to be here today.",
                "Ah-Ah-Ah. You didn't say the magic word.",
                "Funny, I was just about to do that. "
                ] | random }}
          {% endmacro %}

          {% macro interuption() %}
            {{ [
                "Pardon me,",
                "Excuse me,",
                "I do not mean to interupt, but.",
                "I hate to interrupt, but.",
                "I beg your pardon",
                "I don't mean to intrude, but",
                "I'm sorry to interrupt, but.",
                "I thought you might like to know."
                ] | random }}
          {% endmacro %}

          {% macro issue() %}
            {{ [
                "I have detected an issue!",
                "We have a problem!",
                "You might not like this.",
                "There is something that needs your attention."
                ] | random }}
          {% endmacro %}

      
          
          {% macro morning_greeting() %}
              {{ [ "Time to Rise and Shine. ",
                "Let's do this thing.",
                "I hope you got some rest.",
                "It's time to get the day started.",
                "Glad to see everyone made it.",
                "The early bird gets the worm. And look over there. A worm.",
                "The early bird gets the worm.",
                "from the bridge.",
                "Pardom the interuption.",
                "I hope I am not interupting, but I wanted to give you an update."
              ] | random }}
              {% if is_state("input_boolean.guest_mode", "on") %} 
                {{ [ "And a special welcome to our guests. ",
                      "And good morning to our guests as well. ",
                      "Hey look at that, someone came to visit Anchorage House. Hope everyone slept well."
              ] | random }}
              {% endif %}
          {% endmacro %}

          {% macro evening_greeting() %}
            {{ [ "I thought you might like to know. ",
                "The day is almost over.",
                "Before things start to quiet down,",
                "Anchorage House."
              ] | random }}
          {% endmacro %}

          {% macro time_annc() %}

            {% if now().strftime('%H')|int < 12 %}
              It is now {{ now().strftime('%H')|int }} {{ now().strftime('%M')|int }} AM.

            {% elif now().strftime('%H')|int > 12 %}
              It is now {{ now().strftime('%H')|int - 12 }} {{ now().strftime('%M')|int }} PM.
            
            {% else %}

            {% endif %}

          {% endmacro %}

          {% macro weather_alerts() %}
            There are currently {{states.sensor.nws_alerts.state }} active weather alerts for our area.
            The National Weather Service Has issued,
            {% if states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[5] is defined %}
              a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[5] }}.
            {% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[4] is defined %}
             a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[4] }}.
            {% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[3] is defined %}
              a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[3] }}.
            {% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[2] is defined %}
              a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[2] }}.
            {% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[1] is defined %}
              a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[1] }}.
            {% else %}
              a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[0] }}.
            {% endif %}
          {% endmacro %}

          {% macro todays_events() %}
            {% if is_state("sensor.holiday_halloween","0") %}
              This is Halloween, this is halloween. Happy Halloween!
            {% endif %}
            {% if is_state("sensor.holiday_christmas","0") %}
              Merry Christmas Everyone!
            {% endif %}
            {% if is_state("sensor.anniversary_our_wedding","0") %}
              Happy Anniversary! It has been an amazing {{ states.sensor.anniversary_our_wedding.attributes.years }} years!
            {% endif %}
            {% if is_state("calendar.holidays_in_united_states", "on") %}
              Today is {{states.calendar.holidays_in_united_states.attributes.message}}. 
            {% endif %}
            {% if is_state("calendar.anchorage_holidays", "on") %}
              And dont forget Today is also {{states.calendar.anchorage_holidays.attributes.message}}. 
            {% endif %}
            {% if states.calendar.birthdays.state == "on" %}
              Today is {{ states.calendar.birthdays.attributes.message }} birthday!
            {% endif %}
          {% endmacro %}

          {%- macro dark_outside() -%}
            {{ [
                'The sun is officially down. Exterior Cameras are now motion acivated.',
                'The sun has been ushered off the stage. I have armed the perimiter cameras.',
                'You are running out of daylight. Time to wrap up any outisde chores.',
                'Switching to night mode! '
            ]|random }}
          {%- endmacro -%}

          {% macro current_conditions_outisde() %}
            It's currently {{states.sensor.dark_sky_summary.state}} and {{states.sensor.dark_sky_temperature.state|round}} degrees according to Dark Sky. 
            The back porch is {{states.sensor.accurite_back_porch_temperature.state|round}} degrees.
          {% endmacro %}

          {% macro current_conditions_inside() %}
            The inside temperature is {{states.climate.home.attributes.current_temperature|round}} degrees. The Climate is set to {{states.climate.home.state}}.
          {% endmacro %}

          {% macro current_conditions_garage() %}
            The garage is {{states.sensor.accurite_garage_temperature.state|round}} degrees with a humidity of {{states.sensor.accurite_garage_humidity.state|round}} percent.
          {% endmacro %}

          {% macro daily_forecast() %}
            The rest of the day should be {{states.sensor.dark_sky_summary_0d.state}} with a high of {{states.sensor.dark_sky_daytime_high_temperature_0d.state|round}} degrees. There is a {{states.sensor.dark_sky_precip_probability_0d.state|round}} percent chance of rain.  
          {% endmacro %}

          {% macro overnight_forecast() %}
            The low tonight will be {{states.sensor.dark_sky_overnight_low_temperature_0d.state | round}} degrees and there is a {{states.sensor.dark_sky_precip_probability_0d.state|round}} percent chance of rain. 
          {% endmacro %}

          {% macro future_forecast() %}
            Looking into the future you can expect {{states.sensor.dark_sky_daily_summary.state}}.
          {% endmacro %}

          {% macro freeze_warning() %}
            {{ [ "The temperature is expected to be near freezing. Someone might want to bring the lemon tree in. ",
                 "It appears that it will be cold tonight. Think of the poor plants.",
                 "I suggest bringing in the plants otherwise the temperature might kill them. And that will be on you."
               ] | random }}
          {% endmacro %}
          
          {% macro upcoming_birthdays() %}
            {% if states.sensor.birthday_skylar.state | int == 1 %}
              Tomorrow is Skylar's Birthday.
            {% endif %}
            {% if states.sensor.birthday_jeff.state | int == 1 %}
              Tomorrow is Jeff's Birthday.
            {% endif %}
            {% if states.sensor.birthday_kat.state | int == 1 %}
              Tomorrow is Katherine's Birthday.
            {% endif %}
            {% if states.sensor.anniversary_our_wedding.state | int == 1 %}
              Tomorrow is Jeff and Katherine's Wedding Anniversary. 
            {% endif %} 
          {% endmacro %}

          {% macro upcoming_holidays() %}
            {% if states.sensor.holiday_halloween.state | int == 1 %}
              Tomorrow is Halloween. I hope you have picked out a costume.
              {{ [ "I will be going as a dumb home. ",
                "I have prepped the scary music. Just in case.",
                "I will be going as HAL 9000. The Pod Bay Doors are being installed today."
              ] | random }}
            {% elif states.sensor.holiday_halloween.state | int < 30 %}
              There are only {{states.sensor.holiday_halloween.state}} days 
              {{ [ "until Halloween.",
                "until Halloween. It might not be enough time. ",
                "and counting until the best holiday ever.",
                "until you need a costume."
              ] | random }} 
              
            {% else %}  
            {% endif %}

            {% if states.sensor.holiday_christmas.state | int == 1 %}
              Tomorrow is Christmas. It's practically here! Santa is coming tonight! Don't forget the cookies!
            {% elif states.sensor.holiday_christmas.state | int < 25 %}
              There are only {{states.sensor.holiday_christmas.state}} days until christmas. 
            {% else %}
            {% endif %}
          {% endmacro %}

          {% macro skylar_dressed() %}
            Skylar, 
            {% if is_state("sensor.birthday_skylar", "0") %}
              Even birthday boys have to get dressed. So get to it. 
            {% else %} 
              If you haven't gotten dressed,
              {{ [ "It is time to get dressed. ",
                "It is time to put some real clothes on. ",
                "it is now time to change your underwear.",
              ] | random }} 
            {% endif %}
          {% endmacro %}

          {% macro prebed_routine() %}
            Skylar,  
            {{ [ "It is time to get in the bath. ",
                "You only have thirty minutes until bedtime. That means you should be heading for the bathtub.",
                "I cannot comminicate with the bathtub, otherwise, I would have started the water for you already. So you will have to start the bath yourself. ",
                "My sensors are detecting a strange smell. I am running diagnostics, but in the mean time why don't you start a bath.",
              ] | random }} 
            If you go now there may be time for stories. Don't forget to brush your teeth. 
            {% if is_state("sensor.weekday", "fri") %} 
              Oh, and tomorrow is Saturday, so why don't we sleep in.  
            {% endif %}
            {% if is_state("sensor.weekday", "sat") %} 
              Oh, and tomorrow is Sunday, so why don't we sleep in.  
            {% endif %}  
          {% endmacro %}


          {% macro school_today() %}
            {% if states.calendar.skylar_school.attributes.offset_reached == True %}
              Because you have school today!
              {% if states.calendar.skylar_school.attributes.description == "early-release" %}
                And guess what? It is early release!
              {% endif %}
            {% endif %}
          {% endmacro %}

          {% macro skylar_events() %}
            {% if is_state("calendar.skylar_events", "on") %}
              Skylar, You have {{ states.calendar.skylar_events.attributes.message }} today as well!
            {% endif %}
          {% endmacro %}

          {% macro clothes_suggestion() %}
            {% if is_state("sensor.clothing_forecast", "Freezing") %}
              It is going to be freezing today so I suggest wearing long pants, and a heavy coat.
            {% elif is_state("sensor.clothing_forecast","Cold") %}
              It is going to be cold today so I suggest wearing long pants and a light jacket.
            {% elif is_state("sensor.clothing_forecast", "Chilly") %}
              It is going to be chilly today so I suggest wearing at least long pants.
            {% elif is_state("sensor.clothing_forecast", "Nice") %}
              It is going to be           
              {{ [ "nice outside",
                "pretty nice outside "
                "a good day to be outside ",
                "rather enjoyable outside ",
              ] | random }} 
              today so I suggest wearing shorts.
            {% elif is_state("sensor.clothing_forecast", "Toasty") %}
              It is going to be a 
              {{ [ "bit warm ",
                "rather warm outside ",
                "almost too hot outside ",
                "a little warm ",
              ] | random }} 
              today so I suggest wearing shorts. 
            {% elif is_state("sensor.clothing_forecast", "Hot") %}
              It is going to be 
              {{ [ "hot",
                "hotter than the sun "
                "hotter than hot, but in a lot of bad ways ",
                "hotter than the sun outside ",
                "super hot ",
                "hotter than the inside of a volcano "
              ] | random }} 
              today so I suggest wearing shorts.
            {% else %}
              It is going to be {{ states.sensor.clothing_forecast.state }} today so I suggest wearing shorts.
            {% endif %} 
          {% endmacro %}
          
          {% macro reminders() %}
            {% if is_state("input_boolean.heartworm", "on") %}
              Today is the day Winston gets his heartworm medicine. 
            {% endif %}
          {% endmacro %}	  

          {% macro chores() %}
            {% if now().strftime('%H')|int < 12 and now().strftime('%H')|int > 6 %}
              Don't forget to make your beds!
            {% endif %}

            {% if is_state("sensor.weekday", "mon") %}
              {{ [ "Don't forget tomorrow is Trash Day. ",
                "I advise you move the trash cans to the curb for the weekly pickup. ",
                " The trash and recycle should go out"
              ] | random }}
            {% endif %}

            {% if is_state("sensor.weekday", "tue") %}
              {% if now().strftime('%H')|int < 17 %}
                {{ [ "Don't forget to bring in the trash cans. ",
                "The trash cans will feel lonely if you leave them out all night. ",
                "The HOA will get mad if you leave those trash cans out on the street."
              ] | random }}
              {% endif %}
              
            {% endif %}
            
            
          {% endmacro %}

          {% macro door_status() %}
            {% if is_state("group.external_doors", "on") %} 
              There are doors are open.
            {% else %}
              All doors are closed. 
            {% endif %} 
          {% endmacro %}

          {% macro garage_door_status() %}
            {% if is_state("binary_sensor.garage_door_contact_2", "on") %} 
              {{ [ "The garage door is open. ",
                "The pod bay doors are open. ",
                "Someone forgot to close the garage."
              ] | random }}
            {% else %}
              {{ [ "The garage door is closed. ",
                "The cargo hold is sealed. ",
                "Looks like the garage has been secured.",
                "The pod bay doors are closed. Do not ask me to open them."
              ] | random }}
            {% endif %} 
          {% endmacro %}

          {% macro jeff_location() %}
            {% if is_state("sensor.jeff_location", "home") %} 
              {{ [
                      "I am not sure why you are asking me,",
                      "You must be having vision problems.",
                      "I don't want to alarm you, but"
                      ] | random }} 
                Jeff is already home! 
            {% else %}
              {% if is_state("sensor.jeff_destination", "na") %}
                {%- if states.device_tracker.life360_jeffrey_stone.attributes.moving == True %}
                  Jeff is currenly moving, but
                  {{ [
                      "I am unable to determine his destination.",
                      "I am having trouble tracking him.",
                      "His destination is currently unknown."
                      ] | random }}
                    At last check was {{states.sensor.jeff_ett_home.attributes.duration | round}} minutes from home.
                {%- elif states.device_tracker.life360_jeffrey_stone.attributes.driving == True %}
                  Jeff is currenly moving, but
                  {{ [
                      "I am unable to determine his destination.",
                      "I am having trouble tracking him.",
                      "His destination is currently unknown."
                      ] | random }}
                    At last check was {{states.sensor.jeff_ett_home.attributes.duration | round}} minutes from home.
                {% else %}
                  {% if is_state("sensor.jeff_location", "Lost") %}
                    Jeff is lost, but at last check was {{states.sensor.jeff_ett_home.attributes.duration | round}} minutes from home.
                  {% else %}
                    Jeff is currently at {{ states.sensor.jeff_location.state }}.
                  {% endif %}
                {% endif %}
              {% elif is_state("sensor.jeff_destination", "home") %}
                Jeff is currenly heading home and will be here in {{states.sensor.jeff_ett_home.attributes.duration | round}} minutes.
              {% else %}
                Jeff is heading to {{ states.sensor.jeff_destination.state }}.
              {% endif %} 
            {% endif %} 
          {% endmacro %}

          {% macro kat_location() %}
            {% if is_state("sensor.kat_location", "home") %} 
                {{ [
                      "I am not sure why you are asking me,",
                      "You must be having vision problems.",
                      "I don't want to alarm you, but"
                      ] | random }}
                Katherine is already home! 
            {% else %}
              {% if is_state("input_boolean.kat_travel_monitor", "on") %}
                {%- if states.device_tracker.life360_kat_stone.attributes.moving == True %}
                  Katherine is currently heading home and will be here in {{states.sensor.kat_ett_home.attributes.duration | round}} minutes.  
                {%- elif states.device_tracker.life360_kat_stone.attributes.driving == True %}
                  Katherine is currently heading home and will be here in {{states.sensor.kat_ett_home.attributes.duration | round}} minutes.  
                {% else %}
                  {% if is_state("sensor.kat_location", "Lost") %}
                    Katherine is lost, but at last check was {{states.sensor.kat_ett_home.attributes.duration | round}} minutes from home. 
                  {% else %}
                    Katherine is currently at {{ states.sensor.kat_location.state }}.
                  {% endif %}
                {% endif %}
              {% else %}
                {%- if states.device_tracker.life360_kat_stone.attributes.moving == True %}
                  Katherine is currenly moving, and
                  {{ [
                      "I am unable to determine her destination.",
                      "I am having trouble tracking her.",
                      "Her destination is currently unknown."
                      ] | random }}
                    At last check was {{states.sensor.kat_ett_home.attributes.duration | round}} minutes from home.
                {%- elif states.device_tracker.life360_kat_stone.attributes.driving == True %}
                  Katherine is currenly moving, and
                  {{ [
                      "I am unable to determine her destination.",
                      "I am having trouble tracking her.",
                      "Her destination is currently unknown."
                      ] | random }}
                    At last check was {{states.sensor.kat_ett_home.attributes.duration | round}} minutes from home.
                {% else %}
                  {% if is_state("sensor.kat_location", "Lost") %}
                    Katherine is lost, but at last check was {{states.sensor.kat_ett_home.attributes.duration | round}} minutes from home.
                  {% else %}
                    I have located Katherine. She is at {{ states.sensor.kat_location.state }}, and
                      at last check was {{states.sensor.kat_ett_home.attributes.duration | round}} minutes from home.
                  {% endif %}
                {% endif %}
              {% endif %} 
            {% endif %} 
          {% endmacro %}

          {% macro jeff_headed_home() %}
            Jeff appears to be headed home. Based on current traffic conditions he should be here in {{states.sensor.jeff_ett_home.attributes.duration | round}} minutes.
          {% endmacro %}

          {% macro kat_headed_home() %}
            Katherine appears to be headed home. Based on current traffic conditions she should be here in {{states.sensor.kat_ett_home.attributes.duration | round}} minutes.
          {% endmacro %}

          {% macro traffic_conditions() %}
              {%- if states.sensor.home_to_zoo.state|round > 50 %}
                Traffic to the Zoo appears heavy than normnal.
              {% else %}
                Traffic to the Zoo is normal.
              {% endif %}
              Currently it will take {{states.sensor.home_to_zoo.state|round}} minutes to get to the Zoo.

              {%- if states.sensor.home_to_summit.state|round > 50 %}
                Traffic to the Cox Automotive appears heavy than normnal.
              {% else %}
                Traffic to the Cox is normal.
              {% endif %}
              Currently it will take {{states.sensor.home_to_summit.state|round}} minutes to get to Cox Automotive.
          {% endmacro %}


          {% macro iss() %} 
            {{ [
                "But the International Space Station is passing over. Wave.",
                "But The International Space Station just flew by.",
                "But if you were to look up right now, and it was dark outside, and you happened to be looking in the right place you would see the International Space Station go by. But it's already passed by now."
                ] | random}} 
          {% endmacro %}

          {% macro security_report() %} 
            {%- if states.input_boolean.sentry_mode.state == 'on' %}
              {{ [
                "Sentry Mode is enabled.",
                "Sentry mode is currently active.",
                "Barn Door Protocol is currently in effect.",
                "My security system is currently armed."
                ] | random}} 
            {% else %}
              {{ [
                "Sentry Mode is disabled.",
                "Sentry mode is currently active.",
                "My security system is currently disarmed."
                ] | random}} 
            {% endif %}
            {%- if states.group.external_doors.state == 'off' %}
              All external doors are secured.
            {% else %}
              The following doors are open,
              {%- if states.binary_sensor.front_door.state == 'on' %}
                Front Door.
              {% endif %}
              {%- if states.binary_sensor.back_door.state == 'on' %}
                Back Door.
              {% endif %}
              {%- if states.binary_sensor.laundry_room_door.state == 'on' %}
                Laundry Room Door.
              {% endif %}
              {%- if states.binary_sensor.garage_door.state == 'on' %}
                Garage Door.
              {% endif %}
              {%- if states.binary_sensor.side_door.state == 'on' %}
                Side Door.
              {% endif %}
            {% endif %}
          {% endmacro %}

          {%- macro washer_needs_emptying() -%}
            {{ [
            "Just a reminder. Washing machine needs to be emptied.",
            "I'm sure you have more inportant things to do, but the washing machine needs to be emptied.",
            "Looks like the washing machine has completed it's cycle.",
            "Don't forget to move the clothes to the dryer."
            ] | random }}
          {%- endmacro -%}

          {# ********************************************* #}
          {#  ******** Start the Speech routines ********  #}
          {# ********************************************* #}
          
          {% if call_greeting == 1 %}
            {{ greeting() }}
          {% endif %}

          {% if call_confirmation == 1 %}
            {{ confirmation() }}
          {% endif %}

          {% if call_interuption == 1 %}
            {{ interuption() }}
          {% endif %}

          {% if call_issue == 1 %}
            {{ issue() }}
          {% endif %}

          {% if call_morning_greeting == 1 %}
            {{ morning_greeting() }}
          {% endif %}

          {% if call_evening_greeting == 1 %}
            {{ evening_greeting() }}
          {% endif %}

          {% if call_time_annc == 1 %}
            {{ time_annc() }}
          {% endif %}

          {% if states.sensor.nws_alerts.state | int > 0 and call_weather_alerts == 1 %}
            {{ weather_alerts() }}
          {% endif %}

          {% if call_todays_events == 1 %}
            {{ todays_events() }}
          {% endif %}

          {% if call_dark_outside == 1 %}
            {{ dark_outside() }}
          {% endif %}

          {% if call_current_conditions_outside == 1 %}
            {{ current_conditions_outside() }}
          {% endif %}

          {% if call_current_conditions_inside == 1 %}
            {{ current_conditions_inside() }}
          {% endif %}

          {% if call_current_conditions_garage == 1 %}
            {{ current_conditions_garage() }}
          {% endif %}

          {% if call_daily_forecast == 1 %}
            {{ daily_forecast() }}
          {% endif %}

          {% if call_overnight_forecast == 1 %}
            {{ overnight_forecast() }}
          {% endif %}

          {% if call_future_forecast == 1 %}
            {{ future_forecast() }}
          {% endif %}

          {% if call_freeze_warning == 1 %}
            {{ freeze_warning() }}
          {% endif %}

          {% if call_upcoming_birthdays == 1 %}
            {{ upcoming_birthdays() }}
          {% endif %}

          {% if call_upcoming_holidays == 1 %}
            {{ upcoming_holidays() }}
          {% endif %}

          {% if call_skylar_dressed == 1 %}
            {{ skylar_dressed() }}
          {% endif %}

          {% if call_prebed_routine == 1 %}
            {{ prebed_routine() }}
          {% endif %}

          {% if call_school_today == 1 %}
            {{ school_today() }}
          {% endif %}

          {% if call_skylar_events == 1 %}
            {{ skylar_events() }}
          {% endif %}

          {% if call_clothes_suggestion == 1 %}
            {{ clothes_suggestion() }}
          {% endif %}
          
          {% if call_reminders == 1 %}
            {{ reminders() }}
          {% endif %}	  

          {% if call_chores == 1 %}
            {{ chores() }}
          {% endif %}

          {% if call_door_status == 1 %}
            {{ door_status() }}
          {% endif %}

          {% if call_garage_door_status == 1 %}
            {{ garage_door_status() }}
          {% endif %}

          {% if call_jeff_location == 1 %}
            {{ jeff_location() }}
          {% endif %}

          {% if call_kat_location == 1 %}
            {{ kat_location() }}
          {% endif %}

          {% if call_jeff_headed_home == 1 %}
            {{ jeff_headed_home() }}
          {% endif %}
          
          {% if call_kat_headed_home == 1 %}
            {{ kat_headed_home() }}
          {% endif %}

          {% if call_traffic_conditions == 1 %}
            {{ traffic_conditions() }}
          {% endif %}

          {{ usps }}

          {% if call_iss == 1 %}
            {{ iss() }}
          {% endif %}

          {% if call_security_report == 1 %}
            {{ security_report() }}
          {% endif %}

          {% if call_washer_needs_emptying == 1 %}
            {{ washer_needs_emptying() }}
          {% endif %}

          {{ welcome_home }}  

          {{ speech_message }}
        

###############################################################################
# Twitter
###############################################################################

  twitter_notify:
    sequence:
      - service: notify.twitter
        data_template:
          message: >
            {% set msg = "" %}
            {% set msg = msg + " " + message %}
            {% set msg = msg + " #iot #smarthome" %}
            {{ msg }}

  twitter_snark:
    sequence:
      - service: notify.twitter
        data_template:
          message: !include ../templates/twitter_snark.yaml
        

  twitter_stats:
    sequence:
      - service: notify.twitter
        data_template:
          message: !include ../templates/twitter_stats.yaml

  twitter_github:
    sequence:
      - service: notify.twitter
        data_template:
          message: >
            {% set msg = "" %}
            {% set msg = msg + " " + message %}
            {% set msg = msg + " #smarthome #iot " %}
            {{ msg }}

  twitter_new_ha:
    sequence:
      - service: notify.twitter
        data_template:
          message: !include ../templates/twitter_new_ha.yaml

###############################################################################
# Weather Report
###############################################################################
  weather_report:
    sequence:
      - service: script.ah_report
        data:
          call_weather_alerts: 1
          call_current_conditions_outisde: 1
          call_current_conditions_inside: 1
          call_current_conditions_garage: 1
          call_daily_forecast: 1
          call_overnight_forecast: 1
          call_future_forecast: 1


###############################################################################
# Traffic Report
###############################################################################
  traffic_report:
    sequence:
      - service: script.ah_report
        data:
          call_traffic_conditions: 1

###############################################################################
# Security Report
###############################################################################
  security_report:
    sequence:
      - service: script.ah_report
        data:
          call_security_report: 1

###############################################################################
# Jeff Traffic Reports
###############################################################################
  jeff_eta_report:
    sequence:
      - service: script.ah_report
        data:
          call_jeff_location: 1

  jeff_headed_home_annc:
    sequence:
      - service: script.ah_report
        data:
          call_jeff_headed_home: 1


###############################################################################
# Kat Traffic Reports
###############################################################################
  kat_eta_report:
    sequence:
      - service: script.ah_report
        data:
          call_kat_location: 1

  kat_headed_home_annc:
    sequence:
      - service: script.ah_report
        data:
          call_kat_headed_home: 1