Cover State remains unknown

Hi everyone! :slight_smile:

I connected on my entrance gate two magnet sensors, of which:

  1. Returns true or on when the gate is fully open
  2. Returns true or on when the gate is fully closed

Now I have my cover entity of which is state should be:

  • open if S1 is true
  • closed if S2 is true
  • moving if neither S1 nor S2 are true

Here is my configuration.

binary_sensor:
  - platform: mqtt
    name: "Gate Open"
    state_topic: "cmnd/gate-state/POWER1"
    device_class: garage_door

  - platform: mqtt
    name: "Gate Closed"
    state_topic: "cmnd/gate-state/POWER2"
    device_class: garage_door

cover:
  - platform: mqtt
    name: "Gate"
    command_topic: "cmnd/sf-gate/power"
    state_topic: "stat/sf-gate/POWER"
    payload_open: "ON"
    payload_close: "ON"
    value_template: '{% if is_state("binary_sensor.gate_open", "on") -%} open {%- elif is_state("binary_sensor.gate_closed", "on") -%} closed {%- else -%} moving {%- endif %}'

In DevTools’ section, under State panel, my two sensor are reacting correctly showing on or off, but the problem is that the cover’s state is always unknown.

I believe that something is wrong in my value_template attribute, but I googled all night long without finding a solution.

Thank you in advance! :wink:

Does your state_topic change? Value_template is ment to take the response from the state topic and parse the answer. I’m not 100% sure that you can access the state machine like you are trying to do at that point.

1 Like

Hi @petro, thanks for your quick response.
I tested the code of value_template in the DevTools’ section, under Template panel and it works.

I just want to say to the cover - that right now does not know anything about its state: “Hey Cover, if S1 is on you are open, if S2 is on you are closed, else you are moving.”

Hope that helps, thank you again.

Yes I understand that but your value template is accessing the state machine. It will work when the state machine is present. If the state machine not present, it’ll never work.


So your state topic never changes? Try removing just the state_topic and see what happens.

1 Like

Thanks @petro, I tried removing state_topic: "stat/sf-gate/POWER" line from my configuration, and now the gate results always open.

I’ll try to explain how my gate’s relay works.
I have a Sonoff Basic that trigger ON the relay and then automatically OFF after 0.4s when I want it to open the gate. It’s just like a ‘human press of button’. Sonoff don’t know anything about the state of the gate as it doesn’t depend on the state of the relay (it’s always off, it goes on for 0.4s for opening then the closure is automatic after a certain time); that’s why I set this sensors up.

By the way I think that I’m on the right way…

Another solution could be to set-up Tasmota’s Rules (on the relay) to publish on an MQTT topic directly the state of the gate (given from sensors) and so config the cover entity’s position_topic on that topic.

Your value_template can correctly produce the two default values, open and closed:

    value_template: >
      {% if is_state("binary_sensor.gate_open", "on") -%} open
      {%- elif is_state("binary_sensor.gate_closed", "on") -%} closed
      {%- else -%} moving {%- endif %}

However, it is also capable of producing a value called moving.

Based on what I see in the documentation for MQTT Cover, state_topic doesn’t support moving.

If you want to indicate an intermediate position, something between open and closed, MQTT cover offers the position_topic:

position_topic

(string)(Optional)

The MQTT topic subscribed to receive cover position messages. If position_topic is set state_topic is ignored.

However, I don’t believe value_template works with position_topic therefore it presents a problem for you to interpret the states of the two magnetic sensors.

What I would suggest is that you focus on getting the entity to work with just open and closed (exclude moving). Once that has been achieved, you can experiment with ways to report an intermediate state.

2 Likes

Thank you very much, @123!

I know have the open/closed state in the concept behind is => IF S2 is true Gate is CLOSED, ELSE Gate is OPEN.

As I see, there also is the unknown state. Do you think having open, closed and unknown could be a possible way?

Now my Gate config looks like this.

cover:
  - platform: mqtt
    name: "Gate"
    command_topic: "cmnd/sf-gate/power"
    state_topic: "cmnd/gate-state/POWER2"
    state_open: "OFF"
    state_closed: "ON"
    payload_open: "ON"
    payload_close: "ON"

Thank you very much, again! :slight_smile: