Template Cover and inverting states

Hi everyone,
I apologize if this is a basic, or dumb request, but I’ve not really yet grasped the concept of templates too well.

I am using ZHA to connect a xiaomi curtain motor, and sadly, the integration is a bit buggy. it is apparently a common issue, and it’s honestly not that big of a deal, the curtains work, and that’s what matters.

ZHA creates the following entities from the Xiaomi Aqara curtain motor:

  1. On/Off Switch - This works perfectly, apart from the states being inverted (On : closed curtains / Off: Open Curtains)

  2. cover entity: this does NOT work well at all, using the open/close commands puts the curtains in a state of “opening” or “closing” but it stays stuck in that mode, oftentimes with the curtains not doing anything. I just avoid using those commands.

  3. Occupancy Sensor: Not sure why, the curtains have no functionality for occupancy as far as I know

  4. binary sensor: this shows open or closed. seems to work just fine, but I have not looked into it much.

What i wanted to do was create a template cover that would take the states from the entities generated by the ZHA xiaomi cover, and creates a cover entity that works properly.

It would use the on/off switches to open or close the curtains, and it would display the proper state of the curtains. (so when curtains are open it displays them as open)

bonus point if possible to set a button that opens them to 50%

I’ve managed to create a cover template that opens and closes correctly using the switch, but setting the state of the cover has been giving me headaches, would appreciate any help.

thank you

It’s hard to know for sure without having the data, can you share the current states of your existing Aqara device (developer tools -> states -> your device and the cover template you have so far?

I just went through a custom cover for my curtains using a different type of motor so it’s still fresh in my mind :slight_smile:

try this

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: curtain 
        value_template: "{{ is_state('switch.xxx', 'off') }}"
        open_cover:
          service: switch.turn_off
          entity_id: switch.xxx
        close_cover:
          service: switch.turn_on
          entity_id: switch.xxx
        stop_cover:
          service: switch.turn_{{ 'on' if is_state('switch.xxx', 'off') else 'off' }}
          entity_id: switch.xxx

Replace switch.xxx with your entity_id for the switch that works.

2 Likes

That worked perfectly, thank you so much!

Hi all,

For my curtains of my window of about 7 meters, I’m using 2 slides in order to get my covers closed. I have a cover.window.left & cover.window.right. I also grouped them to group.window_covers.

I’m having following issue:

When my two curtains are open (both at position 100%), I get value Closed and it’s visible in HA. But when I closing my curtains, I need to calibrate them at position window left at 15% and window right at position 19%. For the state of both my individual covers, I still get an open value because they are not at 0%, at my cover group, I also get state open because of the fact that my covers are not (in mathematical way) they are not closed.

Does anyone know HOW to set state at existing entities that at position 15% and 19 % (so actually a value < 20%), all states should be ‘closed’ ? I’m able to do that for a sensor, but then I can’t use the functionalities in my theme about covers and their state.

2nd question : has anyone seen any mdi:icons or fas icons with double curtains, left curtain open and right curtain open ?

Thanks in advance for all your feedback !

Kr,

Bart

You’d have to use a position template and transform 19 to 100 to 0 to 100 using an equation.

You can utilize the compensation integration to derive the value for you, then use that sensor in each position template. If you don’t understand the math, you’d probably have to create 2 compensation sensors for the position template and the set position, open_cover, and close_cover:

  1. transform 19-100 to 0-100
  2. transform 0-100 to 19-100

It’s no easy task.

Personally, I’d try to calibrate the hardware. That’s typically how it works. I.e. when it shows up in home assistant, 0 - 100 is 0 to 100, not 19 to 100.

If you don’t care about setting the position. You can simply make a template cover that checks the value in the value_template with an open_cover and close_cover.

It’s like a bit of chinese for me. But one thing in your message : calibrating the hardware should work right. If I’m able to stop it during calibration at position 20 (middle of the window), than it will configure itself as closed at 0%

But if you have other tips & tricks to make my life a bit easier, please let me know :slight_smile:
Thx in advance !

Kr,

Bart

If you don’t understand the math, I’d suggest looking in other threads on the forums for “Cover Template”, or “Invert position cover”. All should show cover templates that contain the math.