##########  ############################################################
# @CCOSTAN
# Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig
##  Announce when people come or go.
## Announce over all Chromecast Audios
#  Read more at https://vCloudInfo.com
######################################################################
- alias: 'People Greeting'

  trigger:
    - platform: state
      entity_id:
        - device_tracker.carlo
        - device_tracker.stacey
      from: 'not_home'
      to: 'home'

  action:
    # This Group is defined in the groups directory.
    - service: group.set
      data_template:
        object_id: "arriving"
        add_entities: >-
          {{ trigger.to_state.entity_id }}

    - wait_template: >-
        {{ states.binary_sensor.kitchen_door.state == 'on' }}
    - delay: '00:01:00'
    - service: script.speech_engine
      data_template:
#         {% set person = state_attr(trigger.entity_id, "friendly_name")%}      This is another way to do it.
#         {% set person = trigger.entity_id.split('.')[1]|replace('_', ' ')%}
        personarriving: >
          {% set person = expand('group.arriving')|map(attribute='name')|join(' and ') %}
          {% set peoplecount = expand('group.arriving') | count %}
          {% if peoplecount == 1 %}
            {% set is_are = ' is ' %}
          {% else %}
            {% set is_are = ' are ' %}
          {% endif %}
          {%- macro greeting_sentence(person, is_are) -%}
          {{ [
          "Welcome back home " ~ person,
          "Guess who is home?" ~ person ~ is_are ,
          person  ~ is_are + " now in the house.",
          "Welcome Home " ~ person + ".  We have missed you. Or at least Molly did.",
          "Our home is now complete, Rest your head and relax your feet! Welcome Back " ~ person,
          "Life is like a song, you’re back where you belong. Welcome home " ~ person,
          "Hey there " ~ person + " Welcome Home!",
          "Knock Knock. Who is There? "   ~ person  ~ is_are ,
          "The garage door opener just told me that " ~ person  ~ is_are +" home.",
          person + " can not hide from the system. Welcome home.",
          person ~ "! You are home!",
          "I know a secret! "  ~ person  ~ is_are +" home!",
          "Hey "  ~ person +". Your arrival has been recorded by the Smartest house on the block.",
          "Take note Molly! "  ~ person  ~ is_are +" home.",
          "I am sensing a disturbance in the force. "  ~ person +" must be home!",
          "And the house becomes a home. Welcome back " ~ person,
          person  ~ is_are + " now here.  Hash tag Welcome Home.",
          person  ~ is_are + " now here.  Hash tag Home.",
          person  ~ is_are + " now here.  Hash tag Smart Home.",
          "Just a quick announcement. " ~ person +" has arrived!",
          "Hey" ~ person + "! High Five! Glad you are finally home.",
          "Pardon the interruption but " ~ person ~ is_are +" home!",
          "My systems are picking up the presence of additional humans. " ~ person  ~ is_are +" being identified as home.",
          "Welcome home "~person + "! It is nice to see you again! Let me turn on the lights. ",
          "It looks like "~person ~ is_are + " finally home! I will get the house ready for you. "
          ] | random }}
          {%- endmacro -%}
          {{greeting_sentence(person, is_are)}}
        call_responsibilities: 1
        call_no_announcement: 1
        call_garage_check: 1
        call_window_check: 1

    - service: group.set
      data_template:
        object_id: "arriving"
        entities: []