Setting state based on attribute?

Hey all,

I just bought a Zemismart Tuya curtain motor and I’m using the new Tuya v2 beta to integrate it into Home Assistant. Unfortunately the Tuya integration adds the curtain motor into Home Assistant as a cover and it’s state is persistently listed as “unknown”. The only attributes that this cover reports is the “current_position”. All I really want is for Home Assistant to report the state of these curtains as either “Open” or “Closed”. I’m thinking that I should be able to use the current_position attribute to create some sort of template or an entirely new cover or so that if the current_position attribute reads 100 it shows that new entity as “Open” and if it shows as 0 then the entity is shown as “Closed.” Can anyone steer me in the right direction as to how I can accomplish this?

1 Like

Create a Template Binary Sensor.

template:
  - binary_sensor:
      - name: "Curtain"
        device_class: opening
        state: "{{ state_attr('cover.curtain', 'current_position') | int > 0 }}"

Replace cover.curtain with the actual name of your current curtain cover.

Ugh, sorry I left out a crucial piece of information. I would like that new entity to be able to also function as a switch or another cover. I get what you did with your example, it’s just creating a sensor that shows the state of the curtains by pulling the attributes from the cover. I want to add that to a “new” device/cover so I can not only toggle the curtains but correctly report their state.

Then create a Template Switch or Template Cover.

Good, then you should have no trouble creating either a Template Switch or Template Cover.

Thanks, that did the trick! I used your Template Binary Sensor example and with that data/ new sensor I created a new Template Cover:

- platform: template
    covers:
      blinds:
        device_class: curtain
        friendly_name: Blinds
        value_template: "{{ is_state('binary_sensor.curtain', 'on') }}"
        open_cover:
          service: cover.open_cover
          data:
            entity_id: cover.ty00408365e8db84c693bf
        close_cover:
          service: cover.close_cover
          data:
            entity_id: cover.ty00408365e8db84c693bf        
        icon_template: >-
          {% if is_state('binary_sensor.curtain', 'on') %}
          mdi:window-shutter-open
          {% else %}
          mdi:window-shutter
          {% endif %} 

Glad to hear the binary_sensor was useful in the Template Cover.

If you wish, you can reduce the final template to this:

        icon_template: "mdi:window-shutter{{ '-open' if is_state('binary_sensor.curtain', 'on') else '' }}"
1 Like