How to handle delayed update on template-switch?

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:

  1. Sensor state is off. Switch is off
  2. I turn on the switch. MQTT command is sent
  3. After ~1-2 seconds, the switch shows off again.
  4. (somewhere in here, the IoT polls mothership and actually turns on)
  5. After ~5-10 seconds, the sensor state goes to “on” as MQTT sends an update
  6. Now the switch shows “on”

What I want:

  1. Sensor state is off. Switch is off
  2. I turn on the switch. MQTT command is sent
  3. Switch immediately becomes disabled/inoperable, state “unavailable/unknown”
  4. (somewhere in here, the IoT polls mothership and actually turns on)
  5. After ~5-10 seconds, the sensor state goes to “on” as MQTT sends an update
  6. Now the switch shows “on”
  7. 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.

Hello Matt - this was a long time ago, but I wonder if you ever found a solution to this?

I have a HA installation on a boat that reaches out to the vessel’s NMEA 2000 control system for some actions and these can take a second or two to process and generate status updates. HA used to be a little tardy at updating switch state, but recent release have got faster and are now outstripping external LAN (let alone cloud) interactions.

Paul

I’ve not. I ended up giving up and just kinda accept that I need to wait 10-ish seconds before deciding if the command worked and ignore the wrong-showing-state in the meantime.

I very rarely use the UI to control stuff so it mostly doesn’t matter and is just a display bug for my needs.