Automation trigger "for" option template error

Hi,

This must be some obvious thing but I’m unable to find it out.
I’ve an automation following the syntax from https://www.home-assistant.io/docs/automation/trigger/#numeric-state-trigger:

alias: Pantry AutoOff
description: ""
trigger:
  - platform: device
    type: turned_on
    device_id: 71e518f09491619bb9ef2524460c16db
    entity_id: light.pantry_light_ha102_light
    domain: light
    for:
      seconds: "{{ states('input_number.pantry_autooff_delay') | int(600) }}"
condition: []
action:
  - type: turn_off
    device_id: 71e518f09491619bb9ef2524460c16db
    entity_id: light.pantry_light_ha102_light
    domain: light
mode: single

Receiving the following error:
“Message malformed: expected float for dictionary value @ data[‘for’][‘seconds’]”

My template evaluates properly:

Any idea what’s wrong with it?
Thanks

No you dont.

You have a device trigger. They are awful and they do not support templates.

Read the document you linked to again.

Hi tom_l,

Thanks for your prompt response. Fair enough.

My revised solution:

alias: Pantry AutoOff
description: ""
trigger:
  - platform: state
    entity_id:
      - light.pantry_light_ha102_light
    to: "on"
    for:
      seconds: "{{ states('input_number.pantry_autooff_delay')|int(30) }}"
condition:
  - condition: template
    value_template: >-
      {{ iif(states('input_number.pantry_autooff_delay')|int(0) == 0, 'False',
      'True') }}
action:
  - type: turn_off
    device_id: 71e518f09491619bb9ef2524460c16db
    entity_id: light.pantry_light_ha102_light
    domain: light
mode: single

Still not sure if this is the most elegant way to do it but it seems to work.
Also added a condition to disable automation when “pantry_autooff_delay” set to zero. I’ve tried to acheive the same by adding “enabled: …” to trigger but it does not seem like a template friendly contruction either or at least I could not make it work.

Pls share any better way to implement this. I’m happy to learn.
Thx, T.

Very good. You can change the condition to this:

  - condition: template
    value_template: >
      {{ states('input_number.pantry_autooff_delay')|int(0) != 0  }}

The inequality test will return true or false for you.

Though valid, you’re still using device actions. This would be better:

action:
  - service: light.turn_off
    target:
      entity_id: light.pantry_light_ha102_light

Really cool, appreciate it!