Creating a condition with a variable in a script

I am trying to create a simple script that takes 2 variables: a “garage_door” entity_id and “open_id” entity_id". I want to check that the state of the “open_id” is on (open) before I run the sequence to turn on the light of “garage_door” (that’s how the opener works. Essentially, I don’t want the script to execute if the door is closed. I have 2 garage doors and am trying to abstract the common components.

Here’s my script:

condition: state
    entity_id: "{{ open_id }}"
    state: "on"
sequence:
  - service: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: "{{ garage_door }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
mode: single

But I get "Message malformed: extra keys not allowed @data[‘condition’]. I’ve tried every format I can find, but nothing works. By the way, the part from sequence and below does work (with the one variable, “garage_door”).

Any help you could provide would help immensely. Thank you.

Scripts do not use separate configuration blocks for conditions and State conditions do not accept templates.

You need to move the condition to the action sequence and use your template in a template condition:

sequence:
  - condition: template
    value_template: "{{ states(open_id)|bool(false) }}"
  - service: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: "{{ garage_door }}"
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
mode: single

Thanks. I tried all manner of things, but nothing would be “compiled”. I think it did not like the variable supplied to the entity_id.

After much trial and error, I found a configuration that worked:

alias: garage_door_opener
sequence:
  - condition: "{{ is_state(open_id, 'on') }}"
  - service: light.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: "{{ garage_door }}"
    enabled: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
mode: single

The magic was this way to formulate the condition: condition: “{{ is_state(open_id, ‘on’) }}”.

I hope this helps anyone else in my predicament.