Set cover position based on current position

Each press of a button should lower the cover by 25% from the current position

            entity: cover.salon_store_se
            tap_action:
              action: call-service
              service: cover.set_cover_position
              service_data:
                entity_id: cover.salon_store_se
                position: "{{ state_attr('cover.salon_store_se', 'current_position')|int - 25 }}"


The code return an INT error.

i think youve missed a space before and after the |

"{{ state_attr('cover.salon_store_se', 'current_position') | int - 25 }}"

Is the current position a percentage? What is the value before you subtract 25 from it?

That makes no difference. xx | int is the same as xx|int or xx |int etc.

You can’t use templates in lovelace service calls, you need a card like this in order to use templates in lovelace.

You also need to take into consideration when the position is currently < 25, because this will not work as you have it now.

I didn’t realise the spaces were insignificant, sorry about that.
I think you could make a script that sets the new value based on this template, then use the button to trigger the script.

That’s exactly what I would do, and pass over the cover entity to the script, so that you can use it for multiple buttons easily.

the script did the trick. Thanks a lot.

            entity: cover.salon_store_se
            tap_action:
              action: call-service
              service: script.turn_on
              service_data:
                entity_id: script.lower_blind

Can you elaborate on how to pass the entity-id to the script. I am afraid this is beyond my current knowledge

Erik

See the docs here -> https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts

Thanks again for the help
I learned a lot today.
Brilliant
Erik

Well, my test worked fine but now trying to use the variables in the script is giving me headaches.
My element on the UI is defined as follows:

          - type: state-icon
            title: BlindSE
            icon: mdi:arrow-down
            entity: cover.salon_store_se
            tap_action:
              action: call-service
              service: script.turn_on
              service_data:
                entity_id: script.set_blind
                variables:
                  blind: "cover.salon_store_se"
                  pos: "-25"
                  tilt: "45"
            style:

and the script:

set_blind:
  alias: 'Lower Blind'
  sequence:
    - service: cover.set_cover_position
      data:
        entity_id: "{{ blind }}"
        position: "{{ state_attr('blind', 'current_position')|int - 'pos' }}"
    - service: cover.set_cover_tilt_position
      data:
        entity_id: "{{ blind }}"
        tilt_position: "{{ tilt }}"

but the position statement does not work…

Remove the single quotes around pos and blind in your script. When you add quotes it will be interpreted as a string.

set_blind:
  alias: 'Lower Blind'
  sequence:
    - service: cover.set_cover_position
      data:
        entity_id: "{{ blind }}"
        position: "{{ state_attr(blind, 'current_position')|int - pos }}"
    - service: cover.set_cover_tilt_position
      data:
        entity_id: "{{ blind }}"
        tilt_position: "{{ tilt }}"

Thanks for the help
Tried that but getting

Error rendering data template: TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

don’t mind the + I changed the sign from - to + since I am sending an negative number (-25)

OK I fixed it

needed |int for pos variable

    - service: cover.set_cover_position
      data:
        entity_id: "{{ blind }}"
        position: "{{ state_attr(blind, 'current_position')|int + pos |int }}"

thanks again for the support

3 Likes

Try castingthe variable pos as an int like this
pos|int

Yes that was it…
Thx

Hi,

I am trying to achieve the same thing mentioned above.

I have a template cover. Below is the code.

- platform: template
  covers:
    virtual_cover_1:
      device_class: shade
      friendly_name: "Virtual Cover 1"
      open_cover:
        service: script.new_script
      close_cover:

      stop_cover:

      set_cover_position:

      set_cover_tilt_position:

and a script that opens the cover by 25% each time it runs

service: cover.set_cover_position
data:
  position: >
    {% set new_position =
    (state_attr('cover.virtual_cover_1','current_position') | int) + 25 %}

    {% if new_position <= 100 -%}
      {{ new_position }}
    {%- else -%}
      100
    {%- endif %}
target:
  entity_id: cover.virtual_cover_1
enabled: true

This script is supposed to run when i click the open_cover button in the cover card. But it does not execute. The open cover button opens the blind 100% without stopping at current_position+25

But if I run the script individually, it runs perfectly. Attaching screengrab below

Recording 2023-04-04 035625

What am I doing wrong here…

@petro … Hei… i have seen you reply on a lot of topics connected with tempate cover. could you please help me out on this??

is this tap action defined on an additional helper button, or are you using the same up and down button on the cover card??

i tried adding a script to the cover card button, but doesent work. for just a sec, the button press stops at 25%, but then goes to 100%. i am only able to get cover closed 0% or cover open 100%. When i add an addition la button helper and aggign the scrip to that button, then the cover moves poperly ay 25% increment on each click. What am i dong wrong here…

this is my code for the cover card.

type: custom:mushroom-cover-card
show_buttons_control: true
entity: cover.virtual_cover_1
show_position_control: true
show_tilt_position_control: true
fill_container: false

My cover template code is as follows

- platform: template
  covers:
    virtual_cover_1:
      device_class: shade
      friendly_name: "Virtual Cover 1"
      open_cover:
        service: script.new_script
      close_cover:

      stop_cover:

      set_cover_position:

      set_cover_tilt_position:

and my script is

alias: New Script
sequence:
  - service: cover.set_cover_position
    data:
      position: >
        {% set new_position =
        (state_attr('cover.virtual_cover_1','current_position') | int) + 25 %}
        {% if new_position <= 100 %}
          {{ new_position }}
        {% else %}
          100
        {% endif %}
    target:
      entity_id: cover.virtual_cover_1
    enabled: true
mode: single