I have the below template switch using IR to turn off a Soundbar and monitoring the USB power to tell when it is on and off. Currently this is working except when I turn off the device from HA the state of the switch changes to off for a second, then back on, and then off. I’m assumnig there is a slight delay in powering down the USB port so it is not instant. I am trying to find a way to tell the switch to wait for around 1 second before updating the state to prevent the on/off toggling effect. I tried the “delayed_off” filter on the binary sensor but this had the opposite effect and increased the toggling period.
I tried this YAML out but now the state of the switch is not being updated. It just always shows as off. Below is the original configuration and you can see it turns back on before turning off.
I’ve just been working through this same issue with a gas heater that takes about 10 seconds to shut down, with the same “toggling” effect in HA before the gpio sensor detects the unit is off.
My solution was to create another binary sensor for the “shutdown” state - in the turn_off_action of my switch I set this to on for 15 seconds then back off again, and added a condition to the lambda so the switch only returns true if the shutdown binary is off. I also toggle the shutdown under turn_on_action to cancel this if I switch it back on within 15 seconds.
This effectively creates an “optimistic off” period where the switch assumes it’s off for the first 15 seconds after being switched off, after which it returns the true state again.