I’m trying to interface a cloud-controlled device over MQTT. I have a template-switch that makes a service-call to publish a MQTT payload, and then I have a binary sensor which tracks the state of the actual device (which is published every many seconds by TheCloud).
The problem I’m having is if you toggle the switch on, it can be 5-10 seconds before it gets confirmation it turned on. This isn’t a HA issue, even the official app has this “issue” probably because of the latency with going thru TheCloud (UGH).
What I have:
- Sensor state is off. Switch is off
- I turn on the switch. MQTT command is sent
- After ~1-2 seconds, the switch shows off again.
- (somewhere in here, the IoT polls mothership and actually turns on)
- After ~5-10 seconds, the sensor state goes to “on” as MQTT sends an update
- Now the switch shows “on”
What I want:
- Sensor state is off. Switch is off
- I turn on the switch. MQTT command is sent
- Switch immediately becomes disabled/inoperable, state “unavailable/unknown”
- (somewhere in here, the IoT polls mothership and actually turns on)
- After ~5-10 seconds, the sensor state goes to “on” as MQTT sends an update
- Now the switch shows “on”
- When the update of the state comes in, the switch will have valid on/off state and be enabled again
Is there some way I can easily “force” the value of the switch state to unknown until it hears the next status-update over MQTT to refresh it whether the on/off was successful? I don’t want users to keep sending commands while it’s trying to process the previously queued commands.
I attempted to look at last-changed and last-updated attributes (figuring I would make the switch state something invalid when the switch-interaction was more recent than the status-sensor last-updated) but it appears that I can’t find an attribute to tell the switch was thrown.
I think I could do it with something like a “switch last-triggered” attribute but haven’t found such a thing.