Template Cover

Yes, that be a great idea! I would need exactly the same feature.
My purpose: I have a window cover connected to two switches, one is up, one is down. A template cover would be perfect to control it via the webui :slight_smile:

Hello,

The new “Template Cover” component is working perfectly. Great work @PhracturedBlue, thank you.

Would it be possible to add “opening time” and “closing time” variables. So that we can also use the slider function of the cover. For instance, if the initial position is 0 and “opening time” is 45 seconds, then if we want to set the cover position to %50, the component should send the “open_cover” command, wait for 22.5 seconds and then send the “stop_cover” command etc.

Thanks in advance.

That sounds like unnecessary complicating the component. You can probably achieve the same functionality using an automation.

The method i described is the method used by homekit. It is also usable in homebridge via homebridge-broadlink-rm plugin.
Having this built in is very user friendly.
In the “template cover” currently the slider is function-less.

I will also be glad to learn how such an automation can be implemented. I looked into time delays in home assistant and it didn’t seem trivial.

1 Like

Here is a crude example of how this might be achieved:
I created an input_slider to act as a state variable to keep track of the current position.
I then use a script to check if the current position is less than the requested position. If so, wait a fixed time and increment the current position by one and try again. HA odesn’t allow loops, so this must be done in two scripts. I only implemented the raising portion, you’d do something similar for lowering. Also, I didn’t add the calls to the start and stop commands, but those would be easy to add.

I did notice that the open/close/stop commands are required in the template, whereas they should not be when using set_position.

I think sendorm’s request may be useful, since this method is quite complicated, and relies on loops that I am not sure are robustly supported in HA.

input_slider:
  coverpos:
    min: 0
    max: 100
    initial: 42
cover:
  platform: template
  covers:
    test:
      position_template: '{{ states.input_slider.coverpos.state | int }}'
      open_cover:
        service: 'cover.open_cover'
        entity_id: 'cover.test'
      close_cover:
        service: 'cover.close_cover'
        entity_id: 'cover.test'
      stop_cover:
        service: 'cover.stop_cover'
        entity_id: 'cover.test'
      set_cover_position:
        service: 'script.change_cover_pos'
        data_template:
          position: '{{ position }}'

script:
  change_cover_pos:
    sequence:
      - delay:
          # tiny delay to prevent script aborting in loop
          seconds: 0.005
      - condition: numeric_state
        entity_id: cover.test
        value_template: '{{ float(position) - float(states.cover.test.attributes.current_position) }}'
        above: 0
      - service: script.raise_one_percent
        data_template:
          position: '{{ position }}'
  raise_one_percent:
    sequence:
      - delay:
          # time to move one percent
          seconds: 0.5
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.coverpos
          value: '{{ float(states.cover.test.attributes.current_position) + 1}}'
      - service: script.change_cover_pos
        data_template:
          position: '{{ position }}'[/details]

I went ahead and implemented the timed-cover concept.

grab the file from here and place it in .homeassistant/custom_components/cover/
https://raw.githubusercontent.com/PhracturedBlue/home-assistant/timed_cover/homeassistant/components/cover/template.py

You must have a open_cover, close_cover, stop_cover commands and NOT have set_cover_position.

Here is an example:

cover:
  platform: template
  covers:
    test:
      position_template: '{{ states.cover.test.attributes.current_position | int }}'
      open_cover:
        service: 'cover.open_cover'
        entity_id: 'cover.test_state'
      close_cover:
        service: 'cover.close_cover'
        entity_id: 'cover.test_state'
      stop_cover:
        service: 'cover.stop_cover'
        entity_id: 'cover.test_state'
      opening_time: 10
      closing_time: 10

Note that I have another commit in flight, so I won’t submit a pull request for this until (a) someone tries it and finds it useful and (b) my other pull-request is accepted.

Thank you very much for doing this again.
I’ve quickly tested it. On the front end the state of the cover is fixed at open (only close and stop buttons are avaliable). Also when i click the cover for details the slider is not avaliable (not visible).

