Comparing to fields input

Hello,
I have a script that receives input via fields. Based on those fields’ input, I would like to set a variable. I am editing the script externally but when I load it I receive an error message (see below).

My script:

raffstore_set_position:
  alias: Raffstore_set_position
  sequence:
  - variables:
      # I know the error is coming from that comparison below. 
      # If I change the fields name to hard coded values, the script runs fine
      tilt: >-
        {% if {{ state_attr( entity_id , 'current_position')|int  <  position|int}} %}
          100
        {% elif {{ state_attr( entity_id , 'current_position')|int >  position|int}} %}
          0
        {% else %}
        {% endif %}
  - event: set_Raffstore_Positionen
    event_data:
      key: '{{ entity_id }}_Tilt'
      value: '{{ tilt }}'
  - action: cover.set_cover_position
    data:
      position: '{{ position }}'
    target:
      entity_id: '{{ entity_id }}'
  fields:
    entity_id:
      description: The cover entity
      example: cover.X
    position:
      description: position to go to
      example: 80
  mode: parallel
  description: Set position of Raffstore
  icon: mdi:blinds-horizontal

Here is the error from the Logs:

Logger: homeassistant.components.script
Source: components/script/config.py:147
integration: Script (documentation, issues)
First occurred: 2:59:42 PM (20 occurrences)
Last logged: 4:52:53 PM

Script with alias 'Raffstore_set_position' could not be validated and has been disabled: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['sequence'][0]['variables']['tilt']. Got "{% if {{ state_attr( entity_id , 'current_position') }} < 80 %}\n 100\n{% elif {{ state_attr( entity_id , 'current_position') }} > 80 %}\n 0\n{% endif %}\n"
Script with alias 'Raffstore_set_position' could not be validated and has been disabled: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['sequence'][0]['variables']['tilt']. Got "{% if {{ state_attr( entity_id , 'current_position') }} < 80 %}\n 100\n{% elif {{ state_attr( entity_id , 'current_position') }} > 80 %}\n 0\n{% endif %}"
Script with alias 'Raffstore_set_position' could not be validated and has been disabled: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['sequence'][0]['variables']['tilt']. Got "{% if state_attr( 'cover.raffstore_hst_links' , 'current_position') < {{position}} %}\n 100\n{% elif state_attr( 'cover.raffstore_hst_links' , 'current_position') > {{position}} %}\n 0\n{% endif %}\n"
Script with alias 'Raffstore_set_position' could not be validated and has been disabled: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['sequence'][0]['variables']['tilt']. Got "{% if state_attr( 'cover.raffstore_hst_links' , 'current_position') < {{position|int}} %}\n 100\n{% elif state_attr( 'cover.raffstore_hst_links' , 'current_position') > {{position|int}} %}\n 0\n{% endif %}"
Script with alias 'Raffstore_set_position' could not be validated and has been disabled: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['sequence'][0]['variables']['tilt']. Got "{% if {{ state_attr( entity_id , 'current_position')|int < position|int}} %}\n 100\n{% elif {{ state_attr( entity_id , 'current_position')|int > position|int}} %}\n 0\n{% else %} {% endif %}"

Thanks for your support!

Tim

Your Jinja is incorrect:

        {% if {{ state_attr( entity_id , 'current_position')|int  <  position|int}} %}
          100
        {% elif {{ state_attr( entity_id , 'current_position')|int >  position|int}} %}
          0
        {% else %}
        {% endif %}

Should be

        {% if state_attr( entity_id , 'current_position')|int  <  position|int %}
          100
        {% elif state_attr( entity_id , 'current_position')|int >  position|int %}
          0
        {% else %}
        {% endif %}

You don’t need to double bracket when inside Jinja like that unless it’s the output.

And you should probably settle that else statement with something in case the two conditions don’t match - which can happen if current position is EQUAL to position, since you are only checking < or >.

Hi Lowrider614,

In addition I would consider setting defaults to stave off errors in these fields. default is one of the available keys.
Scripts - Home Assistant.

Thanks to both of you!

And you should probably settle that else statement with something in case the two conditions don’t match - which can happen if current position is EQUAL to position, since you are only checking < or >.

What would be the correct expression for an empty value? Because that is basically what should happen in the ‘=’ case.

In addition I would consider setting defaults to stave off errors in these fields. default is one of the available keys.

I like that idea, as the missing default has given me issues with other scripts in the past. I would provide an empty entity ID though, as I do not want anything to happen in the default case. If I provide a cover entity here, in all failure cases the default cover is potentially being moved. This prevent the entry in my error log but will confuse everyone in the “real” world.
Do you have an idea what statement I would need to accomplish both, preventing the error and unintended movements of my covers?

I would put in conditionals to prevent actions when you do not want them, instead of relying on the action to err out on missing entities or tilt parameters.