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:
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
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.