Smart lamp control.. very advanced rule

I am looking to do a specific task. I thought I saw a smart app yhat can do this but cannot find anything. I gave a switch on my bedside lamp that also monitors power usage. I want to run the night routine if it is after a certain time at night, I am home based on presence sensor, and the lamp is turned off by sensing watt usage dropping to 0. Anyone done this.

You did.