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.