MQTT and Tasmota problems when Disconnect/Crash

Hi there,
I have a strange problem with my setup, I tried many time to search on the forum or on the web but none of other topic help me as well… The problem is this: When the Sonoff reboot (caused it crashes or simply loose connection to my router) and reconnect to my MQTT server on Hassio it turn ON the relay and than immediately OFF, causing my garage to open randomly :S I have the Sonoff Pro 4ch, and ad I can see in the log page of tasmota this problem is only on the relay where I connect my garage (I mean that if I change to relay3, modifying my configuration from POWER4 to POWER3 the problem goes to relay3.

This is my configuration:

#MQTT
mqtt:
  broker: !secret mqtt_broker
  port: !secret mqtt_port
  client_id: home-assistant-1
  keepalive: 60
  username: !secret mqtt_username
  password: !secret mqtt_password
  protocol: 3.1
  discovery: true
  discovery_prefix: homeassistant
  birth_message:
    topic: "tele/hass1/LWT"
    payload: "Online"
    qos: 1
    retain: true
  will_message:
    topic: "tele/hass1/LWT"
    payload: "Offline"
    qos: 1
    retain: true
# Script
script: !include_dir_merge_named script

# Switch
switch: !include_dir_merge_list switch

# Cover
cover: !include_dir_merge_list cover

Cover:

  - platform: template
    covers:
      saracinesca:
        friendly_name: "Saracinesca"
        value_template: >-
          {% if is_state('binary_sensor.door_window_sensor_158d0001fd6296', 'on') %}
            open
          {% else %}
            closed
          {% endif %}
        open_cover:
          service: script.attivagarage
        close_cover:
          service: script.attivagarage
        stop_cover:
          service: script.attivagarage
        icon_template: >-
          {% if is_state('binary_sensor.door_window_sensor_158d0001fd6296', 'on') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

Switch:

- platform: mqtt
  name: "Sonoff Garage 1"
  command_topic: "cmnd/garage/power1"
  state_topic: "tele/garage/POWER1"
  availability_topic: "tele/garage/LWT"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: false
- platform: mqtt
  name: "Sonoff Garage 2"
  command_topic: "cmnd/garage/power2"
  state_topic: "tele/garage/POWER2"
  availability_topic: "tele/garage/LWT"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: false
- platform: mqtt
  name: "Sonoff Garage 3"
  command_topic: "cmnd/garage/power3"
  state_topic: "tele/garage/POWER3"
  availability_topic: "tele/garage/LWT"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: false
- platform: mqtt
  name: "Sonoff Garage 4"
  state_topic: "tele/garage/POWER4"
  command_topic: "cmnd/garage/power4"
  availability_topic: "tele/garage/LWT"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "Online"
  payload_not_available: "Offline"
  retain: true

Script:

  attivagarage:
    alias: "Apri-Chiudi"
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.sonoff_garage_4

I set in tasmota console:

PowerOnState 0
PowerRetain 0

When this appen, I mean the garage Open/Close randomly I see that “mosquitto_sub -h MYIP -p MYPORT -u MYUSER -P MYPASSWORD -t cmnd/garage/power4” send me an answer ON. For remove the saved state I use this command “mosquitto_pub -h MYIP -p MYPORT -u MYUSER -P MYPASSWORD -d -n -r -t cmnd/garage/power4” where -n sends an empty key and the -r store this change permanently as the wiki say.

I hope that some one can help me to understand wits wrong here…
:slight_smile:

1 Like

you have retain: true for POWER4, but retain: false for the others ?

Can you post the console log from Tasmota when the device reconnects, with ‘More debug’ turned on?

That was only a test after that I read this today morning ( https://github.com/arendst/Sonoff-Tasmota/wiki/Home-Assistant#update-the-configure-of-switch-necessary-with-the-v591-and-home-assistant-0643 ), I can set it to true but the problem persist :confused:
Sure I’ll attach my log this evening when I’ll be at home :slight_smile:

13:11:08 MQT: tele/garage/STATE = {"Time":"2018-03-18T13:11:08","Uptime":"2T14:35:13","Vcc":3.201,"POWER1":"OFF","POWER2":"OFF","POWER3":"OFF","POWER4":"OFF","Wifi":{"AP":1,"SSId":"D-Link Garage","RSSI":66,"APMac":"F8:E9:03:BF:92:80"}}
13:15:59 MQT: Tentativo di connessione...
13:16:15 MQT: Connesso
13:16:15 MQT: tele/garage/LWT = Online (salvato)
13:16:15 MQT: cmnd/garage/POWER = 
13:16:15 MQT: Sottoscrivi a cmnd/garage/#
13:16:15 MQT: Sottoscrivi a cmnd/sonoffs/#
13:16:17 MQT: Sottoscrivi a cmnd/Garage/#
13:16:18 RSL: Gruppo 0, Indice 1, Comando POWER, Dati OFF
13:16:18 MQT: stat/garage/RESULT = {"POWER1":"OFF"}
13:16:18 MQT: stat/garage/POWER1 = OFF
13:16:18 RSL: Gruppo 0, Indice 2, Comando POWER, Dati OFF
13:16:18 MQT: stat/garage/RESULT = {"POWER2":"OFF"}
13:16:18 MQT: stat/garage/POWER2 = OFF
13:16:18 RSL: Gruppo 0, Indice 4, Comando POWER, Dati ON
13:16:18 MQT: stat/garage/RESULT = {"POWER4":"ON"}
13:16:18 MQT: stat/garage/POWER4 = ON
13:16:19 CFG: Salvato nella flash in F8, Conteggio 1303, Bytes 1648
13:16:20 APP: Pulsante4 level 0-1
13:16:20 MQT: stat/garage/RESULT = {"POWER4":"OFF"}
13:16:20 MQT: stat/garage/POWER4 = OFF
13:16:21 CFG: Salvato nella flash in F7, Conteggio 1304, Bytes 1648
13:16:38 HTP: Menu' Principale
13:16:39 HTP: Menu' Principale
13:16:42 HTP: Console

Sorry for delay but as I said, its a totally random behavior :frowning: I hope that this can help su to investigate

that log is with “More debug” active ?

After debugging the other relays on the Sonoff, I notice that everyone store an Off payload sendend from HA mqtt server except for the 4 (garage one). The other relay are working in normal mode but the one that I’m using for the garage is set to inching mode through Sonoff hardware (pins on the board). IMHO In this mode HA Mqtt doesn’t receive any feedbackessage from Sonoff, and Sonoff still retail ON without empy. So When it crash or disconnect it still have On retained :confused: the solutions for me was to set a new script:

  attivagarage:
    alias: "Apri-Chiudi"
    sequence:
      - service: switch.turn_on
        data:
          entity_id: switch.sonoff_garage_4
      - delay:
          seconds: 1
      - service: switch.turn_off
        data:
          entity_id: switch.sonoff_garage_4
2 Likes