Why does my wait for trigger run indefinitely?

What is wrong with this automation?
Once it starts, even after I close the door sensor it just runs indefinitely. Why something this simple seems to have some weird drawback hidden at some detail that is against intuition?

alias: "Entrada - contact puerta luz "
description: "Enciende la luz de la entrada con la puerta "
triggers:
  - entity_id:
      - binary_sensor.opening_3_contact
    from: "off"
    to: "on"
    trigger: state
conditions:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: "-30"
    before_offset: "30"
actions:
  - data: {}
    target:
      entity_id: light.entrada
    action: light.turn_on
  - wait_for_trigger:
      - entity_id:
          - binary_sensor.opening_3_contact
        to: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 30
        trigger: state
  - delay:
      hours: 0
      minutes: 0
      seconds: 45
      milliseconds: 0
  - data: {}
    target:
      entity_id: light.entrada
    action: light.turn_off
mode: restart

How long do you want it to wait then?
You haven’t specified a time to wait so naturally it wants forever, as you configured it to.

You may have missed the part where I specify “even after closing the door sensor”

So you mean the for time is not honored?
Well I can’t say that it was clear since you didn’t specify what happens.

Ok but when does the trace say? What does the history of the sensor say?

I would probably do the automation this way instead:

alias: "Entrada - contact puerta luz "
description: "Enciende la luz de la entrada con la puerta "
triggers:
  - trigger: state
    entity_id: binary_sensor.opening_3_contact
    from: "off"
    to: "on"
  - trigger: state
    entity_id: binary_sensor.opening_3_contact
    from: "on"
    to: "off"
    for:
      seconds: 75
conditions:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: "-30"
    before_offset: "30"
actions:
  - action: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: light.entrada
    data: {}
mode: single

And now I noticed your restart mode. That is probably an issue with this automation.

But note that there is a small chance that the door opens just before the sun goes past 30 degrees. This will not turn off the light.

This is what the trace shows

My expectationbis that, after the door closes and then 30 seconds elapses the automation continues

are you sure that the binary sensor for the door is going to the desired state? Have you checked it in the dev tools states table?

No I was not, because it turned that:

  • I had two sensors, very similarly named both of the same type
  • I had two automations, very similarly named, both doing the same thing

So, the other sensor + the other automation was being triggered, and it had a different timeout.
What a silly mistake. Now I have to try the real ones

Like restarting the automation will put your example into trouble?
I have to admit it is a very smart approach, but as many smart things, maybe it will have unexpected outcomes as you mention

The code I posted is not prone to the same kind of issues your code will.
Delaying code is always an issue.
Setting a for time in trigger is a lot safer.