MQTT non-optimistic switch bounces ON-OFF-ON

Hi, I have a small issue with MQTT switch and non-optimistic mode.

I set up a simple MQTT switch, copypasted from the documentation. I am not using the optimistic feature, so I am “acknowledging” the state change in my subscriber code and publish the new state back onto MQTT. I do this as soon as I receive the status change command.

However, I think that the time window in which HASS expects the state change message is too short, because the behavior I am seeing is:

  1. Click the switch in HASS to ON
  2. HASS UI changes the switch to ON
  3. Subscriber receives the command to change state to ON
  4. Subscriber publishes the ON state event
  5. HASS UI changes the switch to OFF (probably because it does not see the event from 4.)
  6. After a second or so HASS UI changes the switch to ON again (because now it can see the event)

I am using the embedded mqtt broker.

Is there a setting that would allow me to tell the switch to wait a little longer before flicking the UI back and forth?

No there isn’t.

I read your post twice and can’t quite understand what you’ve done. But that’s my problem. :slight_smile:

If you’re not using optimistic mode, the expectation is that when Home Assistant publishes to the command_topic, the subscribed device will acknowledge by publishing its new state to the status_topic. I have many MQTT Light and MQTT Switch entities working this way without the odd behavior you’ve described.

If Home Assistant does not receive an acknowledgement, within a reasonable time period, it will assume the device never received the published on command. As a result, it will change the entity’s state back to off. If the acknowledgement arrives later than the time period, Home Assistant will change the entity’ state back to on.

Do you know why it takes so long to receive the acknowledgement? In my system it is received almost instantaneously.

I recorded the screen with the issue: https://photos.app.goo.gl/3WZVKeRGwDXWjiPRA

I am sorry if my explanation was unclear. I am doing exactly what you are saying. The flipping of the button only manifests sometimes, other times it works as expected (without any changes to anything). So I guess I will blame the network connection.

What I was hoping to see would be a customizable timeout so that if I know that my network sucks, I am able to increase the time hass waits before checking the state of the switched entity. But tom_l says there is no such option, so nevermind :slight_smile:

Thanks, that’s all I needed to know!

Saw the video, yeah, that’s bad; I’ve never had that happen to me.

You may want to investigate why there’s so much latency in the communications, especially since you said you’re using the embedded broker! Maybe try using the Mosquitto broker.

For comparison, I have Home Assistant running on an old netbook and the Mosquitto MQTT broker runs on a separate machine (Raspberry Pi 3B) and there’s almost no noticeable delay.

@melkamar I’m having exactly the issue.
Did you find a way around it other than use optimistic mode?

Sorry but no. I tinkered around and after some restarts it eventually fixed itself in most cases. I still randomly encounter this from time to time but I’ve just decided to live with it

I have the same issue. I understand people have their opinion on why somethings takes a while to respond or whatever… the questions remains, why is this timeout not configurable. I feel it’s needed especially with mqtt. I would love to see a timeout setting!

If you are using MQTT on a local network it should have such low latency that this would especially not be needed. MQTT is a fast lightweight protocol. You should not have latency this high.

Either way you can make a feature request here:

That implies you are using optimistic doesn’t it - the UI is updating immediately the button is clicked …

You do need to make your updates back to MQTT as fast as possible as all sorts of race conditions can develop. However I appreciate if you are waiting for the actual device to tell your app it’s updated then you are somewhat limited to that response time.

Hello,
I don’t like optimistic. I like the switch to show the state, which it does after a few seconds. I prefer the way the switch looks in the GUI as well. With the evolution of LoRaWAN technologies using MQTT it will be slow to report status and I feel this is needed. This is not something configured incorrectly, it’s how the technology works. Thanks for the information on making a request.

I’ve opened a request for a feature to be added: LoRaWAN technology / MQTT Switch Timeout

any news here? have the same problem with some devices (modbus victron) and esphome devices. state does not seem to switch that fast, so the gui switch flips back to the initial state and after 1-2 more seconds it updates with th correct state. simple timout would be the solution.