Template cover - how to get position in set_cover_position

Hello,
I am trying to set up a template cover to control a tilt of other cover entity (by ESPHome), but I cannot figure out how to get the position in set_cover_position action, I tried to do {{position}} as the doc stated, but I got no value.

cover:
  - platform: template
    covers:
      living_room_cover_tilt:
        friendly_name: "Living Room Cover Tilt"
        position_template: "{{ state_attr('cover.living_room_cover', 'current_tilt_position') | int }}"
        availability_template: "{% if is_state('cover.living_room_cover', 'unavailable') %} false {% else %} true {% endif %}"
        entity_id: cover.living_room_cover_tilt
        device_class: blind
        open_cover:
          service: cover.open_cover_tilt
          data:
            entity_id: cover.living_room_cover
        close_cover:
          service: cover.close_cover_tilt
          data:
            entity_id: cover.living_room_cover
        set_cover_position:
          service: cover.set_cover_tilt_position
          data:
            entity_id: cover.living_room_cover
            tilt_position: "{{position}}"
          

Using this I get an error: Failed to call service cover/set_cover_position. expected int for dictionary value @ data['tilt_position']

I even tried to use MQTT to listen to the topic to see the actual value, but there is none.

set_cover_position:
          service: mqtt.publish
          data:
            topic: /test/cover
            payload_template: "{{position}}"

Also tried {{position | int}} as I noticed the position is a float, but that didn’t help.

Also the position_template doesn’t work, I want it to sync with the current tilt position of the cover.living_room_cover.

Can anyone help please?

If you are wondering why I am doing it like this it’s because Google Assistant doesn’t support tilt setting, so I want to expose another entity for tilt.

Need to use data_template:

1 Like

I really don’t see why you are doing this… You have a cover and you’re making a template from said cover. What’s your goal here and are you sure your entities are correct?

Can you please elaborate a bit more? I didn’t find data_template neither on Cover component in any service, nor in template cover. Only data_template I know of is in automations, but I am not using automations.

I just want to sync the tilt position and services (open, close, set position) of one cover with a position of second cover.

When I call service: cover.set_cover_tilt_position in Developer tools with given value, it works, I just can’t seem to get the value of the position in the set_cover_position action

Did you read the last sentence in the original post? I am doing this because I have a Cover set up using ESPHome, this get’s exposed in HA via it’s native API, this is then available in my Google Home for voice control, but it is possible to control only position of the blind by saying: “Set living room cover to 70 percent” for example. This is why I need a second Cover entity, which is called “Living Room Cover Tilt”, where by setting it’s position it will alter the tilt of the first cover, so I will be able to say: “Set living room cover tilt to 50 percent” to tilt the blind to 45 degrees for example.

I was referring to the set_cover_position section of the template cover config. You’re using a template for tilt_position so you need to use data_template: instead of data:

1 Like

Yes I did but it didn’t make sense to me because you’re creating the exact same item… but it turns out I didn’t notice tilt position vs position. Take a chill pill, just trying to help.

@Tediore awesome, that did the trick, thank you very much, I had no clue I have to use data_template instead of just data when using templates, I thought it was simply just like using a variable, but it needs to have this to actually parse the template.

Would you also please happen to know why the position_template is not working. Basically I have a cover called cover.living_room_cover, which supports tilt position. I need to sync current_tilt_position of the cover.living_room_cover with current_position of this template cover.living_room_cover_tilt.
I tried this

position_template: "{{ state_attr('cover.living_room_cover', 'current_tilt_position') | int }}"

but it seems to only update once on reboot, after changing it doesn’t report the correct value.

@petro oh sorry, that wasn’t supposed to sound agressive or anything. I just normally tend to write too many details in my original post and then nobody reads it because it’s too long, so I tried to keep it short this time :slight_smile: I understand the confusion, the tilt position and position mixes up in my brain all the time haha

1 Like

This should also change

        entity_id: cover.living_room_cover_tilt

to this

        entity_id: cover.living_room_cover

entity_id field is for updates to the cover. You don’t want it referencing itself.

You mean in the cover.set_cover_tilt_position service call?
I believe it is not referencing itself, this template cover I posted has entity_id cover.living_room_cover_tilt, which is just a pseudo entity.

The actual working cover device has entity_id: cover.living_room_cover, so I am calling a service cover.set_cover_tilt_position on this entity, from an action in this template cover cover.living_room_cover_tilt, or am I missing something?
It gets a bit confusing with all those tilt words in those names and services aswell.

The entity_id field that you’re using at the root level, outside your service calls. It’s probably not even necessary but you currently have it pointing to itself.

1 Like

Oh you are right, I should read the docs more carefully, I just saw entity_id and assumed it is to manually select the entity_id of this template cover, but actually it is used to react to the state of listed covers, that might actually be the culprit. Again, my brain messed up with playing too much with ESPHome and setting up manually ids of each component.
These covers will kill me, in ESP I also had to make a pseudo template cover, so I can use time-based cover (which doesn’t support tilt), with tilt function.
Thank you :slight_smile:

1 Like

The real problem is how behind google home and alexa are. They should all support this crap but they are taking 10 years to roll it out.

