I want to pass an entity to a script which contains several attributes I configured in customize.yaml.
Here is an entry in customize.yaml for reference:
binary_sensor.back_porch_cam_motion:
icon: mdi:motion_sensor
switch_entity: switch.patio_light_1
enable_entity: input_boolean.enabledrivewayfloodsonmotion
timer_entity: timer.patiolight
duration_entity: input_number.durationlightsmotiondetect
The Idea I have is to create one script which handles all all the exterior lights by passing the state object for the motion detect binary sensor to the script. This will allow a common code block to handle all exterior lights, and eliminate code repetition.
test_script:
alias: Light motion script
fields:
arg_binary_sensor:
description: "Binary sensor object with the required attributes"
default: binary_sensor.onvif2mqtt/back-porch-cam/motion
sequence:
- condition: and
conditions:
- condition: state
# entity_id: input_boolean.enabledrivewayfloodsonmotion
entity_id: "{{ state_attr(arg_binary_sensor, 'enable_entity')}}"
state: "on"
- condition: state
entity_id: timer.patiolight
state: idle
- condition: state
entity_id: switch.patio_light_1
state: "off"
- service: timer.start
data:
duration: "{{states('input_number.durationlightsmotiondetect')}}"
target:
entity_id: timer.patiolight
- service: switch.turn_on
data: {}
target:
entity_id: switch.patio_light_1
mode: queued
icon: mdi:lightbulb-auto
max: 10
This line from above is throwing an error in Studio Code Server:
entity_id: "{{ state_attr(arg_binary_sensor, 'enable_entity')}}"
The error is
String does not match the pattern of "^(?!.+)(?!)[\da-z]+(?<!).(?!)[\da-z_]+(?<!_)\s?(?:,\s?(?!.+)(?!)[\da-z]+(?<!).(?!)[\da-z_]+(…
How do you access the attributes from a state object passed in to a script?