Template cover and position_template

Hi all,

I have a shutter at my door that I don’t want closing when that door is open.
Any automations that detect the shutter closing while the door is open and then quickly try to reopen the shutter are not fast enough for me.

I thought about using a template cover, I’m hoping I can add conditions in the service calls to “only run when the door is closed”.

So far, I can’t get it to work exactly the way I want.

The actual cover is called “cover.keuken_rolluikachterdeur_cover”. So I’m basically trying to create a template that does EXACTLY what the original cover does only add additional conditions in the service calls.

This is what I have so far.

      cover_rolluikachterdeur_security:
        unique_id: "cover_rolluikachterdeur_security"
        device_class: shutter
        friendly_name: "Rolluik Achterdeur"
        position_template: {{ (state_attr('cover.keuken_rolluikachterdeur_cover', 'current_position') | float) | round(0)}}
        open_cover:
          service: cover.open_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        close_cover:
          service: cover.close_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        stop_cover:
          service: cover.stop_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        icon_template: >-
          {% if state_attr('cover.keuken_rolluikachterdeur_cover', 'current_position')|float > 0 %}
            hass:window-shutter-open
          {% else %}
            hass:window-shutter-closed
          {% endif %}

This works. I can open/close and stop the shutter. However, I cannot see the current position or use the slider. Additionally, the icon displays the “open state” even when the actual cover is closed.

The original cover, there is a slider reporting position:
image

The template cover, notice there is no slider available:
image

I don’t understand why this is not working. I’ve used the template editor in the developer tools but this just tells me that the position is retrieved correctly… !
image

If anybody knows how I could fix this, that would be greatly appreciated.
Also on a side note, if anybody can point me in the right direction of adding conditions to this code (only open/close when door is closed) that would be great.

Thanks!

You don’t have a set_cover_position implemented so the slider won’t work but it should move when cover.keuken_rolluikachterdeur_cover moves.

But why are you even doing this? You’re just making a copy of the other cover that does nothing different accept you have an icon template. But you can use the shutter icons by changing the device_class on your existing cover by customizing it as device_class: shutter. So I’m not really sure why you’re even doing this.

EDIT: Nevermind, I see your comments about adding conditions in the future. Ignore that last paragraph.

I noticed you didn’t have quotes too. Anyways this should work.

      cover_rolluikachterdeur_security:
        unique_id: "cover_rolluikachterdeur_security"
        device_class: shutter
        friendly_name: "Rolluik Achterdeur"
        position_template: "{{ state_attr('cover.keuken_rolluikachterdeur_cover', 'current_position') }}"
        set_cover_position:
          service: cover.set_cover_position 
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
          data:
            position: "{{ position }}"
        open_cover:
          service: cover.open_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        close_cover:
          service: cover.close_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        stop_cover:
          service: cover.stop_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        icon_template: "hass:window-shutter-{{ 'open' if state_attr('cover.keuken_rolluikachterdeur_cover', 'current_position')|float > 0 else 'closed' }}"
1 Like

You the man!
Thanks, it works perfectly.
You get a heart :slight_smile:

NOW I’ll work on adding the conditions :wink:

Here I am again.
Not sure how to add the condition.
This is my code currently.

    covers:
      cover_rolluikachterdeur_security:
        unique_id: "cover_rolluikachterdeur_security"
        device_class: shutter
        friendly_name: "Rolluik Achterdeur"
        position_template: "{{ state_attr('cover.keuken_rolluikachterdeur_cover', 'current_position') }}"
        set_cover_position:
          service: cover.set_cover_position 
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
          data:
            position: "{{ position }}"
        open_cover:
          service: cover.open_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        close_cover: 
            service: >
              {% if is_state('binary_sensor.keuken_sensorachterdeur_alarm_access_control', 'off') %} 
                cover.close_cover  
              {% endif %}
            target:
              entity_id: cover.keuken_rolluikachterdeur_cover
        stop_cover:
          service: cover.stop_cover
          target:
            entity_id: cover.keuken_rolluikachterdeur_cover
        icon_template: "hass:window-shutter-{{ 'open' if state_attr('cover.keuken_rolluikachterdeur_cover', 'current_position')|float > 0 else 'closed' }}"   

Obviously the shutter doesn’t close now when the door is open but I do get this error in my hass instance:
“Rolluik Achterdeur: Error executing script. Error for call_service at pos 1: Template rendered invalid service:”

If your cover is not in the ‘off’ state then your close_cover template renders to:

        close_cover: 
            service:
            target:
              entity_id: cover.keuken_rolluikachterdeur_cover

That would not be a valid service definition.

I understand that this is happening, but I don’t know how to do it any other way.
Can you advise?

Turn them into lists so that you can add other actions, like condition or choose.

e.x.:

        close_cover: 
          - condition: state
            state: 'off'
            entity_id: binary_sensor.keuken_sensorachterdeur_alarm_access_control
          - service: cover.close_cover
            target:
              entity_id: cover.keuken_rolluikachterdeur_cover
1 Like

Thanks again petro!