Help with cover position automation

Hi,
I’m trying to create an automation for my cover entity based on an input_select change. The input_select exposes 4 modes and what I’m trying to do is match the position for each mode.
This is what I’ve tried:

- id: window_automation
  alias: Window Automation
  trigger:
  - platform: state
    entity_id: input_select.window
  action:
  - service: cover.set_cover_position
    entity_id: cover.main
    data:
        position : >
          {% if is_state(''input_select.window'', ''Open'') %}
          100
          {% elif is_state(''input_select.window'', ''Half'') %}
          65
          {% elif is_state(''input_select.window'', ''Sleep'') %}
          35
          {% elif is_state(''input_select.window'', ''Close'') %}
          5
          {% endif %}
  mode: single

And this is the error I get:
2023-02-04 12:51:33.686 ERROR (MainThread) [homeassistant.components.automation] Automation with alias 'Window Automation' could not be validated and has been disabled: template value should be a string for dictionary value @ data['action'][0]['data']. Got OrderedDict([('position', "{% if is_state(''input_select.window'', ''Open'') %} 100\n{% elif is_state(''input_select.window'', ''Half'') %} 65\n{% elif is_state(''input_select.window'', ''Sleep'') %} 35\n{% elif is_state(''input_select.window'', ''Close'') %} 5\n{% endif %}")])

Would appreciate your help
Thanks

Remove the space before the colon.

        position : >
                ^
                |
     Remove this space character 

If you’re interested, you can reduce the automation to this:

- id: window_automation
  alias: Window Automation
  trigger:
    - platform: state
      entity_id: input_select.window
  action:
    - variables:
        p:
          'Open': 100
          'Half': 65
          'Sleep': 35
          'Close': 5
    - service: cover.set_cover_position
      target:
        entity_id: cover.main
      data:
        position: "{{ p.get(trigger.to_state.state, 5) }}"
  mode: single
2 Likes

Thanks for the help!
I tried to following (without the space):

- id: window_automation
  alias: Window Automation
  trigger:
  - platform: state
    entity_id: input_select.window
  action:
  - service: cover.set_cover_position
    entity_id: cover.main
    data:
        position: >
          {% if is_state(''input_select.window'', ''Open'') %}
          100
          {% elif is_state(''input_select.window'', ''Half'') %}
          65
          {% elif is_state(''input_select.window'', ''Sleep'') %}
          35
          {% elif is_state(''input_select.window'', ''Close'') %}
          5
          {% endif %}

still getting the same error.
Also, can you please explain how "{{ p.get(trigger.to_state.state, 5) }}" works? Would like to understand the syntax better…

The variable p is a dictionary containing keys (Open, Half, etc) with corresponding values (100, 65, etc).

trigger.to_state.state contains the value of of input_select.window that triggered the automation.

This template uses the value of trigger.to_state.state to find the matching key in the dictionary p. If it finds a matching key, it reports the key’s value. For example, if the key is Sleep it reports 35. If it can’t find a matching key, it reports its default value 5.

1 Like

Thanks a lot! Any idea why my original implementation isn’t working?

Probably due to more syntax errors.