Problem when executing an automation with a time delay

Hello,

I have created the following automation:

id: '1732351519835'
alias: Fensterkontakt
description: ''
triggers:
  - trigger: state
    entity_id:
      - input_boolean.fensterkontakt
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.fensterkontakt
            state: 'on'
            for:
              hours: 0
              minutes: 0
              seconds: 10
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              qos: '0'
              topic: testmessage
              payload: >-
                Windows is more than 10s open
      - conditions:
          - condition: state
            entity_id: input_boolean.fensterkontakt
            state: 'off'
            for:
              hours: 0
              minutes: 0
              seconds: 20
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              qos: '0'
              topic: testmessage
              payload: >-
                Windows is more than 20s closed
mode: restart

The problem is, as soon as I configure a time, the alternative path is always run through:
image

However, if I do not set a time as a delay, the automation works.

What exactly have I configured incorrectly or does the execution not work at all and I simply have to create two individual automations for this?

Thank you very much

That wont work.
The trigger triggers as soon as there is a state change and none of the conditions match.
The automation wont wait for it to match.

id: '1732351519835'
alias: Fensterkontakt
description: ''
triggers:
  - trigger: state
    entity_id:
      - input_boolean.fensterkontakt
    state: 'on'
    id: "on"
    for:
      seconds: 10
  - trigger: state
    entity_id:
      - input_boolean.fensterkontakt
    state: 'off'
    id: "off"
    for:
      seconds: 20
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: "on"
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              qos: '0'
              topic: testmessage
              payload: >-
                Windows is more than 10s open
      - conditions:
          - condition: trigger
            id: off
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              qos: '0'
              topic: testmessage
              payload: >-
                Windows is more than 20s closed
mode: restart

but what I don’t understand is why it works when I set these two lines to 0. So I don’t configure the time delay?

The original idea was that every time the status of the trigger changes, the Choose is used to compare whether it is on or off and then triggers a different action accordingly.
image

First of all. There is no delay.
It’s a for.
Delay means something happens now, and delay something in to the future.
For means something happens now, but the past [seconds / minutes / hours] something had a specific state.

So you trigger on the change but there is no way the state can have been the same for any period of time.

So does my code

EDIT:

This is also a possible code

id: '1732351519835'
alias: Fensterkontakt
description: ''
triggers:
  - trigger: state
    entity_id:
      - input_boolean.fensterkontakt
    for:
      seconds: 20
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.fensterkontakt
            state: 'on'
            for:
              hours: 0
              minutes: 0
              seconds: 10
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              qos: '0'
              topic: testmessage
              payload: >-
                Windows is more than 10s open
      - conditions:
          - condition: state
            entity_id: input_boolean.fensterkontakt
            state: 'off'
            for:
              hours: 0
              minutes: 0
              seconds: 20
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              qos: '0'
              topic: testmessage
              payload: >-
                Windows is more than 20s closed
mode: restart

This will only trigger when the state has been something for 20 seconds which means it works for both possibilities.
But that makes the “on” part delayed by 10 seconds, you could add another state trigger with for 10 seconds but that just makes things messy in my opnion.

Thanks for the explanation regarding the delay and what the times are all about.
I’ve only been with HA for two weeks. So I still have to get to know all the subtleties.

What I didn’t like about your solution was that I now always have to wait the maximum time (you wrote that too).

For the sake of clarity, I have now helped myself by simply duplicating the contact and parameterizing the appropriate state query and then the time directly here. I have no longer parameterized the time in the Choose application.

id: '1732351519835'
alias: Fensterkontakt
description: ''
triggers:
  - trigger: state
    entity_id:
      - input_boolean.fensterkontakt
    for:
      hours: 0
      minutes: 0
      seconds: 10
    to: 'on'
  - trigger: state
    entity_id:
      - input_boolean.fensterkontakt
    for:
      hours: 0
      minutes: 0
      seconds: 20
    to: 'off'

So the solution is at least practicable and comprehensible for me :).

Many thanks for your help.

So you made a copy of my code in post 2 and put “your” code as the solution

I have taken your code as a basis and adapted it accordingly. Hence the marking as a solution. But I am happy to specify your code as a solution :wink:

But ok, I have just marked your solution as a solution :slight_smile: