More reliable state changes

Hi, I noticed HA turned on a light as I’ve scheduled, but did not turn it off after 30min as I have also scheduled (it was temporarily unavailable). This is ok-ish to happen to a light, but could be much worse if it was with a heater, for instance.
I couldn’t find any good way to have reliable state changes other than adding some inline retries or some very complex solutions.
I was thinking perhaps a way to deal with this would be to, instead of doing an action (e.g. “turn on” or “turn off”, I could set a desired_state="on"/"off" and then have an automation that runs on a time pattern every minute that toggles the switch if current_state != desired_state.

(One disadvantage about this method is that it would mostly disregard actions done outside HA - e.g. if you press a physical button - or directly call an API - to toggle a switch - HA would toggle it back ~1min later)

Does that pattern sound reasonable? Am I reinventing the wheel?

You could create a script that takes an entity_id and a desired_state as a parameter and then uses a repeat until the desired state is reached.

However what you should do in 99% of cases is find out why the entity was unavailable in the first case.
Check if it has good signal coverage, proper voltage, etc.
If it’s a cloud based integration, maybe flash the light with Tasmota / ESPHome or check for a local integration.

exactly what I was looking for, thanks!

Again, this is more of a cheap fix :smiley: Try figuring out what’s causing the problem.
If you really need this solution, add a delay so the loop won’t run thousands of times in a minute.