Exactly, they are too far behind, yet I believe they are soo close with this, they have all the voice recognition pieces in place, with all the modes and chained commands, they would easily be able to achieve this, with a little work on their part, only if they wanted to, that’s the problem, they don’t care, so we need to make all these workarounds. Same with source inputs on media_player for example, why do they give you a pre-made list of possible values, why can’t you just send them your list of values to be evaluated with your voice command.

Btw other way I wanted to achieve this cover and tilt function before I decided on doing separate covers was to use template light, brightness would set the position, and then I would set colors for the tilt: green = open, orange = 45degrees, red = closed lol

Template light would have low WAF in my house, Your solution is the best

1 Like

Haha, that’s right. It’s already enough that I have both Alexa and Google Home in the living room and she is always confused who to talk to.

1 Like

I will open a new thread as this one is 3 months old, but I am having a similar problem, everything works fine, except when I try to move the slider on Lovelace. Then I get an error: Failed to call service cover/set_cover_position. can only concatenate str (not “int”) to str.

I have looked all over and tried all kinds of solution I found here, for 2 days yet. Can someone please help me pointing to what am I missing?

cover:
  - platform: template
    covers:
      suite_shades:
        device_class: shade
        friendly_name: "Suite Shades"
        position_template: "{{ states('sensor.shades') }}"
        open_cover:
          service: script.open_shades
          data:
            shade: Shades
        close_cover:
          service: script.close_shades
          data:
            shade: Shades
        stop_cover:
          service: script.stop_shades
          data:
            shade: Shades
        set_cover_position:
          service: script.posiciona_shades
          data_template:
            position: "{{ position }}"
            shade: Shades

and relevant scripts…

script:
  posiciona_shades:
    mode: single
    sequence:
      - choose:
        # IF must open_shades
        - conditions:
            - condition: template
              value_template: "{{ position|int > (states('sensor.{{ shade }}') + 9) }}"
          sequence:
            - service: script.turn_on
              entity_id:
                - script.rise_shades
              data_template:
                variables:
                  deltaporcento: "{{ position|int - states('sensor.{{ shade }}') }}"
                  shade: "{{ shade }}"
            - service: mqtt.publish
              data:
                topic: "void/Shades/Position"
              data_template:
                payload: "{{ position }}"
      # ELSEIF must close_shades
        - conditions:
            - condition: template
              value_template: "{{ position|int < (states('sensor.{{ shade }}') - 9) }}"
          sequence:
            - service: script.turn_on
              entity_id:
                - script.lower_shades
              data_template:
                variables:
                  deltaporcento: "{{ states('sensor.{{ shade }}') - position|int }}"
                  shade: "{{ shade }}"
            - service: mqtt.publish
              data:
                topic: "void/Shades/Position"
              data_template:
                payload: "{{ position }}"

  lower_shades:
    mode: single
    sequence:
      - service: switch.turn_off
        data_template:
          entity_id: "switch.{{ shade }}"
      - delay:
          milliseconds: 1000
      - service: switch.turn_on
        data_template:
          entity_id: "switch.{{ shade }}_up"
      - delay:
          milliseconds: 1000
      - service: switch.turn_on
        data_template:
          entity_id: "switch.{{ shade }}"
      - delay: 
          milliseconds: "{{ deltaporcento|int * 140 }}" # Takes 14s to close
      - service: switch.turn_off
        data_template:
          entity_id: "switch.{{ shade }}"
  rise_shades:
    mode: single
    sequence:
      - service: switch.turn_off
        data_template:
          entity_id: "switch.{{ shade }}"
      - delay:
          milliseconds: 1000
      - service: switch.turn_off
        data_template:
          entity_id: "switch.{{ shade }}_up"
      - delay:
          milliseconds: 1000
      - service: switch.turn_on
        data_template:
          entity_id: "switch.{{ shade }}"
      - delay: 
          milliseconds: "{{ deltaporcento|int * 150 }}" # Takes 15s to open
      - service: switch.turn_off
        data_template:
          entity_id: "switch.{{ shade }}"
  stop_shades:
    sequence:
      - service: switch.turn_off
        data_template:
          entity_id: "switch.{{ shade }}"

Ok, forget about it. It was my bad understanding of state types and conversion syntax.
It had to do with value_template conditions and operations rather than Cover entities…

Can either of you guys post your now corrected, working configs, and maybe if you’re feeling super helpful an explanation of how they work? I’m trying to do the exact same thing I think. I made a microcontroller that creates auto discoverable blinds over MQTT. Everything works fine except as mentioned above - Google Home can’t control their tilt. I was going to try to expose a fake switch or something for each window and then a bunch of voice routines for each individual blind, but this solution seems more elegant if someone doesn’t mind helping explain it a little.

Technically, there may be an even easier solution to my problem, as my blinds only have tilt open or closed, no percentage position. Also, going to “open” is technically the down command because they open when they are all the way down. Whereas “closed” is the stop command, which doubles as the “favorite” command. So I’d settle for even being able to say “hey google, stop the kitchen blinds.” Unfortunately, that seems to always fail as well, because google says the blind is not in motion (regardless of whether or not it is).

Any help you guys can offer would be great, thanks!