Door sensor automation on time open period

Is there a way of defining an automation trigger for a door sensor that goes from closed to open for a time period such as between 2 and 5 seconds?

I can only see the open for x time option which I have set at 2 seconds so anytime longer than this also triggers. I have a postbox delivery notification automation. My postbox has a delivery flap with a sensor and retrieval door with a sensor. However, I have to open the flap in order to open the access door which sends me a new notification that I have mail when I’m emptying it. If I can set a maximum trigger time for post received that is less than the longer time it takes for me to unlock and open the front door then this will work perfectly.

alias: Postbox new post delivery
description: notify when post has been delivered
mode: single
triggers:
  - entity_id:
      - binary_sensor.post_box_flap
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 2
    trigger: state
conditions: []
actions:
  - data: {}
    target:
      entity_id: input_boolean.you_have_post
    action: input_boolean.turn_on
  - data:
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M') }}"
    target:
      entity_id: input_datetime.postman_time
    action: input_datetime.set_datetime
  - action: counter.increment
    metadata: {}
    data: {}
    target:
      entity_id: counter.post_count
  - variables:
      postman_time: >-
        {{as_timestamp(states('input_datetime.postman_time')) |
        timestamp_custom('%A at %H:%M')}}
      post_count: "{{states('counter.post_count')}}"
  - data:
      message: |-
        You have new post now {{post_count}}
        times since emptying
        The postman last delivered {{postman_time}}
      title: New Post
    action: notify.mobile_app_lews_iphone
  - data:
      target:
        entity: null
    enabled: false
    action: notify.alexa_media

Sorry%20I%20cannot%20read%that. :wink:

In an automation for a state trigger, you hve trigger.from_state and trigger.to_state variables. You could compare last_changed attributes in them to decide things based on that.

1 Like

Yeah sorry about that stupid phone formatting, fixed now!

Thanks I’ll take a look.

Not sure I understand what you mean, maybe the reformatted code block will now help describe my scenario. Rather than a ‘for’ time it would be better if there was a ‘between’ time option. State from closed to open for >= 2 seconds & <= 5 seconds.

Seeing more, my first suggestion is not what you need.

First of all, triggers only happen at a very specific time, not for a period of continuous firing, like you ask here.

That would mean it needs to fire like a machine gun, that is not what you want either. You want it to fire less, or when it fires, perform actions less. So you need conditions.

If I understand correctly, you cannot tell if you need to send the notification after two seconds, because you won’t know if you’ll open the retrieval door. So you have two options.

  1. Start a timer, if the retrieval door opens then cancel the timer, if the timer runs out send the mail message.
  2. Wait longer after opening the flap (enough to open the retrieval door), then check if the retrieval door isn’t open or was closed for more than a certain length of time.

The second seems easiest, and can be done with a regular for statement. A for in a condition indeed allows for longer times. You might not even need the test for closed a certain time if accessing the retrieval door takes long enough. It is probably still open by the time the automation fires.

The problem with the second approach might be though, that the flap wasn’t opened long enough when mail is delivered. So I’d go for the first. Then you can keep the automation for starting the timer really short.