Variable "for" in wait_for_trigger doesn't render

Hi all,

I’m currently in the process of trying to create a generic “motion” script which can be called from motion sensor automations. This way I’ll have one place to configure and change the actions, instead of per automation. I’m using a call script with variables to achieve this. I’ve got everything working except for one part:

for some reason including a variable in a wait for trigger action doesn’t work. So the below example doesn’t work:

wait_for_trigger:
  - platform: template
    value_template: '{{ is_state(motion_sensor, ''off'') }}'
    for:
      seconds: '{{ seconds_before_dimmed }}'

While this does:

wait_for_trigger:
  - platform: template
    value_template: '{{ is_state(motion_sensor, ''off'') }}'
    for:
      seconds: 30

Edit: I’m calling said script with the following action inside an automation:

service: script.turn_on
target:
  entity_id: script.motion_generic_component
data:
  variables:
    lights: light.studiekamer_groep
    adaptive_lighting: switch.adaptive_lighting_ruimten
    motion_sensor: binary_sensor.studiekamer_occupancy
    seconds_before_dimmed: 30
    seconds_before_off: 10

Just guessing, do you find any error in the logs? Have you tried adding an int filter to the variable??

wait_for_trigger:
  - platform: template
    value_template: '{{ is_state(motion_sensor, ''off'') }}'
    for:
      seconds: '{{ seconds_before_dimmed|int }}'

I’ve gotten the following errors without setting “int”:

Template variable warning: 'seconds_before_dimmed' is undefined when rendering '{{ seconds_before_dimmed }}'

Error rendering 'Motion - Generic Component' for template: expected float for dictionary value @ data['seconds']

And only this one when “int” (or “float”) is set:

Error rendering 'Motion - Generic Component' for template: UndefinedError: 'seconds_before_dimmed' is undefined

What truly drives me nuts is that in the same script, the following action DOES work, which uses a very similar variable structure:

delay:
  seconds: '{{ seconds_before_off }}'
wait_for_trigger:
  - platform: template
    value_template: '{{ is_state(motion_sensor, ''off'') }}'
    for:
      seconds: '{{ seconds_before_dimmed|int(30) }}'

Just out of curiosity can you try this?

Nope, same error:

