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.
