State trigger. Device on for user defined time

I have this basic automation. See line that starts with for:
How do I add user input value from helper “input_datetime.timeout” in this “for:” field?

alias: 'Alarm: Valve timeout '
description: 'Close valve after x hours x=input_datetime.timeout'
trigger:
  - platform: state
    entity_id: switch.main
    to: 'on'
--> for: {{states('input_datetime.timeout')}} <----- Can I do this?
condition: []
action:
  - service: notify.emailer
    ...

After saving it shows as this:

alias: 'Alarm: Valve timeout '
description: 'Close valve after x hours x=input_datetime'
trigger:
  - platform: state
    entity_id: switch.main
    to: 'on'
    for: '[object Object]'
condition: []
action:
  - service: notify.emailer

Developer tab confirms input field {{states(‘input_datetime.timeout’)}} is 03:00:00

for: "{{ states('input_datetime.timeout') }}"
1 Like

Hi, could you, please, clarify why this template does not work in condition part, but does in trigger part?

trigger:
  - platform: state
    entity_id: switch.esp_2_valve_main
    to: 'on'
    for: '{{ states(''input_datetime.manual_watering_timeout'') }}' <---this works
condition:
  - condition: state
    entity_id: switch.esp_2_valve_main
    state: 'on'
    for: '{{ states(''input_datetime.manual_watering_timeout'') }}' <---same but gives error

Error message:

Message malformed: offset {{ states(‘input_datetime.manual_watering_timeout’) }} should be format ‘HH:MM’, ‘HH:MM:SS’ or ‘HH:MM:SS.F’ for dictionary value @ data[‘condition’][0][‘for’]

I think see that input_datetime (without date) is in the format of HH:MM:SS

The state condition does not support templates in the for: option

https://www.home-assistant.io/docs/scripts/conditions/#state-condition

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

Thanks for reply! I’m just curious if templates should not work the same regardless of where they are used?

Templates work the same everywhere you can use them, but not every configuration option supports templates. In fact most don’t. If it’s not specifically mentioned in the documentation for an option then templates aren’t supported there.