This is the code I am using:

cover:
  - platform: template
    covers:
     testing:
        friendly_name: 'Just Testing'
        position_template: '{{ states.cover.testing.attributes.current_position | int }}'
        open_cover:
          service: switch.turn_on
          entity_id: switch.test
        close_cover:
          service: switch.turn_off
          entity_id: switch.test
        stop_cover:
          service: switch.turn_on
          entity_id: switch.test_stop    
        opening_time: 45
        closing_time: 45

And getting the error:

2017-07-04 19:53:27 ERROR (MainThread) [custom_components.cover.template] UndefinedError: 'None' has no attribute 'attributes'
2017-07-04 19:53:31 ERROR (MainThread) [custom_components.cover.template] UndefinedError: 'mappingproxy object' has no attribute 'current_position'

That is because I hadn’t tested the configuration exactly as I sent it. It turns out you need to initialize the state 1st:

For my example above, try this intead:

      position_template: '{% if states.cover.test %} {{ states.cover.test.attributes.current_position }} {% else %} 0 {% endif %}' 

Note that you need to find some way to know the initial state. I may need to add optimistic mode to keep track of the last state, but I don’t think it would be too reliable. If you have a proportional cover, you probably need some way to measure its location for this to all work reliably.

1 Like

Setting the positions with slider is working now, thank you again.
But there is a slight problem. The up and down arrows on the frontend does not update. They are stuck at only up arrow and stop button available. A good way will be if the position is 0 (fully closed) only the up arrow and stop should be active. If the position is 100, only the down and stop and if the position is in between all three buttons should be enabled. And also if up arrow is pressed the front end slider should be 100 after the calculatated delay and if the down arrow is pressed it should be 0 too.

The problem with “dumb covers” will always be there. In homebridge, I set the cover to fully open or fully close with the buttons first, to let the system know the state. Then I can set to any state I want. From time to time you need to fully close or open, because opening_time and closing_times will always be a little off the real values.
Still waiting for xiaomi to implement the horizontal blind motor with zigbee, which will report it’s current position hopefully.

I don’t; think I have any control over any of that. Home Assistant covers do not have an ‘in-progress’ state that I know of. Really the cover has 2 states Closed or Open, and hen an attribute (position). The frontend does not provide any additional hooks allowing control. over the icon state as far as I know. I don’t think there is much more I can do for you.

Ok. I get the not availability of the “in progress” state.
But the code is also not updating the “states.cover.test.attributes.current_position” value. It is always set at 0. That’s why only the up arrow is available at all times.

I updated the code above. Recopy the template.py file again.
Now you can either set:

optimistic: true

Or you can just remove the position_template line

Either of those should put you in optimistic mode which should properly track the position for you.

Confirmed, after setting the position via the slider, button states are updating now too.

Thank you very much.

Glad it is working. I’ll submit a pull request for it once my other one goes through.

Great job! Not perfect but it worked!

Hello guys!

I’m trying to use a template cover (RF tubular motor) with optimistic mode on with Homebridge.

I can see the cover on my Home App but when I close it, for example, no command is send (Broadlink remote). If I use the native Home Assistant web-ui, it works just fine.

Any idea if the position or value template using optimistic mode is messing with Homebridge?

Thanks!

hello, maybe i do not understand the process but… How I can get the template.py file which accepted the closing and opening time? thx

You can use this version:
https://raw.githubusercontent.com/PhracturedBlue/home-assistant/timed_cover_orig/homeassistant/components/cover/template.py

But I cannot support it or do any further deelopment on it, and cannot gauranee it will continue to work with newer HomeAssistant. This functionality can theoretically be implemented with a script instead, but I have never used them before and don’t know how to do so… We need someone familiar with them to help out.

Did you send the PR? I’d like to have this feature without having to copy manually that file :slight_smile:

The pull request was declined. You either need to use the template override or try to reimplement the functionality with the python scripting. I have not tried to do the latter, and cannot provide any guidance for it.