Zigbee2mqtt cover state is opposite to the one in ha

Hi,
I use zigbee2mqtt with my tuya cover switch and it used to work
I noticed that the state reported by zigbee2mqtt
is opposite to the state HA report

Any ideas how to fix it?

In the case below, the cover is fully open

  • z2m state is “OPEN”
  • while HA state is “closed”

image

using the “invert_cover” settings, invert the state for both so I still get opposite values

I am using HA version 2023.04, but I think the cover state has been broken for some time. However, I’m not sure which upgrade broke it.

Anyone? This issue is very annoying :frowning:

IN z2m go to settings(specific) and change invert_cover

1 Like

I already mentioned that invert_cover didn’t help :cry:
It did invert. Now I get “CLOSED and open” instead of “OPEN and closed” … still opposite states

then you may have to manually change the direction of the motor itself

I don’t see how it will help … :frowning:
It feels like a software/configuration mismatch between hass and z2m
it used to work… something changed in one of the last releases

I ended up creating a new templated cover sensor

# fix my z2m cover
cover:
  - platform: template
    covers:
      bedroom_template_cover:
        device_class: shutter
        friendly_name: "Bedroom shutters"
        # invert the position
        position_template: >-
          {{ 100-state_attr('cover.z2m_tuya_curtain_bedroom', 'current_position') | int }}
        # for 'opening' and 'closing' states
        value_template: >-
            {{ states('cover.z2m_tuya_curtain_bedroom') }}
        close_cover:
          - condition: numeric_state
            entity_id: cover.z2m_tuya_curtain_bedroom
            attribute: current_position
            below: 100
          - service: cover.close_cover
            target:
              entity_id: cover.z2m_tuya_curtain_bedroom
        open_cover:
          - condition: numeric_state
            entity_id: cover.z2m_tuya_curtain_bedroom
            attribute: current_position
            above: 0
          - service: cover.open_cover
            target:
              entity_id: cover.z2m_tuya_curtain_bedroom
        stop_cover:
          service: cover.stop_cover
          target:
            entity_id: cover.z2m_tuya_curtain_bedroom
        set_cover_position:
          service: cover.set_cover_position
          target:
            entity_id: cover.z2m_tuya_curtain_bedroom
          data:
            position: "{{ 100-state_attr('cover.z2m_tuya_curtain_bedroom', 'current_position') | int }}"

Just wanted to reply to your post and say thanks for the workaround, I was tearing my hair out to try to fix the issue, this workaround is a good solution.

@nir_livne Thanks for the approach to solving this. Your exact solution didn’t work for me, but I have roller shades so maybe there’s something different about mine from yours. I thought I’d share what I did in case it helps others. I renamed all of my shades in ZigbeeMQTT to add a prefix of MQTT and set them up in with Invert Cover set to True. I was having the same issue where Home Assistant would show Closed when the Cover was open and vice versa. In my template, I simply override close_cover and over_cover to reverse the buttons and the open/close state of the Cover. I now use this template cover in my Automations, Dashboards, and to share with Alexa through emulated Hue and everything works as expected.
Here is my template

cover:
  - platform: template
    covers:
      office_blind:
        device_class: shade
        friendly_name: "Office Blind"
        # just take the current position of the MQTT version as the position since it's already inverted there.
        position_template: >-
          {{ state_attr('cover.mqtt_office_blind', 'current_position') | int }}
        # The following reverses the behavior of the up/down buttons and Closed/Open
        open_cover:
          - condition: numeric_state
            entity_id: cover.mqtt_office_blind
            attribute: current_position
            below: 100
          - action: cover.close_cover
            target:
              entity_id: cover.mqtt_office_blind
        close_cover:
          - condition: numeric_state
            entity_id: cover.mqtt_office_blind
            attribute: current_position
            above: 0
          - action: cover.open_cover
            target:
              entity_id: cover.mqtt_office_blind
        # For stop and set cover position, just send theseon to the MQTT version
        stop_cover:
          action: cover.stop_cover
          target:
            entity_id: cover.mqtt_office_blind
        set_cover_position:
          action: cover.set_cover_position
          target:
            entity_id: cover.mqtt_office_blind
          data:
            position: "{{ position }}"
        # When the shade is completely open, use the blinds-open icon
        icon_template: >-
          {% if state_attr('cover.mqtt_office_blind' , 'current_position') | int > 99 %}
            mdi:blinds-open
          {% endif %}
        optimistic: false