Is this a bug with "select"?

I built an ESP32 device that publishes to MQTT and does auto-discovery. It seems to be working right. In the HA MQTT integration, my new device appears and contains multiple entities. One of those entities is a sensor that is a “select”. The HA auto-discovery json that is sent to MQTT identifies it as a “select” component, it contains the valid options, and it specifies the state_topic and command_topic. I see the Select appear in HA, and it all seems to work. It knows what the current state is, and when/if I change the state in the UI, a callback is sent to my device informing it of the change. This all seems to be working.

My issue comes up when the state of that select entity changes on the device. If the HA UI is open to a page that is showing the select and the device changes state, then I see the UI correctly update what it is displaying to show the new state. If no pages are open and showing the select, then when I go to such a page, I see the select show the correct new state. BUT, (here is the bug) if I have a UI page open which shows the select, but that page is in a tab that is NOT in the foreground, then when I switch to that tab, I get the wrong behavior. Instead of updating the UI, it seems that the UI sees the old value on my page as more recent than the new state from the device and it pushes that to the command topic which initiates a callback which incorrectly tells my device to change the state.

Any ideas how I can better understand where the bug is? As best I can tell, I do think my device is working right. But I don’t know enough to point a finger at the true culprit. Some more info:

I see the same behavior in tabs in Google Chrome, and in the Android App. In both cases, the issue arrises when the page is open, but not in the foreground.

On the same page, I have another sensor from the same device that is a number box which also has a callback. This one does NOT exhibit this behavior. If the device state changes, the number box properly updates what it is showing, whether the page is foreground/background.

In MQTT Explorer, I see this in the topic “homeassistant/select/Dispatch_Mode”

{
  "component": "select",
  "device": {
    "name": "A2M-AL8002221090041",
    "model": "Smile-BAT-8.2PHA",
    "manufacturer": "AlphaESS",
    "identifiers": [
      "A2M-AL8002221090041"
    ]
  },
  "name": "Dispatch Mode",
  "unique_id": "A2M-AL8002221090041_Dispatch_Mode",
  "device_class": "enum",
  "options": [
    "Stop",
    "Only charge via PV",
    "ECO Mode",
    "FCAS-Mode",
    "Load Following",
    "Maximise consumption",
    "Normal mode",
    "Optimise consumption",
    "PV power setting",
    "SOC control"
  ],
  "state_topic": "Alpha2MQTT/A2M-AL8002221090041/Dispatch_Mode/state",
  "command_topic": "Alpha2MQTT/A2M-AL8002221090041/Dispatch_Mode/command",
  "availability_topic": "Alpha2MQTT/A2M-AL8002221090041/status"
}

Thanks for any/all help.

Go to your profile settings and check what this is set to:

If the connection closes the tab should auto reload when made visible and it re-connects, if the tab is set to stay connected it should be continuously updating in the background.

Thanks for the response. Where is that settings page? I don’t see it in Chrome (on MacOS). Are you using a different browser/ Also I have the same issue in the Android app if I let the app go in the background.

Also, should this matter?? Does it matter if the connection closes? The situation is that there are N valid states. In an example situation, the device is in state “One” and this is properly reflected in the UI. Then the device changes to state “Two”. If the UI is in the foreground, then it properly updates to indicate that the state is now “Two”. But if, instead, the UI was in the background, then when it is brought to the foreground, it notices that it is in state “One” and the device is in state “Two” and it updates the MQTT command_state to indicate a change, and that causes my device to get a callback and think that the state should change back to “One”. The “correct” behavior should happen whether the connection closes and reopens, or not.

My number box (which can also be change by either the device or UI) doesn’t not have this issue. It only publishes an MQTT command_state update when I actually type in a new value. In the select case, it “seems” that something about the graphical presentation of the select options is having something to do with the UI thinking that the old UI value is new.

Circle icon with your initials in it bottom of the left hand menu.

It is something you should investigate.

Got it. Thanks for showing me where to find that. Indeed my setting was set to auto-close the connection. I can confirm the following:

  • With auto-close set, if I wait < 5 minutes then I do not see this behavior.
  • With auto-close set, if I wait > 5 minutes then I DO see this behavior.
  • With auto-close NOT set, if I wait > 5 minutes then I do NOT see this behavior.

So I can avoid this behavior by disabling auto-close in the settings. But that seems to just be masking the real issue (bug?). Any further suggestions?

It seems that I can now describe the issue as this: My UI select and my device start in sync. If the device changes state while the UI is connected all is fine. But if the device changes state when the UI is disconnected, then when the UI reconnects, it will tell the device to update to the OLD state that the UI had.

What card do you use to show the data? Because seeing stale data on a disconnected tab would be expected, but on a reconnect the UI should not submit that old data to the server.

I’m on the settings device page. Settings->“Devices & services”->Devices. Then I see my (MQTT auto-discovered) device and I click on that and I see a page with all the auto-discovered entities for that device. This select is one off those entities.

The two selects (“A2M Test” & “Dispatch Mode”) under “Controls” are exhibiting this issue.

Are you just looking at these selects when they send the stale data back to the esp, or are you clicking the select when it happens?

Just looking. No clicking. Here’s the scenario:

The “A2M Test” select has three valid options: “One”, “Two”, & “Three”. I set the device to state “One” and the (connected) UI properly shows “One”. Then I switch tabs in Chrome so that UI page isn’t showing and I wait 5 minutes. Then (without touching Chrome) I change the device state to “Two”. I can see in MQTT Explorer that the device updates the MQTT broker. Then in Chrome I simply switch to the UI tab, and the UI is then sending a state change (using the command_state) to sat the state back to “One”.

Also, the UI isn’t always sending a state change when it reconnects. If the state of the device (which the UI gets from MQTT) hasn’t changed, then it knows not to send an update. Only when they are different does it send the update. It appears to connect the UI, then get the latest MQTT data, then it “assumes” that it’s state is new (newer?) than the MQTT data.

That seems like a bug to me. You should report it in github, because the developers do not actively scan the forum so they won’t see this.

Also of note: The Box entity (shown on the above screenshot, right below the selects) does NOT have this behavior. It correctly updates the UI on reconnect and does not send the stale data as an update.

Any suggestion for which tracker? Ia this Frontend?

Yeah frontend.

Issue opened:

Thanks for the help.

1 Like