How to do this in HA. Conversion from another system

I’m in the process of converting a second system that I maintain over to Home Assistant. I have some events in the old system that may have a better way of doing the same in HA. Wondering how “you” would do this.

This event (actually takes 3 events) is a Mail notification.
I have a contact closure on the mailbox door and another on the mailbox flag.
Automation 1:
++++++++++++++++++++++++++++++++++++++++++
WHEN
Mailbox Door Contact changes and becomes Open
THEN
IF
Timer Mail Notify’s value is less than 1s
THEN
Wait 5 Minutes, 0 Seconds
If the Event Conditions are True, Run Event Mail 2 - Mail Delivered
End This Event
THEN
IF
Timer Mail Notify’s value is more than 1s
THEN
Stop Timer Mail Notify
Set Timer Mail Notify to time 0 Seconds
End This Event
++++++++++++++++++++++++++++++++++++++++++

Automation 2:
++++++++++++++++++++++++++++++++++++++++++
WHEN
This event is MANUALLY triggered
AND IF
Main Exterior Mailbox Flag Contact has a value equal to Closed
THEN
Play Audio File: Media/youGotmail.wav
To: CHROMECAST:LIVING ROOM SPEAKER
…and wait for playing to finish.
Start Timer Mail Notify
Send a NotificationTo: iPhoneMessage: You got Mail!Sound: HSBuddy Send With High PriorityActions: Default: Open the App
++++++++++++++++++++++++++++++++++++++++++

Automation 3:
++++++++++++++++++++++++++++++++++++++++++
WHEN
Timer Mail Notify’s value becomes 30m, 0s
THEN
If the Event Conditions are True, Run Event Mail 2 - Mail Delivered
++++++++++++++++++++++++++++++++++++++++++

How it works:
When the MB door is opened, if a timer I have for this is at 0 (meaning not running), it waits 5 minutes to give the mailman time to place mail in the box.
After the 5 minutes expires, and if the flag is still down, it will play an announcement so the homeowner knows the mail has been delivered, and it will start the afore mentioned timer… This systems timers count up but HA timers can do the same.
When the timer hits 30 minutes, it re-runs the 2nd automation which annpounces again and restarts the timer. This will repeat every 30 minutes until the mail is retrieved. This is all automation 3 does.

Automation 2 also stipulates that the flag is down. The homeowners use the mailbox to leave mail for the mailman to take. When they do this, I made sure they realize they have to put the flag in the up position (which they’re supposed to do anyway) but that flag up opens the flag contact closure which stops the announcement from running. No need to announce the mail is here if it was just them inserting mail. This completes what the 2nd automation does.

The other condition in automation 1 is what stops the 30 minute loop.
When the door is opened and if the timer is running, it means it’s them retrieving the mail, so it stops the notifications and stops the timer.

What would the HA equivalent be?

I’m not in a position to write whole automations right now. But there are a few pointers I can give quickly.

Home assistant has a for: clause for many triggers and conditions. They indicate how long a situation must persist before matching. So there’s less need for timers. So maybe a somewhat counterintuitive trigger: mailbox was closed for x time tells you it was x time ago the mailbox went from open to closed.

There’s also the Alert integration, who can continue giving alerts until the situation improves:

You can add helper. If for timers or input to remember state or count things. For instance an input boolean or timer. You can create them in in the GUI in settings, or in the latest release even right from the automation editor:

Sequences of actions that are manually triggered are best put in scripts, not automations. Use service calls to trigger them. Service calls are by far the best way to perform actions, each domain (timer, switch, input_boolean,…) has services to act on them.

Many tips can be found in the cookbook:

i’d probably do it in one automation. here’s an approx structure:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.mailbox_door_open
    to: "on"
    id: door_opened
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.announce_mail
    id: maybe_announce_mail
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - door_opened
        sequence:
          - if:
              - condition: state
                entity_id: timer.announce_mail
                state: active       # this means there's mail waiting
            then:
              - service: timer.cancel
                target:
                  entity_id: timer.announce_mail
            else:
              - service: timer.start
                data:
                  duration: "5:00"
                target:
                  entity_id: timer.announce_mail
      - conditions:
          - condition: trigger
            id:
              - maybe_announce_mail
        sequence:
          - if:
              - condition: state
                entity_id: binary_sensor.flag_up
                state: "off"
            then:
              - service: timer.start
                data:
                  duration: "30:00"
                target:
                  entity_id: timer.announce_mail
              - service: media_player.media_play
              - service: notify.persistent_notification