This is a solution for passing and executing actions dynamically in Home Assistant blueprints and scripts. The solution emerged through discussions with ChatGPT and was inspired by user nwithan8 and others in this thread.
Problem
The task was to execute actions defined during the configuration of a motion-activated blueprint. Passing an action as a runtime variable proved challenging considering that the action
selector returns a list of actions.
Solution
The approach involves executing the action set during configuration by employing a choose
condition in the action sequence since the choose
condition will take a list of actions.
Here is a snippet of the action
selector:
action_to_run:
name: Action to run.
description: Action to run when triggered.
selector:
action: {}
choose
with action_to_run
as default
:
- alias: "Run the action"
choose: []
default: !input action_to_run
Here’s the solution in an example blueprint (which is a modification of the default motion_light.yaml):
blueprint:
name: Motion-activated On/Off
description: Run an action when motion is detected, run another action after waiting for a period of no motion.
domain: automation
input:
motion_entity:
name: Motion Sensor
selector:
entity:
domain: binary_sensor
device_class: motion
on_action:
name: On Action
description: Action to run when motion is detected.
selector:
action: {}
off_action:
name: Off Action
description: Action to run when motion is no longer detected.
selector:
action: {}
no_motion_wait:
name: Wait time
description: Time to wait until off_action is called.
default: 2
selector:
number:
min: 0
max: 60
unit_of_measurement: minutes
variables:
delay_var: !input no_motion_wait
# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent
trigger:
platform: state
entity_id: !input motion_entity
from: "off"
to: "on"
action:
- alias: "Run the on action"
choose: []
default: !input on_action
- wait_for_trigger:
platform: state
entity_id: !input motion_entity
to: "off"
- delay: "{{ delay_var | multiply(60) }}"
- alias: "Run the off action"
choose: []
default: !input off_action
Here, choose
has an empty list of conditions and default
executes the action passed as on_action
and off_action
.
This method adds flexibility, as it allows actions performed to be defined at the time of configuring the blueprint or script.