Cover mqtt with unknown state on startup

Hi everybody.
I’m trying to implement an electrical shutter with a sonoff dual flashed with tasmota (and shutter configuraton). In configuration.yaml I added this code for my cover:

  - platform: mqtt
    name: "Camera"
    availability_topic: "tele/sonoffdual001/LWT"
    state_topic: "stat/sonoffdual001/RESULT"
    command_topic: "cmnd/sonoffdual001/Backlog"
    value_template: '{{ value | int }}'
    qos: 1
    retain: false
    payload_open: "ShutterOpen1"
    payload_close: "ShutterClose1"
    payload_stop: "ShutterStop1"
    state_open: "ON"
    state_closed: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    optimistic: false
    set_position_topic: "cmnd/sonoffdual001/ShutterPosition1"
    position_topic: "stat/sonoffdual001/SHUTTER1"

It works fine and everthing is alligned if I use home assistant, tasmota web interface or physical button. Only when I start home assistat, the cover state is “unknown”! But if I move it, no matter how, home assistant get the correct state and position.
I had the same problem with the other sonoffs I use for the lights, and I resolved by this automation:

- alias: Sonoff Announce
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: cmnd/sonoffs/POWER  

Every sonoffs (lights and covers) has the same group topic “sonoffs”. Maybe there’s a different topic to pubblish? Or something else wrong in my configuration?

Thanks in advance!

Some have different practices for using the MQTT retain flag, but for me I want the status sent by a device to be retained in MQTT (so that HA gets the status on restart), and similarly any commands sent by HA I want to be retained when sending (so the device gets the command when it restarts). For the Sonoff Dual, I’m not sure. Check their wiki for commands to see what may need to be set.

If you flashed new tasmota 8.x (not upgraded), the group topic is tasmotas.

I don’t like retain for shutters because I don’t want shutters start to move for something reason. So I prefer to have power off on start.

I know it and I change it. So in every sonoff configuration I set “sonoffs” for group topic

Nobody has any ideas?

Maybe ask for the status too (I only ask the status at a HA restart)

- alias: Sonoff Announce
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: cmnd/sonoffs/POWER  
  - service: mqtt.publish
    data:
      topic: cmnd/sonoffs/status

I tryed it right now: on the Tasmota console I read the answer:

10:52:47 MQT: stat/sonoffdual001/STATUS = {"Status":{"Module":39,"FriendlyName":["Sonoffdual001_1","Sonoffdual001_2"],"Topic":"sonoffdual001","ButtonTopic":"0","Power":0,"PowerOnState":0,"LedState":1,"LedMask":"FFFF","SaveData":1,"SaveState":0,"SwitchTopic":"0","SwitchMode":[0,0,0,0,0,0,0,0],"ButtonRetain":0,"SwitchRetain":0,"SensorRetain":0,"PowerRetain":0}}
10:52:47 MQT: stat/sonoffdual001/RESULT = {"POWER1":"OFF"}
10:52:47 MQT: stat/sonoffdual001/POWER1 = OFF

But on HomeAssistant the state of cover is still “Unkonwn”

Hi @katanza…any news ?

No… :frowning:

But realy no one else had
the same problem?

You could try the different retain options in the tasmota console see if it works that way. SensorRetain 1 might fix your problem.

Hi, I have tried various things like retain power and sensor as well as trying to call the states by various means.

The device reports its position normaly by “stat/window1/RESULT” but only when operating i.e moving. I tried sending a simple stop command to see if it would update it’s position but no.

The device posts its position regularly but the problem is that it posts as a sensor “tele/window1/SENSOR” which Homeassistant isn’t listening to.

So one solution is to take that mqtt message and relay it to “stat/window1/RESULT” which can be easily done by mqtt in and out nodes in Node red.

Another solution is to send “stat/window1/STATUS8” but that returns “10:16:56 MQT: stat/window1/STATUS8 = {“StatusSNS”:{“Time”:“2020-09-21T10:16:56”,“Shutter1”:{“Position”:6,“Direction”:0,“Target”:6},“Shutter2”:{“Position”:100,“Direction”:0,“Target”:100}}}”

Then you need to isolate "={“StatusSNS} away and send it as an stat/window1/RESULT” which I use a json node to turn it on to json objects plus change node to move “payload.StatusSNS” to payload and then I send it back out. I however have not yet implemented it to fire automatically on Homeassistant startup but I’ve seen some ideas and I will post an update when I implement it

I hope somebody understands my gibberish and if someone can make a Homeassistant automation or something better than this solution please post it.

I temporary solved my problem running this script with an automation on HA start:

update_sonoff:
  alias: Update Sonoff at Start
  sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/sonoffs/POWER
      payload: ""
  - service: mqtt.publish
    data:
      topic: cmnd/sonoffs/ShutterPosition
      payload: ""

The first part is for kown sonoffs basic state, the second is for shutters position. Obviously, for every sonoff the “group_topic” has to be “sonoffs”.

The cover item is configurated like this:

- platform: mqtt
  name: "Camera"
  availability_topic: "tele/sonoffdual001/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"
  command_topic: "cmnd/sonoffdual001/Backlog"
  payload_open: "ShutterOpen1"
  payload_close: "ShutterClose1"
  payload_stop: "ShutterStop1"
  set_position_topic: "cmnd/sonoffdual001/ShutterPosition1"
  position_topic: "stat/sonoffdual001/RESULT"
  position_open: 100
  position_closed: 0
  value_template: >
    {% if ('Shutter1' in value_json) and ('Position' in value_json.Shutter1) %}
      {{ value_json.Shutter1.Position }}
    {% elif ('ShutterPosition1' in value_json)%}
      {{ value_json.ShutterPosition1 }}
    {% else %}
      {% if is_state('cover.camera', 'unknown') %}
        50
      {% else %}
        {{ state_attr('cover.camera','current_position') }}
      {% endif %}
    {% endif %}  
  retain: false
  optimistic: false
  qos: 1

I don’t know if this is the best solution, but it works!