How to create virtual copy of the motion sensor with delayed detected state

Hi all.

I couldn’t find satisfactory solution even though I’ve seen some similar topics.

I want to create a template sensor or helper that would act like an actual motion sensor I have connected, but with the “detected” state to be delayed 0.5 or full second after real sensor and when the sensors state changes to clear it would clear as well without delay.

This is the solution I came up with, but I don’t know how to execute it to solve an issue I have with a zigbee motion sensor with lux sensor, or maybe someone already have better solution.

The problem is that the when using this motion sensor in automation to turn on the light after ambient light drops below certain threshold it sometimes doesn’t turn on the lights when it should because the optimal position to detect motion also makes the lux sensor to see the lights it turns on. The motion sensor part works fine, but the lux sensor only sends updated reading to the home assistant when motion sensor changes to detected. so it is possible to have the lux reading high in the darkness not being updated preventing the lights to turn on. It will turn on the lights again after motion is cleared and the lux reading is now reading from being activated previously when the lights didn’t come on.

so just to clarify I want something working like this:

real.motion.sensor state changes to “detected” or on
delay 0.5sec
virtual.motion.sensor state changes to “detected”

real.motion.sensor state changes to “clear” or off
virtual.motion.sensor state changes to “clear”

Thanks in advance!