Welcome home greeting automation

Hello,

I’ve created this automation with the help of ChatGPT, but I can’t get it to work. Maybe it’s not possible to achieve yet? I’ve tried many versions, and it’s still not functioning.

I’m very new to Home Assistant and don’t know how to write in Jinja and YAML, which is why I rely on ChatGPT.

What I want is to receive a greeting when one person arrives home and the door opens. I want to wait 5 seconds and then trigger the automation. So far, this part seems to work. However, I also want it to check if more people are arriving home at the same time, so It can greet them by name. This part seems impossible to get working.

Additionally, I would like it to check if all members arrive home together, in which case it should play a “Welcome Home” greeting without any names.

I’ve even tried to simply add a “Welcome Home” greeting if more than one person arrives, but I couldn’t get that to work either.

I’m not sure if this is achievable, but ChatGPT just repeated itself, so I gave up. Now, I’m hoping someone can help me with this or let me know if it’s not possible, so I can move on to something else. :blush:

Thanks!

alias: Welcome Home 
description: ""
trigger:
  - platform: state
    entity_id:
      - person.bogdan
      - person.burcu
      - person.deva
      - person.oscar
    to: home
action:
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.aqara_door_sensor_ingang_door_2
        to: "on"
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
  - choose:
      - conditions:
          - condition: template
            value_template: |
              {{ is_state('person.bogdan', 'home') and
                 is_state('person.burcu', 'home') and
                 is_state('person.deva', 'home') and
                 is_state('person.oscar', 'home') and
                 is_state('person.bogdan', 'not_home', trigger.from_state) and
                 is_state('person.burcu', 'not_home', trigger.from_state) and
                 is_state('person.deva', 'not_home', trigger.from_state) and
                 is_state('person.oscar', 'not_home', trigger.from_state) }}
        sequence:
          - data:
              entity_id: media_player.google_mini
              message: >-
                Välkommen hem allihopa! 
              language: sv
            action: tts.google_translate_say
      - conditions:
          - condition: template
            value_template: |
              {{ trigger.to_state.state == 'home' }}
        sequence:
          - data:
              entity_id: media_player.google_mini
              message: >
                {% set arriving = [] %}

                {% if is_state('person.bogdan', 'home') and trigger.entity_id ==
                'person.bogdan' %}
                  {% set arriving = arriving + ['Bågdan'] %}
                {% endif %}


                {% if is_state('person.burcu', 'home') and trigger.entity_id ==
                'person.burcu' %}
                  {% set arriving = arriving + ['Burgio'] %}
                {% endif %}


                {% if is_state('person.deva', 'home') and trigger.entity_id ==
                'person.deva' %}
                  {% set arriving = arriving + ['Deh-va'] %}
                {% endif %}


                {% if is_state('person.oscar', 'home') and trigger.entity_id ==
                'person.oscar' %}
                  {% set arriving = arriving + ['Oscar'] %}
                {% endif %}


                {# Handling the greeting message for multiple arrivals #}

                {%- if arriving|length == 1 -%}
                  "Välkommen hem {{ arriving[0] }}!"
                {%- elif arriving|length > 1 -%}
                  "Välkommen hem, {{ arriving|join(', ', ' och ') }}!"
                {%- endif %}
              language: sv
            action: tts.google_translate_say
mode: single

ChatGPT is a bullshit engine…and is extra bad at Home Assistant. Very often it produces configurations with issues that are difficult for new users to recognize because they seem plausible.

All the templates in the provided configuration are at least 50% non-sense.

There is only ever one trigger for a given run of an automation. If you want to figure out if a group of people just arrived you will need to do something like check how long they have been home.

alias: Welcome Home 
description: ""
trigger:
  - platform: state
    entity_id:
      - person.bogdan
      - person.burcu
      - person.deva
      - person.oscar
    to: home
action:
  - wait_for_trigger:
      - platform: state
        entity_id: binary_sensor.aqara_door_sensor_ingang_door_2
        to: "on"
  - delay: 5
  - variables:
      time_span: 2  #Minutes after which person is no longer considered recently arrived
      people: 
        - ent: 'person.bogdan'
          name: 'Bågdan'
        - ent: 'person.burcu'
          name: 'Burgio'
        - ent: 'person.deva'
          name: 'Deh-va'
        - ent: 'person.oscar'
          name: 'Oscar'
      recent: |
        {{ people | map(attribute='ent') 
        | select('is_state', 'home') | expand 
        | selectattr('last_changed', 'gt', (now()-timedelta(minutes=time_span))) | list }}
      r_count: "{{ recent|count }}"
  - action: tts.google_translate_say
    data:
      language: sv
      entity_id: media_player.google_mini
      message: >
        {{'Välkommen hem '}}
        {%- if r_count == people|count -%}
          {{- 'allihopa!' }}
        {%- else %}
          {%- set arriving = people | selectattr('ent', 'in', recent) | map(attribute='name') | list %}
          {{-' och '.join(( arriving | join(', ')).rsplit(', ', 1))}}!
        {%- endif %}
mode: single

Depending on your needs and your family’s annoyance threshold regarding TTS announcements, you will likely want to include some conditions to throttle how often an announcement is played. Here are two that are commonly used:

  - alias: Trigger person was previously away for at least 10 minutes
    condition: template
    value_template: "{{ now() - trigger.from_state.last_changed > timedelta(minutes = 10) }}"
  - alias: Do not run more than once every 15 minutes
    condition: template
    value_template: "{{ this.attributes.last_triggered | default(as_datetime(0), 1) < now() - timedelta(minutes=15) }}"
2 Likes

Hello Bogdan,

Thanks for telling us you used it here.
As you found out yourself, it puts out nonsense and no one is going to want to help you fix this.

I suggest you look at what’s there as a possible flowchart then go into the HA UI Editor and use that supported engine to build code that works.

When you get something close that was built that way, hit us up again for help.

Sorry, there are no shortcuts with writing this code. You are going to have to put a bit of time in to do advanced things.

Hello and thank you guys for your reply.

I didin*t know that ChatGPT is that bad att HA. But what I have noticed is that you need to bee really specific what you want it to do. and yes it spits out a lot of garbage so it’s been back and forwards with it. At least now I know not to use ChatGPT for advanced automations.