Passing an entity object to a script to access its attributes

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?

A State Condition’s entity_id option doesn’t support templates.

Replace the State Condition with a Template Condition.

Example (in shorthand notation):

  sequence:
    - condition: and
      conditions:
        - "{{ is_state(state_attr(arg_binary_sensor, 'enable_entity'), 'on') }}"
        ... etc ...

That was the issue. Thanks for your help!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.