Greeting automation

Hi everyone, I’d like to create an automation that plays a greeting when my wife or I come home using Alexa. The idea was to use the door sensor and the presence sensor. I also added a control that prevents the greeting if I enter and leave in less than an hour. But I’m having trouble starting the automation.

alias: welcome
description: Alexa Welcome
triggers:
  - entity_id:
      - binary_sensor.aqara_door_1_contact
    to: "on"
    trigger: state
    from: "off"
conditions:
  - condition: template
    value_template: "{{ not too_soon}}"
  - condition: template
    value_template: >
      {{ p1_home or p2_home }}
actions:
  - target:
      entity_id: notify.echo_dot_di_p1_annuncio
    data:
      volume_level: 0.6
    action: media_player.volume_set
  - target:
      entity_id: notify.echo_dot_di_p1_annuncio
    data:
      message: "{{ messaggio }}"
    action: media_player.play_media
  - target:
      entity_id: input_datetime.last_bentornato
    data:
      timestamp: "{{ now_ts }}"
    action: input_datetime.set_datetime
variables:
  last_marco: "{{ state_attr('automation.bentornato_a_casa', 'last_triggered') }}"
  now_ts: "{{ as_timestamp(now()) }}"
  p1_home: "{{ is_state('person.p1', 'home') }}"
  p2_home: "{{ is_state('person.p2', 'home') }}"
  too_soon: >
    {% set last = state_attr('input_datetime.last_bentornato', 'timestamp') |
    default(0) %} {{ (now_ts - last) < 3600 }}
  messaggio: |
    {% if p1_home and p2_home %}
      Bentornati ragazzi!
    {% elif rp1_home %}
      Bentornato Marco!
    {% elif p2_home %}
      Bentornata Paola!
    {% else %}
      Bentornato a casa!
    {% endif %}
mode: single

What does the debug Trace show?

Where do you define those variables?

@d921 I updated the script. It was a mistake.

Value_template Is false

maybe the problem is that, before it gets to that control, person is already true

I thought the best way would be to use a door sensor plus a presence sensor using my smartphone. Is that correct?

I have had these conditions for a few years and it has worked all the time as far as I know.

          - condition: template
            value_template: >-
              {{ now().timestamp() -
              as_timestamp(states.person.david.last_changed) < (10*60) }}
          - condition: template
            value_template: >-
              {{ now().timestamp() -
              as_timestamp(states.binary_sensor.ytterdorren_on_off.last_changed)
              < (10*60) }}
          - condition: state
            entity_id: person.david
            state: home

So last change on person entity has to be within 10 minutes
same thing with the door sensor.
And the person entity has to be home using GPS.