######################################################################################################
###Script to send notifications to the ChromeCast Audios during normal hours and only when we are home! Call like this:
  # action:
    # service: script.speech_engine
    # data:
    #   call_no_announcement:
    #   call_dark_outside:
    #   call_window_check:
    #   call_garage_check:
    #   call_responsibilities
    #   call_light_check
    #   call_inside_weather
    #   call_outside_weather
# # @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
######################################################################################################

speech_engine:
  sequence:
    - condition: or
      conditions:
        - condition: state
          entity_id: group.family
          state: 'home'
        - condition: state
          entity_id: input_boolean.guest_mode
          state: 'on'

    - service: script.speech_processing
      data_template:
        media_player: >-
            {% if media_player | length == 0 %}
              {% set media_player = [
                'media_player.livingroomcc',
                'media_player.alarm_clock',
                'media_player.bedroom_alarm_panel'
              ] %}
            {% endif %}

            {% if media_player is not string and media_player is sequence %}
              {% set media_player = media_player|join(', ') %}
            {% endif %}

            {{ media_player }}

        speech_message: >-
          {%- macro dark_outside() -%}
            {{ [
              'It is pretty dark outside. I will turn on the outside lights now.',
              'It is a little past Sunset. Time to turn on the outside lights. I am on it.',
              'I will switch on the outside lights. It is getting dark outside.',
              'Time to turn on the front lights. I will take care of it.'
            ]|random }}
          {%- endmacro -%}

          {%- macro responsibilities() -%}
            {% if now().strftime('%a') == 'Wed' or now().strftime('%a') == 'Sun' %}
              Today is {{ now().strftime('%A') }} and {{ now().strftime('%A') }} is garbage day.
            {% endif %}
            {% if now().strftime('%a') == 'Wed' %}
              Both Recycling and regular Garbage goes out. Please take out all of the garbage cans tonight.
            {% endif %}
            {% if now().strftime('%j')|int % 2 != 0 %}
              Today is Justin's day to do the chores.
            {% else %}
              Today is Paige's day to do the chores.
            {% endif %}
          {%- endmacro -%}

          {%- macro inside_weather() -%}
            Inside the house, it is {{ states.climate.downstairs.attributes['current_temperature'] }} degrees with around {{ states('sensor.downstairs_thermostat_humidity') }} percent humidity.
          {%- endmacro -%}

          {%- macro outside_weather() -%}
            Outside, it is going to be {{ states('sensor.dark_sky_minutely_summary') }}
          {%- endmacro -%}

          {%- macro light_check() -%}
            {% if states.group.all_lights.state != 'off' -%}
              There are
            {% for state in states.light if state.state == 'on' -%}
              {%- if loop.last -%}
                {{ loop.index }}
              {%- endif -%}
            {%- endfor %}
            lights on right now.

            {% set comma = joiner(', ') %}
            The
            {% for group in states.group|groupby('state') -%}
              {%- for entity in group.list if entity.state == 'on' and entity.name.split(' ')[1]|lower == 'lights' and entity.name.split(' ')[0]|lower != 'all' and entity.name.split(' ')[0]|lower != 'interior' -%}
                {{ ' and' if loop.last and not loop.first else comma() }}
                {{ entity.name }}
              {%- endfor -%}
            {%- endfor -%}
            .
            {%- endif -%}
          {%- endmacro -%}

          {%- macro window_check() -%}
            {% if states.group.entry_points.state != 'off' -%}
              {% set comma = joiner(', ') %}
              The
              {% for state in states.binary_sensor if state.state == 'on' and state.attributes.device_class == 'opening' -%}
              {%- endfor %}
              {% for group in states.binary_sensor|groupby('state') -%}
                {%- for entity in group.list  if entity.state == 'on' and entity.attributes.device_class == 'opening'  -%}
                  {{ ' and' if loop.last and not loop.first else comma() }}
                  {{ entity.attributes.friendly_name }}
                {%- endfor -%}
              {% endfor %}
               need to be closed.
            {%- endif -%}
          {%- endmacro -%}

          {%- macro garage_check() -%}
            {% if states.group.garage_doors.state !='closed' -%}
              The
              {%- for state in states.cover -%}
              {%- endfor %}
              {% for group in states.cover|groupby('state') -%}
                {%- for entity in group.list  if entity.state == 'open' and entity.attributes.device_class == 'garage'  -%}
                  {{ ' and' if loop.last and not loop.first }}
                  {{ entity.attributes.friendly_name }}
                {%- endfor -%}
              {%- endfor %}
              need to be closed.
            {%- endif -%}
          {%- endmacro -%}

          {%- macro medicine() -%}
            {% if is_state('input_boolean.medicine', 'off') -%}
              It looks like Carlo has not taken his medicine yet. Please make sure Carlo takes his medicine now.
            {% endif -%}
          {%- endmacro -%}

          {%- macro iss() -%}
            {% if is_state('binary_sensor.iss', 'on') -%}
              Here is something interesting. The international space station is above us now. There are {{ states.binary_sensor.iss.attributes['number_of_people_in_space'] }} people in space right now.
              {{ outside_weather() }}
            {% endif -%}
          {%- endmacro -%}

          {%- macro moon() -%}
            {%- if states.sensor.moon.state == 'Full moon' -%}
              Check out the full moon tonight!
            {%- endif -%}
          {%- endmacro -%}

          {%- macro uv() -%}
            {% if states.sensor.dark_sky_uv_index.state|int >= 6 and states.sensor.dark_sky_uv_index.state|int <= 7.9 %}
              Today's UV index is {{ states.sensor.dark_sky_uv_index.state }}.  You should wear sunscreen if going outside.
            {% elif states.sensor.dark_sky_uv_index.state|int >= 8 and states.sensor.dark_sky_uv_index.state|int <=10.9 %}
              Today's UV index is {{ states.sensor.dark_sky_uv_index.state }}.  This is VERY HIGH. Be sure wear sunscreen and re-apply.
            {% elif states.sensor.dark_sky_uv_index.state|int >= 11 %}
              Today's UV index is {{ states.sensor.dark_sky_uv_index.state }}.  This is EXTREME. You should be very cautious going outside.
            {% endif %}
          {%- endmacro -%}

          {%- macro holiday() -%}
            {% if states.sensor.holiday.state != '' %}
              Today is {{ states.sensor.holiday.state }}.
            {% endif %}
          {%- endmacro -%}


          {# ********************************************* #}
          {#  ******** Start the Speech routines ********  #}
          {# ********************************************* #}

          {% if call_no_announcement != 1 %}
            {% 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 %}
          {% endif %}

          {# Called from Annoucenments #}
          {{ personarriving }}

          {# Called from Nest when thermostats turn on #}
          {{ NestStatus }}

          {% if call_inside_weather == 1 %}
            {{ inside_weather() }}
          {% endif %}

          {% if call_outside_weather == 1 and is_state('sun.sun', 'above_horizon') %}
            {{ outside_weather() }}
          {% endif %}

          {{ DoorOpened }}
          {{ DoorClosed }}

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

          {% if call_garage_check == 1 or is_state('sun.sun', 'below_horizon') %}
            {{ garage_check() }}
          {% endif %}

          {% if (call_window_check == 1 or is_state('sun.sun', 'below_horizon')) or is_state('group.entry_points', 'on') %}
            {{ window_check() }}
          {% endif %}

          {{ NewDevice }}

          {% if call_light_check == 1 %}
            {{ light_check() }}
          {% endif %}

          {% if call_responsibilities == 1 %}
            {{ responsibilities() }}
          {% endif %}

          {% if now().strftime('%H')|int > 21 %}
            {{ medicine() }}
          {% endif %}

          {% if value1 is not none %}
            {{ value1 }}
          {% endif %}

          {# call a Random fact about the house #}
          {{ ([iss, moon, uv, holiday]|random)() }}

    - service: input_boolean.turn_off
      data:
        entity_id:
          - input_boolean.home_stats
          - input_boolean.responsibilities