Dialogflow + ha action issues

I’m having slight issue finishing my Dialogflow + Google Assistant setup to complete satisfaction. And I wonder if anybody can spot any typos/bugs.

The Dialogflow side has intents and entities configured to query and control the state of garage doors, gates and similar. That side is working and talking to my ha. I can get the state of the entities no problem, but when I try to turn on/off the hardware entity I’m hitting a brick wall. So the action part of my script is the focus of my enquiry.

To help to understand my configuration, in Dialogflow I have entity called TypeOfDoor that contains garage door as one of the values and Status with values open, closed. The entity ids in ha is switch.garage_door for a momentary switch and sensor.garage_door_state for the sensor. The action should turn on the switch, delay 0.3 s and switch it off. Whilst it is a momentary switch which will merely “blink” the power, I still prefer to turn it off, so as not to hold the switch in on logical position.

I’ll add that the whole speech section is working without a hitch.

In ha configuration.yaml I have:

setDoorState:
speech:
  text: >
    {%- set TypeOfDoorEntity = TypeOfDoor | replace(' ', '_') -%}
    {%- set entityStatePrefix = "sensor." -%}
    {%- set entityStateSufix = "_state" -%}
    {%- set entityStateID = [entityStatePrefix,TypeOfDoorEntity,entityStateSufix] | join -%}
    {%- set entityState = states(entityStateID) %}
    {%- if Status == "open" -%}
      {% if entityState == "open" %}
        The {{TypeOfDoor}} is already {{entityState}}.
      {% else %}
        Ok, opening the {{TypeOfDoor}}.
      {% endif %}
    {%- elif Status == "closed" -%}
      {% if entityState == "closed" %}
        The {{TypeOfDoor}} is already {{entityState}}.
      {% else %}
        Ok, closing the {{TypeOfDoor}}.
      {% endif %}
    {%- endif -%}
  action:
    - service_template: >
        {%- set TypeOfDoorEntity = TypeOfDoor | replace(' ', '_') -%}
        {%- set entitySwitchPrefix = "switch." -%}
        {%- set entityStatePrefix = "sensor." -%}
        {%- set entityStateSufix = "_state" -%}
        {%- set entitySwitchID = [entityPrefix,TypeOfDoorEntity] | join -%}
        {%- set entityStateID = [entityStatePrefix,TypeOfDoorEntity,entityStateSufix] | join -%}
        {%- set entityState = states(entityStateID) %}
        {%- if Status == "open" -%}
          {%- if entityState == "closed" -%}
            switch.turn_on
          {%- endif -%}
        {%- elif Status == "closed" -%}
          {%- if entityState == "open" -%}
            switch.turn_on
          {%- endif -%}
        {%- endif -%}
      data_template:
        entity_id: switch.{{ TypeOfDoor | replace(' ', '_') }}
    - delay: 0.3s
    - service: switch.turn_off
      data_template:
        entity_id: switch.{{ TypeOfDoor | replace(' ', '_') }}

Any ideas, why the control is not doing what it should? Any way to observe what action has been called? In the Templates debug I seem to have the entities parsing correctly in the - service_template, but I’m not sure if the entity_id is parsed correctly in data_template.

Problem solved. It was in correct indentation. Just for completion here is the code

setDoorState:
    speech:
      text: >
        {%- set TypeOfDoorEntity = TypeOfDoor | replace(' ', '_') -%}
        {%- set entityStatePrefix = "sensor." -%}
        {%- set entityStateSufix = "_state" -%}
        {%- set entityStateID = [entityStatePrefix,TypeOfDoorEntity,entityStateSufix] | join -%}
        {%- set entityState = states(entityStateID) %}
        {%- if Status == "open" and entityState == "closed" -%}
          Ok, opening the {{TypeOfDoor}}.
        {%- elif Status == "closed" and entityState == "open" -%}
          Ok, closing the {{TypeOfDoor}}.
        {%- else -%}
          The {{TypeOfDoor}} is already {{entityState}}.
        {%- endif -%}
    action:
      - service_template: >
          {%- set TypeOfDoorEntity = TypeOfDoor | replace(' ', '_') -%}
          {%- set entitySwitchPrefix = "switch." -%}
          {%- set entityStatePrefix = "sensor." -%}
          {%- set entityStateSufix = "_state" -%}
          {%- set entitySwitchID = [entityPrefix,TypeOfDoorEntity] | join -%}
          {%- set entityStateID = [entityStatePrefix,TypeOfDoorEntity,entityStateSufix] | join -%}
          {%- set entityState = states(entityStateID) %}
          {%- if Status == "open" and entityState == "closed"-%}
            switch.turn_on
          {%- elif Status == "closed" and entityState == "open"-%}
            switch.turn_on
          {%- else -%}
            switch.turn_off
          {%- endif -%}
        data_template:
          entity_id: >
            {%- set TypeOfDoorEntity = TypeOfDoor | replace(' ', '_') -%}
            switch.{{ TypeOfDoorEntity }}