I need help using if-then-else and value_template in an automation

I am trying to create an automation that send me a notification with the amount of minutes that have passed since a sensor reported that the front door was open. The complication is that I want the message to be grammatically correct so that it will say “1 minute” if it is one minute, but “X minutes” when it is more than one minute.

I wrote the following yaml code, but for some reason it does not work when triggered. The code is supposed to trigger once every minute that the door remains open and the notification message is supposed to update with the number of minutes that have elapsed since the door was opened. I think the problem I’m running into has something to do with how I invoke value_template in the notification.

I would be grateful if someone could point out my error.

alias: TEMP
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8d46316ec1e36defb97b4c319ed20b85
    entity_id: binary_sensor.contact_sensor_gen2_intrusion_4
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - condition: state
    entity_id: input_boolean.switch_notify_entry_outer_door
    state: "on"
action:
  - repeat:
      while:
        - condition: state
          entity_id: binary_sensor.contact_sensor_gen2_intrusion_4
          state: "on"
          for:
            hours: 0
            minutes: 1
            seconds: 0
      sequence:
        - condition: template
          value_template: >
            {% if ((as_timestamp(now()) -
            as_timestamp(states.binary_sensor.contact_sensor_gen2_intrusion_4.last_changed))/60)
            > 1 %}
              Outer entry door ajar for {{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.contact_sensor_gen2_intrusion_4.last_changed))/60)|int }} minutes.
            {% else %}
              Outer entry door ajar for 1 minute.
            {% endif %}
        - service: notify.mobile_app_andrew_s_iphone
          data:
            title: "Warning: Outer Entry Door"
            message: "{{ value_template }}"
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
mode: single

Conditions do not return a value like that. You need to set variables instead.

alias: TEMP
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8d46316ec1e36defb97b4c319ed20b85
    entity_id: binary_sensor.contact_sensor_gen2_intrusion_4
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - condition: state
    entity_id: input_boolean.switch_notify_entry_outer_door
    state: "on"
action:
  - repeat:
      while:
        - condition: state
          entity_id: binary_sensor.contact_sensor_gen2_intrusion_4
          state: "on"
      sequence:
        - variables:
            last_changed: >
              {{ as_timestamp(states.binary_sensor.contact_sensor_gen2_intrusion_4.last_changed) }}
            message: >
              {% if not repeat.first %}
                Outer entry door ajar for {{ ((as_timestamp(now()) - last_changed)/60) | int }} 
                minutes.
              {% else %}
                Outer entry door ajar for 1 minute.
              {% endif %}
        - service: notify.mobile_app_andrew_s_iphone
          data:
            title: "Warning: Outer Entry Door"
            message: "{{ message }}"
        - delay:
            minutes: 1
mode: single

@Didgeridrew Thank you. I’m a real newbie. Can you please point me to the right commands/syntax?

Will this work?

alias: TEMP
description: ""
trigger:
  - type: opened
    platform: device
    device_id: 8d46316ec1e36defb97b4c319ed20b85
    entity_id: binary_sensor.contact_sensor_gen2_intrusion_4
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition:
  - condition: state
    entity_id: input_boolean.switch_notify_entry_outer_door
    state: "on"
action:
  - repeat:
      while:
        - condition: state
          entity_id: binary_sensor.contact_sensor_gen2_intrusion_4
          state: "on"
          for:
            hours: 0
            minutes: 1
            seconds: 0
      sequence:
        - service: notify.mobile_app_andrew_s_iphone
          data:
            title: "Warning: Outer Entry Door"
            message: >-
              {% if ((as_timestamp(now()) - as_timestamp(states.binary_sensor.contact_sensor_gen2_intrusion_4.last_changed))/60) > 1 %}
                Outer entry door ajar for {{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.contact_sensor_gen2_intrusion_4.last_changed))/60)|int }} minutes.
              {% else %}
                Outer entry door ajar for 1 minute.
              {% endif %}
        - delay:
            hours: 0
            minutes: 1
            seconds: 0
mode: single

@Didgeridrew Sorry I see that you make a revision to the code in your post! I’m a real newbie! Thank you!