When you toggle the entity via an automation or from device view, first attempt takes about (feeling) 500ms to actually perform the action (relay toggle) while all immediate clicks/automation toggle the relay instantly. Both hardware act the same, so I assume it is not HW, but rather HA somewhere, somehow.
If you wait about 5 seconds and try again, switch will again take about 500ms to perform the action and all other clicks just after are instant.
Feels like some process starts in the HA backend that is alive for about 5 seconds and then destroys itself if no other command is being received
No, I have 2 different HW, one is Shelly 1PM gen3, another is a custom ESP32 with esphome flashed. I have updated the orig post.
The trick is that if you go to device view and start clicking the switch entity to turn it on/off, the first attempt takes longer than any other. It feels to me that there is a “sleep mode” for switch.turn_on action. When you press it first time, it activates itself and then all other clicks are really instant.
After 5 seconds (or so) the task goes back to standby mode.
It could just be the WiFi connection that needs to be re-negotiated.
WiFi is not a lightweight protocol and it has a complicated handshake process that takes time.
Tested with Sonoff ZBMINIL2. Same thing, the first toggle of the switch takes more time than all subsequent toggle commands in the period of next ~5 seconds. I use Z2M with Sonoff dongle.
I guess it is a normal behavior in the HA context.