Logger: homeassistant.components.template.trigger
Source: components/template/trigger.py:126
Integration: Template ([documentation](https://www.home-assistant.io/integrations/template), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+template%22))
First occurred: 09:49:29 (1 occurrences)
Last logged: 09:49:29

Error rendering 'Motion - Generic Component' for template: UndefinedError: 'seconds_before_dimmed' is undefined

You sure you have seconds_before_dimmed defined in your fields? Can you post the entire script?

Pretty certain. The other variables also worked before I even had any fields defined. Tried defining them all during troubleshooting to see if that changed anything, but it didn’t. Here’s the entire script:

alias: Motion - Generic Component
description: >-
  Generic component to turn on light(s) when motion is detected, dims lights
  when no motion is detected and turns light(s) off after a delay
fields:
  lights:
    name: Lights
    description: The light(s) to turn on/off and dim
    required: true
    selector:
      entity:
        domain: light
  adaptive_lighting:
    name: Adaptive Lighting
    description: The corresponding Adaptive Lighting master switch
    required: true
    selector:
      entity:
        domain: switch
        integration: adaptive_lighting
  motion_sensor:
    name: Motion Sensor
    description: Motion or occupancy sensor to monitor
    required: true
    selector:
      entity: null
  seconds_before_dimmed:
    name: Time before lights dim
    description: The time required after no motion is detected for lights to dim
    required: true
    default: 30
    selector:
      number:
        min: 0
        max: 120
        unit_of_measurement: second(s)
  seconds_before_off:
    name: Time before lights off
    description: The time required after lights are dimmed to turn them off
    required: true
    default: 30
    selector:
      number:
        min: 0
        max: 120
        unit_of_measurement: second(s)
sequence:
  - service: adaptive_lighting.apply
    data:
      lights: '{{ lights }}'
      entity_id: '{{ adaptive_lighting }}'
  - service: light.turn_on
    data:
      entity_id: '{{ lights }}'
  - wait_for_trigger:
      - platform: template
        value_template: '{{ is_state(motion_sensor, ''off'') }}'
        for:
          seconds: '{{ seconds_before_dimmed|int }}'
  - condition: template
    value_template: '{{ states(lights) == "on" }}'
  - service: light.turn_on
    data_template:
      entity_id: '{{ lights }}'
      brightness: '{{ (state_attr(lights, ''brightness'') / 2) | int }}'
  - delay:
      seconds: '{{ seconds_before_off }}'
  - condition: template
    value_template: '{{ states(motion_sensor) == "off" }}'
  - service: light.turn_off
    target:
      entity_id: '{{ lights }}'
mode: parallel
icon: mdi:motion
max: 100

Can you try changing the field declaration to this. Comment out all the other paramters (required, default etc etc.)

  seconds_before_dimmed:
    description: 'The time required after no motion is detected for lights to dim'

No difference using only:

  seconds_before_dimmed:
    description: The time required after no motion is detected for lights to dim

Results in:

Logger: homeassistant.components.template.trigger
Source: components/template/trigger.py:126
Integration: Template ([documentation](https://www.home-assistant.io/integrations/template), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+template%22))
First occurred: 10:23:48 (3 occurrences)
Last logged: 10:24:14

Error rendering 'Motion - Generic Component' for template: UndefinedError: 'seconds_before_dimmed' is undefined

For good measure, this is the automation calling the script:

alias: Occupancy - Studiekamer (Generic)
description: >-
  Turn on light when activity is detected, dim after inactivity and turn off
  after a delay
trigger:
  - platform: state
    entity_id: binary_sensor.studiekamer_occupancy
    to: 'on'
    from: 'off'
condition: []
action:
  - service: script.turn_on
    target:
      entity_id: script.motion_generic_component
    data:
      variables:
        lights: light.studiekamer_groep
        adaptive_lighting: switch.adaptive_lighting_ruimten
        motion_sensor: binary_sensor.studiekamer_occupancy
        seconds_before_dimmed: 30
        seconds_before_off: 10
mode: restart

Have you looked in the script debugger to see what is getting passed to it? Click the first node at the top then Changed Variables on the left. You should see a list of the variables passed to the script on the last run.

Huh. Just for completeness sake can you try it again with just this

  seconds_before_dimmed:
    description: ''

The other thing you can try is commenting out the wait_for_trigger step and replace it with this to see what shows up in the log.

service: system_log.write
data:
  message: "Value: {{ seconds_before_dimmed }}"

Looks like it does get passed, yet HA keeps seeing it’s undefined (I changed it to 10 seconds from 30 in the automation for faster troubleshooting):

Double huh. Can you try my other suggestion using the system_log.write? And replace the description with quotes.

Here’s what I get when using the system_log.write (and changed description):

Logger: homeassistant.components.system_log.external
Source: components/system_log/__init__.py:189
Integration: System Log ([documentation](https://www.home-assistant.io/integrations/system_log), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+system_log%22))
First occurred: 10:41:17 (1 occurrences)
Last logged: 10:41:17

Value: 10

So the value got passed to the script and it can be read…

Can you put the wait_for_trigger back in but leave the description as quotes?

Wait for trigger back and description as quotes :pensive::

Logger: homeassistant.components.template.trigger
Source: components/template/trigger.py:126
Integration: Template ([documentation](https://www.home-assistant.io/integrations/template), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+template%22))
First occurred: 10:50:44 (1 occurrences)
Last logged: 10:50:44

Error rendering 'Motion - Generic Component' for template: UndefinedError: 'seconds_before_dimmed' is undefined

Did you happen leave the system_log.write call in there by any chance? If you want to try to see what happens, put it before the wait_for_trigger.

This is bugging me. I feel like there is something really simple here we’re missing…

I just added it back in like so:

  - service: system_log.write
    data:
      message: "Value: {{ seconds_before_dimmed }}"
  - wait_for_trigger:
      - platform: template
        value_template: '{{ is_state(motion_sensor, ''off'') }}'
        for:
          seconds: '{{ seconds_before_dimmed|int }}'

Which gives me the previous results:

It’s really weird, I feel like this should just work but it doesn’t :woozy_face: