Allow for: duration in triggers to use templates or variables (e.g. input_number)

Context

Currently, in Home Assistant automations, the for: option in a trigger must be a fixed duration.
For example:

alias: 126 - Turn off AC after window or door opened, or room empty
triggers:
  - entity_id: binary_sensor.126_apertura_puerta_contact
    from: "off"
    to: "on"
    for: "00:03:00"
    trigger: state
  - entity_id: binary_sensor.126_apertura_ventana_contact
    from: "off"
    to: "on"
    for: "00:03:00"
    trigger: state
  - entity_id: input_boolean.126_ocupacion
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 3
      seconds: 0
    trigger: state

This works perfectly when the waiting time is static, but it becomes inconvenient when the user wants to adjust that time dynamically (for example, via an input_number in the UI).

Current workaround

To make the duration configurable, we have to move the delay to the actions section, like this:

alias: 001 - Turn off AC after window or door opened
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.001_apertura_puerta_contact
      - binary_sensor.001_apertura_salon_contact
      - binary_sensor.001_apertura_dormitorio_contact
    from: "off"
    to: "on"

actions:
  - delay:
      seconds: >
        {{ states('input_number.global_tiempo_de_espera_para_el_apagado_de_aire_tras_abrir_puerta_en_segundos') | int }}
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: binary_sensor.001_apertura_puerta_contact
            state: "on"
          - condition: state
            entity_id: binary_sensor.001_apertura_salon_contact
            state: "on"
          - condition: state
            entity_id: binary_sensor.001_apertura_dormitorio_contact
            state: "on"
    then:
      - action: switch.turn_off
        target:
          entity_id: switch.001_aire_state

This works, but it’s less efficient and harder to maintain, since we need to repeat the conditions inside the actions to ensure that the trigger is still valid after the delay.

Proposed improvement

Allow using templates or helpers (e.g. input_number) in the for: option of triggers.
Example syntax:

trigger:
  - platform: state
    entity_id: binary_sensor.001_apertura_puerta_contact
    from: "off"
    to: "on"
    for:
      seconds: >
        {{ states('input_number.global_tiempo_espera_aire') | int }}

Benefits

  • Cleaner and shorter automations.
  • Easier to adjust timeouts from the UI.
  • More consistency with how templates are already supported in other parts of automations (like delay, wait_for_trigger, etc.).
  • Reduces logic duplication and risk of desynchronization between triggers and conditions.

Feature requests languish on this forum, where they are very unlikely to be seen by the appropriate developers. They should be posted here:

Note also that your workaround code is not 100% correct as you posted it. It doesn’t account for the possibility that a sensor changes state and then returns to the triggering state during the delay period; you could fix that by setting the automation mode to restart (but that won’t let you roll multiple entities into a single automation like you’re trying to do now).

Also, since you aren’t keeping track of which sensor triggers your automation, and do a single or condition to test if the state of any of the entities is the desired state, your automation will continue after the delay even if the triggering sensor has changed state, so long as some other sensors has the desired state. I believe that differs from how for works.

Not true.

Already possible, please refer to the trigger documentation:

https://www.home-assistant.io/docs/automation/trigger/#holding-a-state-or-attribute

2 Likes