MQTT delayed on Sonoff switches

Recently I have observed that the triggering of my Sonoff switches (ESPeasy) can be pretty delayed. Sometimes they react instantly but very often they react 1-20 seconds delayed.
I have now tried reading all the messages on the subject and get the following:

Client mosqsub/720-hassbian received PUBLISH (d0, q0, r0, m0, ‘/Sonoff_4/gpio/12’, … (1 bytes))
/Sonoff_4/gpio/12 1 (this reacts instantly)

Client mosqsub/720-hassbian received PUBLISH (d0, q0, r0, m0, ‘/Sonoff_4/relay/state’, … (1 bytes))
/Sonoff_4/relay/state 1 (this response often comes delayed 1-20 seconds)

What can explain this delay? I have off course tried to reboot HA, power Down/up the sonoffs etc.

So the sonoff receives the command message immediately, but then takes 20 seconds to respond?

I am not quite sure how it Works and if the Sonoffs receives the commands but I guess the commands are sent instantly at least.

I was running Hassbian 0.74.2 when this behaviour started and have afterwards tried to update to 0.82.0 without any change.
The Sonoffs shows RSSI of at least -62dB so I guess that is ok anyway.

Which broker are you using?

I am using Mosquitto

I often get this error in my log after restart and don´t quite know what it Means and if this could be connected to my problem.

2018-11-17 17:10:11 ERROR (MainThread) [aiohttp.server] Unhandled exception
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/”, line 411, in start
await resp.write_eof()
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/”, line 596, in write_eof
await super().write_eof(body)
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/”, line 401, in write_eof
await self._payload_writer.write_eof(data)
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/”, line 136, in write_eof
File “/srv/homeassistant/lib/python3.5/site-packages/aiohttp/”, line 67, in _write
raise ConnectionResetError(‘Cannot write to closing transport’)
ConnectionResetError: Cannot write to closing transport

I doubt if that error message has anything to do with MQTT problems as it is an http error.

I realize now that the log in your first message is a mosquitto log. If there is twenty seconds difference at that point, then the delay is within the sonoff. If that is the case, you are best taking it up with the author of ESPEasy as HA has nothing to do with it.

just strange that they all have been working fine for a few years.
I wonder if it might be some sort of wifi problem after all??

It could be a wifi problem, although I would expect to see lots of disconnect and connect messages in the mosquitto log if that was the case.

The only way to check if the wifi is just slow, is to do the timing on the sonoff.

I have just tried to flash the sonoff with Tasmota firmware instead and I am getting the same delayed response. I guess maybe it could maybe be the wifi, but what do you mean by doing the timing on the sonoff?

Only as simple as seeing on the console at what time the message comes in, and then seeing what time the response is sent.

If you have changed the firmware and are still getting the same response time, I think you are down to either the hardware or the wifi, and checking the timing on the sonoff console should enable you to find out which is the problem.

I will have a look. Thanks for helping :slight_smile: