Help with templates in "for:" statement

I’m trying to use a “if” template in a motion sensor “for:” statement and it is falling. Basically I’m trying this:

wait_for_trigger:
  - type: no_motion
    platform: device
    device_id: <that_long_hash_here>
    entity_id: <the_entity_id>
    domain: binary_sensor
    for: "{% if states('input_boolean.test') == 'off' %}00:05:00{% else %}00:01:00{% endif %}"

I get the error when trying to save the automation:

Message malformed: expected dict for dictionary value @ data[‘for’]

  • The template evaluates correctly in the Developer Tools.
  • If I replace the template by “00:05:00” the automation works perfectly.
  • I tried using the template in the “minutes:” field as well and I get a similar error.
  • I tried to play with quotation marks with no success.
  • I tried the same code in the “for:” of other automations and always the same error.

Am I trying to do something not allowed? I’m using Home Assistant 2021.2.3

Try this:

wait_for_trigger:
  - platform: state
    entity_id: <the_entity_id>
    to: 'off'
    for: "{{ '00:05:00' if is_state('input_boolean.test', 'off') else '00:01:00' }}"

Thanks @tom_l, but no luck… Same error:

Message malformed: expected dict for dictionary value @ data[‘for’]

I tried to do the same only for minutes, but it doesnt work as well:

for: 
  minutes: "{{ '05' if is_state('input_boolean.test', 'off') else '01' }}"

It says:

Message malformed: expected float for dictionary value @ data[‘for’][‘minutes’]

I’m start thinking that templates does not work for “for:” …

It’s because you’re using a device trigger.

Use a state trigger as @tom_l suggested.

Thanks @anon43302295 and @tom_l !
I didn’t realize Tom suggested to use state trigger instead of device trigger.
It worked like a charm!

Cheers.

Alex.