Trouble passing parameter from ESPHome to HA script

I have been unable to get a parameter passed from ESPHome to a HA script to work. I am only having a problem with scripts. I have number of cases where passing a parameter to a service and those work just fine. I have looked at the documentation on scripts and a number of forum entries/examples and still have not been able to get the script to work. There is no error generated in the log file so am at a loss as to what could be wrong. I know the ESPHome code and the script works if I hard code the entity_id in the script. Once i replace the entity_id with ‘{{ entity_id }}’ it no longer works. The code for each is below.

---- ESPHome yaml code -------------------------

## Backdoor lock button
  - platform: nextion
    id: backdoor_lock
    name: $device_name Toggle Backdoor Lock
    page_id: 2
    component_id: 5
    on_click:
      - homeassistant.service:
          service: script.lock_toggle_backdoor
          data:
            entity_id: lock.schlage_touchscreen_deadbolt

------ script yaml with parameter entity_id --------------------

lock_toggle_backdoor:
  alias: Toggle Backdoor Lock
  fields:
    entity_id:
      description: Lock Entity ID
      example: lock.front_door
      required: true
      selector:
        entity:
          domain: lock
  sequence:
  - service: >
      {% if states( "'{{ entity_id }}'" ) == "locked" %}
      lock.unlock
      {% else %}
      lock.lock
      {% endif %}
    data:
      entity_id: '{{ entity_id }}'
  mode: single
  icon: mdi:lock

--------------- script with hard coded entity that works --------------

lock_toggle_backdoor:
  alias: Toggle Backdoor Lock
  fields:
    entity_id:
      description: Lock Entity ID
      example: lock.front_door
      required: true
      selector:
        entity:
          domain: lock
  sequence:
  - service: >
      {% if states( "lock.schlage_touchscreen_deadbolt" ) == "locked" %}
      lock.unlock
      {% else %}
      lock.lock
      {% endif %}
    data:
      entity_id: lock.schlage_touchscreen_deadbolt
  mode: single
  icon: mdi:lock

Can someone identify what I am doing wrong?

Why do you want it to be a variable?
Is the script supposed to do more than this door?
The script name seems to say it’s only for this one door script.lock_toggle_backdoor.

The way you are writing the ESP code you will run out of memory quite fast.

I would recommend that you in Nextion make one variable and on each button press this variable is populated with the name of the button you pressed.
Then on release clear the variable again.
Expose this variable to ESP-Home by making it global.

In ESP read this variable and pass it to Home Assistant.

Now you have sent any button click from Nextion → HA with just one entity in ESP-Home and you will probably not run out of memory.
And in HA you can trigger on the button click in a normal HA automation.

Try this:

  sequence:
  - service: >
      {% if states( entity_id ) == "locked" %}

@Hellis81 - Yes, the idea is to have the one script work for multiple locks. While your suggestion could be a solution I am trying to keep it simple rather than having to create complexity in the code. I can always create a separate script for each one but it seems like what I am trying to do should work.

@tom_I - I tried your suggestion and it works when the lock is in the unlocked state but does nothing when it is in the locked state. Which tells me that the condition statement failed (did not substitute the actual entity_id) so it executed the else portion to lock the door. This also indicates that the parameter is being passed and is being substituted in the statement " entity_id: ‘{{ entity_id }}’ ". Almost looks like there is something going on with the substitution in the if clause which causes the entire script to fail. Still no error on failure.

It will be much easier to switch to a method now that will last than keep going in a way that will fail once you have like 15-ish entities in the ESP.
When you get to that point the OTA will fail and you need to flash it manually and start removing entities to get it back running.