Templates in automation, for

Hi. I have an automation that start an fan if an motion sensor is on and automation that stop the fan if the motion sensor is off for 3 minutes. I’d like to use an input_number helper (easy to set in frontend) for the minutes when the motion sensor is off but it give me an error: Message malformed: expected float for dictionary value @ data[‘for’][‘minutes’]

description: ""
mode: single
trigger:
  - type: no_motion
    platform: device
    device_id: 3dbb672d7536f94657cbfce2cbd3fe55
    entity_id: bb58bff95c83454b312f5c3f5802a6b0
    domain: binary_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: Motion OFF
condition: []
action:
  - type: turn_off
    device_id: ac310fa45937f4c610002e9e1c264aff
    entity_id: 703c063670dbe388609ae458405b3ed4
    domain: switch

Instead of :

    for:
      hours: 0
      minutes: 3
      seconds: 0

I would like to use:

    for:
      hours: 0
      minutes: {{ state('input_number.motion4auton') }}
      seconds: 0

It is possible ? It seems that the for statement do not accept templates. Where am I wrong ?
I know I’m not so good in yaml … I try “”{{ (( states(‘input_number.motion4autoff’) | float )) | int }}“” and other variants but nothing !
Thank you for your help.

IIRC, you will need to switch to a State trigger in order to use a templated for duration. Device triggers and actions are mostly designed for simple automations, so they do not always support options that other types do.

description: ""
mode: single
trigger:
  - platform: state
    to: 'off'
    from: 'on'
    entity_id: binary_sensor.YOUR_SENSOR_ID_HERE
    for:
      minutes: "{{ states('input_number.motion4autoff') | float }}"
    id: Motion OFF
condition: []
action:
  - type: turn_off
    device_id: ac310fa45937f4c610002e9e1c264aff
    entity_id: 703c063670dbe388609ae458405b3ed4
    domain: switch
2 Likes

Note that you also need to wrap single line templates in quotes.
The float filter is not really required, the template parser will take care of that :slight_smile:

It work ! Thank you .