Automation: Using templates with "for:" generates error

I have the following automation which is working without problems

alias: Light Livingroom
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.light_livingroom_and_entrance_l1
    from: "off"
    to: "on"
    id: light_off-on
  - platform: state
    entity_id:
      - switch.light_livingroom_and_entrance_l1
    from: "on"
    to: "off"
    id: light_on-off
  - type: motion
    platform: device
    device_id: b2a46dba0cbae5d2c181012c2bd51def
    entity_id: binary_sensor.motion_livingroom_occupancy
    domain: binary_sensor
    id: motion_occupancy_started
  - type: no_motion
    platform: device
    device_id: b2a46dba0cbae5d2c181012c2bd51def
    entity_id: binary_sensor.motion_livingroom_occupancy
    domain: binary_sensor
    id: motion_occupancy_stopped
    for:
      minutes: 5
      seconds: 0
condition:
  - condition: state
    entity_id: input_boolean.disable_all_automations
    state: "off"
action:
  - choose:
      - conditions:
          - condition: trigger
            id: light_off-on
          - condition: not
            conditions:
              - condition: state
                entity_id: timer.light_livingroom_helper_timer
                state: active
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.light_livingroom_manual
      - conditions:
          - condition: trigger
            id: motion_occupancy_started
          - condition: and
            conditions:
              - condition: state
                entity_id: sensor.illumination_low_livingroom
                state: "True"
              - condition: state
                entity_id: input_boolean.markus_in_bed
                state: "off"
        sequence:
          - type: turn_on
            device_id: 9ab5e0d172b321dfb290973b3401f7dd
            entity_id: switch.light_livingroom_and_entrance_l1
            domain: switch
          - service: timer.start
            data: {}
            target:
              entity_id: timer.light_livingroom_helper_timer
      - conditions:
          - condition: trigger
            id: motion_occupancy_stopped
          - condition: state
            entity_id: input_boolean.light_livingroom_manual
            state: "off"
        sequence:
          - type: turn_off
            device_id: 9ab5e0d172b321dfb290973b3401f7dd
            entity_id: switch.light_livingroom_and_entrance_l1
            domain: switch
      - conditions:
          - condition: trigger
            id: light_on-off
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.light_livingroom_manual
mode: single

I would like to replace the values in the trigger “for:” section

    for:
      minutes: 5
      seconds: 0

with templates which get the minute and second values from a input_number

    for:
      minutes: "{{(states('input_number.motion_light_duration')|int/ 60)|round(0,'floor')}}"
      seconds: "{{states('input_number.motion_light_duration')|int% 60}}"

Unfortunately this generates the error “Message malformed: expected float for dictionary value @ data[‘for’][‘minutes’]” if I try to save the automation. I have tested the templates using the developer tools. They deliver the numbers 5 and 0.
What’s the problem here? The documentation states that " You can also use templates in the for option." and gives the example

      for:
        minutes: "{{ states('input_number.high_temp_min')|int }}"
        seconds: "{{ states('input_number.high_temp_sec')|int }}"

You can use templates. I am able to save an automation with this trigger without an error.

platform: state
entity_id:
  - binary_sensor.air_quality_alert
for:
  minutes: "{{ (states('input_number.alarm_clock_nap_time')|int/ 60)|round(0,'floor') }}"
  seconds: "{{ states('input_number.alarm_clock_nap_time')|int% 60 }}"

I can’t see what the difference is between mine and yours other than the input number entity. You must have something else happening there.

Try creating a new test automation from scratch with this trigger and see what happens. (Don’t forget to change the original one back to something that is known to be working first.)

Still not working for me. I’m puzzled.
As suggested, I created a new, very simple automation in the UI. Then selected “edit in YAML” and inserted the templates. Same thing happened where it didn’t let me save the changes. Then I thought that this might be happening because I created it in the UI which has spin boxes for the duration values which cannot display the templates. Therefore I edited the automations.yaml and removed the ID value. With normal numbers this works. The automation is listed in the UI (with the warning that it cannot be edited via the UI because of the missing ID).
Then I edited the automations.yaml and replaced the numbers with templates. The configuration checker says that the configuration will not prevent HA from starting, but I get a notification with a configuration error. I simplified the templates to just output a number (see below) but I still get the error notification

Logger: homeassistant.config
Source: config.py:455
First occurred: 09:28:01 (8 occurrences)
Last logged: 09:49:07

Invalid config for [automation]: expected float for dictionary value @ data['for']['minutes']. Got None expected float for dictionary value @ data['for']['seconds']. Got None. (See ?, line ?).

Here is the test automation from my automations. yaml file

- alias: Test
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 5e47bc68526edaa4aacab976ba90d4d0
    entity_id: binary_sensor.motion_kitchen_occupancy
    domain: binary_sensor
    id: motion_started
  - type: no_motion
    platform: device
    device_id: 5e47bc68526edaa4aacab976ba90d4d0
    entity_id: binary_sensor.motion_kitchen_occupancy
    domain: binary_sensor
    for:
      minutes: "{{5|float}}"
      seconds: "{{0|float}}"
    id: motion_stopped
  condition: []
  action:
  - if:
    - condition: trigger
      id: motion_started
    then:
    - type: turn_on
      device_id: 21903071448080aa0a9993c1aadf35a0
      entity_id: switch.light_kitchen_and_dining_area_l1
      domain: switch
    else: []
  - if:
    - condition: trigger
      id: motion_stopped
    then:
    - type: turn_off
      device_id: 21903071448080aa0a9993c1aadf35a0
      entity_id: switch.light_kitchen_and_dining_area_l1
      domain: switch
  mode: single

Any other ideas what I can try next to solve that problem?

Templates are not supported by Device Action, Device Condition, or Device Action.

You’re attempting to use a template in a Device Trigger. Templates are not supported by Device Trigger, Device Condition, or Device Action.

Use a State Trigger instead of a Device Trigger.

The following example turns the switch on and off based on the binary_sensor’s state.

- alias: Test
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_kitchen_occupancy
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_kitchen_occupancy
    from: 'on'
    to: 'off'
    for:
      minutes: "{{ (states('input_number.motion_light_duration') | int(0) / 60) | round(0,'floor') }}"
      seconds: "{{ states('input_number.motion_light_duration') | int(0) % 60 }}"
  condition: []
  action:
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: switch.light_kitchen_and_dining_area_l1
  mode: single

Thank you very much 123 Taras. That was the problem. I changed the motion sensor device triggers into state change triggers and now it works with the templates. I would have never figured that out alone. The documentation doesn’t say anything about that.

1 Like