Publish MQTT command before doing POWER OFF

I have a Tasmota relay (Sonoff Basic) that is controlled using Home Assistant / MQTT. Whenever it receives a POWER OFF command, I want it to first publish a command (to power down another device), then wait for x seconds, and then actually power off.

I tried using rules for this (see rule below), but using ‘Power1#State’ is not working, the relay is already switched off before the rules are executed.

rule1 ON Power1#state=0 do backlog publish x; delay y; power off ENDON

I’ve been trying and searching for two days now. Maybe I’m not using the correct keywords. Anyone?

https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/

Last Will and Testament is for ungraceful disconnects. I don’t think this is the solution to my problem. I want my relay to switch off AFTER it publishes a command and wait for x seconds.

I’m not familiar with Tasmota, but if the power off signal is only controlled from HA, couldn’t you run the shutdown sequence as an animation from HA?

Yes I could, and this works now, but preferably I would like a single on/off switch in my HA that automatically does the steps mentioned above.