Template cover - Provide variables to script

Hello,

I am building a template cover at the moment, to use the tilt functionality with Shelly 2 PM actors, that do not provide this functionality out-of-the-box. I know there are generation 3 variants of these Shellys, but I have 10 Shellys built into the walls and they are working well, so I do not want to change them.

I want to use template covers to store the current tilt angle. Whenever there is a position change to the template cover, the current tilt angle attribute of the changed template cover is updated.

I have created 5 scripts, that perform open, close, stop, set position and set tilt position actions and set the current tilt angle attribute to the template cover.

As an example, please find the script for the set position action below.

script:
  raffstore_set_position:
    alias: Raffstore_set_position
    fields:
      entity_id:
        description: The cover entity
        example: cover.X
      template_id:
        description: The entity of the template cover
        example: cover.X_template
      position:
        description: position to go to
        example: '80'
    sequence:
    - variables:
        tilt: "{% if state_attr( entity_id , 'current_position')|int  <  position|int %}
          100
          {% elif state_attr( entity_id , 'current_position')|int >  position|int%}
          0
          {% endif %}"
    - action: logbook.log
      data:
        message: >
          Entity: {{entity_id}} ##
          Template: {{template_id}} ##
          Tilt set by Script: {{ tilt }} ## 
          Current position: {{ state_attr( entity_id , 'current_position')|int }} ## 
          Position to be set: {{ position }} ## 
        name: Debugging 
        entity_id: "{{ entity_id }}"
    - if:
      - condition: template
        value_template: '{{ state_attr( entity_id , ''current_position'')|int  !=  position|int}}'
      then:
      - action: cover.set_cover_position
        data:
          position: '{{ position }}'
        target:
          entity_id: '{{ entity_id }}'
      - action: python_script.hass_entities
        data:
          action: set_attributes
          entity_id: "{{ template_id }}"
          attributes:
          - current_tilt_position: "{{ tilt }}"
          log_enabled: true
      - wait_template: '{{ states( entity_id ) == ''open'' or states( entity_id ) == ''closed'' }}'
        continue_on_timeout: true
        timeout: '65'
      - action: python_script.hass_entities
        data:
          action: set_attributes
          entity_id: "{{ template_id }}"
          attributes:
          - current_tilt_position: "{{ tilt }}"
          log_enabled: true
    mode: single
    description: Set position of Raffstore
    icon: mdi:blinds-horizontal

The two parts

      - action: python_script.hass_entities
        data:
          action: set_attributes
          entity_id: "{{ template_id }}"
          attributes:
          - current_tilt_position: "{{ tilt }}"
          log_enabled: true

call a python script, which is setting the attribute “current_tilt_position”.

If I call the script directly from the actions tab in the developer section, it works as expected. The attribute “current_tilt_position” is set correctly in the template cover.

This is how I call the script directly.

action: script.raffstore_set_position
data: 
  entity_id: cover.raffstore_hst_links
  template_id: cover.template_hst_links
  position: 80

Now I have created the template cover like below. It is supposed to call my custom script from above, when the standard “cover.set_position” action is called on the template cover.

cover:
  - platform: template
    covers:
      template_hst_links:
        device_class: blind
        friendly_name: "Raffstore HST links template"
        unique_id: 5e809014-a83b-4e30-b9e8-5723c28ed177
        value_template: "{{ states('cover.raffstore_hst_links') }}"
        position_template: "{{ state_attr('cover.raffstore_hst_links', 'current_position') }}"
        set_cover_position:
          action: script.raffstore_set_position
          data:
            entity_id: cover.raffstore_hst_links
            template_id: cover.template_hst_links
            position: "{{ position }}"

No I call the standard cover.set_position action and use the template cover as target.

action: cover.set_cover_position
data:
  position: 90
target:
  entity_id: cover.template_hst_links

To whatever reason, the attribute “cover_tilt_position” is not set anymore, when I call the standard cover.set_position action with the template cover as target. The logging part of my raffstore.set_position script is not logging a template_id anymore either, so the reason for my trouble seems to be, that the template_id is not handed over to my custom script, when I call the standard cover.set_position script with the template cover as target.

Any ideas why this happens?

Thanks and best regards,

Tim